using System;
using System.Globalization;
using System.IO;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using DPumpHydr.WinFrmUI.WenSkin.Json.Converters;
using DPumpHydr.WinFrmUI.WenSkin.Json.Utilities;
namespace DPumpHydr.WinFrmUI.WenSkin.Json
{
///
/// Provides methods for converting between common language runtime types and JSON types.
///
///
///
///
public static class JsonConvert
{
///
/// Represents JavaScript's boolean value true as a string. This field is read-only.
///
public static readonly string True = "true";
///
/// Represents JavaScript's boolean value false as a string. This field is read-only.
///
public static readonly string False = "false";
///
/// Represents JavaScript's null as a string. This field is read-only.
///
public static readonly string Null = "null";
///
/// Represents JavaScript's undefined as a string. This field is read-only.
///
public static readonly string Undefined = "undefined";
///
/// Represents JavaScript's positive infinity as a string. This field is read-only.
///
public static readonly string PositiveInfinity = "Infinity";
///
/// Represents JavaScript's negative infinity as a string. This field is read-only.
///
public static readonly string NegativeInfinity = "-Infinity";
///
/// Represents JavaScript's NaN as a string. This field is read-only.
///
public static readonly string NaN = "NaN";
///
/// Gets or sets a function that creates default .
/// Default settings are automatically used by serialization methods on ,
/// and and on .
/// To serialize without using any default settings create a with
/// .
///
public static Func DefaultSettings { get; set; }
///
/// Converts the to its JSON string representation.
///
/// The value to convert.
/// A JSON string representation of the .
public static string ToString(DateTime value)
{
return ToString(value, DateFormatHandling.IsoDateFormat, DateTimeZoneHandling.RoundtripKind);
}
///
/// Converts the to its JSON string representation using the specified.
///
/// The value to convert.
/// The format the date will be converted to.
/// The time zone handling when the date is converted to a string.
/// A JSON string representation of the .
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();
}
///
/// Converts the to its JSON string representation.
///
/// The value to convert.
/// A JSON string representation of the .
public static string ToString(DateTimeOffset value)
{
return ToString(value, DateFormatHandling.IsoDateFormat);
}
///
/// Converts the to its JSON string representation using the specified.
///
/// The value to convert.
/// The format the date will be converted to.
/// A JSON string representation of the .
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();
}
///
/// Converts the to its JSON string representation.
///
/// The value to convert.
/// A JSON string representation of the .
public static string ToString(bool value)
{
if (!value)
{
return False;
}
return True;
}
///
/// Converts the to its JSON string representation.
///
/// The value to convert.
/// A JSON string representation of the .
public static string ToString(char value)
{
return ToString(char.ToString(value));
}
///
/// Converts the to its JSON string representation.
///
/// The value to convert.
/// A JSON string representation of the .
public static string ToString(Enum value)
{
return value.ToString("D");
}
///
/// Converts the to its JSON string representation.
///
/// The value to convert.
/// A JSON string representation of the .
public static string ToString(int value)
{
return value.ToString(null, CultureInfo.InvariantCulture);
}
///
/// Converts the to its JSON string representation.
///
/// The value to convert.
/// A JSON string representation of the .
public static string ToString(short value)
{
return value.ToString(null, CultureInfo.InvariantCulture);
}
///
/// Converts the to its JSON string representation.
///
/// The value to convert.
/// A JSON string representation of the .
[CLSCompliant(false)]
public static string ToString(ushort value)
{
return value.ToString(null, CultureInfo.InvariantCulture);
}
///
/// Converts the to its JSON string representation.
///
/// The value to convert.
/// A JSON string representation of the .
[CLSCompliant(false)]
public static string ToString(uint value)
{
return value.ToString(null, CultureInfo.InvariantCulture);
}
///
/// Converts the to its JSON string representation.
///
/// The value to convert.
/// A JSON string representation of the .
public static string ToString(long value)
{
return value.ToString(null, CultureInfo.InvariantCulture);
}
private static string ToStringInternal(BigInteger value)
{
return value.ToString(null, CultureInfo.InvariantCulture);
}
///
/// Converts the to its JSON string representation.
///
/// The value to convert.
/// A JSON string representation of the .
[CLSCompliant(false)]
public static string ToString(ulong value)
{
return value.ToString(null, CultureInfo.InvariantCulture);
}
///
/// Converts the to its JSON string representation.
///
/// The value to convert.
/// A JSON string representation of the .
public static string ToString(float value)
{
return EnsureDecimalPlace(value, value.ToString("R", CultureInfo.InvariantCulture));
}
internal static string ToString(float value, FloatFormatHandling floatFormatHandling, char quoteChar, bool nullable)
{
return EnsureFloatFormat(value, EnsureDecimalPlace(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 + text + quoteChar;
}
///
/// Converts the to its JSON string representation.
///
/// The value to convert.
/// A JSON string representation of the .
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) || text.IndexOf('.') != -1 || text.IndexOf('E') != -1 || text.IndexOf('e') != -1)
{
return text;
}
return text + ".0";
}
private static string EnsureDecimalPlace(string text)
{
if (text.IndexOf('.') != -1)
{
return text;
}
return text + ".0";
}
///
/// Converts the to its JSON string representation.
///
/// The value to convert.
/// A JSON string representation of the .
public static string ToString(byte value)
{
return value.ToString(null, CultureInfo.InvariantCulture);
}
///
/// Converts the to its JSON string representation.
///
/// The value to convert.
/// A JSON string representation of the .
[CLSCompliant(false)]
public static string ToString(sbyte value)
{
return value.ToString(null, CultureInfo.InvariantCulture);
}
///
/// Converts the to its JSON string representation.
///
/// The value to convert.
/// A JSON string representation of the .
public static string ToString(decimal value)
{
return EnsureDecimalPlace(value.ToString(null, CultureInfo.InvariantCulture));
}
///
/// Converts the to its JSON string representation.
///
/// The value to convert.
/// A JSON string representation of the .
public static string ToString(Guid value)
{
return ToString(value, '"');
}
internal static string ToString(Guid value, char quoteChar)
{
string text = value.ToString("D", CultureInfo.InvariantCulture);
string text2 = quoteChar.ToString(CultureInfo.InvariantCulture);
return text2 + text + text2;
}
///
/// Converts the to its JSON string representation.
///
/// The value to convert.
/// A JSON string representation of the .
public static string ToString(TimeSpan value)
{
return ToString(value, '"');
}
internal static string ToString(TimeSpan value, char quoteChar)
{
return ToString(value.ToString(), quoteChar);
}
///
/// Converts the to its JSON string representation.
///
/// The value to convert.
/// A JSON string representation of the .
public static string ToString(Uri value)
{
if (value == null)
{
return Null;
}
return ToString(value, '"');
}
internal static string ToString(Uri value, char quoteChar)
{
return ToString(value.OriginalString, quoteChar);
}
///
/// Converts the to its JSON string representation.
///
/// The value to convert.
/// A JSON string representation of the .
public static string ToString(string value)
{
return ToString(value, '"');
}
///
/// Converts the to its JSON string representation.
///
/// The value to convert.
/// The string delimiter character.
/// A JSON string representation of the .
public static string ToString(string value, char delimiter)
{
return ToString(value, delimiter, StringEscapeHandling.Default);
}
///
/// Converts the to its JSON string representation.
///
/// The value to convert.
/// The string delimiter character.
/// The string escape handling.
/// A JSON string representation of the .
public static string ToString(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, appendDelimiters: true, stringEscapeHandling);
}
///
/// Converts the to its JSON string representation.
///
/// The value to convert.
/// A JSON string representation of the .
public static string ToString(object value)
{
if (value == null)
{
return Null;
}
return ConvertUtils.GetTypeCode(value.GetType()) switch
{
PrimitiveTypeCode.String => ToString((string)value),
PrimitiveTypeCode.Char => ToString((char)value),
PrimitiveTypeCode.Boolean => ToString((bool)value),
PrimitiveTypeCode.SByte => ToString((sbyte)value),
PrimitiveTypeCode.Int16 => ToString((short)value),
PrimitiveTypeCode.UInt16 => ToString((ushort)value),
PrimitiveTypeCode.Int32 => ToString((int)value),
PrimitiveTypeCode.Byte => ToString((byte)value),
PrimitiveTypeCode.UInt32 => ToString((uint)value),
PrimitiveTypeCode.Int64 => ToString((long)value),
PrimitiveTypeCode.UInt64 => ToString((ulong)value),
PrimitiveTypeCode.Single => ToString((float)value),
PrimitiveTypeCode.Double => ToString((double)value),
PrimitiveTypeCode.DateTime => ToString((DateTime)value),
PrimitiveTypeCode.Decimal => ToString((decimal)value),
PrimitiveTypeCode.DBNull => Null,
PrimitiveTypeCode.DateTimeOffset => ToString((DateTimeOffset)value),
PrimitiveTypeCode.Guid => ToString((Guid)value),
PrimitiveTypeCode.Uri => ToString((Uri)value),
PrimitiveTypeCode.TimeSpan => ToString((TimeSpan)value),
PrimitiveTypeCode.BigInteger => ToStringInternal((BigInteger)value),
_ => throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType())),
};
}
///
/// Serializes the specified object to a JSON string.
///
/// The object to serialize.
/// A JSON string representation of the object.
public static string SerializeObject(object value)
{
return SerializeObject(value, (Type)null, (JsonSerializerSettings)null);
}
///
/// Serializes the specified object to a JSON string using formatting.
///
/// The object to serialize.
/// Indicates how the output is formatted.
///
/// A JSON string representation of the object.
///
public static string SerializeObject(object value, Formatting formatting)
{
return SerializeObject(value, formatting, (JsonSerializerSettings)null);
}
///
/// Serializes the specified object to a JSON string using a collection of .
///
/// The object to serialize.
/// A collection converters used while serializing.
/// A JSON string representation of the object.
public static string SerializeObject(object value, params JsonConverter[] converters)
{
JsonSerializerSettings settings = ((converters != null && converters.Length != 0) ? new JsonSerializerSettings
{
Converters = converters
} : null);
return SerializeObject(value, null, settings);
}
///
/// Serializes the specified object to a JSON string using formatting and a collection of .
///
/// The object to serialize.
/// Indicates how the output is formatted.
/// A collection converters used while serializing.
/// A JSON string representation of the object.
public static string SerializeObject(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);
}
///
/// Serializes the specified object to a JSON string using .
///
/// The object to serialize.
/// The used to serialize the object.
/// If this is null, default serialization settings will be used.
///
/// A JSON string representation of the object.
///
public static string SerializeObject(object value, JsonSerializerSettings settings)
{
return SerializeObject(value, null, settings);
}
///
/// Serializes the specified object to a JSON string using a type, formatting and .
///
/// The object to serialize.
/// The used to serialize the object.
/// If this is null, default serialization settings will be used.
///
/// The type of the value being serialized.
/// This parameter is used when is Auto to write out the type name if the type of the value does not match.
/// Specifing the type is optional.
///
///
/// A JSON string representation of the object.
///
public static string SerializeObject(object value, Type type, JsonSerializerSettings settings)
{
JsonSerializer jsonSerializer = JsonSerializer.CreateDefault(settings);
return SerializeObjectInternal(value, type, jsonSerializer);
}
///
/// Serializes the specified object to a JSON string using formatting and .
///
/// The object to serialize.
/// Indicates how the output is formatted.
/// The used to serialize the object.
/// If this is null, default serialization settings will be used.
///
/// A JSON string representation of the object.
///
public static string SerializeObject(object value, Formatting formatting, JsonSerializerSettings settings)
{
return SerializeObject(value, null, formatting, settings);
}
///
/// Serializes the specified object to a JSON string using a type, formatting and .
///
/// The object to serialize.
/// Indicates how the output is formatted.
/// The used to serialize the object.
/// If this is null, default serialization settings will be used.
///
/// The type of the value being serialized.
/// This parameter is used when is Auto to write out the type name if the type of the value does not match.
/// Specifing the type is optional.
///
///
/// A JSON string representation of the object.
///
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(object value, 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();
}
///
/// Asynchronously serializes the specified object to a JSON string.
/// Serialization will happen on a new thread.
///
/// The object to serialize.
///
/// A task that represents the asynchronous serialize operation. The value of the TResult parameter contains a JSON string representation of the object.
///
[Obsolete("SerializeObjectAsync is obsolete. Use the Task.Factory.StartNew method to serialize JSON asynchronously: Task.Factory.StartNew(() => JsonConvert.SerializeObject(value))")]
public static Task SerializeObjectAsync(object value)
{
return SerializeObjectAsync(value, Formatting.None, null);
}
///
/// Asynchronously serializes the specified object to a JSON string using formatting.
/// Serialization will happen on a new thread.
///
/// The object to serialize.
/// Indicates how the output is formatted.
///
/// A task that represents the asynchronous serialize operation. The value of the TResult parameter contains a JSON string representation of the object.
///
[Obsolete("SerializeObjectAsync is obsolete. Use the Task.Factory.StartNew method to serialize JSON asynchronously: Task.Factory.StartNew(() => JsonConvert.SerializeObject(value, formatting))")]
public static Task SerializeObjectAsync(object value, Formatting formatting)
{
return SerializeObjectAsync(value, formatting, null);
}
///
/// Asynchronously serializes the specified object to a JSON string using formatting and a collection of .
/// Serialization will happen on a new thread.
///
/// The object to serialize.
/// Indicates how the output is formatted.
/// The used to serialize the object.
/// If this is null, default serialization settings will be used.
///
/// A task that represents the asynchronous serialize operation. The value of the TResult parameter contains a JSON string representation of the object.
///
[Obsolete("SerializeObjectAsync is obsolete. Use the Task.Factory.StartNew method to serialize JSON asynchronously: Task.Factory.StartNew(() => JsonConvert.SerializeObject(value, formatting, settings))")]
public static Task SerializeObjectAsync(object value, Formatting formatting, JsonSerializerSettings settings)
{
return Task.Factory.StartNew(() => SerializeObject(value, formatting, settings));
}
///
/// Deserializes the JSON to a .NET object.
///
/// The JSON to deserialize.
/// The deserialized object from the JSON string.
public static object DeserializeObject(string value)
{
return DeserializeObject(value, (Type)null, (JsonSerializerSettings)null);
}
///
/// Deserializes the JSON to a .NET object using .
///
/// The JSON to deserialize.
///
/// The used to deserialize the object.
/// If this is null, default serialization settings will be used.
///
/// The deserialized object from the JSON string.
public static object DeserializeObject(string value, JsonSerializerSettings settings)
{
return DeserializeObject(value, null, settings);
}
///
/// Deserializes the JSON to the specified .NET type.
///
/// The JSON to deserialize.
/// The of object being deserialized.
/// The deserialized object from the JSON string.
public static object DeserializeObject(string value, Type type)
{
return DeserializeObject(value, type, (JsonSerializerSettings)null);
}
///
/// Deserializes the JSON to the specified .NET type.
///
/// The type of the object to deserialize to.
/// The JSON to deserialize.
/// The deserialized object from the JSON string.
public static T DeserializeObject(string value)
{
return JsonConvert.DeserializeObject(value, (JsonSerializerSettings)null);
}
///
/// Deserializes the JSON to the given anonymous type.
///
///
/// The anonymous type to deserialize to. This can't be specified
/// traditionally and must be infered from the anonymous type passed
/// as a parameter.
///
/// The JSON to deserialize.
/// The anonymous type object.
/// The deserialized anonymous type from the JSON string.
public static T DeserializeAnonymousType(string value, T anonymousTypeObject)
{
return DeserializeObject(value);
}
///
/// Deserializes the JSON to the given anonymous type using .
///
///
/// The anonymous type to deserialize to. This can't be specified
/// traditionally and must be infered from the anonymous type passed
/// as a parameter.
///
/// The JSON to deserialize.
/// The anonymous type object.
///
/// The used to deserialize the object.
/// If this is null, default serialization settings will be used.
///
/// The deserialized anonymous type from the JSON string.
public static T DeserializeAnonymousType(string value, T anonymousTypeObject, JsonSerializerSettings settings)
{
return DeserializeObject(value, settings);
}
///
/// Deserializes the JSON to the specified .NET type using a collection of .
///
/// The type of the object to deserialize to.
/// The JSON to deserialize.
/// Converters to use while deserializing.
/// The deserialized object from the JSON string.
public static T DeserializeObject(string value, params JsonConverter[] converters)
{
return (T)DeserializeObject(value, typeof(T), converters);
}
///
/// Deserializes the JSON to the specified .NET type using .
///
/// The type of the object to deserialize to.
/// The object to deserialize.
///
/// The used to deserialize the object.
/// If this is null, default serialization settings will be used.
///
/// The deserialized object from the JSON string.
public static T DeserializeObject(string value, JsonSerializerSettings settings)
{
return (T)DeserializeObject(value, typeof(T), settings);
}
///
/// Deserializes the JSON to the specified .NET type using a collection of .
///
/// The JSON to deserialize.
/// The type of the object to deserialize.
/// Converters to use while deserializing.
/// The deserialized object from the JSON string.
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);
}
///
/// Deserializes the JSON to the specified .NET type using .
///
/// The JSON to deserialize.
/// The type of the object to deserialize to.
///
/// The used to deserialize the object.
/// If this is null, default serialization settings will be used.
///
/// The deserialized object from the JSON string.
public static object DeserializeObject(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);
}
///
/// Asynchronously deserializes the JSON to the specified .NET type.
/// Deserialization will happen on a new thread.
///
/// The type of the object to deserialize to.
/// The JSON to deserialize.
///
/// A task that represents the asynchronous deserialize operation. The value of the TResult parameter contains the deserialized object from the JSON string.
///
[Obsolete("DeserializeObjectAsync is obsolete. Use the Task.Factory.StartNew method to deserialize JSON asynchronously: Task.Factory.StartNew(() => JsonConvert.DeserializeObject(value))")]
public static Task DeserializeObjectAsync(string value)
{
return DeserializeObjectAsync(value, null);
}
///
/// Asynchronously deserializes the JSON to the specified .NET type using .
/// Deserialization will happen on a new thread.
///
/// The type of the object to deserialize to.
/// The JSON to deserialize.
///
/// The used to deserialize the object.
/// If this is null, default serialization settings will be used.
///
///
/// A task that represents the asynchronous deserialize operation. The value of the TResult parameter contains the deserialized object from the JSON string.
///
[Obsolete("DeserializeObjectAsync is obsolete. Use the Task.Factory.StartNew method to deserialize JSON asynchronously: Task.Factory.StartNew(() => JsonConvert.DeserializeObject(value, settings))")]
public static Task DeserializeObjectAsync(string value, JsonSerializerSettings settings)
{
return Task.Factory.StartNew(() => DeserializeObject(value, settings));
}
///
/// Asynchronously deserializes the JSON to the specified .NET type.
/// Deserialization will happen on a new thread.
///
/// The JSON to deserialize.
///
/// A task that represents the asynchronous deserialize operation. The value of the TResult parameter contains the deserialized object from the JSON string.
///
[Obsolete("DeserializeObjectAsync is obsolete. Use the Task.Factory.StartNew method to deserialize JSON asynchronously: Task.Factory.StartNew(() => JsonConvert.DeserializeObject(value))")]
public static Task