-Fixed unhelpful error when failing conversion in JsonReader.ReadAsInt32

This commit is contained in:
James Newton-King
2018-02-17 18:04:00 +13:00
parent ce7f283b94
commit 73203653ba
4 changed files with 93 additions and 42 deletions
@@ -56,6 +56,16 @@ namespace Newtonsoft.Json.Tests.JsonTextReaderTests
#endif #endif
public class ReadAsyncTests : TestFixtureBase public class ReadAsyncTests : TestFixtureBase
{ {
[Test]
public async Task ReadAsInt32Async_IntegerTooLarge_ThrowsJsonReaderException()
{
JValue token = new JValue(long.MaxValue);
await ExceptionAssert.ThrowsAsync<JsonReaderException>(
() => token.CreateReader().ReadAsInt32Async()
);
}
[Test] [Test]
public async Task ReadMissingInt64() public async Task ReadMissingInt64()
{ {
@@ -61,6 +61,16 @@ namespace Newtonsoft.Json.Tests.JsonTextReaderTests
#endif #endif
public class ReadTests : TestFixtureBase public class ReadTests : TestFixtureBase
{ {
[Test]
public void ReadAsInt32_IntegerTooLarge_ThrowsJsonReaderException()
{
JValue token = new JValue(long.MaxValue);
ExceptionAssert.Throws<JsonReaderException>(
() => token.CreateReader().ReadAsInt32()
);
}
[Test] [Test]
public void ReadMissingInt64() public void ReadMissingInt64()
{ {
@@ -971,6 +971,11 @@ namespace Newtonsoft.Json.Tests.Serialization
{ {
JsonSerializer serializer = new JsonSerializer(); JsonSerializer serializer = new JsonSerializer();
#pragma warning disable CS0618 // Type or member is obsolete
Assert.IsNotNull(serializer.Binder);
#pragma warning restore CS0618 // Type or member is obsolete
Assert.IsNotNull(serializer.SerializationBinder);
DefaultSerializationBinder customBinder = new DefaultSerializationBinder(); DefaultSerializationBinder customBinder = new DefaultSerializationBinder();
#pragma warning disable CS0618 // Type or member is obsolete #pragma warning disable CS0618 // Type or member is obsolete
serializer.Binder = customBinder; serializer.Binder = customBinder;
@@ -1090,6 +1095,11 @@ namespace Newtonsoft.Json.Tests.Serialization
{ {
JsonSerializerSettings settings = new JsonSerializerSettings(); JsonSerializerSettings settings = new JsonSerializerSettings();
#pragma warning disable CS0618 // Type or member is obsolete
Assert.IsNull(settings.Binder);
#pragma warning restore CS0618 // Type or member is obsolete
Assert.IsNull(settings.SerializationBinder);
DefaultSerializationBinder customBinder = new DefaultSerializationBinder(); DefaultSerializationBinder customBinder = new DefaultSerializationBinder();
#pragma warning disable CS0618 // Type or member is obsolete #pragma warning disable CS0618 // Type or member is obsolete
settings.Binder = customBinder; settings.Binder = customBinder;
@@ -1205,6 +1215,11 @@ namespace Newtonsoft.Json.Tests.Serialization
{ {
JsonSerializerProxy serializerProxy = new JsonSerializerProxy(new JsonSerializerInternalReader(new JsonSerializer())); JsonSerializerProxy serializerProxy = new JsonSerializerProxy(new JsonSerializerInternalReader(new JsonSerializer()));
#pragma warning disable CS0618 // Type or member is obsolete
Assert.IsNotNull(serializerProxy.Binder);
#pragma warning restore CS0618 // Type or member is obsolete
Assert.IsNotNull(serializerProxy.SerializationBinder);
DefaultSerializationBinder customBinder = new DefaultSerializationBinder(); DefaultSerializationBinder customBinder = new DefaultSerializationBinder();
#pragma warning disable CS0618 // Type or member is obsolete #pragma warning disable CS0618 // Type or member is obsolete
serializerProxy.Binder = customBinder; serializerProxy.Binder = customBinder;
+58 -42
View File
@@ -408,12 +408,24 @@ namespace Newtonsoft.Json
return null; return null;
case JsonToken.Integer: case JsonToken.Integer:
case JsonToken.Float: case JsonToken.Float:
if (!(Value is int)) object v = Value;
if (v is int i)
{ {
SetToken(JsonToken.Integer, Convert.ToInt32(Value, CultureInfo.InvariantCulture), false); return i;
} }
return (int)Value; 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);
return i;
case JsonToken.String: case JsonToken.String:
string s = (string)Value; string s = (string)Value;
return ReadInt32String(s); return ReadInt32String(s);
@@ -462,17 +474,18 @@ namespace Newtonsoft.Json
if (JsonTokenUtils.IsPrimitiveToken(t)) if (JsonTokenUtils.IsPrimitiveToken(t))
{ {
if (Value != null) object v = Value;
if (v != null)
{ {
string s; string s;
if (Value is IFormattable formattable) if (v is IFormattable formattable)
{ {
s = formattable.ToString(null, Culture); s = formattable.ToString(null, Culture);
} }
else else
{ {
Uri uri = Value as Uri; Uri uri = v as Uri;
s = uri != null ? uri.OriginalString : Value.ToString(); s = uri != null ? uri.OriginalString : v.ToString();
} }
SetToken(JsonToken.String, s, false); SetToken(JsonToken.String, s, false);
@@ -520,9 +533,9 @@ namespace Newtonsoft.Json
{ {
data = CollectionUtils.ArrayEmpty<byte>(); data = CollectionUtils.ArrayEmpty<byte>();
} }
else if (ConvertUtils.TryConvertGuid(s, out Guid g)) else if (ConvertUtils.TryConvertGuid(s, out Guid g1))
{ {
data = g.ToByteArray(); data = g1.ToByteArray();
} }
else else
{ {
@@ -537,9 +550,9 @@ namespace Newtonsoft.Json
case JsonToken.EndArray: case JsonToken.EndArray:
return null; return null;
case JsonToken.Bytes: case JsonToken.Bytes:
if (ValueType == typeof(Guid)) if (Value is Guid g2)
{ {
byte[] data = ((Guid)Value).ToByteArray(); byte[] data = g2.ToByteArray();
SetToken(JsonToken.Bytes, data, false); SetToken(JsonToken.Bytes, data, false);
return data; return data;
} }
@@ -606,24 +619,26 @@ namespace Newtonsoft.Json
return null; return null;
case JsonToken.Integer: case JsonToken.Integer:
case JsonToken.Float: case JsonToken.Float:
if (!(Value is double)) object v = Value;
if (v is double d)
{ {
double d; return d;
#if HAVE_BIG_INTEGER
if (Value is BigInteger value)
{
d = (double)value;
}
else
#endif
{
d = Convert.ToDouble(Value, CultureInfo.InvariantCulture);
}
SetToken(JsonToken.Float, d, false);
} }
return (double)Value; #if HAVE_BIG_INTEGER
if (v is BigInteger value)
{
d = (double)value;
}
else
#endif
{
d = Convert.ToDouble(v, CultureInfo.InvariantCulture);
}
SetToken(JsonToken.Float, d, false);
return (double)d;
case JsonToken.String: case JsonToken.String:
return ReadDoubleString((string)Value); return ReadDoubleString((string)Value);
} }
@@ -680,7 +695,6 @@ namespace Newtonsoft.Json
} }
SetToken(JsonToken.Boolean, b, false); SetToken(JsonToken.Boolean, b, false);
return b; return b;
case JsonToken.String: case JsonToken.String:
return ReadBooleanString((string)Value); return ReadBooleanString((string)Value);
@@ -727,24 +741,26 @@ namespace Newtonsoft.Json
return null; return null;
case JsonToken.Integer: case JsonToken.Integer:
case JsonToken.Float: case JsonToken.Float:
if (!(Value is decimal)) object v = Value;
{
decimal d;
#if HAVE_BIG_INTEGER
if (Value is BigInteger value)
{
d = (decimal)value;
}
else
#endif
{
d = Convert.ToDecimal(Value, CultureInfo.InvariantCulture);
}
SetToken(JsonToken.Float, d, false); if (v is decimal d)
{
return d;
} }
return (decimal)Value; #if HAVE_BIG_INTEGER
if (v is BigInteger value)
{
d = (decimal)value;
}
else
#endif
{
d = Convert.ToDecimal(v, CultureInfo.InvariantCulture);
}
SetToken(JsonToken.Float, d, false);
return d;
case JsonToken.String: case JsonToken.String:
return ReadDecimalString((string)Value); return ReadDecimalString((string)Value);
} }