private static string SwapPersonNames() { var writer = JsonText.CreateWriter(new StringBuilder()); writer.WriteStartObject(); writer.WriteMember("id"); writer.WriteString("12345"); writer.WriteMember("method"); writer.WriteString("ru.siqube.itan.swapNames"); writer.WriteMember("params"); writer.WriteStartObject(); writer.WriteMember("p"); writer.WriteStartObject(); writer.WriteMember("firstName"); writer.WriteString("Alex"); writer.WriteMember("lastName"); writer.WriteString("Baranov"); writer.WriteEndObject(); writer.WriteMember("needChange"); writer.WriteBoolean(false); writer.WriteEndObject(); writer.WriteEndObject(); return(writer.ToString()); }
public static void Export(object value, TextWriter writer) { if (writer == null) { throw new ArgumentNullException("writer"); } Export(value, JsonText.CreateWriter(writer)); }
public void Export() { var sb = new StringBuilder(); var writer = JsonText.CreateWriter(sb); var buffer = JsonBuffer.From("[42,{x:123,y:456},foo,true,null,false]"); var exporter = new JsonBufferExporter(); exporter.Export(JsonConvert.CreateExportContext(), buffer, writer); Assert.AreEqual("[42,{\"x\":123,\"y\":456},\"foo\",true,null,false]", sb.ToString()); }
public void Process(TextReader request, TextWriter response, bool authorised) { if (request == null) { throw new ArgumentNullException("request"); } if (response == null) { throw new ArgumentNullException("response"); } Process(JsonText.CreateReader(request), JsonText.CreateWriter(response), authorised); }
public void Export() { var context = JsonConvert.CreateExportContext(); dynamic obj = new ExpandoObject(); obj.num = 123; obj.flag = true; obj.str = "foobar"; obj.nums = new[] { 123, 456, 789 }; var json = new StringBuilder(); new ExpandoObjectExporter().Export(context, obj, JsonText.CreateWriter(json)); Assert.AreEqual("{\"num\":123,\"flag\":true,\"str\":\"foobar\",\"nums\":[123,456,789]}", json.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)); } 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. // StringWriter sw = new StringWriter(); JsonWriter writer = JsonText.CreateWriter(sw); 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 = JsonRpcDispatcherFactory.CreateDispatcher(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(sw.ToString()), Response.Output, true); if (padded) { Response.Write(')'); } } }