public void ReferencedComponentList() { var c1 = new TestComponentSimple(); ReferencedList <TestComponentSimple> l = new ReferencedList <TestComponentSimple>(); l.Add(c1); l.Add(new TestComponentSimple()); l.Add(c1); string json = JsonConvert.SerializeObject(l, Formatting.Indented); StringAssert.AreEqual(@"[ { ""$id"": ""1"", ""MyProperty"": 0 }, { ""$id"": ""2"", ""MyProperty"": 0 }, { ""$ref"": ""1"" } ]", json); }
public void ReferencedComponentDictionary() { var c1 = new TestComponentSimple(); ReferencedDictionary <TestComponentSimple> l = new ReferencedDictionary <TestComponentSimple>(); l.Add("First", c1); l.Add("Second", new TestComponentSimple()); l.Add("Third", c1); string json = JsonConvert.SerializeObject(l, Formatting.Indented); StringAssert.AreEqual(@"{ ""First"": { ""$id"": ""1"", ""MyProperty"": 0 }, ""Second"": { ""$id"": ""2"", ""MyProperty"": 0 }, ""Third"": { ""$ref"": ""1"" } }", json); ReferencedDictionary <TestComponentSimple> d = JsonConvert.DeserializeObject <ReferencedDictionary <TestComponentSimple> >(json); Assert.AreEqual(3, d.Count); Assert.IsTrue(ReferenceEquals(d["First"], d["Third"])); }
public void PropertyItemIsReferenceObject() { TestComponentSimple c1 = new TestComponentSimple(); PropertyItemIsReferenceObject o1 = new PropertyItemIsReferenceObject { Data = new PropertyItemIsReferenceBody { Prop1 = c1, Prop2 = c1, Data = new List <TestComponentSimple> { c1 } } }; string json = JsonConvert.SerializeObject(o1, Formatting.Indented); StringAssert.AreEqual( @"{ ""Data"": { ""Prop1"": { ""$id"": ""1"", ""MyProperty"": 0 }, ""Prop2"": { ""$ref"": ""1"" }, ""Data"": { ""$id"": ""2"", ""$values"": [ { ""MyProperty"": 0 } ] } } }", json ); PropertyItemIsReferenceObject o2 = JsonConvert.DeserializeObject <PropertyItemIsReferenceObject>(json); TestComponentSimple c2 = o2.Data.Prop1; TestComponentSimple c3 = o2.Data.Prop2; TestComponentSimple c4 = o2.Data.Data[0]; Assert.IsTrue(ReferenceEquals(c2, c3)); Assert.IsFalse(ReferenceEquals(c2, c4)); }
public void TypeNameComponentList() { var c1 = new TestComponentSimple(); TypeNameList <object> l = new TypeNameList <object>(); l.Add(c1); l.Add(new Employee { BirthDate = new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc), Department = "Department!" }); l.Add("String!"); l.Add(long.MaxValue); string json = JsonConvert.SerializeObject(l, Formatting.Indented); Assert.AreEqual(@"[ { ""$type"": ""Newtonsoft.Json.Tests.Serialization.TestComponentSimple, Newtonsoft.Json.Tests"", ""MyProperty"": 0 }, { ""$type"": ""Newtonsoft.Json.Tests.TestObjects.Employee, Newtonsoft.Json.Tests"", ""FirstName"": null, ""LastName"": null, ""BirthDate"": ""2000-12-12T12:12:12Z"", ""Department"": ""Department!"", ""JobTitle"": null }, ""String!"", 9223372036854775807 ]", json); TypeNameList <object> l2 = JsonConvert.DeserializeObject <TypeNameList <object> >(json); Assert.AreEqual(4, l2.Count); CustomAssert.IsInstanceOfType(typeof(TestComponentSimple), l2[0]); CustomAssert.IsInstanceOfType(typeof(Employee), l2[1]); CustomAssert.IsInstanceOfType(typeof(string), l2[2]); CustomAssert.IsInstanceOfType(typeof(long), l2[3]); }