public void StringTest() { StringWriter writer = new StringWriter(); JsonSerializer serializer = new JsonSerializer(); serializer.Serialize(writer, "abc123\t\r\n\b\\\" \u0001\u001f"); Assert.AreEqual( "\"abc123\\t\\r\\n\\b\\\\\\\" \\u0001\\u001F\"", writer.ToString()); }
public static void SendJson(HttpContext context, object o) { context.Response.ContentType = JsonConstants.MediaType; JsonSerializer js = new JsonSerializer(); // TODO: Subclass this from a DataResponsePage SendPreamble(context, JsonConstants.MediaType); js.Serialize(context.Response.OutputStream, o); SendPostamble(context, JsonConstants.MediaType); }
public void TransformPropertyOnSerializationTest() { var serializer = new JsonSerializer(); var person = new { name = "Sue", age = 5 }; var transformAge = new PropertyTransformation( /*name*/ "age", /*transform*/ (age) => { return (int)age * 2; } ); var options = new SerializationOptions { Include = new[] { "age" }, Transformations = new[] { transformAge } }; var doc = serializer.Serialize(person, options); Assert.Equal(@"{ ""age"": 10 }", doc.ToString()); }
private void SendJson(object o) { Response.ContentType = JsonConstants.MediaType; JsonSerializer js = new JsonSerializer(); // TODO: Subclass this from a DataResponsePage SendPreamble(JsonConstants.MediaType); js.Serialize(Response.OutputStream, o); SendPostamble(JsonConstants.MediaType); }