From b96f8ed254e4ffb020b510403d1b10ecef1d4237 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Thu, 26 Jun 2014 16:23:05 +1200 Subject: [PATCH] -Tests --- Src/Newtonsoft.Json.Tests/DemoTests.cs | 348 +++++++++++++++++++++++++ 1 file changed, 348 insertions(+) diff --git a/Src/Newtonsoft.Json.Tests/DemoTests.cs b/Src/Newtonsoft.Json.Tests/DemoTests.cs index beb5f7b0..95bd010f 100644 --- a/Src/Newtonsoft.Json.Tests/DemoTests.cs +++ b/Src/Newtonsoft.Json.Tests/DemoTests.cs @@ -28,7 +28,9 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Newtonsoft.Json.Converters; using Newtonsoft.Json.Schema; +using Newtonsoft.Json.Serialization; #if !NETFX_CORE using NUnit.Framework; #else @@ -167,5 +169,351 @@ namespace Newtonsoft.Json.Tests return objectType == typeof(HtmlColor); } } + + [Test] + public void SerializationGuide() + { + IList roles = new List + { + "User", + "Admin" + }; + + string roleJson = JsonConvert.SerializeObject(roles, Formatting.Indented); + // [ + // "User", + // "Admin" + // ] + + IDictionary dailyRegistrations = new Dictionary + { + { new DateTime(2014, 6, 1), 23 }, + { new DateTime(2014, 6, 2), 50 } + }; + + string regJson = JsonConvert.SerializeObject(dailyRegistrations, Formatting.Indented); + // { + // "2014-06-01T00:00:00": 23, + // "2014-06-02T00:00:00": 50 + // } + + City c = new City { Name = "Oslo", Population = 650000 }; + + string cityJson = JsonConvert.SerializeObject(c, Formatting.Indented); + // { + // "Name": "Oslo", + // "Population": 650000 + // } + } + + [Test] + public void SerializationBasics() + { + IList roles = new List + { + "User", + "Admin" + }; + + MemoryTraceWriter traceWriter = new MemoryTraceWriter(); + + string j = JsonConvert.SerializeObject(roles, new JsonSerializerSettings + { + Formatting = Formatting.Indented, + TraceWriter = traceWriter + }); + + string trace = traceWriter.ToString(); + // Started serializing System.Collections.Generic.List`1[System.String]. + // Finished serializing System.Collections.Generic.List`1[System.String]. + // 2014-05-13T13:41:53.706 Verbose Serialized JSON: + // [ + // "User", + // "Admin" + // ] + + Console.WriteLine(trace); + } + + [Test] + public void SerializationBasics2() + { + var s = new Session + { + Name = "Serialize All The Things", + Date = new DateTime(2014, 6, 4) + }; + + string j = JsonConvert.SerializeObject(s, new JsonSerializerSettings + { + Formatting = Formatting.Indented, + Converters = { new JavaScriptDateTimeConverter() } + }); + // { + // "Name": "Serialize All The Things", + // "Date": new Date(1401796800000) + // } + + Console.WriteLine(j); + } + + [Test] + public void DeserializationBasics1() + { + string j = @"{ + 'Name': 'Serialize All The Things', + 'Date': new Date(1401796800000) + }"; + + var s = JsonConvert.DeserializeObject(j, new JsonSerializerSettings + { + Converters = { new JavaScriptDateTimeConverter() } + }); + // Name = Serialize All The Things + // Date = Tuesday, 3 June 2014 + } + + [Test] + public void DeserializationBasics2() + { + Session s = new Session(); + s.Date = new DateTime(2014, 6, 4); + + string j = @"{ + 'Name': 'Serialize All The Things' + }"; + + JsonConvert.PopulateObject(j, s); + // Name = Serialize All The Things + // Date = Tuesday, 3 June 2014 + } + + public class City + { + public string Name { get; set; } + public int Population { get; set; } + } + + public class Employee + { + public string Name { get; set; } + } + + public class Manager : Employee + { + public IList Reportees { get; set; } + } + + [Test] + public void SerializeReferencesByValue() + { + Employee arnie = new Employee { Name = "Arnie Admin" }; + Manager mike = new Manager { Name = "Mike Manager" }; + Manager susan = new Manager { Name = "Susan Supervisor" }; + + mike.Reportees = new[] { arnie, susan }; + susan.Reportees = new[] { arnie }; + + string json = JsonConvert.SerializeObject(mike, Formatting.Indented); + // { + // "Reportees": [ + // { + // "Name": "Arnie Admin" + // }, + // { + // "Reportees": [ + // { + // "Name": "Arnie Admin" + // } + // ], + // "Name": "Susan Supervisor" + // } + // ], + // "Name": "Mike Manager" + // } + Console.WriteLine(json); + } + + [Test] + public void SerializeReferencesWithMetadata() + { + Employee arnie = new Employee { Name = "Arnie Admin" }; + Manager mike = new Manager { Name = "Mike Manager" }; + Manager susan = new Manager { Name = "Susan Supervisor" }; + + mike.Reportees = new[] { arnie, susan }; + susan.Reportees = new[] { arnie }; + + string json = JsonConvert.SerializeObject(mike, new JsonSerializerSettings + { + Formatting = Formatting.Indented, + TypeNameHandling = TypeNameHandling.Objects, + PreserveReferencesHandling = PreserveReferencesHandling.Objects + }); + // { + // "$id": "1", + // "$type": "YourNamespace.Manager, YourAssembly", + // "Name": "Mike Manager", + // "Reportees": [ + // { + // "$id": "2", + // "$type": "YourNamespace.Employee, YourAssembly", + // "Name": "Arnie Admin" + // }, + // { + // "$id": "3", + // "$type": "YourNamespace.Manager, YourAssembly", + // "Name": "Susan Supervisor", + // "Reportees": [ + // { + // "$ref": "2" + // } + // ] + // } + // ] + // } + Console.WriteLine(json); + } + + [Test] + public void RoundtripTypesAndReferences() + { + string json = @"{ + '$id': '1', + '$type': 'Newtonsoft.Json.Tests.DemoTests+Manager, Newtonsoft.Json.Tests', + 'Reportees': [ + { + '$id': '2', + '$type': 'Newtonsoft.Json.Tests.DemoTests+Employee, Newtonsoft.Json.Tests', + 'Name': 'Arnie Admin' + }, + { + '$id': '3', + '$type': 'Newtonsoft.Json.Tests.DemoTests+Manager, Newtonsoft.Json.Tests', + 'Reportees': [ + { + '$ref': '2' + } + ], + 'Name': 'Susan Supervisor' + } + ], + 'Name': 'Mike Manager' +}"; + + var e = JsonConvert.DeserializeObject(json, new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.Objects, + PreserveReferencesHandling = PreserveReferencesHandling.Objects + }); + // Name = Mike Manager + // Reportees = Arnie Admin, Susan Supervisor + + Manager mike = (Manager)e; + Manager susan = (Manager)mike.Reportees[1]; + + Object.ReferenceEquals(mike.Reportees[0], susan.Reportees[0]); + // true + + Assert.IsTrue(ReferenceEquals(mike.Reportees[0], susan.Reportees[0])); + } + + public class House + { + public string StreetAddress { get; set; } + public DateTime BuildDate { get; set; } + public int Bedrooms { get; set; } + public decimal FloorArea { get; set; } + } + + public class House1 + { + public string StreetAddress { get; set; } + [JsonIgnore] + public int Bedrooms { get; set; } + [JsonIgnore] + public decimal FloorArea { get; set; } + [JsonIgnore] + public DateTime BuildDate { get; set; } + } + + [JsonObject(MemberSerialization.OptIn)] + public class House3 + { + [JsonProperty] + public string StreetAddress { get; set; } + public int Bedrooms { get; set; } + public decimal FloorArea { get; set; } + public DateTime BuildDate { get; set; } + } + + [JsonObject(MemberSerialization.OptIn)] + public class House2 + { + [JsonProperty("address")] + public string StreetAddress { get; set; } + public int Bedrooms { get; set; } + public decimal FloorArea { get; set; } + public DateTime BuildDate { get; set; } + } + + [JsonObject(MemberSerialization.OptIn)] + public class House4 + { + [JsonProperty("address", Order = 2)] + public string StreetAddress { get; set; } + public int Bedrooms { get; set; } + public decimal FloorArea { get; set; } + [JsonProperty("buildDate", Order = 1)] + public DateTime BuildDate { get; set; } + } + + [JsonObject(MemberSerialization.OptIn)] + public class House5 + { + [JsonProperty("address", Order = 2)] + public string StreetAddress { get; set; } + public int Bedrooms { get; set; } + public decimal FloorArea { get; set; } + [JsonProperty("buildDate", Order = 1)] + [JsonConverter(typeof(JavaScriptDateTimeConverter))] + public DateTime BuildDate { get; set; } + } + + [Test] + public void SerializeAttributes() + { + var house = new House3(); + house.StreetAddress = "221B Baker Street"; + house.Bedrooms = 2; + house.FloorArea = 100m; + house.BuildDate = new DateTime(1890, 1, 1); + + string json = JsonConvert.SerializeObject(house, Formatting.Indented); + // { + // "StreetAddress": "221B Baker Street", + // "Bedrooms": 2, + // "FloorArea": 100.0, + // "BuildDate": "1890-01-01T00:00:00" + // } + + // { + // "StreetAddress": "221B Baker Street" + // } + + // { + // "address": "221B Baker Street" + // } + + // { + // "buildDate": "1890-01-01T00:00:00", + // "address": "221B Baker Street" + // } + + // { + // "buildDate": new Date(-2524568400000), + // "address": "221B Baker Street" + // } + } } } \ No newline at end of file