public void SpecifiedTest() { SpecifiedTestClass c = new SpecifiedTestClass(); c.Name = "James"; c.Age = 27; c.NameSpecified = false; string json = JsonConvert.SerializeObject(c, Formatting.Indented); Assert.AreEqual(@"{ ""Age"": 27 }", json); SpecifiedTestClass deserialized = JsonConvert.DeserializeObject <SpecifiedTestClass>(json); Assert.IsNull(deserialized.Name); Assert.IsFalse(deserialized.NameSpecified); Assert.IsFalse(deserialized.WeightSpecified); Assert.IsFalse(deserialized.HeightSpecified); Assert.IsFalse(deserialized.FavoriteNumberSpecified); Assert.AreEqual(27, deserialized.Age); c.NameSpecified = true; c.WeightSpecified = true; c.HeightSpecified = true; c.FavoriteNumber = 23; json = JsonConvert.SerializeObject(c, Formatting.Indented); Assert.AreEqual(@"{ ""Name"": ""James"", ""Age"": 27, ""Weight"": 0, ""Height"": 0, ""FavoriteNumber"": 23 }", json); deserialized = JsonConvert.DeserializeObject <SpecifiedTestClass>(json); Assert.AreEqual("James", deserialized.Name); Assert.IsTrue(deserialized.NameSpecified); Assert.IsTrue(deserialized.WeightSpecified); Assert.IsTrue(deserialized.HeightSpecified); Assert.IsTrue(deserialized.FavoriteNumberSpecified); Assert.AreEqual(27, deserialized.Age); Assert.AreEqual(23, deserialized.FavoriteNumber); }
public void SpecifiedTest() { SpecifiedTestClass c = new SpecifiedTestClass(); c.Name = "James"; c.Age = 27; c.NameSpecified = false; InMemoryTraceWriter traceWriter = new InMemoryTraceWriter { LevelFilter = TraceLevel.Verbose }; string json = JsonConvert.SerializeObject(c, Formatting.Indented, new JsonSerializerSettings { TraceWriter = traceWriter }); Assert.AreEqual("Started serializing BESSy.Json.Tests.Serialization.SpecifiedTestClass. Path ''.", traceWriter.TraceRecords[0].Message); Assert.AreEqual("IsSpecified result for property 'Name' on BESSy.Json.Tests.Serialization.SpecifiedTestClass: False. Path ''.", traceWriter.TraceRecords[1].Message); Assert.AreEqual("IsSpecified result for property 'Weight' on BESSy.Json.Tests.Serialization.SpecifiedTestClass: False. Path 'Age'.", traceWriter.TraceRecords[2].Message); Assert.AreEqual("IsSpecified result for property 'Height' on BESSy.Json.Tests.Serialization.SpecifiedTestClass: False. Path 'Age'.", traceWriter.TraceRecords[3].Message); Assert.AreEqual("IsSpecified result for property 'FavoriteNumber' on BESSy.Json.Tests.Serialization.SpecifiedTestClass: False. Path 'Age'.", traceWriter.TraceRecords[4].Message); Assert.AreEqual("Finished serializing BESSy.Json.Tests.Serialization.SpecifiedTestClass. Path ''.", traceWriter.TraceRecords[5].Message); Assert.AreEqual(@"{ ""Age"": 27 }", json); traceWriter = new InMemoryTraceWriter { LevelFilter = TraceLevel.Verbose }; SpecifiedTestClass deserialized = JsonConvert.DeserializeObject <SpecifiedTestClass>(json, new JsonSerializerSettings { TraceWriter = traceWriter }); Assert.AreEqual("Started deserializing BESSy.Json.Tests.Serialization.SpecifiedTestClass. Path 'Age', line 2, position 9.", traceWriter.TraceRecords[0].Message); Assert.IsTrue(traceWriter.TraceRecords[1].Message.StartsWith("Finished deserializing BESSy.Json.Tests.Serialization.SpecifiedTestClass. Path ''")); Assert.IsNull(deserialized.Name); Assert.IsFalse(deserialized.NameSpecified); Assert.IsFalse(deserialized.WeightSpecified); Assert.IsFalse(deserialized.HeightSpecified); Assert.IsFalse(deserialized.FavoriteNumberSpecified); Assert.AreEqual(27, deserialized.Age); c.NameSpecified = true; c.WeightSpecified = true; c.HeightSpecified = true; c.FavoriteNumber = 23; json = JsonConvert.SerializeObject(c, Formatting.Indented); Assert.AreEqual(@"{ ""Name"": ""James"", ""Age"": 27, ""Weight"": 0, ""Height"": 0, ""FavoriteNumber"": 23 }", json); traceWriter = new InMemoryTraceWriter { LevelFilter = TraceLevel.Verbose }; deserialized = JsonConvert.DeserializeObject <SpecifiedTestClass>(json, new JsonSerializerSettings { TraceWriter = traceWriter }); Assert.AreEqual("Started deserializing BESSy.Json.Tests.Serialization.SpecifiedTestClass. Path 'Name', line 2, position 10.", traceWriter.TraceRecords[0].Message); Assert.AreEqual("IsSpecified for property 'Name' on BESSy.Json.Tests.Serialization.SpecifiedTestClass set to true. Path 'Name', line 2, position 18.", traceWriter.TraceRecords[1].Message); Assert.AreEqual("IsSpecified for property 'Weight' on BESSy.Json.Tests.Serialization.SpecifiedTestClass set to true. Path 'Weight', line 4, position 14.", traceWriter.TraceRecords[2].Message); Assert.AreEqual("IsSpecified for property 'Height' on BESSy.Json.Tests.Serialization.SpecifiedTestClass set to true. Path 'Height', line 5, position 14.", traceWriter.TraceRecords[3].Message); Assert.IsTrue(traceWriter.TraceRecords[4].Message.StartsWith("Finished deserializing BESSy.Json.Tests.Serialization.SpecifiedTestClass. Path ''")); Assert.AreEqual("James", deserialized.Name); Assert.IsTrue(deserialized.NameSpecified); Assert.IsTrue(deserialized.WeightSpecified); Assert.IsTrue(deserialized.HeightSpecified); Assert.IsTrue(deserialized.FavoriteNumberSpecified); Assert.AreEqual(27, deserialized.Age); Assert.AreEqual(23, deserialized.FavoriteNumber); }