public void WriteNull() { var writer = new JsonTextWriter(new StringWriter()); writer.WriteNull(); Assert.AreEqual("[null]", writer.ToString()); }
public static void JayrockWriterObjects () { for (int j = 0; j < Common.Iterations; j++) { StringBuilder output = new StringBuilder (); JsonWriter writer = new JsonTextWriter (new StringWriter (output)); int n = Common.SampleObject.Length; for (int i = 0; i < n; i += 2) { switch ((char) Common.SampleObject[i]) { case '{': writer.WriteStartObject (); break; case '}': writer.WriteEndObject (); break; case '[': writer.WriteStartArray (); break; case ']': writer.WriteEndArray (); break; case 'P': writer.WriteMember ( (string) Common.SampleObject[i + 1]); break; case 'I': writer.WriteNumber ( (int) Common.SampleObject[i + 1]); break; case 'D': writer.WriteNumber ( (double) Common.SampleObject[i + 1]); break; case 'S': writer.WriteString ( (string) Common.SampleObject[i + 1]); break; case 'B': writer.WriteBoolean ( (bool) Common.SampleObject[i + 1]); break; case 'N': writer.WriteNull (); break; } } } }
protected override void ProcessRequest() { string httpMethod = Request.RequestType; if (!CaselessString.Equals(httpMethod, "GET") && !CaselessString.Equals(httpMethod, "HEAD")) { throw new JsonRpcException(string.Format("HTTP {0} is not supported for RPC execution. Use HTTP GET or HEAD only.", httpMethod)); } string callback = Mask.NullString(Request.QueryString["jsonp"]); bool padded = callback.Length > 0; // // The response type depends on whether JSONP (JSON with // Padding) is in effect or not. // Response.ContentType = padded ? "text/javascript" : "application/json"; // // Validate that the JSONP callback method conforms to the // allowed syntax. If not, issue a client-side exception // that will certainly help to bring problem to light, even if // a little too aggressively. // if (padded) { if (!_jsonpex.IsMatch(callback)) { Response.Write("throw new Error('Invalid JSONP callback parameter value.');"); Response.End(); } } // // Convert the query string into a call object. // JsonWriter writer = new JsonTextWriter(); writer.WriteStartObject(); writer.WriteMember("id"); writer.WriteNumber(-1); writer.WriteMember("method"); string methodName = Mask.NullString(Request.PathInfo); if (methodName.Length == 0) { writer.WriteNull(); } else { // // If the method name contains periods then we replace it // with dashes to mean the one and same thing. This is // done to provide dashes as an alternative to some periods // since some web servers may block requests (for security // reasons) if a path component of the URL contains more // than one period. // writer.WriteString(methodName.Substring(1).Replace('-', '.')); } writer.WriteMember("params"); NameValueCollection query = new NameValueCollection(Request.QueryString); query.Remove(string.Empty); JsonConvert.Export(Request.QueryString, writer); writer.WriteEndObject(); // // Delegate rest of the work to JsonRpcDispatcher. // JsonRpcDispatcher dispatcher = new JsonRpcDispatcher(Service); using (new JsonRpcDispatchScope(dispatcher, Context)) { dispatcher.RequireIdempotency = true; if (padded) { // // For JSONP, see details here: // http://bob.pythonmac.org/archives/2005/12/05/remote-json-jsonp/ // Response.Write(callback); Response.Write('('); } dispatcher.Process(new StringReader(writer.ToString()), Response.Output); if (padded) Response.Write(')'); } }
public void WriteNull() { JsonTextWriter writer = new JsonTextWriter(new StringWriter()); writer.WriteNull(); Assert.AreEqual("[null]", writer.ToString()); }
protected override void ProcessRequest() { string httpMethod = Request.RequestType; if (!CaselessString.Equals(httpMethod, "GET") && !CaselessString.Equals(httpMethod, "HEAD")) { throw new JsonRpcException(string.Format("HTTP {0} is not supported for RPC execution. Use HTTP GET or HEAD only.", httpMethod)); } // // Response will be plain text, though it would have been nice to // be more specific, like text/json. // Response.ContentType = "text/plain"; // // Convert the query string into a call object. // JsonWriter writer = new JsonTextWriter(); writer.WriteStartObject(); writer.WriteMember("id"); writer.WriteNumber(-1); writer.WriteMember("method"); string methodName = Mask.NullString(Request.PathInfo); if (methodName.Length == 0) { writer.WriteNull(); } else { // // If the method name contains periods then we replace it // with dashes to mean the one and same thing. This is // done to provide dashes as an alternative to some periods // since some web servers may block requests (for security // reasons) if a path component of the URL contains more // than one period. // writer.WriteString(methodName.Substring(1).Replace('-', '.')); } writer.WriteMember("params"); NameValueCollection query = new NameValueCollection(Request.QueryString); query.Remove(string.Empty); JsonConvert.Export(Request.QueryString, writer); writer.WriteEndObject(); // // Delegate rest of the work to JsonRpcDispatcher. // JsonRpcDispatcher dispatcher = new JsonRpcDispatcher(Service); dispatcher.RequireIdempotency = true; if (HttpRequestSecurity.IsLocal(Request)) dispatcher.SetLocalExecution(); dispatcher.Process(new StringReader(writer.ToString()), Response.Output); }
public void ToJSON(JsonTextWriter jwriter) { jwriter.WriteStartArray(); foreach (DictionaryEntry item in this.Dictionary) { jwriter.WriteStartObject(); jwriter.WriteMember("key"); jwriter.WriteString(item.Key.ToString()); //write the qualified type name of the value //write the value if (item.Value == null) { jwriter.WriteMember("type"); jwriter.WriteNull(); jwriter.WriteMember("value"); jwriter.WriteNull(); } else { TypeConverter typeConverter = TypeDescriptor.GetConverter(item.Value.GetType()); jwriter.WriteMember("type"); jwriter.WriteString(item.Value.GetType().AssemblyQualifiedName); jwriter.WriteMember("value"); jwriter.WriteString(typeConverter.ConvertToString(item.Value)); } jwriter.WriteEndObject(); } jwriter.WriteEndArray(); }
public static void JayrockOutputFile () { using (FileStream fs = new FileStream ("jayrock_out.txt", FileMode.Create)) { StreamWriter out_stream = new StreamWriter (fs); StringReader sr = new StringReader (Common.JsonText); JsonReader reader = new JsonTextReader (sr); out_stream.WriteLine ( "*** Reading with Jayrock.Json.JsonReader"); while (reader.Read ()) { out_stream.Write ("Token: {0}", reader.TokenClass); if (reader.Text != null) out_stream.WriteLine (" Value: {0}", reader.Text); else out_stream.WriteLine (""); } out_stream.WriteLine ( "\n*** Writing with Jayrock.Json.JsonWriter"); JsonWriter writer = new JsonTextWriter (out_stream); int n = Common.SampleObject.Length; for (int i = 0; i < n; i += 2) { switch ((char) Common.SampleObject[i]) { case '{': writer.WriteStartObject (); break; case '}': writer.WriteEndObject (); break; case '[': writer.WriteStartArray (); break; case ']': writer.WriteEndArray (); break; case 'P': writer.WriteMember ( (string) Common.SampleObject[i + 1]); break; case 'I': writer.WriteNumber ( (int) Common.SampleObject[i + 1]); break; case 'D': writer.WriteNumber ( (double) Common.SampleObject[i + 1]); break; case 'S': writer.WriteString ( (string) Common.SampleObject[i + 1]); break; case 'B': writer.WriteBoolean ( (bool) Common.SampleObject[i + 1]); break; case 'N': writer.WriteNull (); break; } } out_stream.WriteLine ( "\n\n*** Data imported with " + "Jayrock.Json.Conversion.JsonConvert\n"); Person art = (Person) JsonConvert.Import (typeof (Person), Common.PersonJson); out_stream.Write (art.ToString ()); out_stream.WriteLine ( "\n\n*** Object exported with " + "Jayrock.Json.Conversion.JsonConvert\n"); out_stream.Write (JsonConvert.ExportToString ( Common.SamplePerson)); out_stream.WriteLine ( "\n\n*** Generic object exported with " + "Jayrock.Json.Conversion.JsonConvert\n"); JsonObject person = (JsonObject) JsonConvert.Import ( Common.PersonJson); out_stream.Write (JsonConvert.ExportToString (person)); out_stream.WriteLine ( "\n\n*** Hashtable exported with " + "Jayrock.Json.Conversion.JsonConvert\n"); out_stream.Write (JsonConvert.ExportToString ( Common.HashtablePerson)); out_stream.Close (); } }