JsonConvert
Provides methods for converting between .NET types and JSON types.
            
                using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Utilities;
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace Newtonsoft.Json
{
    [System.Runtime.CompilerServices.NullableContext(1)]
    [System.Runtime.CompilerServices.Nullable(0)]
    public static class JsonConvert
    {
        public static readonly string True = "true";
        public static readonly string False = "false";
        public static readonly string Null = "null";
        public static readonly string Undefined = "undefined";
        public static readonly string PositiveInfinity = "Infinity";
        public static readonly string NegativeInfinity = "-Infinity";
        public static readonly string NaN = "NaN";
        [System.Runtime.CompilerServices.Nullable(new byte[] {
            2,
            1
        })]
        [field: System.Runtime.CompilerServices.Nullable(new byte[] {
            2,
            1
        })]
        public static Func<JsonSerializerSettings> DefaultSettings {
            [return: System.Runtime.CompilerServices.Nullable(new byte[] {
                2,
                1
            })]
            get;
            [param: System.Runtime.CompilerServices.Nullable(new byte[] {
                2,
                1
            })]
            set;
        }
        public static string ToString(DateTime value)
        {
            return ToString(value, DateFormatHandling.IsoDateFormat, DateTimeZoneHandling.RoundtripKind);
        }
        public static string ToString(DateTime value, DateFormatHandling format, DateTimeZoneHandling timeZoneHandling)
        {
            DateTime value2 = DateTimeUtils.EnsureDateTime(value, timeZoneHandling);
            using (StringWriter stringWriter = StringUtils.CreateStringWriter(64)) {
                stringWriter.Write('"');
                DateTimeUtils.WriteDateTimeString(stringWriter, value2, format, null, CultureInfo.InvariantCulture);
                stringWriter.Write('"');
                return stringWriter.ToString();
            }
        }
        public static string ToString(DateTimeOffset value)
        {
            return ToString(value, DateFormatHandling.IsoDateFormat);
        }
        public static string ToString(DateTimeOffset value, DateFormatHandling format)
        {
            using (StringWriter stringWriter = StringUtils.CreateStringWriter(64)) {
                stringWriter.Write('"');
                DateTimeUtils.WriteDateTimeOffsetString(stringWriter, value, format, null, CultureInfo.InvariantCulture);
                stringWriter.Write('"');
                return stringWriter.ToString();
            }
        }
        public static string ToString(bool value)
        {
            if (!value)
                return False;
            return True;
        }
        public static string ToString(char value)
        {
            return ToString(char.ToString(value));
        }
        public static string ToString(Enum value)
        {
            return value.ToString("D");
        }
        public static string ToString(int value)
        {
            return value.ToString(null, CultureInfo.InvariantCulture);
        }
        public static string ToString(short value)
        {
            return value.ToString(null, CultureInfo.InvariantCulture);
        }
        [CLSCompliant(false)]
        public static string ToString(ushort value)
        {
            return value.ToString(null, CultureInfo.InvariantCulture);
        }
        [CLSCompliant(false)]
        public static string ToString(uint value)
        {
            return value.ToString(null, CultureInfo.InvariantCulture);
        }
        public static string ToString(long value)
        {
            return value.ToString(null, CultureInfo.InvariantCulture);
        }
        [CLSCompliant(false)]
        public static string ToString(ulong value)
        {
            return value.ToString(null, CultureInfo.InvariantCulture);
        }
        public static string ToString(float value)
        {
            return EnsureDecimalPlace((double)value, value.ToString("R", CultureInfo.InvariantCulture));
        }
        internal static string ToString(float value, FloatFormatHandling floatFormatHandling, char quoteChar, bool nullable)
        {
            return EnsureFloatFormat((double)value, EnsureDecimalPlace((double)value, value.ToString("R", CultureInfo.InvariantCulture)), floatFormatHandling, quoteChar, nullable);
        }
        private static string EnsureFloatFormat(double value, string text, FloatFormatHandling floatFormatHandling, char quoteChar, bool nullable)
        {
            if (floatFormatHandling == FloatFormatHandling.Symbol || (!double.IsInfinity(value) && !double.IsNaN(value)))
                return text;
            if (floatFormatHandling == FloatFormatHandling.DefaultValue) {
                if (nullable)
                    return Null;
                return "0.0";
            }
            return quoteChar.ToString() + text + quoteChar.ToString();
        }
        public static string ToString(double value)
        {
            return EnsureDecimalPlace(value, value.ToString("R", CultureInfo.InvariantCulture));
        }
        internal static string ToString(double value, FloatFormatHandling floatFormatHandling, char quoteChar, bool nullable)
        {
            return EnsureFloatFormat(value, EnsureDecimalPlace(value, value.ToString("R", CultureInfo.InvariantCulture)), floatFormatHandling, quoteChar, nullable);
        }
        private static string EnsureDecimalPlace(double value, string text)
        {
            if (double.IsNaN(value) || double.IsInfinity(value) || StringUtils.IndexOf(text, '.') != -1 || StringUtils.IndexOf(text, 'E') != -1 || StringUtils.IndexOf(text, 'e') != -1)
                return text;
            return text + ".0";
        }
        private static string EnsureDecimalPlace(string text)
        {
            if (StringUtils.IndexOf(text, '.') != -1)
                return text;
            return text + ".0";
        }
        public static string ToString(byte value)
        {
            return value.ToString(null, CultureInfo.InvariantCulture);
        }
        [CLSCompliant(false)]
        public static string ToString(sbyte value)
        {
            return value.ToString(null, CultureInfo.InvariantCulture);
        }
        public static string ToString(decimal value)
        {
            return EnsureDecimalPlace(value.ToString(null, CultureInfo.InvariantCulture));
        }
        public static string ToString(Guid value)
        {
            return ToString(value, '"');
        }
        internal static string ToString(Guid value, char quoteChar)
        {
            string str = value.ToString("D", CultureInfo.InvariantCulture);
            string text = quoteChar.ToString(CultureInfo.InvariantCulture);
            return text + str + text;
        }
        public static string ToString(TimeSpan value)
        {
            return ToString(value, '"');
        }
        internal static string ToString(TimeSpan value, char quoteChar)
        {
            return ToString(value.ToString(), quoteChar);
        }
        public static string ToString([System.Runtime.CompilerServices.Nullable(2)] Uri value)
        {
            if (value == (Uri)null)
                return Null;
            return ToString(value, '"');
        }
        internal static string ToString(Uri value, char quoteChar)
        {
            return ToString(value.OriginalString, quoteChar);
        }
        public static string ToString([System.Runtime.CompilerServices.Nullable(2)] string value)
        {
            return ToString(value, '"');
        }
        public static string ToString([System.Runtime.CompilerServices.Nullable(2)] string value, char delimiter)
        {
            return ToString(value, delimiter, StringEscapeHandling.Default);
        }
        public static string ToString([System.Runtime.CompilerServices.Nullable(2)] string value, char delimiter, StringEscapeHandling stringEscapeHandling)
        {
            if (delimiter != '"' && delimiter != '\'')
                throw new ArgumentException("Delimiter must be a single or double quote.", "delimiter");
            return JavaScriptUtils.ToEscapedJavaScriptString(value, delimiter, true, stringEscapeHandling);
        }
        public static string ToString([System.Runtime.CompilerServices.Nullable(2)] object value)
        {
            if (value != null) {
                switch (ConvertUtils.GetTypeCode(value.GetType())) {
                case PrimitiveTypeCode.String:
                    return ToString((string)value);
                case PrimitiveTypeCode.Char:
                    return ToString((char)value);
                case PrimitiveTypeCode.Boolean:
                    return ToString((bool)value);
                case PrimitiveTypeCode.SByte:
                    return ToString((sbyte)value);
                case PrimitiveTypeCode.Int16:
                    return ToString((short)value);
                case PrimitiveTypeCode.UInt16:
                    return ToString((ushort)value);
                case PrimitiveTypeCode.Int32:
                    return ToString((int)value);
                case PrimitiveTypeCode.Byte:
                    return ToString((byte)value);
                case PrimitiveTypeCode.UInt32:
                    return ToString((uint)value);
                case PrimitiveTypeCode.Int64:
                    return ToString((long)value);
                case PrimitiveTypeCode.UInt64:
                    return ToString((ulong)value);
                case PrimitiveTypeCode.Single:
                    return ToString((float)value);
                case PrimitiveTypeCode.Double:
                    return ToString((double)value);
                case PrimitiveTypeCode.DateTime:
                    return ToString((DateTime)value);
                case PrimitiveTypeCode.Decimal:
                    return ToString((decimal)value);
                case PrimitiveTypeCode.DBNull:
                    return Null;
                case PrimitiveTypeCode.DateTimeOffset:
                    return ToString((DateTimeOffset)value);
                case PrimitiveTypeCode.Guid:
                    return ToString((Guid)value);
                case PrimitiveTypeCode.Uri:
                    return ToString((Uri)value);
                case PrimitiveTypeCode.TimeSpan:
                    return ToString((TimeSpan)value);
                default:
                    throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
                }
            }
            return Null;
        }
        [DebuggerStepThrough]
        public static string SerializeObject([System.Runtime.CompilerServices.Nullable(2)] object value)
        {
            return SerializeObject(value, (Type)null, (JsonSerializerSettings)null);
        }
        [DebuggerStepThrough]
        public static string SerializeObject([System.Runtime.CompilerServices.Nullable(2)] object value, Formatting formatting)
        {
            return SerializeObject(value, formatting, (JsonSerializerSettings)null);
        }
        [DebuggerStepThrough]
        public static string SerializeObject([System.Runtime.CompilerServices.Nullable(2)] object value, params JsonConverter[] converters)
        {
            JsonSerializerSettings settings = (converters != null && converters.Length != 0) ? new JsonSerializerSettings {
                Converters = converters
            } : null;
            return SerializeObject(value, null, settings);
        }
        [DebuggerStepThrough]
        public static string SerializeObject([System.Runtime.CompilerServices.Nullable(2)] object value, Formatting formatting, params JsonConverter[] converters)
        {
            JsonSerializerSettings settings = (converters != null && converters.Length != 0) ? new JsonSerializerSettings {
                Converters = converters
            } : null;
            return SerializeObject(value, null, formatting, settings);
        }
        [System.Runtime.CompilerServices.NullableContext(2)]
        [DebuggerStepThrough]
        [return: System.Runtime.CompilerServices.Nullable(1)]
        public static string SerializeObject(object value, JsonSerializerSettings settings)
        {
            return SerializeObject(value, null, settings);
        }
        [System.Runtime.CompilerServices.NullableContext(2)]
        [DebuggerStepThrough]
        [return: System.Runtime.CompilerServices.Nullable(1)]
        public static string SerializeObject(object value, Type type, JsonSerializerSettings settings)
        {
            JsonSerializer jsonSerializer = JsonSerializer.CreateDefault(settings);
            return SerializeObjectInternal(value, type, jsonSerializer);
        }
        [System.Runtime.CompilerServices.NullableContext(2)]
        [DebuggerStepThrough]
        [return: System.Runtime.CompilerServices.Nullable(1)]
        public static string SerializeObject(object value, Formatting formatting, JsonSerializerSettings settings)
        {
            return SerializeObject(value, null, formatting, settings);
        }
        [System.Runtime.CompilerServices.NullableContext(2)]
        [DebuggerStepThrough]
        [return: System.Runtime.CompilerServices.Nullable(1)]
        public static string SerializeObject(object value, Type type, Formatting formatting, JsonSerializerSettings settings)
        {
            JsonSerializer jsonSerializer = JsonSerializer.CreateDefault(settings);
            jsonSerializer.Formatting = formatting;
            return SerializeObjectInternal(value, type, jsonSerializer);
        }
        private static string SerializeObjectInternal([System.Runtime.CompilerServices.Nullable(2)] object value, [System.Runtime.CompilerServices.Nullable(2)] Type type, JsonSerializer jsonSerializer)
        {
            StringWriter stringWriter = new StringWriter(new StringBuilder(256), CultureInfo.InvariantCulture);
            using (JsonTextWriter jsonTextWriter = new JsonTextWriter(stringWriter)) {
                jsonTextWriter.Formatting = jsonSerializer.Formatting;
                jsonSerializer.Serialize(jsonTextWriter, value, type);
            }
            return stringWriter.ToString();
        }
        [DebuggerStepThrough]
        [return: System.Runtime.CompilerServices.Nullable(2)]
        public static object DeserializeObject(string value)
        {
            return DeserializeObject(value, (Type)null, (JsonSerializerSettings)null);
        }
        [DebuggerStepThrough]
        [return: System.Runtime.CompilerServices.Nullable(2)]
        public static object DeserializeObject(string value, JsonSerializerSettings settings)
        {
            return DeserializeObject(value, null, settings);
        }
        [DebuggerStepThrough]
        [return: System.Runtime.CompilerServices.Nullable(2)]
        public static object DeserializeObject(string value, Type type)
        {
            return DeserializeObject(value, type, (JsonSerializerSettings)null);
        }
        [System.Runtime.CompilerServices.NullableContext(2)]
        [DebuggerStepThrough]
        public static T DeserializeObject<T>([System.Runtime.CompilerServices.Nullable(1)] string value)
        {
            return JsonConvert.DeserializeObject<T>(value, (JsonSerializerSettings)null);
        }
        [DebuggerStepThrough]
        [return: System.Runtime.CompilerServices.Nullable(2)]
        public static T DeserializeAnonymousType<[System.Runtime.CompilerServices.Nullable(2)] T>(string value, T anonymousTypeObject)
        {
            return DeserializeObject<T>(value);
        }
        [DebuggerStepThrough]
        [return: System.Runtime.CompilerServices.Nullable(2)]
        public static T DeserializeAnonymousType<[System.Runtime.CompilerServices.Nullable(2)] T>(string value, T anonymousTypeObject, JsonSerializerSettings settings)
        {
            return DeserializeObject<T>(value, settings);
        }
        [DebuggerStepThrough]
        [return: System.Runtime.CompilerServices.Nullable(2)]
        public static T DeserializeObject<[System.Runtime.CompilerServices.Nullable(2)] T>(string value, params JsonConverter[] converters)
        {
            return (T)DeserializeObject(value, typeof(T), converters);
        }
        [System.Runtime.CompilerServices.NullableContext(2)]
        [DebuggerStepThrough]
        public static T DeserializeObject<T>([System.Runtime.CompilerServices.Nullable(1)] string value, JsonSerializerSettings settings)
        {
            return (T)DeserializeObject(value, typeof(T), settings);
        }
        [DebuggerStepThrough]
        [return: System.Runtime.CompilerServices.Nullable(2)]
        public static object DeserializeObject(string value, Type type, params JsonConverter[] converters)
        {
            JsonSerializerSettings settings = (converters != null && converters.Length != 0) ? new JsonSerializerSettings {
                Converters = converters
            } : null;
            return DeserializeObject(value, type, settings);
        }
        [System.Runtime.CompilerServices.NullableContext(2)]
        public static object DeserializeObject([System.Runtime.CompilerServices.Nullable(1)] string value, Type type, JsonSerializerSettings settings)
        {
            ValidationUtils.ArgumentNotNull(value, "value");
            JsonSerializer jsonSerializer = JsonSerializer.CreateDefault(settings);
            if (!jsonSerializer.IsCheckAdditionalContentSet())
                jsonSerializer.CheckAdditionalContent = true;
            using (JsonTextReader reader = new JsonTextReader(new StringReader(value)))
                return jsonSerializer.Deserialize(reader, type);
        }
        [DebuggerStepThrough]
        public static void PopulateObject(string value, object target)
        {
            PopulateObject(value, target, null);
        }
        public static void PopulateObject(string value, object target, [System.Runtime.CompilerServices.Nullable(2)] JsonSerializerSettings settings)
        {
            JsonSerializer jsonSerializer = JsonSerializer.CreateDefault(settings);
            using (JsonReader jsonReader = new JsonTextReader(new StringReader(value))) {
                jsonSerializer.Populate(jsonReader, target);
                if (settings != null && settings.CheckAdditionalContent) {
                    do {
                        if (!jsonReader.Read())
                            return;
                    } while (jsonReader.TokenType == JsonToken.Comment);
                    throw JsonSerializationException.Create(jsonReader, "Additional text found in JSON string after finishing deserializing object.");
                }
            }
        }
        public static string SerializeXmlNode([System.Runtime.CompilerServices.Nullable(2)] XmlNode node)
        {
            return SerializeXmlNode(node, Formatting.None);
        }
        public static string SerializeXmlNode([System.Runtime.CompilerServices.Nullable(2)] XmlNode node, Formatting formatting)
        {
            XmlNodeConverter xmlNodeConverter = new XmlNodeConverter();
            return SerializeObject(node, formatting, xmlNodeConverter);
        }
        public static string SerializeXmlNode([System.Runtime.CompilerServices.Nullable(2)] XmlNode node, Formatting formatting, bool omitRootObject)
        {
            XmlNodeConverter xmlNodeConverter = new XmlNodeConverter {
                OmitRootObject = omitRootObject
            };
            return SerializeObject(node, formatting, xmlNodeConverter);
        }
        [return: System.Runtime.CompilerServices.Nullable(2)]
        public static XmlDocument DeserializeXmlNode(string value)
        {
            return DeserializeXmlNode(value, null);
        }
        [System.Runtime.CompilerServices.NullableContext(2)]
        public static XmlDocument DeserializeXmlNode([System.Runtime.CompilerServices.Nullable(1)] string value, string deserializeRootElementName)
        {
            return DeserializeXmlNode(value, deserializeRootElementName, false);
        }
        [System.Runtime.CompilerServices.NullableContext(2)]
        public static XmlDocument DeserializeXmlNode([System.Runtime.CompilerServices.Nullable(1)] string value, string deserializeRootElementName, bool writeArrayAttribute)
        {
            return DeserializeXmlNode(value, deserializeRootElementName, writeArrayAttribute, false);
        }
        [System.Runtime.CompilerServices.NullableContext(2)]
        public static XmlDocument DeserializeXmlNode([System.Runtime.CompilerServices.Nullable(1)] string value, string deserializeRootElementName, bool writeArrayAttribute, bool encodeSpecialCharacters)
        {
            XmlNodeConverter xmlNodeConverter = new XmlNodeConverter();
            xmlNodeConverter.DeserializeRootElementName = deserializeRootElementName;
            xmlNodeConverter.WriteArrayAttribute = writeArrayAttribute;
            xmlNodeConverter.EncodeSpecialCharacters = encodeSpecialCharacters;
            return (XmlDocument)DeserializeObject(value, typeof(XmlDocument), xmlNodeConverter);
        }
        public static string SerializeXNode([System.Runtime.CompilerServices.Nullable(2)] XObject node)
        {
            return SerializeXNode(node, Formatting.None);
        }
        public static string SerializeXNode([System.Runtime.CompilerServices.Nullable(2)] XObject node, Formatting formatting)
        {
            return SerializeXNode(node, formatting, false);
        }
        public static string SerializeXNode([System.Runtime.CompilerServices.Nullable(2)] XObject node, Formatting formatting, bool omitRootObject)
        {
            XmlNodeConverter xmlNodeConverter = new XmlNodeConverter {
                OmitRootObject = omitRootObject
            };
            return SerializeObject(node, formatting, xmlNodeConverter);
        }
        [return: System.Runtime.CompilerServices.Nullable(2)]
        public static XDocument DeserializeXNode(string value)
        {
            return DeserializeXNode(value, null);
        }
        [System.Runtime.CompilerServices.NullableContext(2)]
        public static XDocument DeserializeXNode([System.Runtime.CompilerServices.Nullable(1)] string value, string deserializeRootElementName)
        {
            return DeserializeXNode(value, deserializeRootElementName, false);
        }
        [System.Runtime.CompilerServices.NullableContext(2)]
        public static XDocument DeserializeXNode([System.Runtime.CompilerServices.Nullable(1)] string value, string deserializeRootElementName, bool writeArrayAttribute)
        {
            return DeserializeXNode(value, deserializeRootElementName, writeArrayAttribute, false);
        }
        [System.Runtime.CompilerServices.NullableContext(2)]
        public static XDocument DeserializeXNode([System.Runtime.CompilerServices.Nullable(1)] string value, string deserializeRootElementName, bool writeArrayAttribute, bool encodeSpecialCharacters)
        {
            XmlNodeConverter xmlNodeConverter = new XmlNodeConverter();
            xmlNodeConverter.DeserializeRootElementName = deserializeRootElementName;
            xmlNodeConverter.WriteArrayAttribute = writeArrayAttribute;
            xmlNodeConverter.EncodeSpecialCharacters = encodeSpecialCharacters;
            return (XDocument)DeserializeObject(value, typeof(XDocument), xmlNodeConverter);
        }
    }
}