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.Equal("ShouldSerialize result for property 'Name' on OpenGamingLibrary.Json.Test.Serialization.ShouldSerializeTestClass: True. Path ''.", traceWriter.TraceRecords[1].Message); Assert.Equal(TraceLevel.Verbose, traceWriter.TraceRecords[1].Level); traceWriter = new InMemoryTraceWriter { LevelFilter = TraceLevel.Verbose }; c._shouldSerializeName = false; JsonConvert.SerializeObject(c, new JsonSerializerSettings { TraceWriter = traceWriter }); Assert.Equal("ShouldSerialize result for property 'Name' on OpenGamingLibrary.Json.Test.Serialization.ShouldSerializeTestClass: False. Path ''.", traceWriter.TraceRecords[1].Message); Assert.Equal(TraceLevel.Verbose, traceWriter.TraceRecords[1].Level); }
public void ShouldSerializeTest() { ShouldSerializeTestClass c = new ShouldSerializeTestClass(); c.Name = "James"; c.Age = 27; string json = JsonConvert.SerializeObject(c, Formatting.Indented); StringAssert.Equal(@"{ ""Age"": 27 }", json); c._shouldSerializeName = true; json = JsonConvert.SerializeObject(c, Formatting.Indented); StringAssert.Equal(@"{ ""Name"": ""James"", ""Age"": 27 }", json); ShouldSerializeTestClass deserialized = JsonConvert.DeserializeObject<ShouldSerializeTestClass>(json); Assert.Equal("James", deserialized.Name); Assert.Equal(27, deserialized.Age); }