JToken
public abstract class JToken : IJEnumerable<JToken>, IEnumerable<JToken>, IEnumerable, IJsonLineInfo, IDynamicMetaObjectProvider
Represents an abstract JSON token.
using Newtonsoft.Json.Linq.JsonPath;
using Newtonsoft.Json.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Numerics;
using System.Text;
namespace Newtonsoft.Json.Linq
{
public abstract class JToken : IJEnumerable<JToken>, IEnumerable<JToken>, IEnumerable, IJsonLineInfo, IDynamicMetaObjectProvider
{
private JContainer _parent;
private JToken _previous;
private JToken _next;
private static JTokenEqualityComparer _equalityComparer;
private int? _lineNumber;
private int? _linePosition;
private static readonly JTokenType[] BooleanTypes = new JTokenType[6] {
JTokenType.Integer,
JTokenType.Float,
JTokenType.String,
JTokenType.Comment,
JTokenType.Raw,
JTokenType.Boolean
};
private static readonly JTokenType[] NumberTypes = new JTokenType[6] {
JTokenType.Integer,
JTokenType.Float,
JTokenType.String,
JTokenType.Comment,
JTokenType.Raw,
JTokenType.Boolean
};
private static readonly JTokenType[] BigIntegerTypes = new JTokenType[7] {
JTokenType.Integer,
JTokenType.Float,
JTokenType.String,
JTokenType.Comment,
JTokenType.Raw,
JTokenType.Boolean,
JTokenType.Bytes
};
private static readonly JTokenType[] StringTypes = new JTokenType[11] {
JTokenType.Date,
JTokenType.Integer,
JTokenType.Float,
JTokenType.String,
JTokenType.Comment,
JTokenType.Raw,
JTokenType.Boolean,
JTokenType.Bytes,
JTokenType.Guid,
JTokenType.TimeSpan,
JTokenType.Uri
};
private static readonly JTokenType[] GuidTypes = new JTokenType[5] {
JTokenType.String,
JTokenType.Comment,
JTokenType.Raw,
JTokenType.Guid,
JTokenType.Bytes
};
private static readonly JTokenType[] TimeSpanTypes = new JTokenType[4] {
JTokenType.String,
JTokenType.Comment,
JTokenType.Raw,
JTokenType.TimeSpan
};
private static readonly JTokenType[] UriTypes = new JTokenType[4] {
JTokenType.String,
JTokenType.Comment,
JTokenType.Raw,
JTokenType.Uri
};
private static readonly JTokenType[] CharTypes = new JTokenType[5] {
JTokenType.Integer,
JTokenType.Float,
JTokenType.String,
JTokenType.Comment,
JTokenType.Raw
};
private static readonly JTokenType[] DateTimeTypes = new JTokenType[4] {
JTokenType.Date,
JTokenType.String,
JTokenType.Comment,
JTokenType.Raw
};
private static readonly JTokenType[] BytesTypes = new JTokenType[5] {
JTokenType.Bytes,
JTokenType.String,
JTokenType.Comment,
JTokenType.Raw,
JTokenType.Integer
};
public static JTokenEqualityComparer EqualityComparer {
get {
if (_equalityComparer == null)
_equalityComparer = new JTokenEqualityComparer();
return _equalityComparer;
}
}
public JContainer Parent {
[DebuggerStepThrough]
get {
return _parent;
}
internal set {
_parent = value;
}
}
public JToken Root {
get {
JContainer parent = Parent;
if (parent == null)
return this;
while (parent.Parent != null) {
parent = parent.Parent;
}
return parent;
}
}
public abstract JTokenType Type { get; }
public abstract bool HasValues { get; }
public JToken Next {
get {
return _next;
}
internal set {
_next = value;
}
}
public JToken Previous {
get {
return _previous;
}
internal set {
_previous = value;
}
}
public string Path {
get {
if (Parent == null)
return string.Empty;
IList<JToken> list = Ancestors().Reverse().ToList();
list.Add(this);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < list.Count; i++) {
JToken jToken = list[i];
JToken jToken2 = null;
if (i + 1 < list.Count)
jToken2 = list[i + 1];
else if (list[i].Type == JTokenType.Property) {
jToken2 = list[i];
}
if (jToken2 != null) {
switch (jToken.Type) {
case JTokenType.Property: {
JProperty jProperty = (JProperty)jToken;
if (stringBuilder.Length > 0)
stringBuilder.Append('.');
stringBuilder.Append(jProperty.Name);
break;
}
case JTokenType.Array:
case JTokenType.Constructor: {
int value = ((IList<JToken>)jToken).IndexOf(jToken2);
stringBuilder.Append('[');
stringBuilder.Append(value);
stringBuilder.Append(']');
break;
}
}
}
}
return stringBuilder.ToString();
}
}
public virtual JToken this[object key] {
get {
throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType()));
}
set {
throw new InvalidOperationException("Cannot set child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType()));
}
}
public virtual JToken First {
get {
throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType()));
}
}
public virtual JToken Last {
get {
throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType()));
}
}
IJEnumerable<JToken> IJEnumerable<JToken>.this[object key] {
get {
return this[key];
}
}
int IJsonLineInfo.LineNumber {
get {
return _lineNumber ?? 0;
}
}
int IJsonLineInfo.LinePosition {
get {
return _linePosition ?? 0;
}
}
internal abstract JToken CloneToken();
internal abstract bool DeepEquals(JToken node);
public static bool DeepEquals(JToken t1, JToken t2)
{
if (t1 != t2) {
if (t1 != null && t2 != null)
return t1.DeepEquals(t2);
return false;
}
return true;
}
internal JToken()
{
}
public void AddAfterSelf(object content)
{
if (_parent == null)
throw new InvalidOperationException("The parent is missing.");
int num = _parent.IndexOfItem(this);
_parent.AddInternal(num + 1, content, false);
}
public void AddBeforeSelf(object content)
{
if (_parent == null)
throw new InvalidOperationException("The parent is missing.");
int index = _parent.IndexOfItem(this);
_parent.AddInternal(index, content, false);
}
public IEnumerable<JToken> Ancestors()
{
for (JToken parent = Parent; parent != null; parent = parent.Parent) {
yield return parent;
}
}
public IEnumerable<JToken> AfterSelf()
{
if (Parent != null) {
for (JToken o = Next; o != null; o = o.Next) {
yield return o;
}
}
}
public IEnumerable<JToken> BeforeSelf()
{
for (JToken o = Parent.First; o != this; o = o.Next) {
yield return o;
}
}
public virtual T Value<T>(object key)
{
JToken jToken = this[key];
if (jToken != null)
return jToken.Convert<JToken, T>();
return default(T);
}
public virtual JEnumerable<JToken> Children()
{
return JEnumerable<JToken>.Empty;
}
public JEnumerable<T> Children<T>() where T : JToken
{
return new JEnumerable<T>(Children().OfType<T>());
}
public virtual IEnumerable<T> Values<T>()
{
throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType()));
}
public void Remove()
{
if (_parent == null)
throw new InvalidOperationException("The parent is missing.");
_parent.RemoveItem(this);
}
public void Replace(JToken value)
{
if (_parent == null)
throw new InvalidOperationException("The parent is missing.");
_parent.ReplaceItem(this, value);
}
public abstract void WriteTo(JsonWriter writer, params JsonConverter[] converters);
public override string ToString()
{
return ToString(Formatting.Indented);
}
public string ToString(Formatting formatting, params JsonConverter[] converters)
{
using (StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture)) {
JsonTextWriter jsonTextWriter = new JsonTextWriter(stringWriter);
jsonTextWriter.Formatting = formatting;
WriteTo(jsonTextWriter, converters);
return stringWriter.ToString();
}
}
private static JValue EnsureValue(JToken value)
{
if (value == null)
throw new ArgumentNullException("value");
if (value is JProperty)
value = ((JProperty)value).Value;
return value as JValue;
}
private static string GetType(JToken token)
{
ValidationUtils.ArgumentNotNull(token, "token");
if (token is JProperty)
token = ((JProperty)token).Value;
return token.Type.ToString();
}
private static bool ValidateToken(JToken o, JTokenType[] validTypes, bool nullable)
{
if (Array.IndexOf(validTypes, o.Type) == -1) {
if (nullable) {
if (o.Type != JTokenType.Null)
return o.Type == JTokenType.Undefined;
return true;
}
return false;
}
return true;
}
public static explicit operator bool(JToken value)
{
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, BooleanTypes, false))
throw new ArgumentException("Can not convert {0} to Boolean.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return Convert.ToBoolean((int)(BigInteger)jValue.Value);
return Convert.ToBoolean(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator DateTimeOffset(JToken value)
{
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, DateTimeTypes, false))
throw new ArgumentException("Can not convert {0} to DateTimeOffset.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is DateTimeOffset)
return (DateTimeOffset)jValue.Value;
if (jValue.Value is string)
return DateTimeOffset.Parse((string)jValue.Value, CultureInfo.InvariantCulture);
return new DateTimeOffset(Convert.ToDateTime(jValue.Value, CultureInfo.InvariantCulture));
}
public static explicit operator bool?(JToken value)
{
if (value == null)
return null;
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, BooleanTypes, true))
throw new ArgumentException("Can not convert {0} to Boolean.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return Convert.ToBoolean((int)(BigInteger)jValue.Value);
if (jValue.Value == null)
return null;
return Convert.ToBoolean(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator long(JToken value)
{
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, NumberTypes, false))
throw new ArgumentException("Can not convert {0} to Int64.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return (long)(BigInteger)jValue.Value;
return Convert.ToInt64(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator DateTime?(JToken value)
{
if (value == null)
return null;
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, DateTimeTypes, true))
throw new ArgumentException("Can not convert {0} to DateTime.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is DateTimeOffset)
return ((DateTimeOffset)jValue.Value).DateTime;
if (jValue.Value == null)
return null;
return Convert.ToDateTime(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator DateTimeOffset?(JToken value)
{
if (value == null)
return null;
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, DateTimeTypes, true))
throw new ArgumentException("Can not convert {0} to DateTimeOffset.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value == null)
return null;
if (jValue.Value is DateTimeOffset)
return (DateTimeOffset?)jValue.Value;
if (jValue.Value is string)
return DateTimeOffset.Parse((string)jValue.Value, CultureInfo.InvariantCulture);
return new DateTimeOffset(Convert.ToDateTime(jValue.Value, CultureInfo.InvariantCulture));
}
public static explicit operator decimal?(JToken value)
{
if (value == null)
return null;
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, NumberTypes, true))
throw new ArgumentException("Can not convert {0} to Decimal.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return (decimal)(BigInteger)jValue.Value;
if (jValue.Value == null)
return null;
return Convert.ToDecimal(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator double?(JToken value)
{
if (value == null)
return null;
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, NumberTypes, true))
throw new ArgumentException("Can not convert {0} to Double.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return (double)(BigInteger)jValue.Value;
if (jValue.Value == null)
return null;
return Convert.ToDouble(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator char?(JToken value)
{
if (value == null)
return null;
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, CharTypes, true))
throw new ArgumentException("Can not convert {0} to Char.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return (char)(ushort)(BigInteger)jValue.Value;
if (jValue.Value == null)
return null;
return Convert.ToChar(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator int(JToken value)
{
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, NumberTypes, false))
throw new ArgumentException("Can not convert {0} to Int32.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return (int)(BigInteger)jValue.Value;
return Convert.ToInt32(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator short(JToken value)
{
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, NumberTypes, false))
throw new ArgumentException("Can not convert {0} to Int16.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return (short)(BigInteger)jValue.Value;
return Convert.ToInt16(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator ushort(JToken value)
{
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, NumberTypes, false))
throw new ArgumentException("Can not convert {0} to UInt16.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return (ushort)(BigInteger)jValue.Value;
return Convert.ToUInt16(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator char(JToken value)
{
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, CharTypes, false))
throw new ArgumentException("Can not convert {0} to Char.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return (char)(ushort)(BigInteger)jValue.Value;
return Convert.ToChar(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator byte(JToken value)
{
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, NumberTypes, false))
throw new ArgumentException("Can not convert {0} to Byte.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return (byte)(BigInteger)jValue.Value;
return Convert.ToByte(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator sbyte(JToken value)
{
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, NumberTypes, false))
throw new ArgumentException("Can not convert {0} to SByte.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return (sbyte)(BigInteger)jValue.Value;
return Convert.ToSByte(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator int?(JToken value)
{
if (value == null)
return null;
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, NumberTypes, true))
throw new ArgumentException("Can not convert {0} to Int32.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return (int)(BigInteger)jValue.Value;
if (jValue.Value == null)
return null;
return Convert.ToInt32(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator short?(JToken value)
{
if (value == null)
return null;
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, NumberTypes, true))
throw new ArgumentException("Can not convert {0} to Int16.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return (short)(BigInteger)jValue.Value;
if (jValue.Value == null)
return null;
return Convert.ToInt16(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator ushort?(JToken value)
{
if (value == null)
return null;
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, NumberTypes, true))
throw new ArgumentException("Can not convert {0} to UInt16.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return (ushort)(BigInteger)jValue.Value;
if (jValue.Value == null)
return null;
return Convert.ToUInt16(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator byte?(JToken value)
{
if (value == null)
return null;
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, NumberTypes, true))
throw new ArgumentException("Can not convert {0} to Byte.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return (byte)(BigInteger)jValue.Value;
if (jValue.Value == null)
return null;
return Convert.ToByte(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator sbyte?(JToken value)
{
if (value == null)
return null;
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, NumberTypes, true))
throw new ArgumentException("Can not convert {0} to SByte.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return (sbyte)(BigInteger)jValue.Value;
if (jValue.Value == null)
return null;
return (sbyte)Convert.ToByte(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator DateTime(JToken value)
{
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, DateTimeTypes, false))
throw new ArgumentException("Can not convert {0} to DateTime.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is DateTimeOffset)
return ((DateTimeOffset)jValue.Value).DateTime;
return Convert.ToDateTime(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator long?(JToken value)
{
if (value == null)
return null;
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, NumberTypes, true))
throw new ArgumentException("Can not convert {0} to Int64.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return (long)(BigInteger)jValue.Value;
if (jValue.Value == null)
return null;
return Convert.ToInt64(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator float?(JToken value)
{
if (value == null)
return null;
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, NumberTypes, true))
throw new ArgumentException("Can not convert {0} to Single.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return (float)(BigInteger)jValue.Value;
if (jValue.Value == null)
return null;
return Convert.ToSingle(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator decimal(JToken value)
{
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, NumberTypes, false))
throw new ArgumentException("Can not convert {0} to Decimal.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return (decimal)(BigInteger)jValue.Value;
return Convert.ToDecimal(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator uint?(JToken value)
{
if (value == null)
return null;
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, NumberTypes, true))
throw new ArgumentException("Can not convert {0} to UInt32.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return (uint)(BigInteger)jValue.Value;
if (jValue.Value == null)
return null;
return Convert.ToUInt32(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator ulong?(JToken value)
{
if (value == null)
return null;
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, NumberTypes, true))
throw new ArgumentException("Can not convert {0} to UInt64.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return (ulong)(BigInteger)jValue.Value;
if (jValue.Value == null)
return null;
return Convert.ToUInt64(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator double(JToken value)
{
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, NumberTypes, false))
throw new ArgumentException("Can not convert {0} to Double.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return (double)(BigInteger)jValue.Value;
return Convert.ToDouble(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator float(JToken value)
{
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, NumberTypes, false))
throw new ArgumentException("Can not convert {0} to Single.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return (float)(BigInteger)jValue.Value;
return Convert.ToSingle(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator string(JToken value)
{
if (value == null)
return null;
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, StringTypes, true))
throw new ArgumentException("Can not convert {0} to String.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value == null)
return null;
if (jValue.Value is byte[])
return Convert.ToBase64String((byte[])jValue.Value);
if (jValue.Value is BigInteger)
return ((BigInteger)jValue.Value).ToString(CultureInfo.InvariantCulture);
return Convert.ToString(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator uint(JToken value)
{
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, NumberTypes, false))
throw new ArgumentException("Can not convert {0} to UInt32.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return (uint)(BigInteger)jValue.Value;
return Convert.ToUInt32(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator ulong(JToken value)
{
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, NumberTypes, false))
throw new ArgumentException("Can not convert {0} to UInt64.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is BigInteger)
return (ulong)(BigInteger)jValue.Value;
return Convert.ToUInt64(jValue.Value, CultureInfo.InvariantCulture);
}
public static explicit operator byte[](JToken value)
{
if (value == null)
return null;
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, BytesTypes, false))
throw new ArgumentException("Can not convert {0} to byte array.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is string)
return Convert.FromBase64String(Convert.ToString(jValue.Value, CultureInfo.InvariantCulture));
if (jValue.Value is BigInteger)
return ((BigInteger)jValue.Value).ToByteArray();
if (jValue.Value is byte[])
return (byte[])jValue.Value;
throw new ArgumentException("Can not convert {0} to byte array.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
}
public static explicit operator Guid(JToken value)
{
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, GuidTypes, false))
throw new ArgumentException("Can not convert {0} to Guid.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value is byte[])
return new Guid((byte[])jValue.Value);
if (!(jValue.Value is Guid))
return new Guid(Convert.ToString(jValue.Value, CultureInfo.InvariantCulture));
return (Guid)jValue.Value;
}
public static explicit operator Guid?(JToken value)
{
if (value == null)
return null;
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, GuidTypes, true))
throw new ArgumentException("Can not convert {0} to Guid.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value == null)
return null;
if (jValue.Value is byte[])
return new Guid((byte[])jValue.Value);
return (jValue.Value is Guid) ? ((Guid)jValue.Value) : new Guid(Convert.ToString(jValue.Value, CultureInfo.InvariantCulture));
}
public static explicit operator TimeSpan(JToken value)
{
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, TimeSpanTypes, false))
throw new ArgumentException("Can not convert {0} to TimeSpan.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (!(jValue.Value is TimeSpan))
return ConvertUtils.ParseTimeSpan(Convert.ToString(jValue.Value, CultureInfo.InvariantCulture));
return (TimeSpan)jValue.Value;
}
public static explicit operator TimeSpan?(JToken value)
{
if (value == null)
return null;
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, TimeSpanTypes, true))
throw new ArgumentException("Can not convert {0} to TimeSpan.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value == null)
return null;
return (jValue.Value is TimeSpan) ? ((TimeSpan)jValue.Value) : ConvertUtils.ParseTimeSpan(Convert.ToString(jValue.Value, CultureInfo.InvariantCulture));
}
public static explicit operator Uri(JToken value)
{
if (value == null)
return null;
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, UriTypes, true))
throw new ArgumentException("Can not convert {0} to Uri.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value == null)
return null;
if (!(jValue.Value is Uri))
return new Uri(Convert.ToString(jValue.Value, CultureInfo.InvariantCulture));
return (Uri)jValue.Value;
}
private static BigInteger ToBigInteger(JToken value)
{
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, BigIntegerTypes, false))
throw new ArgumentException("Can not convert {0} to BigInteger.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
return ConvertUtils.ToBigInteger(jValue.Value);
}
private static BigInteger? ToBigIntegerNullable(JToken value)
{
JValue jValue = EnsureValue(value);
if (jValue == null || !ValidateToken(jValue, BigIntegerTypes, true))
throw new ArgumentException("Can not convert {0} to BigInteger.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (jValue.Value == null)
return null;
return ConvertUtils.ToBigInteger(jValue.Value);
}
public static implicit operator JToken(bool value)
{
return new JValue(value);
}
public static implicit operator JToken(DateTimeOffset value)
{
return new JValue(value);
}
public static implicit operator JToken(byte value)
{
return new JValue(value);
}
public static implicit operator JToken(byte? value)
{
return new JValue(value);
}
public static implicit operator JToken(sbyte value)
{
return new JValue(value);
}
public static implicit operator JToken(sbyte? value)
{
return new JValue(value);
}
public static implicit operator JToken(bool? value)
{
return new JValue(value);
}
public static implicit operator JToken(long value)
{
return new JValue(value);
}
public static implicit operator JToken(DateTime? value)
{
return new JValue(value);
}
public static implicit operator JToken(DateTimeOffset? value)
{
return new JValue(value);
}
public static implicit operator JToken(decimal? value)
{
return new JValue(value);
}
public static implicit operator JToken(double? value)
{
return new JValue(value);
}
public static implicit operator JToken(short value)
{
return new JValue(value);
}
public static implicit operator JToken(ushort value)
{
return new JValue(value);
}
public static implicit operator JToken(int value)
{
return new JValue(value);
}
public static implicit operator JToken(int? value)
{
return new JValue(value);
}
public static implicit operator JToken(DateTime value)
{
return new JValue(value);
}
public static implicit operator JToken(long? value)
{
return new JValue(value);
}
public static implicit operator JToken(float? value)
{
return new JValue(value);
}
public static implicit operator JToken(decimal value)
{
return new JValue(value);
}
public static implicit operator JToken(short? value)
{
return new JValue(value);
}
public static implicit operator JToken(ushort? value)
{
return new JValue(value);
}
public static implicit operator JToken(uint? value)
{
return new JValue(value);
}
public static implicit operator JToken(ulong? value)
{
return new JValue(value);
}
public static implicit operator JToken(double value)
{
return new JValue(value);
}
public static implicit operator JToken(float value)
{
return new JValue(value);
}
public static implicit operator JToken(string value)
{
return new JValue(value);
}
public static implicit operator JToken(uint value)
{
return new JValue(value);
}
public static implicit operator JToken(ulong value)
{
return new JValue(value);
}
public static implicit operator JToken(byte[] value)
{
return new JValue(value);
}
public static implicit operator JToken(Uri value)
{
return new JValue(value);
}
public static implicit operator JToken(TimeSpan value)
{
return new JValue(value);
}
public static implicit operator JToken(TimeSpan? value)
{
return new JValue(value);
}
public static implicit operator JToken(Guid value)
{
return new JValue(value);
}
public static implicit operator JToken(Guid? value)
{
return new JValue(value);
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<JToken>)this).GetEnumerator();
}
IEnumerator<JToken> IEnumerable<JToken>.GetEnumerator()
{
return Children().GetEnumerator();
}
internal abstract int GetDeepHashCode();
public JsonReader CreateReader()
{
return new JTokenReader(this, Path);
}
internal static JToken FromObjectInternal(object o, JsonSerializer jsonSerializer)
{
ValidationUtils.ArgumentNotNull(o, "o");
ValidationUtils.ArgumentNotNull(jsonSerializer, "jsonSerializer");
using (JTokenWriter jTokenWriter = new JTokenWriter()) {
jsonSerializer.Serialize(jTokenWriter, o);
return jTokenWriter.Token;
}
}
public static JToken FromObject(object o)
{
return FromObjectInternal(o, JsonSerializer.CreateDefault());
}
public static JToken FromObject(object o, JsonSerializer jsonSerializer)
{
return FromObjectInternal(o, jsonSerializer);
}
public T ToObject<T>()
{
return (T)ToObject(typeof(T));
}
public object ToObject(Type objectType)
{
if (JsonConvert.DefaultSettings == null) {
switch (ConvertUtils.GetTypeCode(objectType)) {
case PrimitiveTypeCode.BooleanNullable:
return (bool?)this;
case PrimitiveTypeCode.Boolean:
return (bool)this;
case PrimitiveTypeCode.CharNullable:
return (char?)this;
case PrimitiveTypeCode.Char:
return (char)this;
case PrimitiveTypeCode.SByte:
return (sbyte?)this;
case PrimitiveTypeCode.SByteNullable:
return (sbyte)this;
case PrimitiveTypeCode.ByteNullable:
return (byte?)this;
case PrimitiveTypeCode.Byte:
return (byte)this;
case PrimitiveTypeCode.Int16Nullable:
return (short?)this;
case PrimitiveTypeCode.Int16:
return (short)this;
case PrimitiveTypeCode.UInt16Nullable:
return (ushort?)this;
case PrimitiveTypeCode.UInt16:
return (ushort)this;
case PrimitiveTypeCode.Int32Nullable:
return (int?)this;
case PrimitiveTypeCode.Int32:
return (int)this;
case PrimitiveTypeCode.UInt32Nullable:
return (uint?)this;
case PrimitiveTypeCode.UInt32:
return (uint)this;
case PrimitiveTypeCode.Int64Nullable:
return (long?)this;
case PrimitiveTypeCode.Int64:
return (long)this;
case PrimitiveTypeCode.UInt64Nullable:
return (ulong?)this;
case PrimitiveTypeCode.UInt64:
return (ulong)this;
case PrimitiveTypeCode.SingleNullable:
return (float?)this;
case PrimitiveTypeCode.Single:
return (float)this;
case PrimitiveTypeCode.DoubleNullable:
return (double?)this;
case PrimitiveTypeCode.Double:
return (double)this;
case PrimitiveTypeCode.DecimalNullable:
return (decimal?)this;
case PrimitiveTypeCode.Decimal:
return (decimal)this;
case PrimitiveTypeCode.DateTimeNullable:
return (DateTime?)this;
case PrimitiveTypeCode.DateTime:
return (DateTime)this;
case PrimitiveTypeCode.DateTimeOffsetNullable:
return (DateTimeOffset?)this;
case PrimitiveTypeCode.DateTimeOffset:
return (DateTimeOffset)this;
case PrimitiveTypeCode.String:
return (string)this;
case PrimitiveTypeCode.GuidNullable:
return (Guid?)this;
case PrimitiveTypeCode.Guid:
return (Guid)this;
case PrimitiveTypeCode.Uri:
return (Uri)this;
case PrimitiveTypeCode.TimeSpanNullable:
return (TimeSpan?)this;
case PrimitiveTypeCode.TimeSpan:
return (TimeSpan)this;
case PrimitiveTypeCode.BigIntegerNullable:
return ToBigIntegerNullable(this);
case PrimitiveTypeCode.BigInteger:
return ToBigInteger(this);
}
}
return ToObject(objectType, JsonSerializer.CreateDefault());
}
public T ToObject<T>(JsonSerializer jsonSerializer)
{
return (T)ToObject(typeof(T), jsonSerializer);
}
public object ToObject(Type objectType, JsonSerializer jsonSerializer)
{
ValidationUtils.ArgumentNotNull(jsonSerializer, "jsonSerializer");
using (JTokenReader reader = new JTokenReader(this))
return jsonSerializer.Deserialize(reader, objectType);
}
public static JToken ReadFrom(JsonReader reader)
{
ValidationUtils.ArgumentNotNull(reader, "reader");
if (reader.TokenType != 0 || reader.Read()) {
IJsonLineInfo lineInfo = reader as IJsonLineInfo;
switch (reader.TokenType) {
case JsonToken.StartObject:
return JObject.Load(reader);
case JsonToken.StartArray:
return JArray.Load(reader);
case JsonToken.StartConstructor:
return JConstructor.Load(reader);
case JsonToken.PropertyName:
return JProperty.Load(reader);
case JsonToken.Integer:
case JsonToken.Float:
case JsonToken.String:
case JsonToken.Boolean:
case JsonToken.Date:
case JsonToken.Bytes: {
JValue jValue = new JValue(reader.Value);
jValue.SetLineInfo(lineInfo);
return jValue;
}
case JsonToken.Comment: {
JValue jValue = JValue.CreateComment(reader.Value.ToString());
jValue.SetLineInfo(lineInfo);
return jValue;
}
case JsonToken.Null: {
JValue jValue = JValue.CreateNull();
jValue.SetLineInfo(lineInfo);
return jValue;
}
case JsonToken.Undefined: {
JValue jValue = JValue.CreateUndefined();
jValue.SetLineInfo(lineInfo);
return jValue;
}
default:
throw JsonReaderException.Create(reader, "Error reading JToken from JsonReader. Unexpected token: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
}
}
throw JsonReaderException.Create(reader, "Error reading JToken from JsonReader.");
}
public static JToken Parse(string json)
{
using (JsonReader jsonReader = new JsonTextReader(new StringReader(json))) {
JToken result = Load(jsonReader);
if (jsonReader.Read() && jsonReader.TokenType != JsonToken.Comment)
throw JsonReaderException.Create(jsonReader, "Additional text found in JSON string after parsing content.");
return result;
}
}
public static JToken Load(JsonReader reader)
{
return ReadFrom(reader);
}
internal void SetLineInfo(IJsonLineInfo lineInfo)
{
if (lineInfo != null && lineInfo.HasLineInfo())
SetLineInfo(lineInfo.LineNumber, lineInfo.LinePosition);
}
internal void SetLineInfo(int lineNumber, int linePosition)
{
_lineNumber = lineNumber;
_linePosition = linePosition;
}
bool IJsonLineInfo.HasLineInfo()
{
if (_lineNumber.HasValue)
return _linePosition.HasValue;
return false;
}
public JToken SelectToken(string path)
{
return SelectToken(path, false);
}
public JToken SelectToken(string path, bool errorWhenNoMatch)
{
JPath jPath = new JPath(path);
JToken jToken = null;
foreach (JToken item in jPath.Evaluate(this, errorWhenNoMatch)) {
if (jToken != null)
throw new JsonException("Path returned multiple tokens.");
jToken = item;
}
return jToken;
}
public IEnumerable<JToken> SelectTokens(string path)
{
return SelectTokens(path, false);
}
public IEnumerable<JToken> SelectTokens(string path, bool errorWhenNoMatch)
{
JPath jPath = new JPath(path);
return jPath.Evaluate(this, errorWhenNoMatch);
}
protected virtual DynamicMetaObject GetMetaObject(Expression parameter)
{
return new DynamicProxyMetaObject<JToken>(parameter, this, new DynamicProxy<JToken>(), true);
}
DynamicMetaObject IDynamicMetaObjectProvider.GetMetaObject(Expression parameter)
{
return GetMetaObject(parameter);
}
public JToken DeepClone()
{
return CloneToken();
}
}
}