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 SelfReferencingTest() { // Not sure how we'll determine what depth to serialize. var person1 = new Person { Name = "Jason" }; var person2 = new Person { Name = "Joe" }; var connection1 = new Connection { Source = person1, Target = person2 }; var connection2 = new Connection { Source = person2, Target = person1 }; person1.Connections = new[] { connection1, connection2 }; person2.Connections = new[] { connection2, connection1 }; var post = new Post { Title = "Hello world", Body = "Not much to say here.", Created = new DateTime(2008, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), Creator = person1, Tags = new[] { "Monsters", "Blue" } }; var doc = new JsonSerializer().Serialize(post); Assert.Equal(@"{ ""Title"": ""Hello world"", ""Body"": ""Not much to say here."", ""Creator"": { ""Name"": ""Jason"", ""SSN"": null, ""Birth"": null, ""Email"": null, ""Gender"": ""Male"", ""Connections"": [ { ""Source"": { ""Name"": ""Jason"", ""SSN"": null, ""Birth"": null, ""Email"": null, ""Gender"": ""Male"", ""Connections"": [ { ""Source"": { }, ""Target"": { } }, { ""Source"": { }, ""Target"": { } } ], ""Url"": null }, ""Target"": { ""Name"": ""Joe"", ""SSN"": null, ""Birth"": null, ""Email"": null, ""Gender"": ""Male"", ""Connections"": [ { ""Source"": { }, ""Target"": { } }, { ""Source"": { }, ""Target"": { } } ], ""Url"": null } }, { ""Source"": { ""Name"": ""Joe"", ""SSN"": null, ""Birth"": null, ""Email"": null, ""Gender"": ""Male"", ""Connections"": [ { ""Source"": { }, ""Target"": { } }, { ""Source"": { }, ""Target"": { } } ], ""Url"": null }, ""Target"": { ""Name"": ""Jason"", ""SSN"": null, ""Birth"": null, ""Email"": null, ""Gender"": ""Male"", ""Connections"": [ { ""Source"": { }, ""Target"": { } }, { ""Source"": { }, ""Target"": { } } ], ""Url"": null } } ], ""Url"": null }, ""Media"": [ ], ""Tags"": [ ""Monsters"", ""Blue"" ], ""Issued"": null, ""Created"": ""2008-01-01Z"" }", doc.ToString()); //Console.WriteLine(doc.ToString()); }
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); }