-Fixed unhelpful error when failing conversion in JsonReader.ReadAsDecimal

-Fixed JsonReader.ReadAsInt32 with BigInteger values
This commit is contained in:
James Newton-King
2018-02-17 18:46:27 +13:00
parent ac17463d05
commit 91af95d77e
3 changed files with 67 additions and 6 deletions
@@ -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<JsonReaderException>(
() => 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] [Test]
public async Task ReadMissingInt64() public async Task ReadMissingInt64()
{ {
@@ -72,6 +72,28 @@ namespace Newtonsoft.Json.Tests.JsonTextReaderTests
); );
} }
[Test]
public void ReadAsDecimal_IntegerTooLarge_ThrowsJsonReaderException()
{
JValue token = new JValue(double.MaxValue);
ExceptionAssert.Throws<JsonReaderException>(
() => 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] [Test]
public void ReadMissingInt64() public void ReadMissingInt64()
{ {
+17
View File
@@ -414,6 +414,14 @@ namespace Newtonsoft.Json
return i; return i;
} }
#if HAVE_BIG_INTEGER
if (v is BigInteger value)
{
i = (int)value;
}
else
#endif
{
try try
{ {
i = Convert.ToInt32(v, CultureInfo.InvariantCulture); i = Convert.ToInt32(v, CultureInfo.InvariantCulture);
@@ -423,6 +431,7 @@ namespace Newtonsoft.Json
// handle error for large integer overflow exceptions // handle error for large integer overflow exceptions
throw JsonReaderException.Create(this, "Could not convert to integer: {0}.".FormatWith(CultureInfo.InvariantCulture, v), ex); throw JsonReaderException.Create(this, "Could not convert to integer: {0}.".FormatWith(CultureInfo.InvariantCulture, v), ex);
} }
}
SetToken(JsonToken.Integer, i, false); SetToken(JsonToken.Integer, i, false);
return i; return i;
@@ -755,9 +764,17 @@ namespace Newtonsoft.Json
} }
else else
#endif #endif
{
try
{ {
d = Convert.ToDecimal(v, CultureInfo.InvariantCulture); 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); SetToken(JsonToken.Float, d, false);
return d; return d;