public void CircularStructure_SerializesWithReference() { var serializer = new JsonSerializer(); var obj = new ObjectWithExtendedProps { StringProp = "stringValue", IntProp = 42, BoolProp = true }; var obj2 = new ObjectWithExtendedProps { StringProp = "stringValue", IntProp = 42, BoolProp = true, LoopProperty = obj }; obj.LoopProperty = obj2; var actual = serializer.Serialize(obj); Assert.IsNotNull(actual); Assert.AreEqual(JsonValueType.Object, actual.Type); Assert.IsTrue(actual.Object.ContainsKey("#Def")); Assert.IsTrue(actual.Object.ContainsKey("LoopProperty")); Assert.AreEqual(JsonValueType.Object, actual.Object["LoopProperty"].Type); Assert.IsTrue(actual.Object["LoopProperty"].Object.ContainsKey("LoopProperty")); Assert.AreEqual(JsonValueType.Object, actual.Object["LoopProperty"].Object["LoopProperty"].Type); Assert.IsTrue(actual.Object["LoopProperty"].Object["LoopProperty"].Object.ContainsKey("#Ref")); Assert.AreEqual(actual.Object["#Def"], actual.Object["LoopProperty"].Object["LoopProperty"].Object["#Ref"]); Assert.AreEqual(0, serializer.SerializationMap.Count); }
public void ObjectWithAllDefaultValues() { var obj = new ObjectWithExtendedProps(); var serializer = new JsonSerializer { Options = { AutoSerializeFields = true, EncodeDefaultValues = true } }; var json = serializer.Serialize(obj); Console.WriteLine(json); }
public void CircularStructure_MaintainsReferences() { var serializer = new JsonSerializer(); var json = new JsonObject { { "LoopProperty", new JsonObject { { "LoopProperty", new JsonObject { { "#Ref", "f3a2993b-9b0c-4296-872e-95a9210295f4" } } }, { "StringProp", "stringValueB" }, { "IntProp", 6 }, { "BoolProp", true } } }, { "StringProp", "stringValueA" }, { "IntProp", 42 }, { "#Def", "f3a2993b-9b0c-4296-872e-95a9210295f4" } }; var expected = new ObjectWithExtendedProps { StringProp = "stringValueA", IntProp = 42 }; var obj2 = new ObjectWithExtendedProps { StringProp = "stringValueB", IntProp = 6, BoolProp = true, LoopProperty = expected }; expected.LoopProperty = obj2; var actual = serializer.Deserialize <ObjectWithExtendedProps>(json); Assert.IsNotNull(actual); Assert.AreEqual(expected.StringProp, actual.StringProp); Assert.IsNotNull(actual.LoopProperty); Assert.AreEqual(expected.LoopProperty.StringProp, actual.LoopProperty.StringProp); Assert.IsNotNull(actual.LoopProperty.LoopProperty); Assert.AreSame(actual, actual.LoopProperty.LoopProperty); Assert.AreEqual(0, serializer.SerializationMap.Count); }
public void CircularStructure_SerializesWithReference() { var serializer = new JsonSerializer(); var obj = new ObjectWithExtendedProps { StringProp = "stringValue", IntProp = 42, BoolProp = true }; var obj2 = new ObjectWithExtendedProps { StringProp = "stringValue2", IntProp = 43, BoolProp = true, LoopProperty = obj }; obj.LoopProperty = obj2; JsonValue expected = new JsonObject { ["StringProp"] = "stringValue", ["IntProp"] = 42, ["BoolProp"] = true, ["LoopProperty"] = new JsonObject { ["StringProp"] = "stringValue2", ["IntProp"] = 43, ["BoolProp"] = true, ["LoopProperty"] = new JsonObject { ["$ref"] = "#" } } }; var actual = serializer.Serialize(obj); Assert.AreEqual(expected, actual); }