-Added BigInteger support

This commit is contained in:
James Newton-King
2013-03-16 19:06:05 +13:00
parent b81dfd352a
commit 1356c31670
24 changed files with 957 additions and 50 deletions
+214 -2
View File
@@ -30,6 +30,9 @@ using System.Dynamic;
using System.Linq.Expressions;
#endif
using System.IO;
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
using System.Numerics;
#endif
using System.Text;
using Newtonsoft.Json.Utilities;
using System.Diagnostics;
@@ -64,13 +67,14 @@ namespace Newtonsoft.Json.Linq
private static readonly JTokenType[] BooleanTypes = new[] { JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Boolean };
private static readonly JTokenType[] NumberTypes = new[] { JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Boolean };
private static readonly JTokenType[] BigIntegerTypes = new[] { JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Boolean, JTokenType.Bytes };
private static readonly JTokenType[] StringTypes = new[] { 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.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Guid };
private static readonly JTokenType[] TimeSpanTypes = new[] { JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.TimeSpan };
private static readonly JTokenType[] UriTypes = new[] { JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Uri };
private static readonly JTokenType[] CharTypes = new[] { JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Comment, JTokenType.Raw };
private static readonly JTokenType[] DateTimeTypes = new[] { JTokenType.Date, JTokenType.String, JTokenType.Comment, JTokenType.Raw };
private static readonly JTokenType[] BytesTypes = new[] { JTokenType.Bytes, JTokenType.String, JTokenType.Comment, JTokenType.Raw };
private static readonly JTokenType[] BytesTypes = new[] { JTokenType.Bytes, JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Integer };
/// <summary>
/// Gets a comparer that can compare two tokens for value equality.
@@ -451,6 +455,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, BooleanTypes, false))
throw new ArgumentException("Can not convert {0} to Boolean.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return Convert.ToBoolean((int)(BigInteger)v.Value);
#endif
return Convert.ToBoolean(v.Value, CultureInfo.InvariantCulture);
}
@@ -488,6 +497,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, BooleanTypes, true))
throw new ArgumentException("Can not convert {0} to Boolean.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return Convert.ToBoolean((int)(BigInteger)v.Value);
#endif
return (v.Value != null) ? (bool?)Convert.ToBoolean(v.Value, CultureInfo.InvariantCulture) : null;
}
@@ -502,6 +516,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, NumberTypes, false))
throw new ArgumentException("Can not convert {0} to Int64.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return (long)(BigInteger)v.Value;
#endif
return Convert.ToInt64(v.Value, CultureInfo.InvariantCulture);
}
@@ -519,6 +538,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, DateTimeTypes, true))
throw new ArgumentException("Can not convert {0} to DateTime.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !NET20
if (v.Value is DateTimeOffset)
return ((DateTimeOffset)v.Value).DateTime;
#endif
return (v.Value != null) ? (DateTime?)Convert.ToDateTime(v.Value, CultureInfo.InvariantCulture) : null;
}
@@ -561,6 +585,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, NumberTypes, true))
throw new ArgumentException("Can not convert {0} to Decimal.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return (decimal?)(BigInteger)v.Value;
#endif
return (v.Value != null) ? (decimal?)Convert.ToDecimal(v.Value, CultureInfo.InvariantCulture) : null;
}
@@ -578,6 +607,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, NumberTypes, true))
throw new ArgumentException("Can not convert {0} to Double.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return (double?)(BigInteger)v.Value;
#endif
return (v.Value != null) ? (double?)Convert.ToDouble(v.Value, CultureInfo.InvariantCulture) : null;
}
@@ -595,6 +629,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, CharTypes, true))
throw new ArgumentException("Can not convert {0} to Char.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return (char?)(BigInteger)v.Value;
#endif
return (v.Value != null) ? (char?)Convert.ToChar(v.Value, CultureInfo.InvariantCulture) : null;
}
@@ -609,6 +648,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, NumberTypes, false))
throw new ArgumentException("Can not convert {0} to Int32.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return (int)(BigInteger)v.Value;
#endif
return Convert.ToInt32(v.Value, CultureInfo.InvariantCulture);
}
@@ -623,6 +667,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, NumberTypes, false))
throw new ArgumentException("Can not convert {0} to Int16.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return (short)(BigInteger)v.Value;
#endif
return Convert.ToInt16(v.Value, CultureInfo.InvariantCulture);
}
@@ -638,6 +687,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, NumberTypes, false))
throw new ArgumentException("Can not convert {0} to UInt16.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return (ushort)(BigInteger)v.Value;
#endif
return Convert.ToUInt16(v.Value, CultureInfo.InvariantCulture);
}
@@ -653,6 +707,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, CharTypes, false))
throw new ArgumentException("Can not convert {0} to Char.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return (char)(BigInteger)v.Value;
#endif
return Convert.ToChar(v.Value, CultureInfo.InvariantCulture);
}
@@ -667,6 +726,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, NumberTypes, false))
throw new ArgumentException("Can not convert {0} to Byte.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return (byte)(BigInteger)v.Value;
#endif
return Convert.ToByte(v.Value, CultureInfo.InvariantCulture);
}
@@ -684,6 +748,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, NumberTypes, true))
throw new ArgumentException("Can not convert {0} to Int32.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return (int?)(BigInteger)v.Value;
#endif
return (v.Value != null) ? (int?)Convert.ToInt32(v.Value, CultureInfo.InvariantCulture) : null;
}
@@ -701,6 +770,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, NumberTypes, true))
throw new ArgumentException("Can not convert {0} to Int16.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return (short?)(BigInteger)v.Value;
#endif
return (v.Value != null) ? (short?)Convert.ToInt16(v.Value, CultureInfo.InvariantCulture) : null;
}
@@ -719,6 +793,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, NumberTypes, true))
throw new ArgumentException("Can not convert {0} to UInt16.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return (ushort?)(BigInteger)v.Value;
#endif
return (v.Value != null) ? (ushort?)Convert.ToUInt16(v.Value, CultureInfo.InvariantCulture) : null;
}
@@ -736,6 +815,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, NumberTypes, true))
throw new ArgumentException("Can not convert {0} to Byte.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return (byte?)(BigInteger)v.Value;
#endif
return (v.Value != null) ? (byte?)Convert.ToByte(v.Value, CultureInfo.InvariantCulture) : null;
}
@@ -750,6 +834,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, DateTimeTypes, false))
throw new ArgumentException("Can not convert {0} to DateTime.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !NET20
if (v.Value is DateTimeOffset)
return ((DateTimeOffset) v.Value).DateTime;
#endif
return Convert.ToDateTime(v.Value, CultureInfo.InvariantCulture);
}
@@ -767,6 +856,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, NumberTypes, true))
throw new ArgumentException("Can not convert {0} to Int64.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return (long?)(BigInteger)v.Value;
#endif
return (v.Value != null) ? (long?)Convert.ToInt64(v.Value, CultureInfo.InvariantCulture) : null;
}
@@ -784,6 +878,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, NumberTypes, true))
throw new ArgumentException("Can not convert {0} to Single.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return (float?)(BigInteger)v.Value;
#endif
return (v.Value != null) ? (float?)Convert.ToSingle(v.Value, CultureInfo.InvariantCulture) : null;
}
@@ -798,6 +897,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, NumberTypes, false))
throw new ArgumentException("Can not convert {0} to Decimal.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return (decimal)(BigInteger)v.Value;
#endif
return Convert.ToDecimal(v.Value, CultureInfo.InvariantCulture);
}
@@ -816,6 +920,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, NumberTypes, true))
throw new ArgumentException("Can not convert {0} to UInt32.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return (uint?)(BigInteger)v.Value;
#endif
return (v.Value != null) ? (uint?)Convert.ToUInt32(v.Value, CultureInfo.InvariantCulture) : null;
}
@@ -834,6 +943,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, NumberTypes, true))
throw new ArgumentException("Can not convert {0} to UInt64.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return (ulong?)(BigInteger)v.Value;
#endif
return (v.Value != null) ? (ulong?)Convert.ToUInt64(v.Value, CultureInfo.InvariantCulture) : null;
}
@@ -848,6 +962,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, NumberTypes, false))
throw new ArgumentException("Can not convert {0} to Double.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return (double) (BigInteger) v.Value;
#endif
return Convert.ToDouble(v.Value, CultureInfo.InvariantCulture);
}
@@ -862,6 +981,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, NumberTypes, false))
throw new ArgumentException("Can not convert {0} to Single.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return (float)(BigInteger)v.Value;
#endif
return Convert.ToSingle(v.Value, CultureInfo.InvariantCulture);
}
@@ -883,6 +1007,10 @@ namespace Newtonsoft.Json.Linq
return null;
if (v.Value is byte[])
return Convert.ToBase64String((byte[]) v.Value);
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return ((BigInteger)v.Value).ToString(CultureInfo.InvariantCulture);
#endif
return Convert.ToString(v.Value, CultureInfo.InvariantCulture);
}
@@ -899,6 +1027,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, NumberTypes, false))
throw new ArgumentException("Can not convert {0} to UInt32.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return (uint)(BigInteger)v.Value;
#endif
return Convert.ToUInt32(v.Value, CultureInfo.InvariantCulture);
}
@@ -914,6 +1047,11 @@ namespace Newtonsoft.Json.Linq
if (v == null || !ValidateToken(v, NumberTypes, false))
throw new ArgumentException("Can not convert {0} to UInt64.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return (ulong)(BigInteger)v.Value;
#endif
return Convert.ToUInt64(v.Value, CultureInfo.InvariantCulture);
}
@@ -933,7 +1071,15 @@ namespace Newtonsoft.Json.Linq
if (v.Value is string)
return Convert.FromBase64String(Convert.ToString(v.Value, CultureInfo.InvariantCulture));
return (byte[])v.Value;
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (v.Value is BigInteger)
return ((BigInteger)v.Value).ToByteArray();
#endif
if (v.Value is byte[])
return (byte[])v.Value;
throw new ArgumentException("Can not convert {0} to byte array.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
}
/// <summary>
@@ -1023,6 +1169,39 @@ namespace Newtonsoft.Json.Linq
return (v.Value is Uri) ? (Uri)v.Value : new Uri(Convert.ToString(v.Value, CultureInfo.InvariantCulture));
}
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
/// <summary>
/// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Numerics.BigInteger"/>.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator BigInteger(JToken value)
{
JValue v = EnsureValue(value);
if (v == null || !ValidateToken(v, BigIntegerTypes, false))
throw new ArgumentException("Can not convert {0} to BigInteger.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
return ConvertUtils.ToBigInteger(v.Value);
}
/// <summary>
/// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{BigInteger}"/>.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator BigInteger?(JToken value)
{
JValue v = EnsureValue(value);
if (v == null || !ValidateToken(v, BigIntegerTypes, true))
throw new ArgumentException("Can not convert {0} to BigInteger.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
if (v.Value == null)
return null;
return ConvertUtils.ToBigInteger(v.Value);
}
#endif
#endregion
#region Cast to operators
@@ -1236,6 +1415,18 @@ namespace Newtonsoft.Json.Linq
return new JValue(value);
}
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
/// <summary>
/// Performs an implicit conversion from <see cref="Nullable{BigInteger}"/> to <see cref="Newtonsoft.Json.Linq.JToken"/>.
/// </summary>
/// <param name="value">The value to create a <see cref="JValue"/> from.</param>
/// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
public static implicit operator JToken(BigInteger? value)
{
return new JValue(value);
}
#endif
/// <summary>
/// Performs an implicit conversion from <see cref="Double"/> to <see cref="JToken"/>.
/// </summary>
@@ -1298,6 +1489,18 @@ namespace Newtonsoft.Json.Linq
return new JValue(value);
}
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
/// <summary>
/// Performs an implicit conversion from <see cref="T:System.Numerics.BigInteger"/> to <see cref="Newtonsoft.Json.Linq.JToken"/>.
/// </summary>
/// <param name="value">The value to create a <see cref="JValue"/> from.</param>
/// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
public static implicit operator JToken(BigInteger value)
{
return new JValue(value);
}
#endif
/// <summary>
/// Performs an implicit conversion from <see cref="T:System.Uri"/> to <see cref="Newtonsoft.Json.Linq.JToken"/>.
/// </summary>
@@ -1544,6 +1747,15 @@ namespace Newtonsoft.Json.Linq
return (TimeSpan) this;
}
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (objectType == typeof(BigInteger))
{
if (isNullable)
return (BigInteger?)this;
return (BigInteger)this;
}
#endif
return ToObject(objectType, new JsonSerializer());
}