From e5ee60497e90022bf9fa9f190ca0c9e68b25c81c Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Sun, 24 Mar 2013 21:37:29 +1300 Subject: [PATCH] -Fixed converting BigInteger to IConvertable type when deserializing -Updated build and NuGet files for upgraded builds --- Build/Newtonsoft.Json.nuspec | 2 +- Build/build.ps1 | 8 ++-- Doc/readme.txt | 38 ++++++++++--------- .../Serialization/JsonSerializerTest.cs | 20 ++++++++-- .../JsonSerializerInternalReader.cs | 18 +++++---- Src/Newtonsoft.Json/Utilities/ConvertUtils.cs | 33 ++++++++-------- 6 files changed, 72 insertions(+), 47 deletions(-) diff --git a/Build/Newtonsoft.Json.nuspec b/Build/Newtonsoft.Json.nuspec index a09c9114..6d811062 100644 --- a/Build/Newtonsoft.Json.nuspec +++ b/Build/Newtonsoft.Json.nuspec @@ -2,7 +2,7 @@ Newtonsoft.Json - 4.5.11 + 5.0.1 Json.NET Json.NET is a popular high-performance JSON framework for .NET James Newton-King diff --git a/Build/build.ps1 b/Build/build.ps1 index 17ff5080..02e306af 100644 --- a/Build/build.ps1 +++ b/Build/build.ps1 @@ -18,10 +18,10 @@ $workingDir = "$baseDir\Working" $builds = @( @{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.WinRT"; TestsName = $null; Constants="NETFX_CORE"; FinalDir="WinRT"; NuGetDir = "winrt45"; 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.Silverlight"; TestsName = "Newtonsoft.Json.Tests.Silverlight"; Constants="SILVERLIGHT"; FinalDir="Silverlight"; NuGetDir = "sl4"; 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 = "netcore45"; Framework="net-4.5"; 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 = "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.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} diff --git a/Doc/readme.txt b/Doc/readme.txt index efeb677d..c1f7899b 100644 --- a/Doc/readme.txt +++ b/Doc/readme.txt @@ -2,19 +2,19 @@ Json.NET http://james.newtonking.com/projects/json-net.aspx http://www.codeplex.com/json/ +https://github.com/JamesNK/Newtonsoft.Json Description: -Json.NET makes working with JSON formatted data in .NET simple. Quickly read and write -JSON using LINQ to JSON or serialize your .NET objects with a single method call using -the JsonSerializer. +Json.NET is a popular high-performance JSON framework for .NET --Flexible JSON serializer to convert .NET objects to JSON and back again --LINQ to JSON for reading and writing JSON --Writes indented, easy to read JSON --Convert JSON to and from XML --Supports Silverlight and Windows Phone +-Flexible JSON serializer for converting between .NET objects and JSON +-LINQ to JSON for manually reading and writing JSON +-High performance, faster than .NET's built-in JSON serializers +-Write indented, easy to read JSON +-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: @@ -26,26 +26,29 @@ Versions: Json.NET comes in different versions for the various .NET frameworks. +-Net45: + .NET latest + -Net40: - .NET latest (4.0 & 4.5) + .NET 4.0, Mono -Net35: - .NET 3.5 SP1, Mono + .NET 3.5 SP1 -Net20: .NET 2.0 -Silverlight: - Silverlight 4 & 5 + Silverlight 5 -WindowsPhone: - Windows Phone 7 & 7.1 + Windows Phone 8 --Metro - Windows 8, WinRT 4.5 +-WinRT + Windows 8 Store -Portable - Portable Class Library (.NET 4.0, WP7, SL4) + Portable Class Library (.NET 4.5, SL5, WP8, Win8 Store) Notes: @@ -53,7 +56,9 @@ Notes: 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 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: @@ -63,7 +68,6 @@ Instructions: 2. Add a reference to Newtonsoft.Json.dll within Visual Studio.NET to your project. - License: Copyright (c) 2007 James Newton-King diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index 784c0257..240c151e 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -7500,13 +7500,27 @@ Parameter name: value", }", json); } + [Test] + public void ReadTooLargeInteger() + { + string json = @"[999999999999999999999999999999999999999999999999]"; + + IList l = JsonConvert.DeserializeObject>(json); + + Assert.AreEqual(BigInteger.Parse("999999999999999999999999999999999999999999999999"), l[0]); + + ExceptionAssert.Throws( + "Error converting value 999999999999999999999999999999999999999999999999 to type 'System.Int64'. Path '[0]', line 1, position 49.", + () => JsonConvert.DeserializeObject>(json)); + } + [Test] public void ReadStringFloatingPointSymbols() { string json = @"[ - 'NaN', - 'Infinity', - '-Infinity' + ""NaN"", + ""Infinity"", + ""-Infinity"" ]"; IList floats = JsonConvert.DeserializeObject>(json); diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index 687f3418..a74daa1f 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -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); diff --git a/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs b/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs index b8c58a93..5e721104 100644 --- a/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs @@ -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