public void MemoryTraceWriterSerializeTest() { Staff staff = new Staff(); staff.Name = "Arnie Admin"; staff.Roles = new List <string> { "Administrator" }; staff.StartDate = new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc); ITraceWriter traceWriter = new MemoryTraceWriter(); JsonConvert.SerializeObject( staff, new JsonSerializerSettings { TraceWriter = traceWriter, Converters = { new JavaScriptDateTimeConverter() } }); Console.WriteLine(traceWriter); // 2012-11-11T12:08:42.761 Info Started serializing Newtonsoft.Json.Tests.Serialization.Staff. Path ''. // 2012-11-11T12:08:42.785 Info Started serializing System.DateTime with converter Newtonsoft.Json.Converters.JavaScriptDateTimeConverter. Path 'StartDate'. // 2012-11-11T12:08:42.791 Info Finished serializing System.DateTime with converter Newtonsoft.Json.Converters.JavaScriptDateTimeConverter. Path 'StartDate'. // 2012-11-11T12:08:42.797 Info Started serializing System.Collections.Generic.List`1[System.String]. Path 'Roles'. // 2012-11-11T12:08:42.798 Info Finished serializing System.Collections.Generic.List`1[System.String]. Path 'Roles'. // 2012-11-11T12:08:42.799 Info Finished serializing Newtonsoft.Json.Tests.Serialization.Staff. Path ''. MemoryTraceWriter memoryTraceWriter = (MemoryTraceWriter)traceWriter; string output = memoryTraceWriter.ToString(); Assert.AreEqual(916, output.Length); Assert.AreEqual(7, memoryTraceWriter.GetTraceMessages().Count()); string json = @"Serialized JSON: { ""Name"": ""Arnie Admin"", ""StartDate"": new Date( 976623132000 ), ""Roles"": [ ""Administrator"" ] }"; Assert.IsTrue(output.Contains(json)); }
public void MemoryTraceWriterTest() { #region MemoryTraceWriterExample Staff staff = new Staff(); staff.Name = "Arnie Admin"; staff.Roles = new List <string> { "Administrator" }; staff.StartDate = new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc); ITraceWriter traceWriter = new MemoryTraceWriter(); JsonConvert.SerializeObject( staff, new JsonSerializerSettings { TraceWriter = traceWriter, Converters = { new JavaScriptDateTimeConverter() } } ); Console.WriteLine(traceWriter); // 2012-11-11T12:08:42.761 Info Started serializing Newtonsoft.Json.Tests.Serialization.Staff. Path ''. // 2012-11-11T12:08:42.785 Info Started serializing System.DateTime with converter Newtonsoft.Json.Converters.JavaScriptDateTimeConverter. Path 'StartDate'. // 2012-11-11T12:08:42.791 Info Finished serializing System.DateTime with converter Newtonsoft.Json.Converters.JavaScriptDateTimeConverter. Path 'StartDate'. // 2012-11-11T12:08:42.797 Info Started serializing System.Collections.Generic.List`1[System.String]. Path 'Roles'. // 2012-11-11T12:08:42.798 Info Finished serializing System.Collections.Generic.List`1[System.String]. Path 'Roles'. // 2012-11-11T12:08:42.799 Info Finished serializing Newtonsoft.Json.Tests.Serialization.Staff. Path ''. // 2013-05-18T21:38:11.255 Verbose Serialized JSON: // { // "Name": "Arnie Admin", // "StartDate": new Date( // 976623132000 // ), // "Roles": [ // "Administrator" // ] // } #endregion MemoryTraceWriter memoryTraceWriter = (MemoryTraceWriter)traceWriter; Assert.AreEqual(916, memoryTraceWriter.ToString().Length); Assert.AreEqual(7, memoryTraceWriter.GetTraceMessages().Count()); }
public void JsonTest() { dynamic o = new NullableDynamicObject(); o.Magic = 100; var memoryTraceWriter = new MemoryTraceWriter(); var jsonSerializerSettings = new JsonSerializerSettings { TraceWriter = memoryTraceWriter }; NullableDynamicObject dynamicObject = o as NullableDynamicObject; Assert.NotNull(dynamicObject); string json = JsonConvert.SerializeObject(dynamicObject, jsonSerializerSettings); dynamic n = JsonConvert.DeserializeObject <NullableDynamicObject>(json, jsonSerializerSettings); _output.WriteLine(memoryTraceWriter.ToString()); Assert.Equal((int)n.Magic, (int)o.Magic); }
private string Serialize(Foo1 f) { //Code copied from JsonConvert.SerializeObject(), with addition of trace writing JsonSerializer jsonSerializer = JsonSerializer.CreateDefault(); var traceWriter = new MemoryTraceWriter(); jsonSerializer.TraceWriter = traceWriter; StringBuilder sb = new StringBuilder(256); StringWriter sw = new StringWriter(sb, CultureInfo.InvariantCulture); using (JsonTextWriter jsonWriter = new JsonTextWriter(sw)) { jsonWriter.Formatting = Formatting.None; jsonSerializer.Serialize(jsonWriter, f, typeof(Foo1)); } Console.Out.WriteLine("Trace output:\n{0}", traceWriter.ToString()); return(sw.ToString()); }
public void Example() { #region Usage string json = @"{ 'FullName': 'Dan Deleted', 'Deleted': true, 'DeletedDate': '2013-01-20T00:00:00' }"; MemoryTraceWriter traceWriter = new MemoryTraceWriter(); Account account = JsonConvert.DeserializeObject <Account>(json, new JsonSerializerSettings { TraceWriter = traceWriter }); Console.WriteLine(traceWriter.ToString()); // 2013-01-21T01:36:24.422 Info Started deserializing Newtonsoft.Json.Tests.Documentation.Examples.TraceWriter+Account. Path 'FullName', line 2, position 20. // 2013-01-21T01:36:24.442 Verbose Could not find member 'DeletedDate' on Newtonsoft.Json.Tests.Documentation.Examples.TraceWriter+Account. Path 'DeletedDate', line 4, position 23. // 2013-01-21T01:36:24.447 Info Finished deserializing Newtonsoft.Json.Tests.Documentation.Examples.TraceWriter+Account. Path '', line 5, position 8. #endregion }