diff --git a/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ReadAsyncTests.cs b/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ReadAsyncTests.cs index 585064e1..c9525f74 100644 --- a/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ReadAsyncTests.cs +++ b/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ReadAsyncTests.cs @@ -67,6 +67,28 @@ namespace Newtonsoft.Json.Tests.JsonTextReaderTests ); } + [Test] + public async Task ReadAsDecimalAsync_IntegerTooLarge_ThrowsJsonReaderException() + { + JValue token = new JValue(double.MaxValue); + + await ExceptionAssert.ThrowsAsync( + () => token.CreateReader().ReadAsDecimalAsync(), + "Could not convert to decimal: 1.79769313486232E+308. Path ''." + ); + } + +#if !(NET20 || NET35 || PORTABLE40 || PORTABLE) || NETSTANDARD1_3 || NETSTANDARD2_0 + [Test] + public async Task ReadAsInt32Async_BigIntegerValue_Success() + { + JValue token = new JValue(BigInteger.Parse("1")); + + int? i = await token.CreateReader().ReadAsInt32Async(); + Assert.AreEqual(1, i); + } +#endif + [Test] public async Task ReadMissingInt64() { diff --git a/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ReadTests.cs b/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ReadTests.cs index 54395da6..f4a19237 100644 --- a/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ReadTests.cs +++ b/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ReadTests.cs @@ -72,6 +72,28 @@ namespace Newtonsoft.Json.Tests.JsonTextReaderTests ); } + [Test] + public void ReadAsDecimal_IntegerTooLarge_ThrowsJsonReaderException() + { + JValue token = new JValue(double.MaxValue); + + ExceptionAssert.Throws( + () => token.CreateReader().ReadAsDecimal(), + "Could not convert to decimal: 1.79769313486232E+308. Path ''." + ); + } + +#if !(NET20 || NET35 || PORTABLE40 || PORTABLE) || NETSTANDARD1_3 || NETSTANDARD2_0 + [Test] + public void ReadAsInt32_BigIntegerValue_Success() + { + JValue token = new JValue(BigInteger.Parse("1")); + + int? i = token.CreateReader().ReadAsInt32(); + Assert.AreEqual(1, i); + } +#endif + [Test] public void ReadMissingInt64() { diff --git a/Src/Newtonsoft.Json/JsonReader.cs b/Src/Newtonsoft.Json/JsonReader.cs index 2e595138..8b2adae1 100644 --- a/Src/Newtonsoft.Json/JsonReader.cs +++ b/Src/Newtonsoft.Json/JsonReader.cs @@ -414,14 +414,23 @@ namespace Newtonsoft.Json return i; } - try +#if HAVE_BIG_INTEGER + if (v is BigInteger value) { - i = Convert.ToInt32(v, CultureInfo.InvariantCulture); + i = (int)value; } - catch (Exception ex) + else +#endif { - // handle error for large integer overflow exceptions - throw JsonReaderException.Create(this, "Could not convert to integer: {0}.".FormatWith(CultureInfo.InvariantCulture, v), ex); + try + { + i = Convert.ToInt32(v, CultureInfo.InvariantCulture); + } + catch (Exception ex) + { + // handle error for large integer overflow exceptions + throw JsonReaderException.Create(this, "Could not convert to integer: {0}.".FormatWith(CultureInfo.InvariantCulture, v), ex); + } } SetToken(JsonToken.Integer, i, false); @@ -756,7 +765,15 @@ namespace Newtonsoft.Json else #endif { - d = Convert.ToDecimal(v, CultureInfo.InvariantCulture); + try + { + d = Convert.ToDecimal(v, CultureInfo.InvariantCulture); + } + catch (Exception ex) + { + // handle error for large integer overflow exceptions + throw JsonReaderException.Create(this, "Could not convert to decimal: {0}.".FormatWith(CultureInfo.InvariantCulture, v), ex); + } } SetToken(JsonToken.Float, d, false);