PageRenderTime 82ms CodeModel.GetById 2ms app.highlight 70ms RepoModel.GetById 1ms app.codeStats 1ms

/Src/Newtonsoft.Json.Tests/Serialization/TraceWriterTests.cs

https://bitbucket.org/Ecarestia/newtonsoft.json
C# | 1234 lines | 1062 code | 150 blank | 22 comment | 21 complexity | 0a2468e555e7cdf4c2239ce204e6018a MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1using System;
   2using System.Collections.Generic;
   3using System.Diagnostics;
   4using System.Globalization;
   5using System.IO;
   6using Newtonsoft.Json.Linq;
   7#if !(NET20 || NET35 || PORTABLE || PORTABLE40) || NETSTANDARD1_1
   8using System.Numerics;
   9#endif
  10using System.Runtime.Serialization;
  11using System.Runtime.Serialization.Formatters;
  12using System.Text;
  13#if DNXCORE50
  14using Xunit;
  15using Test = Xunit.FactAttribute;
  16using Assert = Newtonsoft.Json.Tests.XUnitAssert;
  17#else
  18using NUnit.Framework;
  19#endif
  20using Newtonsoft.Json.Converters;
  21using Newtonsoft.Json.Serialization;
  22using Newtonsoft.Json.Tests.TestObjects;
  23using Newtonsoft.Json.Tests.TestObjects.Organization;
  24using Newtonsoft.Json.Utilities;
  25#if NET20
  26using Newtonsoft.Json.Utilities.LinqBridge;
  27#else
  28using System.Linq;
  29
  30#endif
  31
  32namespace Newtonsoft.Json.Tests.Serialization
  33{
  34    public class Staff
  35    {
  36        public string Name { get; set; }
  37        public DateTime StartDate { get; set; }
  38        public IList<string> Roles { get; set; }
  39    }
  40
  41    [TestFixture]
  42    public class TraceWriterTests : TestFixtureBase
  43    {
  44#if !(PORTABLE || DNXCORE50 || PORTABLE40)
  45        [Test]
  46        public void DiagnosticsTraceWriterTest()
  47        {
  48            StringWriter sw = new StringWriter();
  49            TextWriterTraceListener listener = new TextWriterTraceListener(sw);
  50
  51            try
  52            {
  53                Trace.AutoFlush = true;
  54                Trace.Listeners.Add(listener);
  55
  56                DiagnosticsTraceWriter traceWriter = new DiagnosticsTraceWriter();
  57                traceWriter.Trace(TraceLevel.Verbose, "Verbose!", null);
  58                traceWriter.Trace(TraceLevel.Info, "Info!", null);
  59                traceWriter.Trace(TraceLevel.Warning, "Warning!", null);
  60                traceWriter.Trace(TraceLevel.Error, "Error!", null);
  61                traceWriter.Trace(TraceLevel.Off, "Off!", null);
  62
  63                StringAssert.AreEqual(@"Newtonsoft.Json Verbose: 0 : Verbose!
  64Newtonsoft.Json Information: 0 : Info!
  65Newtonsoft.Json Warning: 0 : Warning!
  66Newtonsoft.Json Error: 0 : Error!
  67", sw.ToString());
  68            }
  69            finally
  70            {
  71                Trace.Listeners.Remove(listener);
  72                Trace.AutoFlush = false;
  73            }
  74        }
  75#endif
  76
  77        [Test]
  78        public void WriteNullableByte()
  79        {
  80            StringWriter sw = new StringWriter();
  81            TraceJsonWriter traceJsonWriter = new TraceJsonWriter(new JsonTextWriter(sw));
  82            traceJsonWriter.WriteStartArray();
  83            traceJsonWriter.WriteValue((byte?)null);
  84            traceJsonWriter.WriteEndArray();
  85
  86            StringAssert.AreEqual(@"Serialized JSON: 
  87[
  88  null
  89]", traceJsonWriter.GetSerializedJsonMessage());
  90        }
  91
  92        [Test]
  93        public void WriteNullObject()
  94        {
  95            StringWriter sw = new StringWriter();
  96            TraceJsonWriter traceJsonWriter = new TraceJsonWriter(new JsonTextWriter(sw));
  97            traceJsonWriter.WriteStartArray();
  98            traceJsonWriter.WriteValue((object)null);
  99            traceJsonWriter.WriteEndArray();
 100
 101            StringAssert.AreEqual(@"Serialized JSON: 
 102[
 103  null
 104]", traceJsonWriter.GetSerializedJsonMessage());
 105        }
 106
 107        [Test]
 108        public void WriteNullString()
 109        {
 110            StringWriter sw = new StringWriter();
 111            TraceJsonWriter traceJsonWriter = new TraceJsonWriter(new JsonTextWriter(sw));
 112            traceJsonWriter.WriteStartArray();
 113            traceJsonWriter.WriteValue((string)null);
 114            traceJsonWriter.WriteEndArray();
 115
 116            StringAssert.AreEqual(@"Serialized JSON: 
 117[
 118  null
 119]", traceJsonWriter.GetSerializedJsonMessage());
 120        }
 121
 122        [Test]
 123        public void WriteNullUri()
 124        {
 125            StringWriter sw = new StringWriter();
 126            TraceJsonWriter traceJsonWriter = new TraceJsonWriter(new JsonTextWriter(sw));
 127            traceJsonWriter.WriteStartArray();
 128            traceJsonWriter.WriteValue((Uri)null);
 129            traceJsonWriter.WriteEndArray();
 130
 131            StringAssert.AreEqual(@"Serialized JSON: 
 132[
 133  null
 134]", traceJsonWriter.GetSerializedJsonMessage());
 135        }
 136
 137        [Test]
 138        public void WriteNullByteArray()
 139        {
 140            StringWriter sw = new StringWriter();
 141            TraceJsonWriter traceJsonWriter = new TraceJsonWriter(new JsonTextWriter(sw));
 142            traceJsonWriter.WriteStartArray();
 143            traceJsonWriter.WriteValue((byte[])null);
 144            traceJsonWriter.WriteEndArray();
 145
 146            StringAssert.AreEqual(@"Serialized JSON: 
 147[
 148  null
 149]", traceJsonWriter.GetSerializedJsonMessage());
 150        }
 151
 152        [Test]
 153        public void WriteJRaw()
 154        {
 155            ITraceWriter traceWriter = new MemoryTraceWriter();
 156
 157            JRaw settings = new JRaw("$('#element')");
 158            string json = JsonConvert.SerializeObject(settings, new JsonSerializerSettings
 159            {
 160                TraceWriter = traceWriter
 161            });
 162
 163            Assert.AreEqual("$('#element')", json);
 164
 165            Assert.IsTrue(traceWriter.ToString().EndsWith("Verbose Serialized JSON: " + Environment.NewLine + "$('#element')", StringComparison.Ordinal));
 166        }
 167
 168        [Test]
 169        public void WriteJRawInArray()
 170        {
 171            ITraceWriter traceWriter = new MemoryTraceWriter();
 172
 173            List<JRaw> raws = new List<JRaw>
 174            {
 175                new JRaw("$('#element')"),
 176                new JRaw("$('#element')"),
 177                new JRaw("$('#element')")
 178            };
 179
 180            string json = JsonConvert.SerializeObject(raws, new JsonSerializerSettings
 181            {
 182                TraceWriter = traceWriter,
 183                Formatting = Formatting.Indented
 184            });
 185
 186            StringAssert.AreEqual(@"[
 187  $('#element'),
 188  $('#element'),
 189  $('#element')
 190]", json);
 191
 192            Assert.IsTrue(traceWriter.ToString().EndsWith(@"Verbose Serialized JSON: 
 193[
 194  $('#element'),
 195  $('#element'),
 196  $('#element')
 197]", StringComparison.Ordinal));
 198        }
 199
 200        [Test]
 201        public void MemoryTraceWriterSerializeTest()
 202        {
 203            Staff staff = new Staff();
 204            staff.Name = "Arnie Admin";
 205            staff.Roles = new List<string> { "Administrator" };
 206            staff.StartDate = new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc);
 207
 208            ITraceWriter traceWriter = new MemoryTraceWriter();
 209
 210            JsonConvert.SerializeObject(
 211                staff,
 212                new JsonSerializerSettings { TraceWriter = traceWriter, Converters = { new JavaScriptDateTimeConverter() } });
 213
 214            // 2012-11-11T12:08:42.761 Info Started serializing Newtonsoft.Json.Tests.Serialization.Staff. Path ''.
 215            // 2012-11-11T12:08:42.785 Info Started serializing System.DateTime with converter Newtonsoft.Json.Converters.JavaScriptDateTimeConverter. Path 'StartDate'.
 216            // 2012-11-11T12:08:42.791 Info Finished serializing System.DateTime with converter Newtonsoft.Json.Converters.JavaScriptDateTimeConverter. Path 'StartDate'.
 217            // 2012-11-11T12:08:42.797 Info Started serializing System.Collections.Generic.List`1[System.String]. Path 'Roles'.
 218            // 2012-11-11T12:08:42.798 Info Finished serializing System.Collections.Generic.List`1[System.String]. Path 'Roles'.
 219            // 2012-11-11T12:08:42.799 Info Finished serializing Newtonsoft.Json.Tests.Serialization.Staff. Path ''.
 220
 221            MemoryTraceWriter memoryTraceWriter = (MemoryTraceWriter)traceWriter;
 222            string output = memoryTraceWriter.ToString();
 223
 224            Assert.AreEqual(916, output.Length);
 225            Assert.AreEqual(7, memoryTraceWriter.GetTraceMessages().Count());
 226
 227            string json = @"Serialized JSON: 
 228{
 229  ""Name"": ""Arnie Admin"",
 230  ""StartDate"": new Date(
 231    976623132000
 232  ),
 233  ""Roles"": [
 234    ""Administrator""
 235  ]
 236}";
 237
 238            json = StringAssert.Normalize(json);
 239            output = StringAssert.Normalize(output);
 240
 241            Assert.IsTrue(output.Contains(json));
 242        }
 243
 244        [Test]
 245        public void MemoryTraceWriterDeserializeTest()
 246        {
 247            string json = @"{
 248  ""Name"": ""Arnie Admin"",
 249  ""StartDate"": new Date(
 250    976623132000
 251  ),
 252  ""Roles"": [
 253    ""Administrator""
 254  ]
 255}";
 256
 257            Staff staff = new Staff();
 258            staff.Name = "Arnie Admin";
 259            staff.Roles = new List<string> { "Administrator" };
 260            staff.StartDate = new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc);
 261
 262            ITraceWriter traceWriter = new MemoryTraceWriter();
 263
 264            JsonConvert.DeserializeObject<Staff>(
 265                json,
 266                new JsonSerializerSettings
 267                {
 268                    TraceWriter = traceWriter,
 269                    Converters = { new JavaScriptDateTimeConverter() },
 270                    MetadataPropertyHandling = MetadataPropertyHandling.Default
 271                });
 272
 273            // 2012-11-11T12:08:42.761 Info Started serializing Newtonsoft.Json.Tests.Serialization.Staff. Path ''.
 274            // 2012-11-11T12:08:42.785 Info Started serializing System.DateTime with converter Newtonsoft.Json.Converters.JavaScriptDateTimeConverter. Path 'StartDate'.
 275            // 2012-11-11T12:08:42.791 Info Finished serializing System.DateTime with converter Newtonsoft.Json.Converters.JavaScriptDateTimeConverter. Path 'StartDate'.
 276            // 2012-11-11T12:08:42.797 Info Started serializing System.Collections.Generic.List`1[System.String]. Path 'Roles'.
 277            // 2012-11-11T12:08:42.798 Info Finished serializing System.Collections.Generic.List`1[System.String]. Path 'Roles'.
 278            // 2012-11-11T12:08:42.799 Info Finished serializing Newtonsoft.Json.Tests.Serialization.Staff. Path ''.
 279            // 2013-05-19T00:07:24.360 Verbose Deserialized JSON: 
 280            // {
 281            //   "Name": "Arnie Admin",
 282            //   "StartDate": new Date(
 283            //     976623132000
 284            //   ),
 285            //   "Roles": [
 286            //     "Administrator"
 287            //   ]
 288            // }
 289
 290            MemoryTraceWriter memoryTraceWriter = (MemoryTraceWriter)traceWriter;
 291            string output = memoryTraceWriter.ToString();
 292
 293            Assert.AreEqual(1058, output.Length);
 294            Assert.AreEqual(7, memoryTraceWriter.GetTraceMessages().Count());
 295
 296            json = StringAssert.Normalize(json);
 297            output = StringAssert.Normalize(output);
 298
 299            Assert.IsTrue(output.Contains(json));
 300        }
 301
 302        [Test]
 303        public void MemoryTraceWriterLimitTest()
 304        {
 305            MemoryTraceWriter traceWriter = new MemoryTraceWriter();
 306
 307            for (int i = 0; i < 1005; i++)
 308            {
 309                traceWriter.Trace(TraceLevel.Verbose, (i + 1).ToString(CultureInfo.InvariantCulture), null);
 310            }
 311
 312            IList<string> traceMessages = traceWriter.GetTraceMessages().ToList();
 313
 314            Assert.AreEqual(1000, traceMessages.Count);
 315
 316            Assert.IsTrue(traceMessages.First().EndsWith(" 6"));
 317            Assert.IsTrue(traceMessages.Last().EndsWith(" 1005"));
 318        }
 319
 320        [Test]
 321        public void Serialize()
 322        {
 323            var traceWriter = new InMemoryTraceWriter
 324            {
 325                LevelFilter = TraceLevel.Info
 326            };
 327
 328            string json =
 329                JsonConvert.SerializeObject(
 330                    new TraceTestObject
 331                    {
 332                        StringArray = new[] { "1", "2" },
 333                        IntList = new List<int> { 1, 2 },
 334                        Version = new Version(1, 2, 3, 4),
 335                        StringDictionary =
 336                            new Dictionary<string, string>
 337                            {
 338                                { "1", "!" },
 339                                { "Two", "!!" },
 340                                { "III", "!!!" }
 341                            },
 342                        Double = 1.1d
 343                    },
 344                    new JsonSerializerSettings
 345                    {
 346                        TraceWriter = traceWriter,
 347                        Formatting = Formatting.Indented
 348                    });
 349
 350            Assert.AreEqual("Started serializing Newtonsoft.Json.Tests.Serialization.TraceTestObject. Path ''.", traceWriter.TraceRecords[0].Message);
 351            Assert.AreEqual("Started serializing System.Collections.Generic.List`1[System.Int32]. Path 'IntList'.", traceWriter.TraceRecords[1].Message);
 352            Assert.AreEqual("Finished serializing System.Collections.Generic.List`1[System.Int32]. Path 'IntList'.", traceWriter.TraceRecords[2].Message);
 353            Assert.AreEqual("Started serializing System.String[]. Path 'StringArray'.", traceWriter.TraceRecords[3].Message);
 354            Assert.AreEqual("Finished serializing System.String[]. Path 'StringArray'.", traceWriter.TraceRecords[4].Message);
 355            Assert.AreEqual("Started serializing System.Version. Path 'Version'.", traceWriter.TraceRecords[5].Message);
 356            Assert.AreEqual("Finished serializing System.Version. Path 'Version'.", traceWriter.TraceRecords[6].Message);
 357            Assert.AreEqual("Started serializing System.Collections.Generic.Dictionary`2[System.String,System.String]. Path 'StringDictionary'.", traceWriter.TraceRecords[7].Message);
 358            Assert.AreEqual("Finished serializing System.Collections.Generic.Dictionary`2[System.String,System.String]. Path 'StringDictionary'.", traceWriter.TraceRecords[8].Message);
 359            Assert.AreEqual("Finished serializing Newtonsoft.Json.Tests.Serialization.TraceTestObject. Path ''.", traceWriter.TraceRecords[9].Message);
 360
 361            Assert.IsFalse(traceWriter.TraceRecords.Any(r => r.Level == TraceLevel.Verbose));
 362        }
 363
 364        [Test]
 365        public void Deserialize()
 366        {
 367            InMemoryTraceWriter traceWriter = new InMemoryTraceWriter
 368            {
 369                LevelFilter = TraceLevel.Info
 370            };
 371
 372            TraceTestObject o2 = JsonConvert.DeserializeObject<TraceTestObject>(
 373                @"{
 374  ""IntList"": [
 375    1,
 376    2
 377  ],
 378  ""StringArray"": [
 379    ""1"",
 380    ""2""
 381  ],
 382  ""Version"": {
 383    ""Major"": 1,
 384    ""Minor"": 2,
 385    ""Build"": 3,
 386    ""Revision"": 4,
 387    ""MajorRevision"": 0,
 388    ""MinorRevision"": 4
 389  },
 390  ""StringDictionary"": {
 391    ""1"": ""!"",
 392    ""Two"": ""!!"",
 393    ""III"": ""!!!""
 394  },
 395  ""Double"": 1.1
 396}",
 397                new JsonSerializerSettings
 398                {
 399                    TraceWriter = traceWriter
 400                });
 401
 402            Assert.AreEqual(2, o2.IntList.Count);
 403            Assert.AreEqual(2, o2.StringArray.Length);
 404            Assert.AreEqual(1, o2.Version.Major);
 405            Assert.AreEqual(2, o2.Version.Minor);
 406            Assert.AreEqual(3, o2.StringDictionary.Count);
 407            Assert.AreEqual(1.1d, o2.Double);
 408
 409            Assert.AreEqual("Started deserializing Newtonsoft.Json.Tests.Serialization.TraceTestObject. Path 'IntList', line 2, position 12.", traceWriter.TraceRecords[0].Message);
 410            Assert.AreEqual("Started deserializing System.Collections.Generic.IList`1[System.Int32]. Path 'IntList', line 2, position 14.", traceWriter.TraceRecords[1].Message);
 411            Assert.IsTrue(traceWriter.TraceRecords[2].Message.StartsWith("Finished deserializing System.Collections.Generic.IList`1[System.Int32]. Path 'IntList'"));
 412            Assert.AreEqual("Started deserializing System.String[]. Path 'StringArray', line 6, position 18.", traceWriter.TraceRecords[3].Message);
 413            Assert.IsTrue(traceWriter.TraceRecords[4].Message.StartsWith("Finished deserializing System.String[]. Path 'StringArray'"));
 414            Assert.AreEqual("Deserializing System.Version using creator with parameters: Major, Minor, Build, Revision. Path 'Version.Major', line 11, position 12.", traceWriter.TraceRecords[5].Message);
 415            Assert.IsTrue(traceWriter.TraceRecords[6].Message.StartsWith("Started deserializing System.Version. Path 'Version'"));
 416            Assert.IsTrue(traceWriter.TraceRecords[7].Message.StartsWith("Finished deserializing System.Version. Path 'Version'"));
 417            Assert.AreEqual("Started deserializing System.Collections.Generic.IDictionary`2[System.String,System.String]. Path 'StringDictionary.1', line 19, position 8.", traceWriter.TraceRecords[8].Message);
 418            Assert.IsTrue(traceWriter.TraceRecords[9].Message.StartsWith("Finished deserializing System.Collections.Generic.IDictionary`2[System.String,System.String]. Path 'StringDictionary'"));
 419            Assert.IsTrue(traceWriter.TraceRecords[10].Message.StartsWith("Finished deserializing Newtonsoft.Json.Tests.Serialization.TraceTestObject. Path ''"));
 420
 421            Assert.IsFalse(traceWriter.TraceRecords.Any(r => r.Level == TraceLevel.Verbose));
 422        }
 423
 424        [Test]
 425        public void Populate()
 426        {
 427            InMemoryTraceWriter traceWriter = new InMemoryTraceWriter
 428            {
 429                LevelFilter = TraceLevel.Info
 430            };
 431
 432            TraceTestObject o2 = new TraceTestObject();
 433
 434            JsonConvert.PopulateObject(@"{
 435  ""IntList"": [
 436    1,
 437    2
 438  ],
 439  ""StringArray"": [
 440    ""1"",
 441    ""2""
 442  ],
 443  ""Version"": {
 444    ""Major"": 1,
 445    ""Minor"": 2,
 446    ""Build"": 3,
 447    ""Revision"": 4,
 448    ""MajorRevision"": 0,
 449    ""MinorRevision"": 4
 450  },
 451  ""StringDictionary"": {
 452    ""1"": ""!"",
 453    ""Two"": ""!!"",
 454    ""III"": ""!!!""
 455  },
 456  ""Double"": 1.1
 457}",
 458                o2,
 459                new JsonSerializerSettings
 460                {
 461                    TraceWriter = traceWriter,
 462                    MetadataPropertyHandling = MetadataPropertyHandling.Default
 463                });
 464
 465            Assert.AreEqual(2, o2.IntList.Count);
 466            Assert.AreEqual(2, o2.StringArray.Length);
 467            Assert.AreEqual(1, o2.Version.Major);
 468            Assert.AreEqual(2, o2.Version.Minor);
 469            Assert.AreEqual(3, o2.StringDictionary.Count);
 470            Assert.AreEqual(1.1d, o2.Double);
 471
 472            Assert.AreEqual("Started deserializing Newtonsoft.Json.Tests.Serialization.TraceTestObject. Path 'IntList', line 2, position 12.", traceWriter.TraceRecords[0].Message);
 473            Assert.AreEqual("Started deserializing System.Collections.Generic.IList`1[System.Int32]. Path 'IntList', line 2, position 14.", traceWriter.TraceRecords[1].Message);
 474            Assert.IsTrue(traceWriter.TraceRecords[2].Message.StartsWith("Finished deserializing System.Collections.Generic.IList`1[System.Int32]. Path 'IntList'"));
 475            Assert.AreEqual("Started deserializing System.String[]. Path 'StringArray', line 6, position 18.", traceWriter.TraceRecords[3].Message);
 476            Assert.IsTrue(traceWriter.TraceRecords[4].Message.StartsWith("Finished deserializing System.String[]. Path 'StringArray'"));
 477            Assert.AreEqual("Deserializing System.Version using creator with parameters: Major, Minor, Build, Revision. Path 'Version.Major', line 11, position 12.", traceWriter.TraceRecords[5].Message);
 478            Assert.IsTrue(traceWriter.TraceRecords[6].Message.StartsWith("Started deserializing System.Version. Path 'Version'"));
 479            Assert.IsTrue(traceWriter.TraceRecords[7].Message.StartsWith("Finished deserializing System.Version. Path 'Version'"));
 480            Assert.AreEqual("Started deserializing System.Collections.Generic.IDictionary`2[System.String,System.String]. Path 'StringDictionary.1', line 19, position 8.", traceWriter.TraceRecords[8].Message);
 481            Assert.IsTrue(traceWriter.TraceRecords[9].Message.StartsWith("Finished deserializing System.Collections.Generic.IDictionary`2[System.String,System.String]. Path 'StringDictionary'"));
 482            Assert.IsTrue(traceWriter.TraceRecords[10].Message.StartsWith("Finished deserializing Newtonsoft.Json.Tests.Serialization.TraceTestObject. Path ''"));
 483
 484            Assert.IsFalse(traceWriter.TraceRecords.Any(r => r.Level == TraceLevel.Verbose));
 485        }
 486
 487        [Test]
 488        public void ErrorDeserializing()
 489        {
 490            string json = @"{""Integer"":""hi""}";
 491
 492            var traceWriter = new InMemoryTraceWriter
 493            {
 494                LevelFilter = TraceLevel.Info
 495            };
 496
 497            ExceptionAssert.Throws<Exception>(() =>
 498            {
 499                JsonConvert.DeserializeObject<IntegerTestClass>(
 500                    json,
 501                    new JsonSerializerSettings
 502                    {
 503                        TraceWriter = traceWriter
 504                    });
 505            }, "Could not convert string to integer: hi. Path 'Integer', line 1, position 15.");
 506
 507            Assert.AreEqual(2, traceWriter.TraceRecords.Count);
 508
 509            Assert.AreEqual(TraceLevel.Info, traceWriter.TraceRecords[0].Level);
 510            Assert.AreEqual("Started deserializing Newtonsoft.Json.Tests.Serialization.IntegerTestClass. Path 'Integer', line 1, position 11.", traceWriter.TraceRecords[0].Message);
 511
 512            Assert.AreEqual(TraceLevel.Error, traceWriter.TraceRecords[1].Level);
 513            Assert.AreEqual("Error deserializing Newtonsoft.Json.Tests.Serialization.IntegerTestClass. Could not convert string to integer: hi. Path 'Integer', line 1, position 15.", traceWriter.TraceRecords[1].Message);
 514        }
 515
 516        [Test]
 517        public void ErrorDeserializingNested()
 518        {
 519            string json = @"{""IntList"":[1, ""two""]}";
 520
 521            var traceWriter = new InMemoryTraceWriter
 522            {
 523                LevelFilter = TraceLevel.Info
 524            };
 525
 526            ExceptionAssert.Throws<Exception>(() =>
 527            {
 528                JsonConvert.DeserializeObject<TraceTestObject>(
 529                    json,
 530                    new JsonSerializerSettings
 531                    {
 532                        TraceWriter = traceWriter
 533                    });
 534            }, "Could not convert string to integer: two. Path 'IntList[1]', line 1, position 20.");
 535
 536            Assert.AreEqual(3, traceWriter.TraceRecords.Count);
 537
 538            Assert.AreEqual(TraceLevel.Info, traceWriter.TraceRecords[0].Level);
 539            Assert.AreEqual("Started deserializing Newtonsoft.Json.Tests.Serialization.TraceTestObject. Path 'IntList', line 1, position 11.", traceWriter.TraceRecords[0].Message);
 540
 541            Assert.AreEqual(TraceLevel.Info, traceWriter.TraceRecords[1].Level);
 542            Assert.AreEqual("Started deserializing System.Collections.Generic.IList`1[System.Int32]. Path 'IntList', line 1, position 12.", traceWriter.TraceRecords[1].Message);
 543
 544            Assert.AreEqual(TraceLevel.Error, traceWriter.TraceRecords[2].Level);
 545            Assert.AreEqual("Error deserializing System.Collections.Generic.IList`1[System.Int32]. Could not convert string to integer: two. Path 'IntList[1]', line 1, position 20.", traceWriter.TraceRecords[2].Message);
 546        }
 547
 548        [Test]
 549        public void SerializeDictionarysWithPreserveObjectReferences()
 550        {
 551            PreserveReferencesHandlingTests.CircularDictionary circularDictionary = new PreserveReferencesHandlingTests.CircularDictionary();
 552            circularDictionary.Add("other", new PreserveReferencesHandlingTests.CircularDictionary { { "blah", null } });
 553            circularDictionary.Add("self", circularDictionary);
 554
 555            InMemoryTraceWriter traceWriter = new InMemoryTraceWriter
 556            {
 557                LevelFilter = TraceLevel.Verbose
 558            };
 559
 560            JsonConvert.SerializeObject(
 561                circularDictionary,
 562                Formatting.Indented,
 563                new JsonSerializerSettings
 564                {
 565                    PreserveReferencesHandling = PreserveReferencesHandling.All,
 566                    TraceWriter = traceWriter
 567                });
 568
 569            Assert.IsTrue(traceWriter.TraceRecords.Any(r => r.Message == "Writing object reference Id '1' for Newtonsoft.Json.Tests.Serialization.PreserveReferencesHandlingTests+CircularDictionary. Path ''."));
 570            Assert.IsTrue(traceWriter.TraceRecords.Any(r => r.Message == "Writing object reference Id '2' for Newtonsoft.Json.Tests.Serialization.PreserveReferencesHandlingTests+CircularDictionary. Path 'other'."));
 571            Assert.IsTrue(traceWriter.TraceRecords.Any(r => r.Message == "Writing object reference to Id '1' for Newtonsoft.Json.Tests.Serialization.PreserveReferencesHandlingTests+CircularDictionary. Path 'self'."));
 572        }
 573
 574        [Test]
 575        public void DeserializeDictionarysWithPreserveObjectReferences()
 576        {
 577            string json = @"{
 578  ""$id"": ""1"",
 579  ""other"": {
 580    ""$id"": ""2"",
 581    ""blah"": null
 582  },
 583  ""self"": {
 584    ""$ref"": ""1""
 585  }
 586}";
 587
 588            InMemoryTraceWriter traceWriter = new InMemoryTraceWriter
 589            {
 590                LevelFilter = TraceLevel.Verbose
 591            };
 592
 593            JsonConvert.DeserializeObject<PreserveReferencesHandlingTests.CircularDictionary>(json,
 594                new JsonSerializerSettings
 595                {
 596                    PreserveReferencesHandling = PreserveReferencesHandling.All,
 597                    MetadataPropertyHandling = MetadataPropertyHandling.Default,
 598                    TraceWriter = traceWriter
 599                });
 600
 601            Assert.IsTrue(traceWriter.TraceRecords.Any(r => r.Message == "Read object reference Id '1' for Newtonsoft.Json.Tests.Serialization.PreserveReferencesHandlingTests+CircularDictionary. Path 'other', line 3, position 10."));
 602            Assert.IsTrue(traceWriter.TraceRecords.Any(r => r.Message == "Read object reference Id '2' for Newtonsoft.Json.Tests.Serialization.PreserveReferencesHandlingTests+CircularDictionary. Path 'other.blah', line 5, position 11."));
 603            Assert.IsTrue(traceWriter.TraceRecords.Any(r => r.Message.StartsWith("Resolved object reference '1' to Newtonsoft.Json.Tests.Serialization.PreserveReferencesHandlingTests+CircularDictionary. Path 'self'")));
 604        }
 605
 606        [Test]
 607        public void WriteTypeNameForObjects()
 608        {
 609            InMemoryTraceWriter traceWriter = new InMemoryTraceWriter
 610            {
 611                LevelFilter = TraceLevel.Verbose
 612            };
 613
 614            IList<object> l = new List<object>
 615            {
 616                new Dictionary<string, string> { { "key!", "value!" } },
 617                new Version(1, 2, 3, 4)
 618            };
 619
 620            JsonConvert.SerializeObject(l, Formatting.Indented, new JsonSerializerSettings
 621            {
 622                TypeNameHandling = TypeNameHandling.All,
 623                TraceWriter = traceWriter
 624            });
 625
 626            Assert.AreEqual("Started serializing System.Collections.Generic.List`1[System.Object]. Path ''.", traceWriter.TraceRecords[0].Message);
 627            Assert.AreEqual("Writing type name '" + ReflectionUtils.GetTypeName(typeof(List<object>), 0, DefaultSerializationBinder.Instance) + "' for System.Collections.Generic.List`1[System.Object]. Path ''.", traceWriter.TraceRecords[1].Message);
 628            Assert.AreEqual("Started serializing System.Collections.Generic.Dictionary`2[System.String,System.String]. Path '$values'.", traceWriter.TraceRecords[2].Message);
 629            Assert.AreEqual("Writing type name '" + ReflectionUtils.GetTypeName(typeof(Dictionary<string, string>), 0, DefaultSerializationBinder.Instance) + "' for System.Collections.Generic.Dictionary`2[System.String,System.String]. Path '$values[0]'.", traceWriter.TraceRecords[3].Message);
 630            Assert.AreEqual("Finished serializing System.Collections.Generic.Dictionary`2[System.String,System.String]. Path '$values[0]'.", traceWriter.TraceRecords[4].Message);
 631            Assert.AreEqual("Started serializing System.Version. Path '$values[0]'.", traceWriter.TraceRecords[5].Message);
 632            Assert.AreEqual("Writing type name '" + ReflectionUtils.GetTypeName(typeof(Version), 0, DefaultSerializationBinder.Instance) + "' for System.Version. Path '$values[1]'.", traceWriter.TraceRecords[6].Message);
 633            Assert.AreEqual("Finished serializing System.Version. Path '$values[1]'.", traceWriter.TraceRecords[7].Message);
 634            Assert.AreEqual("Finished serializing System.Collections.Generic.List`1[System.Object]. Path ''.", traceWriter.TraceRecords[8].Message);
 635        }
 636
 637        [Test]
 638        public void SerializeConverter()
 639        {
 640            InMemoryTraceWriter traceWriter = new InMemoryTraceWriter
 641            {
 642                LevelFilter = TraceLevel.Verbose
 643            };
 644
 645            IList<DateTime> d = new List<DateTime>
 646            {
 647                new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc)
 648            };
 649
 650            string json = JsonConvert.SerializeObject(d, Formatting.Indented, new JsonSerializerSettings
 651            {
 652                Converters = { new JavaScriptDateTimeConverter() },
 653                TraceWriter = traceWriter
 654            });
 655
 656            Assert.AreEqual("Started serializing System.Collections.Generic.List`1[System.DateTime]. Path ''.", traceWriter.TraceRecords[0].Message);
 657            Assert.AreEqual("Started serializing System.DateTime with converter Newtonsoft.Json.Converters.JavaScriptDateTimeConverter. Path ''.", traceWriter.TraceRecords[1].Message);
 658            Assert.AreEqual("Finished serializing System.DateTime with converter Newtonsoft.Json.Converters.JavaScriptDateTimeConverter. Path '[0]'.", traceWriter.TraceRecords[2].Message);
 659            Assert.AreEqual("Finished serializing System.Collections.Generic.List`1[System.DateTime]. Path ''.", traceWriter.TraceRecords[3].Message);
 660        }
 661
 662        [Test]
 663        public void DeserializeConverter()
 664        {
 665            string json = @"[new Date(976623132000)]";
 666
 667            InMemoryTraceWriter traceWriter =
 668                new InMemoryTraceWriter
 669                {
 670                    LevelFilter = TraceLevel.Verbose
 671                };
 672
 673            JsonConvert.DeserializeObject<List<DateTime>>(
 674                json,
 675                new JsonSerializerSettings
 676                {
 677                    Converters = { new JavaScriptDateTimeConverter() },
 678                    TraceWriter = traceWriter
 679                });
 680
 681            Assert.AreEqual("Started deserializing System.Collections.Generic.List`1[System.DateTime]. Path '', line 1, position 1.", traceWriter.TraceRecords[0].Message);
 682            Assert.AreEqual("Started deserializing System.DateTime with converter Newtonsoft.Json.Converters.JavaScriptDateTimeConverter. Path '[0]', line 1, position 10.", traceWriter.TraceRecords[1].Message);
 683            Assert.AreEqual("Finished deserializing System.DateTime with converter Newtonsoft.Json.Converters.JavaScriptDateTimeConverter. Path '[0]', line 1, position 23.", traceWriter.TraceRecords[2].Message);
 684            Assert.AreEqual("Finished deserializing System.Collections.Generic.List`1[System.DateTime]. Path '', line 1, position 24.", traceWriter.TraceRecords[3].Message);
 685        }
 686
 687        [Test]
 688        public void DeserializeTypeName()
 689        {
 690            InMemoryTraceWriter traceWriter = new InMemoryTraceWriter
 691            {
 692                LevelFilter = TraceLevel.Verbose
 693            };
 694
 695            string json = @"{
 696  ""$type"": ""System.Collections.Generic.List`1[[System.Object, mscorlib]], mscorlib"",
 697  ""$values"": [
 698    {
 699      ""$type"": ""System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.String, mscorlib]], mscorlib"",
 700      ""key!"": ""value!""
 701    },
 702    {
 703      ""$type"": ""System.Version, mscorlib"",
 704      ""Major"": 1,
 705      ""Minor"": 2,
 706      ""Build"": 3,
 707      ""Revision"": 4,
 708      ""MajorRevision"": 0,
 709      ""MinorRevision"": 4
 710    }
 711  ]
 712}";
 713
 714            JsonConvert.DeserializeObject(json, null, new JsonSerializerSettings
 715            {
 716                TypeNameHandling = TypeNameHandling.All,
 717                MetadataPropertyHandling = MetadataPropertyHandling.Default,
 718                TraceWriter = traceWriter
 719            });
 720
 721            Assert.AreEqual("Resolved type 'System.Collections.Generic.List`1[[System.Object, mscorlib]], mscorlib' to System.Collections.Generic.List`1[System.Object]. Path '$type', line 2, position 83.", traceWriter.TraceRecords[0].Message);
 722            Assert.AreEqual("Started deserializing System.Collections.Generic.List`1[System.Object]. Path '$values', line 3, position 14.", traceWriter.TraceRecords[1].Message);
 723            Assert.AreEqual("Resolved type 'System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.String, mscorlib]], mscorlib' to System.Collections.Generic.Dictionary`2[System.String,System.String]. Path '$values[0].$type', line 5, position 119.", traceWriter.TraceRecords[2].Message);
 724            Assert.AreEqual("Started deserializing System.Collections.Generic.Dictionary`2[System.String,System.String]. Path '$values[0].key!', line 6, position 13.", traceWriter.TraceRecords[3].Message);
 725            Assert.IsTrue(traceWriter.TraceRecords[4].Message.StartsWith("Finished deserializing System.Collections.Generic.Dictionary`2[System.String,System.String]. Path '$values[0]'"));
 726            Assert.AreEqual("Resolved type 'System.Version, mscorlib' to System.Version. Path '$values[1].$type', line 9, position 41.", traceWriter.TraceRecords[5].Message);
 727            Assert.AreEqual("Deserializing System.Version using creator with parameters: Major, Minor, Build, Revision. Path '$values[1].Major', line 10, position 14.", traceWriter.TraceRecords[6].Message);
 728            Assert.IsTrue(traceWriter.TraceRecords[7].Message.StartsWith("Started deserializing System.Version. Path '$values[1]'"));
 729            Assert.IsTrue(traceWriter.TraceRecords[8].Message.StartsWith("Finished deserializing System.Version. Path '$values[1]'"));
 730            Assert.IsTrue(traceWriter.TraceRecords[9].Message.StartsWith("Finished deserializing System.Collections.Generic.List`1[System.Object]. Path '$values'"));
 731        }
 732
 733#if !(PORTABLE || DNXCORE50 || PORTABLE40)
 734        [Test]
 735        public void DeserializeISerializable()
 736        {
 737            InMemoryTraceWriter traceWriter = new InMemoryTraceWriter
 738            {
 739                LevelFilter = TraceLevel.Verbose
 740            };
 741
 742            ExceptionAssert.Throws<SerializationException>(() =>
 743            {
 744                JsonConvert.DeserializeObject<Exception>(
 745                    "{}",
 746                    new JsonSerializerSettings
 747                    {
 748                        TraceWriter = traceWriter
 749                    });
 750            }, "Member 'ClassName' was not found.");
 751
 752            Assert.IsTrue(traceWriter.TraceRecords[0].Message.StartsWith("Deserializing System.Exception using ISerializable constructor. Path ''"));
 753            Assert.AreEqual(TraceLevel.Info, traceWriter.TraceRecords[0].Level);
 754            Assert.AreEqual("Error deserializing System.Exception. Member 'ClassName' was not found. Path '', line 1, position 2.", traceWriter.TraceRecords[1].Message);
 755            Assert.AreEqual(TraceLevel.Error, traceWriter.TraceRecords[1].Level);
 756        }
 757#endif
 758
 759        [Test]
 760        public void DeserializeMissingMember()
 761        {
 762            InMemoryTraceWriter traceWriter = new InMemoryTraceWriter
 763            {
 764                LevelFilter = TraceLevel.Verbose
 765            };
 766
 767            JsonConvert.DeserializeObject<Person>(
 768                "{'MissingMemberProperty':'!!'}",
 769                new JsonSerializerSettings
 770                {
 771                    TraceWriter = traceWriter
 772                });
 773
 774            Assert.AreEqual("Started deserializing Newtonsoft.Json.Tests.TestObjects.Organization.Person. Path 'MissingMemberProperty', line 1, position 25.", traceWriter.TraceRecords[0].Message);
 775            Assert.AreEqual("Could not find member 'MissingMemberProperty' on Newtonsoft.Json.Tests.TestObjects.Organization.Person. Path 'MissingMemberProperty', line 1, position 25.", traceWriter.TraceRecords[1].Message);
 776            Assert.IsTrue(traceWriter.TraceRecords[2].Message.StartsWith("Finished deserializing Newtonsoft.Json.Tests.TestObjects.Organization.Person. Path ''"));
 777        }
 778
 779        [Test]
 780        public void DeserializeMissingMemberConstructor()
 781        {
 782            InMemoryTraceWriter traceWriter = new InMemoryTraceWriter
 783            {
 784                LevelFilter = TraceLevel.Verbose
 785            };
 786
 787            string json = @"{
 788  ""Major"": 1,
 789  ""Minor"": 2,
 790  ""Build"": 3,
 791  ""Revision"": 4,
 792  ""MajorRevision"": 0,
 793  ""MinorRevision"": 4,
 794  ""MissingMemberProperty"": null
 795}";
 796
 797            JsonConvert.DeserializeObject<Version>(json, new JsonSerializerSettings
 798            {
 799                TraceWriter = traceWriter
 800            });
 801
 802            Assert.AreEqual("Deserializing System.Version using creator with parameters: Major, Minor, Build, Revision. Path 'Major', line 2, position 10.", traceWriter.TraceRecords[0].Message);
 803            Assert.AreEqual("Could not find member 'MissingMemberProperty' on System.Version. Path 'MissingMemberProperty', line 8, position 31.", traceWriter.TraceRecords[1].Message);
 804            Assert.IsTrue(traceWriter.TraceRecords[2].Message.StartsWith("Started deserializing System.Version. Path ''"));
 805            Assert.IsTrue(traceWriter.TraceRecords[3].Message.StartsWith("Finished deserializing System.Version. Path ''"));
 806        }
 807
 808        [Test]
 809        public void PublicParameterizedConstructorWithPropertyNameConflictWithAttribute()
 810        {
 811            InMemoryTraceWriter traceWriter = new InMemoryTraceWriter
 812            {
 813                LevelFilter = TraceLevel.Verbose
 814            };
 815
 816            string json = @"{name:""1""}";
 817
 818            PublicParameterizedConstructorWithPropertyNameConflictWithAttribute c = JsonConvert.DeserializeObject<PublicParameterizedConstructorWithPropertyNameConflictWithAttribute>(json, new JsonSerializerSettings
 819            {
 820                TraceWriter = traceWriter
 821            });
 822
 823            Assert.IsNotNull(c);
 824            Assert.AreEqual(1, c.Name);
 825
 826            Assert.AreEqual("Deserializing Newtonsoft.Json.Tests.TestObjects.PublicParameterizedConstructorWithPropertyNameConflictWithAttribute using creator with parameters: name. Path 'name', line 1, position 6.", traceWriter.TraceRecords[0].Message);
 827        }
 828
 829        [Test]
 830        public void ShouldSerializeTestClass()
 831        {
 832            ShouldSerializeTestClass c = new ShouldSerializeTestClass();
 833            c.Age = 29;
 834            c.Name = "Jim";
 835            c._shouldSerializeName = true;
 836
 837            InMemoryTraceWriter traceWriter = new InMemoryTraceWriter
 838            {
 839                LevelFilter = TraceLevel.Verbose
 840            };
 841
 842            JsonConvert.SerializeObject(c, new JsonSerializerSettings { TraceWriter = traceWriter });
 843
 844            Assert.AreEqual("ShouldSerialize result for property 'Name' on Newtonsoft.Json.Tests.Serialization.ShouldSerializeTestClass: True. Path ''.", traceWriter.TraceRecords[1].Message);
 845            Assert.AreEqual(TraceLevel.Verbose, traceWriter.TraceRecords[1].Level);
 846
 847            traceWriter = new InMemoryTraceWriter
 848            {
 849                LevelFilter = TraceLevel.Verbose
 850            };
 851
 852            c._shouldSerializeName = false;
 853
 854            JsonConvert.SerializeObject(c, new JsonSerializerSettings { TraceWriter = traceWriter });
 855
 856            Assert.AreEqual("ShouldSerialize result for property 'Name' on Newtonsoft.Json.Tests.Serialization.ShouldSerializeTestClass: False. Path ''.", traceWriter.TraceRecords[1].Message);
 857            Assert.AreEqual(TraceLevel.Verbose, traceWriter.TraceRecords[1].Level);
 858        }
 859
 860        [Test]
 861        public void SpecifiedTest()
 862        {
 863            SpecifiedTestClass c = new SpecifiedTestClass();
 864            c.Name = "James";
 865            c.Age = 27;
 866            c.NameSpecified = false;
 867
 868            InMemoryTraceWriter traceWriter = new InMemoryTraceWriter
 869            {
 870                LevelFilter = TraceLevel.Verbose
 871            };
 872
 873            string json = JsonConvert.SerializeObject(c, Formatting.Indented, new JsonSerializerSettings { TraceWriter = traceWriter });
 874
 875            Assert.AreEqual("Started serializing Newtonsoft.Json.Tests.Serialization.SpecifiedTestClass. Path ''.", traceWriter.TraceRecords[0].Message);
 876            Assert.AreEqual("IsSpecified result for property 'Name' on Newtonsoft.Json.Tests.Serialization.SpecifiedTestClass: False. Path ''.", traceWriter.TraceRecords[1].Message);
 877            Assert.AreEqual("IsSpecified result for property 'Weight' on Newtonsoft.Json.Tests.Serialization.SpecifiedTestClass: False. Path 'Age'.", traceWriter.TraceRecords[2].Message);
 878            Assert.AreEqual("IsSpecified result for property 'Height' on Newtonsoft.Json.Tests.Serialization.SpecifiedTestClass: False. Path 'Age'.", traceWriter.TraceRecords[3].Message);
 879            Assert.AreEqual("IsSpecified result for property 'FavoriteNumber' on Newtonsoft.Json.Tests.Serialization.SpecifiedTestClass: False. Path 'Age'.", traceWriter.TraceRecords[4].Message);
 880            Assert.AreEqual("Finished serializing Newtonsoft.Json.Tests.Serialization.SpecifiedTestClass. Path ''.", traceWriter.TraceRecords[5].Message);
 881
 882            StringAssert.AreEqual(@"{
 883  ""Age"": 27
 884}", json);
 885
 886            traceWriter = new InMemoryTraceWriter
 887            {
 888                LevelFilter = TraceLevel.Verbose
 889            };
 890
 891            SpecifiedTestClass deserialized = JsonConvert.DeserializeObject<SpecifiedTestClass>(json, new JsonSerializerSettings { TraceWriter = traceWriter });
 892
 893            Assert.AreEqual("Started deserializing Newtonsoft.Json.Tests.Serialization.SpecifiedTestClass. Path 'Age', line 2, position 8.", traceWriter.TraceRecords[0].Message);
 894            Assert.IsTrue(traceWriter.TraceRecords[1].Message.StartsWith("Finished deserializing Newtonsoft.Json.Tests.Serialization.SpecifiedTestClass. Path ''"));
 895
 896            Assert.IsNull(deserialized.Name);
 897            Assert.IsFalse(deserialized.NameSpecified);
 898            Assert.IsFalse(deserialized.WeightSpecified);
 899            Assert.IsFalse(deserialized.HeightSpecified);
 900            Assert.IsFalse(deserialized.FavoriteNumberSpecified);
 901            Assert.AreEqual(27, deserialized.Age);
 902
 903            c.NameSpecified = true;
 904            c.WeightSpecified = true;
 905            c.HeightSpecified = true;
 906            c.FavoriteNumber = 23;
 907            json = JsonConvert.SerializeObject(c, Formatting.Indented);
 908
 909            StringAssert.AreEqual(@"{
 910  ""Name"": ""James"",
 911  ""Age"": 27,
 912  ""Weight"": 0,
 913  ""Height"": 0,
 914  ""FavoriteNumber"": 23
 915}", json);
 916
 917            traceWriter = new InMemoryTraceWriter
 918            {
 919                LevelFilter = TraceLevel.Verbose
 920            };
 921
 922            deserialized = JsonConvert.DeserializeObject<SpecifiedTestClass>(json, new JsonSerializerSettings { TraceWriter = traceWriter });
 923
 924            Assert.AreEqual("Started deserializing Newtonsoft.Json.Tests.Serialization.SpecifiedTestClass. Path 'Name', line 2, position 9.", traceWriter.TraceRecords[0].Message);
 925            Assert.AreEqual("IsSpecified for property 'Name' on Newtonsoft.Json.Tests.Serialization.SpecifiedTestClass set to true. Path 'Name', line 2, position 17.", traceWriter.TraceRecords[1].Message);
 926            Assert.AreEqual("IsSpecified for property 'Weight' on Newtonsoft.Json.Tests.Serialization.SpecifiedTestClass set to true. Path 'Weight', line 4, position 13.", traceWriter.TraceRecords[2].Message);
 927            Assert.AreEqual("IsSpecified for property 'Height' on Newtonsoft.Json.Tests.Serialization.SpecifiedTestClass set to true. Path 'Height', line 5, position 13.", traceWriter.TraceRecords[3].Message);
 928            Assert.IsTrue(traceWriter.TraceRecords[4].Message.StartsWith("Finished deserializing Newtonsoft.Json.Tests.Serialization.SpecifiedTestClass. Path ''"));
 929
 930            Assert.AreEqual("James", deserialized.Name);
 931            Assert.IsTrue(deserialized.NameSpecified);
 932            Assert.IsTrue(deserialized.WeightSpecified);
 933            Assert.IsTrue(deserialized.HeightSpecified);
 934            Assert.IsTrue(deserialized.FavoriteNumberSpecified);
 935            Assert.AreEqual(27, deserialized.Age);
 936            Assert.AreEqual(23, deserialized.FavoriteNumber);
 937        }
 938
 939#if !(NET20 || NET35 || PORTABLE || PORTABLE40) || NETSTANDARD1_1
 940        [Test]
 941        public void TraceJsonWriterTest()
 942        {
 943            StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
 944            JsonTextWriter w = new JsonTextWriter(sw);
 945            TraceJsonWriter traceWriter = new TraceJsonWriter(w);
 946
 947            traceWriter.WriteStartObject();
 948            traceWriter.WritePropertyName("Array");
 949            traceWriter.WriteStartArray();
 950            traceWriter.WriteValue("String!");
 951            traceWriter.WriteValue(new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc));
 952            traceWriter.WriteValue(new DateTimeOffset(2000, 12, 12, 12, 12, 12, TimeSpan.FromHours(2)));
 953            traceWriter.WriteValue(1.1f);
 954            traceWriter.WriteValue(1.1d);
 955            traceWriter.WriteValue(1.1m);
 956            traceWriter.WriteValue(1);
 957            traceWriter.WriteValue((char)'!');
 958            traceWriter.WriteValue((short)1);
 959            traceWriter.WriteValue((ushort)1);
 960            traceWriter.WriteValue((int)1);
 961            traceWriter.WriteValue((uint)1);
 962            traceWriter.WriteValue((sbyte)1);
 963            traceWriter.WriteValue((byte)1);
 964            traceWriter.WriteValue((long)1);
 965            traceWriter.WriteValue((ulong)1);
 966            traceWriter.WriteValue((bool)true);
 967
 968            traceWriter.WriteValue((DateTime?)new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc));
 969            traceWriter.WriteValue((DateTimeOffset?)new DateTimeOffset(2000, 12, 12, 12, 12, 12, TimeSpan.FromHours(2)));
 970            traceWriter.WriteValue((float?)1.1f);
 971            traceWriter.WriteValue((double?)1.1d);
 972            traceWriter.WriteValue((decimal?)1.1m);
 973            traceWriter.WriteValue((int?)1);
 974            traceWriter.WriteValue((char?)'!');
 975            traceWriter.WriteValue((short?)1);
 976            traceWriter.WriteValue((ushort?)1);
 977            traceWriter.WriteValue((int?)1);
 978            traceWriter.WriteValue((uint?)1);
 979            traceWriter.WriteValue((sbyte?)1);
 980            traceWriter.WriteValue((byte?)1);
 981            traceWriter.WriteValue((long?)1);
 982            traceWriter.WriteValue((ulong?)1);
 983            traceWriter.WriteValue((bool?)true);
 984            traceWriter.WriteValue(BigInteger.Parse("9999999990000000000000000000000000000000000"));
 985
 986            traceWriter.WriteValue((object)true);
 987            traceWriter.WriteValue(TimeSpan.FromMinutes(1));
 988            traceWriter.WriteValue(Guid.Empty);
 989            traceWriter.WriteValue(new Uri("http://www.google.com/"));
 990            traceWriter.WriteValue(Encoding.UTF8.GetBytes("String!"));
 991            traceWriter.WriteRawValue("[1],");
 992            traceWriter.WriteRaw("[2]");
 993            traceWriter.WriteNull();
 994            traceWriter.WriteUndefined();
 995            traceWriter.WriteStartConstructor("ctor");
 996            traceWriter.WriteValue(1);
 997            traceWriter.WriteEndConstructor();
 998            traceWriter.WriteComment("A comment");
 999            traceWriter.WriteWhitespace("       ");
1000            traceWriter.WriteEnd();
1001            traceWriter.WriteEndObject();
1002            traceWriter.Flush();
1003            traceWriter.Close();
1004
1005            string json = @"{
1006  ""Array"": [
1007    ""String!"",
1008    ""2000-12-12T12:12:12Z"",
1009    ""2000-12-12T12:12:12+02:00"",
1010    1.1,
1011    1.1,
1012    1.1,
1013    1,
1014    ""!"",
1015    1,
1016    1,
1017    1,
1018    1,
1019    1,
1020    1,
1021    1,
1022    1,
1023    true,
1024    ""2000-12-12T12:12:12Z"",
1025    ""2000-12-12T12:12:12+02:00"",
1026    1.1,
1027    1.1,
1028    1.1,
1029    1,
1030    ""!"",
1031    1,
1032    1,
1033    1,
1034    1,
1035    1,
1036    1,
1037    1,
1038    1,
1039    true,
1040    9999999990000000000000000000000000000000000,
1041    true,
1042    true,
1043    ""00:01:00"",
1044    ""00000000-0000-0000-0000-000000000000"",
1045    ""http://www.google.com/"",
1046    ""U3RyaW5nIQ=="",
1047    [1],[2],
1048    null,
1049    undefined,
1050    new ctor(
1051      1
1052    )
1053    /*A comment*/       
1054  ]
1055}";
1056
1057            StringAssert.AreEqual("Serialized JSON: " + Environment.NewLine + json, traceWriter.GetSerializedJsonMessage());
1058        }
1059
1060        [Test]
1061        public void TraceJsonReaderTest()
1062        {
1063            string json = @"{
1064  ""Array"": [
1065    ""String!"",
1066    ""2000-12-12T12:12:12Z"",
1067    ""2000-12-12T12:12:12Z"",
1068    ""2000-12-12T12:12:12+00:00"",
1069    ""U3RyaW5nIQ=="",
1070    1,
1071    1.1,
1072    1.2,
1073    9999999990000000000000000000000000000000000,
1074    null,
1075    undefined,
1076    new ctor(
1077      1
1078    )
1079    /*A comment*/
1080  ]
1081}";
1082
1083            StringReader sw = new StringReader(json);
1084            JsonTextReader w = new JsonTextReader(sw);
1085            TraceJsonReader traceReader = new TraceJsonReader(w);
1086
1087            traceReader.Read();
1088            Assert.AreEqual(JsonToken.StartObject, traceReader.TokenType);
1089
1090            traceReader.Read();
1091            Assert.AreEqual(JsonToken.PropertyName, traceReader.TokenType);
1092            Assert.AreEqual("Array", traceReader.Value);
1093
1094            traceReader.Read();
1095            Assert.AreEqual(JsonToken.StartArray, traceReader.TokenType);
1096            Assert.AreEqual(null, traceReader.Value);
1097
1098            traceReader.ReadAsString();
1099            Assert.AreEqual(JsonToken.String, traceReader.TokenType);
1100            Assert.AreEqual('"', traceReader.QuoteChar);
1101            Assert.AreEqual("String!", traceReader.Value);
1102
1103            // for great code coverage justice!
1104            traceReader.QuoteChar = '\'';
1105            Assert.AreEqual('\'', traceReader.QuoteChar);
1106
1107            traceReader.ReadAsString();
1108            Assert.AreEqual(JsonToken.String, traceReader.TokenType);
1109            Assert.AreEqual("2000-12-12T12:12:12Z", traceReader.Value);
1110
1111            traceReader.ReadAsDateTime();
1112            Assert.AreEqual(JsonToken.Date, traceReader.TokenType);
1113            Assert.AreEqual(new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc), traceReader.Value);
1114
1115            traceReader.ReadAsDateTimeOffset();
1116            Assert.AreEqual(JsonToken.Date, traceReader.TokenType);
1117            Assert.AreEqual(new DateTimeOf

Large files files are truncated, but you can click here to view the full file