Skip to content

Commit

Permalink
-Fixed converting BigInteger to IConvertable type when deserializing
Browse files Browse the repository at this point in the history
-Updated build and NuGet files for upgraded builds
  • Loading branch information
JamesNK committed Mar 24, 2013
1 parent d9d96b3 commit e5ee604
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Build/Newtonsoft.Json.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>Newtonsoft.Json</id>
<version>4.5.11</version>
<version>5.0.1</version>
<title>Json.NET</title>
<description>Json.NET is a popular high-performance JSON framework for .NET</description>
<authors>James Newton-King</authors>
Expand Down
8 changes: 4 additions & 4 deletions Build/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
38 changes: 21 additions & 17 deletions Doc/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -26,34 +26,39 @@ 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:

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:
Expand All @@ -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
Expand Down
20 changes: 17 additions & 3 deletions Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7500,13 +7500,27 @@ public void JsonSerializerStringEscapeHandling()
}", 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);
Expand Down
18 changes: 11 additions & 7 deletions Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#endif
using System.Diagnostics;
using System.Globalization;
using System.Numerics;
using System.Reflection;
using System.Runtime.Serialization;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -657,14 +658,17 @@ private object EnsureType(JsonReader reader, object value, CultureInfo culture,
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);
}

if (value is BigInteger)
return ConvertUtils.FromBigInteger((BigInteger)value, targetType);

return Convert.ChangeType(value, contract.NonNullableUnderlyingType, culture);
return Convert.ChangeType(value, contract.NonNullableUnderlyingType, culture);
}

return ConvertUtils.ConvertOrCast(value, culture, contract.NonNullableUnderlyingType);
Expand Down
33 changes: 18 additions & 15 deletions Src/Newtonsoft.Json/Utilities/ConvertUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,22 @@ internal static BigInteger ToBigInteger(object value)

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
Expand Down Expand Up @@ -377,23 +393,9 @@ public static object Convert(object initialValue, CultureInfo culture, Type targ

#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)
Expand Down Expand Up @@ -437,6 +439,7 @@ public static object Convert(object initialValue, CultureInfo culture, Type targ

throw new InvalidOperationException("Can not convert from {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, initialType, targetType));
}

#endregion

#region TryConvert
Expand Down

0 comments on commit e5ee604

Please sign in to comment.