-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
+1 -1
View File
@@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"> <package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata> <metadata>
<id>Newtonsoft.Json</id> <id>Newtonsoft.Json</id>
<version>4.5.11</version> <version>5.0.1</version>
<title>Json.NET</title> <title>Json.NET</title>
<description>Json.NET is a popular high-performance JSON framework for .NET</description> <description>Json.NET is a popular high-performance JSON framework for .NET</description>
<authors>James Newton-King</authors> <authors>James Newton-King</authors>
+4 -4
View File
@@ -18,10 +18,10 @@
$workingDir = "$baseDir\Working" $workingDir = "$baseDir\Working"
$builds = @( $builds = @(
@{Name = "Newtonsoft.Json"; TestsName = "Newtonsoft.Json.Tests"; Constants=""; FinalDir="Net45"; NuGetDir = "net45"; Framework="net-4.0"; Sign=$true}, @{Name = "Newtonsoft.Json"; TestsName = "Newtonsoft.Json.Tests"; Constants=""; FinalDir="Net45"; NuGetDir = "net45"; Framework="net-4.0"; Sign=$true},
@{Name = "Newtonsoft.Json.Portable"; TestsName = "Newtonsoft.Json.Tests.Portable"; Constants="PORTABLE"; FinalDir="Portable"; NuGetDir = "portable-net40+sl4+wp7+win8"; Framework="net-4.0"; Sign=$true}, @{Name = "Newtonsoft.Json.Portable"; TestsName = "Newtonsoft.Json.Tests.Portable"; Constants="PORTABLE"; FinalDir="Portable"; NuGetDir = "portable-net45+sl5+windowsphone8+win8"; Framework="net-4.0"; Sign=$true},
@{Name = "Newtonsoft.Json.WinRT"; TestsName = $null; Constants="NETFX_CORE"; FinalDir="WinRT"; NuGetDir = "winrt45"; Framework="net-4.5"; Sign=$true}, @{Name = "Newtonsoft.Json.WinRT"; TestsName = $null; Constants="NETFX_CORE"; FinalDir="WinRT"; NuGetDir = "netcore45"; Framework="net-4.5"; Sign=$true},
@{Name = "Newtonsoft.Json.WindowsPhone"; TestsName = $null; Constants="SILVERLIGHT;WINDOWS_PHONE"; FinalDir="WindowsPhone"; NuGetDir = "sl3-wp,sl4-windowsphone71"; Framework="net-4.0"; Sign=$true}, @{Name = "Newtonsoft.Json.WindowsPhone"; TestsName = $null; Constants="SILVERLIGHT;WINDOWS_PHONE"; FinalDir="WindowsPhone"; NuGetDir = "windowsphone8"; Framework="net-4.0"; Sign=$true},
@{Name = "Newtonsoft.Json.Silverlight"; TestsName = "Newtonsoft.Json.Tests.Silverlight"; Constants="SILVERLIGHT"; FinalDir="Silverlight"; NuGetDir = "sl4"; Framework="net-4.0"; Sign=$true}, @{Name = "Newtonsoft.Json.Silverlight"; TestsName = "Newtonsoft.Json.Tests.Silverlight"; Constants="SILVERLIGHT"; FinalDir="Silverlight"; NuGetDir = "sl5"; Framework="net-4.0"; Sign=$true},
@{Name = "Newtonsoft.Json.Net40"; TestsName = "Newtonsoft.Json.Tests.Net40"; Constants="NET40"; FinalDir="Net40"; NuGetDir = "net40"; Framework="net-4.0"; Sign=$true}, @{Name = "Newtonsoft.Json.Net40"; TestsName = "Newtonsoft.Json.Tests.Net40"; Constants="NET40"; FinalDir="Net40"; NuGetDir = "net40"; Framework="net-4.0"; Sign=$true},
@{Name = "Newtonsoft.Json.Net35"; TestsName = "Newtonsoft.Json.Tests.Net35"; Constants="NET35"; FinalDir="Net35"; NuGetDir = "net35"; Framework="net-2.0"; Sign=$true}, @{Name = "Newtonsoft.Json.Net35"; TestsName = "Newtonsoft.Json.Tests.Net35"; Constants="NET35"; FinalDir="Net35"; NuGetDir = "net35"; Framework="net-2.0"; Sign=$true},
@{Name = "Newtonsoft.Json.Net20"; TestsName = "Newtonsoft.Json.Tests.Net20"; Constants="NET20"; FinalDir="Net20"; NuGetDir = "net20"; Framework="net-2.0"; Sign=$true} @{Name = "Newtonsoft.Json.Net20"; TestsName = "Newtonsoft.Json.Tests.Net20"; Constants="NET20"; FinalDir="Net20"; NuGetDir = "net20"; Framework="net-2.0"; Sign=$true}
+21 -17
View File
@@ -2,19 +2,19 @@ Json.NET
http://james.newtonking.com/projects/json-net.aspx http://james.newtonking.com/projects/json-net.aspx
http://www.codeplex.com/json/ http://www.codeplex.com/json/
https://github.com/JamesNK/Newtonsoft.Json
Description: Description:
Json.NET makes working with JSON formatted data in .NET simple. Quickly read and write Json.NET is a popular high-performance JSON framework for .NET
JSON using LINQ to JSON or serialize your .NET objects with a single method call using
the JsonSerializer.
-Flexible JSON serializer to convert .NET objects to JSON and back again -Flexible JSON serializer for converting between .NET objects and JSON
-LINQ to JSON for reading and writing JSON -LINQ to JSON for manually reading and writing JSON
-Writes indented, easy to read JSON -High performance, faster than .NET's built-in JSON serializers
-Convert JSON to and from XML -Write indented, easy to read JSON
-Supports Silverlight and Windows Phone -Convert JSON to and from XML
-Supports .NET 2, .NET 3.5, .NET 4, .NET 4.5, Silverlight, Windows Phone and Windows 8 Store
Documentation: Documentation:
@@ -26,26 +26,29 @@ Versions:
Json.NET comes in different versions for the various .NET frameworks. Json.NET comes in different versions for the various .NET frameworks.
-Net45:
.NET latest
-Net40: -Net40:
.NET latest (4.0 & 4.5) .NET 4.0, Mono
-Net35: -Net35:
.NET 3.5 SP1, Mono .NET 3.5 SP1
-Net20: -Net20:
.NET 2.0 .NET 2.0
-Silverlight: -Silverlight:
Silverlight 4 & 5 Silverlight 5
-WindowsPhone: -WindowsPhone:
Windows Phone 7 & 7.1 Windows Phone 8
-Metro -WinRT
Windows 8, WinRT 4.5 Windows 8 Store
-Portable -Portable
Portable Class Library (.NET 4.0, WP7, SL4) Portable Class Library (.NET 4.5, SL5, WP8, Win8 Store)
Notes: Notes:
@@ -53,7 +56,9 @@ Notes:
Microsoft stopped support for the Compact Framework in Visual Studio 2010. Microsoft stopped support for the Compact Framework in Visual Studio 2010.
For a Compact Framework 3.5 build download Json.NET 3.5. For a Compact Framework 3.5 build download Json.NET 3.5.
For a Silverlight 3.0 build download Json.NET 3.5. For a Silverlight 3 build download Json.NET 3.5.
For a Silverlight 4 or Windows Phone 7 build download Json.NET 4.5.
Instructions: Instructions:
@@ -63,7 +68,6 @@ Instructions:
2. Add a reference to Newtonsoft.Json.dll within Visual Studio.NET to your project. 2. Add a reference to Newtonsoft.Json.dll within Visual Studio.NET to your project.
License: License:
Copyright (c) 2007 James Newton-King Copyright (c) 2007 James Newton-King
@@ -7500,13 +7500,27 @@ Parameter name: value",
}", json); }", 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] [Test]
public void ReadStringFloatingPointSymbols() public void ReadStringFloatingPointSymbols()
{ {
string json = @"[ string json = @"[
'NaN', ""NaN"",
'Infinity', ""Infinity"",
'-Infinity' ""-Infinity""
]"; ]";
IList<float> floats = JsonConvert.DeserializeObject<IList<float>>(json); IList<float> floats = JsonConvert.DeserializeObject<IList<float>>(json);
@@ -33,6 +33,7 @@ using System.Dynamic;
#endif #endif
using System.Diagnostics; using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.Numerics;
using System.Reflection; using System.Reflection;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using Newtonsoft.Json.Linq; 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.IsConvertable)
{ {
if (contract.NonNullableUnderlyingType.IsEnum()) if (contract.NonNullableUnderlyingType.IsEnum())
{ {
if (value is string) if (value is string)
return Enum.Parse(contract.NonNullableUnderlyingType, value.ToString(), true); return Enum.Parse(contract.NonNullableUnderlyingType, value.ToString(), true);
else if (ConvertUtils.IsInteger(value)) else if (ConvertUtils.IsInteger(value))
return Enum.ToObject(contract.NonNullableUnderlyingType, 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); 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())); 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 #endif
#region Convert #region Convert
@@ -377,23 +393,9 @@ namespace Newtonsoft.Json.Utilities
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE) #if !(NET20 || NET35 || SILVERLIGHT || PORTABLE)
if (targetType == typeof(BigInteger)) if (targetType == typeof(BigInteger))
{
return ToBigInteger(initialValue); return ToBigInteger(initialValue);
}
if (initialValue is BigInteger) if (initialValue is BigInteger)
{ return FromBigInteger((BigInteger)initialValue, targetType);
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);
}
#endif #endif
#if !(NETFX_CORE || PORTABLE) #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)); throw new InvalidOperationException("Can not convert from {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, initialType, targetType));
} }
#endregion #endregion
#region TryConvert #region TryConvert