-Fixed converting BigInteger to IConvertable type when deserializing

-Updated build and NuGet files for upgraded builds
This commit is contained in:
James Newton-King
2013-03-24 21:37:29 +13:00
parent d9d96b3e5c
commit e5ee60497e
6 changed files with 72 additions and 47 deletions
@@ -7500,13 +7500,27 @@ Parameter name: value",
}", json);
}
[Test]
public void ReadTooLargeInteger()
{
string json = @"[999999999999999999999999999999999999999999999999]";
IList<BigInteger> l = JsonConvert.DeserializeObject<IList<BigInteger>>(json);
Assert.AreEqual(BigInteger.Parse("999999999999999999999999999999999999999999999999"), l[0]);
ExceptionAssert.Throws<JsonSerializationException>(
"Error converting value 999999999999999999999999999999999999999999999999 to type 'System.Int64'. Path '[0]', line 1, position 49.",
() => JsonConvert.DeserializeObject<IList<long>>(json));
}
[Test]
public void ReadStringFloatingPointSymbols()
{
string json = @"[
'NaN',
'Infinity',
'-Infinity'
""NaN"",
""Infinity"",
""-Infinity""
]";
IList<float> floats = JsonConvert.DeserializeObject<IList<float>>(json);
@@ -33,6 +33,7 @@ using System.Dynamic;
#endif
using System.Diagnostics;
using System.Globalization;
using System.Numerics;
using System.Reflection;
using System.Runtime.Serialization;
using Newtonsoft.Json.Linq;
@@ -657,14 +658,17 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
if (contract.IsConvertable)
{
if (contract.NonNullableUnderlyingType.IsEnum())
{
if (value is string)
return Enum.Parse(contract.NonNullableUnderlyingType, value.ToString(), true);
else if (ConvertUtils.IsInteger(value))
return Enum.ToObject(contract.NonNullableUnderlyingType, value);
}
{
if (value is string)
return Enum.Parse(contract.NonNullableUnderlyingType, value.ToString(), true);
else if (ConvertUtils.IsInteger(value))
return Enum.ToObject(contract.NonNullableUnderlyingType, value);
}
return Convert.ChangeType(value, contract.NonNullableUnderlyingType, culture);
if (value is BigInteger)
return ConvertUtils.FromBigInteger((BigInteger)value, targetType);
return Convert.ChangeType(value, contract.NonNullableUnderlyingType, culture);
}
return ConvertUtils.ConvertOrCast(value, culture, contract.NonNullableUnderlyingType);
+18 -15
View File
@@ -317,6 +317,22 @@ namespace Newtonsoft.Json.Utilities
throw new InvalidCastException("Cannot convert {0} to BigInteger.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
}
public static object FromBigInteger(BigInteger i, Type targetType)
{
if (targetType == typeof (decimal))
return (decimal) i;
if (targetType == typeof (double))
return (double) i;
if (targetType == typeof (float))
return (float) i;
if (targetType == typeof (ulong))
return (ulong) i;
if (IsConvertible(targetType))
return System.Convert.ChangeType((long) i, targetType, CultureInfo.InvariantCulture);
throw new InvalidOperationException("Can not convert from BigInteger to {0}.".FormatWith(CultureInfo.InvariantCulture, targetType));
}
#endif
#region Convert
@@ -377,23 +393,9 @@ namespace Newtonsoft.Json.Utilities
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (targetType == typeof(BigInteger))
{
return ToBigInteger(initialValue);
}
if (initialValue is BigInteger)
{
BigInteger i = (BigInteger) initialValue;
if (targetType == typeof(decimal))
return (decimal) i;
if (targetType == typeof(double))
return (double) i;
if (targetType == typeof(float))
return (float) i;
if (targetType == typeof(ulong))
return (ulong) i;
if (IsConvertible(targetType))
return System.Convert.ChangeType((long) i, targetType, CultureInfo.InvariantCulture);
}
return FromBigInteger((BigInteger)initialValue, targetType);
#endif
#if !(NETFX_CORE || PORTABLE)
@@ -437,6 +439,7 @@ namespace Newtonsoft.Json.Utilities
throw new InvalidOperationException("Can not convert from {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, initialType, targetType));
}
#endregion
#region TryConvert