public void ShouldSerializeTest() { ShouldSerializeTestClass c = new ShouldSerializeTestClass(); c.Name = "James"; c.Age = 27; string json = JsonConvert.SerializeObject(c, Formatting.Indented); Assert.AreEqual(@"{ ""Age"": 27 }", json); c._shouldSerializeName = true; json = JsonConvert.SerializeObject(c, Formatting.Indented); Assert.AreEqual(@"{ ""Name"": ""James"", ""Age"": 27 }", json); ShouldSerializeTestClass deserialized = JsonConvert.DeserializeObject <ShouldSerializeTestClass>(json); Assert.AreEqual("James", deserialized.Name); Assert.AreEqual(27, deserialized.Age); }
public void ShouldSerializeTestClass() { ShouldSerializeTestClass c = new ShouldSerializeTestClass(); c.Age = 29; c.Name = "Jim"; c._shouldSerializeName = true; InMemoryTraceWriter traceWriter = new InMemoryTraceWriter { LevelFilter = TraceLevel.Verbose }; JsonConvert.SerializeObject(c, new JsonSerializerSettings { TraceWriter = traceWriter }); Assert.AreEqual("ShouldSerialize result for property 'Name' on BESSy.Json.Tests.Serialization.ShouldSerializeTestClass: True. Path ''.", traceWriter.TraceRecords[1].Message); Assert.AreEqual(TraceLevel.Verbose, traceWriter.TraceRecords[1].Level); traceWriter = new InMemoryTraceWriter { LevelFilter = TraceLevel.Verbose }; c._shouldSerializeName = false; JsonConvert.SerializeObject(c, new JsonSerializerSettings { TraceWriter = traceWriter }); Assert.AreEqual("ShouldSerialize result for property 'Name' on BESSy.Json.Tests.Serialization.ShouldSerializeTestClass: False. Path ''.", traceWriter.TraceRecords[1].Message); Assert.AreEqual(TraceLevel.Verbose, traceWriter.TraceRecords[1].Level); }