context: #region License
  // Copyright (c) 2007 James Newton-King
  //
  // Permission is hereby granted, free of charge, to any person
  // obtaining a copy of this software and associated documentation
  // files (the "Software"), to deal in the Software without
  // restriction, including without limitation the rights to use,
  // copy, modify, merge, publish, distribute, sublicense, and/or sell
  // copies of the Software, and to permit persons to whom the
  // Software is furnished to do so, subject to the following
  // conditions:
  //
  // The above copyright notice and this permission notice shall be
  // included in all copies or substantial portions of the Software.
  //
  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  // OTHER DEALINGS IN THE SOFTWARE.
  #endregion
  
  using System;
  using System.Collections.Generic;
  using System.Globalization;
  using System.IO;
  using Newtonsoft.Json.Schema;
  #if !(NET20 || NET35 || PORTABLE40 || PORTABLE || DNXCORE50)
  using System.Numerics;
  #endif
  using System.Runtime.Serialization;
  using System.Text;
  #if !(NET20 || NET35)
  using System.Threading.Tasks;
  #endif
  using System.Xml;
  using Newtonsoft.Json.Converters;
  using Newtonsoft.Json.Linq;
  using Newtonsoft.Json.Serialization;
  using Newtonsoft.Json.Tests.Serialization;
  using Newtonsoft.Json.Tests.TestObjects;
  using Newtonsoft.Json.Utilities;
  #if NETFX_CORE
  using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
  using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
  using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
  #elif DNXCORE50
  using Xunit;
  using Test = Xunit.FactAttribute;
  using Assert = Newtonsoft.Json.Tests.XUnitAssert;
  #else
  using NUnit.Framework;
  #endif
  
  namespace Newtonsoft.Json.Tests
  {
      [TestFixture]
      public class JsonConvertTest : TestFixtureBase
      {
          [Test]
          public void DefaultSettings()
          {
              try
              {
                  JsonConvert.DefaultSettings = () => new JsonSerializerSettings
                  {
                      Formatting = Formatting.Indented
                  };
  
                  string json = JsonConvert.SerializeObject(new { test = new[] { 1, 2, 3 } });
  
                  StringAssert.AreEqual(@"{
    ""test"": [
      1,
      2,
      3
    ]
  }", json);
              }
              finally
              {
                  JsonConvert.DefaultSettings = null;
              }
          }
  
          public class NameTableTestClass
          {
              public string Value { get; set; }
          }
  
          public class NameTableTestClassConverter : JsonConverter
          {
              public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
              {
                  throw new NotImplementedException();
              }
  
              public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
              {
                  reader.Read();
                  reader.Read();
  
                  JsonTextReader jsonTextReader = (JsonTextReader)reader;
                  Assert.IsNotNull(jsonTextReader.NameTable);
  
                  string s = serializer.Deserialize<string>(reader);
                  Assert.AreEqual("hi", s);
                  Assert.IsNotNull(jsonTextReader.NameTable);
  
                  NameTableTestClass o = new NameTableTestClass
                  {
                      Value = s
                  };
  
                  return o;
              }
  
              public override bool CanConvert(Type objectType)
              {
                  return objectType == typeof(NameTableTestClass);
              }
          }
  
          [Test]
          public void NameTableTest()
          {
              StringReader sr = new StringReader("{'property':'hi'}");
              JsonTextReader jsonTextReader = new JsonTextReader(sr);
  
              Assert.IsNull(jsonTextReader.NameTable);
  
              JsonSerializer serializer = new JsonSerializer();
              serializer.Converters.Add(new NameTableTestClassConverter());
              NameTableTestClass o = serializer.Deserialize<NameTableTestClass>(jsonTextReader);
  
              Assert.IsNull(jsonTextReader.NameTable);
              Assert.AreEqual("hi", o.Value);
          }
  
          [Test]
          public void DefaultSettings_Example()
          {
              try
              {
                  JsonConvert.DefaultSettings = () => new JsonSerializerSettings
                  {
                      Formatting = Formatting.Indented,
                      ContractResolver = new CamelCasePropertyNamesContractResolver()
                  };
  
                  Employee e = new Employee
                  {
                      FirstName = "Eric",
                      LastName = "Example",
                      BirthDate = new DateTime(1980, 4, 20, 0, 0, 0, DateTimeKind.Utc),
                      Department = "IT",
                      JobTitle = "Web Dude"
                  };
  
                  string json = JsonConvert.SerializeObject(e);
                  // {
                  //   "firstName": "Eric",
                  //   "lastName": "Example",
                  //   "birthDate": "1980-04-20T00:00:00Z",
                  //   "department": "IT",
                  //   "jobTitle": "Web Dude"
                  // }
  
                  StringAssert.AreEqual(@"{
    ""firstName"": ""Eric"",
    ""lastName"": ""Example"",
    ""birthDate"": ""1980-04-20T00:00:00Z"",
    ""department"": ""IT"",
    ""jobTitle"": ""Web Dude""
  }", json);
              }
              finally
              {
                  JsonConvert.DefaultSettings = null;
              }
          }
  
          [Test]
          public void DefaultSettings_Override()
          {
              try
              {
                  JsonConvert.DefaultSettings = () => new JsonSerializerSettings
                  {
                      Formatting = Formatting.Indented
                  };
  
                  string json = JsonConvert.SerializeObject(new { test = new[] { 1, 2, 3 } }, new JsonSerializerSettings
                  {
                      Formatting = Formatting.None
                  });
  
                  Assert.AreEqual(@"{""test"":[1,2,3]}", json);
              }
              finally
              {
                  JsonConvert.DefaultSettings = null;
              }
          }
  
          [Test]
          public void DefaultSettings_Override_JsonConverterOrder()
          {
              try
              {
                  JsonConvert.DefaultSettings = () => new JsonSerializerSettings
                  {
                      Formatting = Formatting.Indented,
                      Converters = { new IsoDateTimeConverter { DateTimeFormat = "yyyy" } }
                  };
  
                  string json = JsonConvert.SerializeObject(new[] { new DateTime(2000, 12, 12, 4, 2, 4, DateTimeKind.Utc) }, new JsonSerializerSettings
                  {
                      Formatting = Formatting.None,
                      Converters =
                      {
                          // should take precedence
                          new JavaScriptDateTimeConverter(),
                          new IsoDateTimeConverter { DateTimeFormat = "dd" }
                      }
                  });
  
                  Assert.AreEqual(@"[new Date(976593724000)]", json);
              }
              finally
              {
                  JsonConvert.DefaultSettings = null;
              }
          }
  
          [Test]
          public void DefaultSettings_Create()
          {
              try
              {
                  JsonConvert.DefaultSettings = () => new JsonSerializerSettings
                  {
                      Formatting = Formatting.Indented
                  };
  
                  IList<int> l = new List<int> { 1, 2, 3 };
  
                  StringWriter sw = new StringWriter();
                  JsonSerializer serializer = JsonSerializer.CreateDefault();
                  serializer.Serialize(sw, l);
  
                  StringAssert.AreEqual(@"[
    1,
    2,
    3
  ]", sw.ToString());
  
                  sw = new StringWriter();
                  serializer.Formatting = Formatting.None;
                  serializer.Serialize(sw, l);
  
                  Assert.AreEqual(@"[1,2,3]", sw.ToString());
  
                  sw = new StringWriter();
                  serializer = new JsonSerializer();
                  serializer.Serialize(sw, l);
  
                  Assert.AreEqual(@"[1,2,3]", sw.ToString());
  
                  sw = new StringWriter();
                  serializer = JsonSerializer.Create();
                  serializer.Serialize(sw, l);
  
                  Assert.AreEqual(@"[1,2,3]", sw.ToString());
              }
              finally
              {
                  JsonConvert.DefaultSettings = null;
              }
          }
  
          [Test]
          public void DefaultSettings_CreateWithSettings()
          {
              try
              {
                  JsonConvert.DefaultSettings = () => new JsonSerializerSettings
                  {
                      Formatting = Formatting.Indented
                  };
  
                  IList<int> l = new List<int> { 1, 2, 3 };
  
                  StringWriter sw = new StringWriter();
                  JsonSerializer serializer = JsonSerializer.CreateDefault(new JsonSerializerSettings
                  {
                      Converters = { new IntConverter() }
                  });
                  serializer.Serialize(sw, l);
  
                  StringAssert.AreEqual(@"[
    2,
    4,
    6
  ]", sw.ToString());
  
                  sw = new StringWriter();
                  serializer.Converters.Clear();
                  serializer.Serialize(sw, l);
  
                  StringAssert.AreEqual(@"[
    1,
    2,
    3
  ]", sw.ToString());
  
                  sw = new StringWriter();
                  serializer = JsonSerializer.Create(new JsonSerializerSettings { Formatting = Formatting.Indented });
                  serializer.Serialize(sw, l);
  
                  StringAssert.AreEqual(@"[
    1,
    2,
    3
  ]", sw.ToString());
              }
              finally
              {
                  JsonConvert.DefaultSettings = null;
              }
          }
  
          public class IntConverter : JsonConverter
          {
              public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
              {
                  int i = (int)value;
                  writer.WriteValue(i * 2);
              }
  
              public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
              {
                  throw new NotImplementedException();
              }
  
              public override bool CanConvert(Type objectType)
              {
                  return objectType == typeof(int);
              }
          }
  
          [Test]
          public void DeserializeObject_EmptyString()
          {
              object result = JsonConvert.DeserializeObject(string.Empty);
              Assert.IsNull(result);
          }
  
          [Test]
          public void DeserializeObject_Integer()
          {
              object result = JsonConvert.DeserializeObject("1");
              Assert.AreEqual(1L, result);
          }
  
          [Test]
          public void DeserializeObject_Integer_EmptyString()
          {
              int? value = JsonConvert.DeserializeObject<int?>("");
              Assert.IsNull(value);
          }
  
          [Test]
          public void DeserializeObject_Decimal_EmptyString()
          {
              decimal? value = JsonConvert.DeserializeObject<decimal?>("");
              Assert.IsNull(value);
          }
  
          [Test]
          public void DeserializeObject_DateTime_EmptyString()
          {
              DateTime? value = JsonConvert.DeserializeObject<DateTime?>("");
              Assert.IsNull(value);
          }
  
          [Test]
          public void EscapeJavaScriptString()
          {
              string result;
  
              result = JavaScriptUtils.ToEscapedJavaScriptString("How now brown cow?", '"', true);
              Assert.AreEqual(@"""How now brown cow?""", result);
  
              result = JavaScriptUtils.ToEscapedJavaScriptString("How now 'brown' cow?", '"', true);
              Assert.AreEqual(@"""How now 'brown' cow?""", result);
  
              result = JavaScriptUtils.ToEscapedJavaScriptString("How now <brown> cow?", '"', true);
              Assert.AreEqual(@"""How now <brown> cow?""", result);
  
              result = JavaScriptUtils.ToEscapedJavaScriptString("How \r\nnow brown cow?", '"', true);
              Assert.AreEqual(@"""How \r\nnow brown cow?""", result);
  
              result = JavaScriptUtils.ToEscapedJavaScriptString("\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007", '"', true);
              Assert.AreEqual(@"""\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007""", result);
  
              result =
                  JavaScriptUtils.ToEscapedJavaScriptString("\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013", '"', true);
              Assert.AreEqual(@"""\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013""", result);
  
              result =
                  JavaScriptUtils.ToEscapedJavaScriptString(
                      "\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f ", '"', true);
              Assert.AreEqual(@"""\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f """, result);
  
              result =
                  JavaScriptUtils.ToEscapedJavaScriptString(
                      "!\"#$%&\u0027()*+,-./0123456789:;\u003c=\u003e?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]", '"', true);
              Assert.AreEqual(@"""!\""#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]""", result);
  
              result = JavaScriptUtils.ToEscapedJavaScriptString("^_`abcdefghijklmnopqrstuvwxyz{|}~", '"', true);
              Assert.AreEqual(@"""^_`abcdefghijklmnopqrstuvwxyz{|}~""", result);
  
              string data =
                  "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f !\"#$%&\u0027()*+,-./0123456789:;\u003c=\u003e?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
              string expected =
                  @"""\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f !\""#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~""";
  
              result = JavaScriptUtils.ToEscapedJavaScriptString(data, '"', true);
              Assert.AreEqual(expected, result);
  
              result = JavaScriptUtils.ToEscapedJavaScriptString("Fred's cat.", '\'', true);
              Assert.AreEqual(result, @"'Fred\'s cat.'");
  
              result = JavaScriptUtils.ToEscapedJavaScriptString(@"""How are you gentlemen?"" said Cats.", '"', true);
              Assert.AreEqual(result, @"""\""How are you gentlemen?\"" said Cats.""");
  
              result = JavaScriptUtils.ToEscapedJavaScriptString(@"""How are' you gentlemen?"" said Cats.", '"', true);
              Assert.AreEqual(result, @"""\""How are' you gentlemen?\"" said Cats.""");
  
              result = JavaScriptUtils.ToEscapedJavaScriptString(@"Fred's ""cat"".", '\'', true);
              Assert.AreEqual(result, @"'Fred\'s ""cat"".'");
  
              result = JavaScriptUtils.ToEscapedJavaScriptString("\u001farray\u003caddress", '"', true);
              Assert.AreEqual(result, @"""\u001farray<address""");
          }
  
          [Test]
          public void EscapeJavaScriptString_UnicodeLinefeeds()
          {
              string result;
  
              result = JavaScriptUtils.ToEscapedJavaScriptString("before" + '\u0085' + "after", '"', true);
              Assert.AreEqual(@"""before\u0085after""", result);
  
              result = JavaScriptUtils.ToEscapedJavaScriptString("before" + '\u2028' + "after", '"', true);
              Assert.AreEqual(@"""before\u2028after""", result);
  
              result = JavaScriptUtils.ToEscapedJavaScriptString("before" + '\u2029' + "after", '"', true);
              Assert.AreEqual(@"""before\u2029after""", result);
          }
  
          [Test]
          public void ToStringInvalid()
          {
              ExceptionAssert.Throws<ArgumentException>(() => { JsonConvert.ToString(new Version(1, 0)); }, "Unsupported type: System.Version. Use the JsonSerializer class to get the object's JSON representation.");
          }
  
          [Test]
          public void GuidToString()
          {
              Guid guid = new Guid("BED7F4EA-1A96-11d2-8F08-00A0C9A6186D");
  
gt:             string json = JsonConvert.ToString(guid);