-Fixed not throwing JsonReaderException when parsing certain invalid numbers
This commit is contained in:
@@ -47,6 +47,27 @@ namespace Newtonsoft.Json.Tests
|
|||||||
[TestFixture]
|
[TestFixture]
|
||||||
public class JsonTextReaderTest : TestFixtureBase
|
public class JsonTextReaderTest : TestFixtureBase
|
||||||
{
|
{
|
||||||
|
[Test]
|
||||||
|
public void ReadInvalidNonBase10Number()
|
||||||
|
{
|
||||||
|
string json = "0aq2dun13.hod";
|
||||||
|
|
||||||
|
JsonTextReader reader = new JsonTextReader(new StringReader(json));
|
||||||
|
|
||||||
|
ExceptionAssert.Throws<JsonReaderException>("Input string '0a' is not a valid number. Path '', line 1, position 2.",
|
||||||
|
() => { reader.Read(); });
|
||||||
|
|
||||||
|
reader = new JsonTextReader(new StringReader(json));
|
||||||
|
|
||||||
|
ExceptionAssert.Throws<JsonReaderException>("Input string '0a' is not a valid decimal. Path '', line 1, position 2.",
|
||||||
|
() => { reader.ReadAsDecimal(); });
|
||||||
|
|
||||||
|
reader = new JsonTextReader(new StringReader(json));
|
||||||
|
|
||||||
|
ExceptionAssert.Throws<JsonReaderException>("Input string '0a' is not a valid integer. Path '', line 1, position 2.",
|
||||||
|
() => { reader.ReadAsInt32(); });
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void ThrowErrorWhenParsingUnquoteStringThatStartsWithNE()
|
public void ThrowErrorWhenParsingUnquoteStringThatStartsWithNE()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -75,4 +75,4 @@ using System.Security;
|
|||||||
// by using the '*' as shown below:
|
// by using the '*' as shown below:
|
||||||
|
|
||||||
[assembly: AssemblyVersion("6.0.0.0")]
|
[assembly: AssemblyVersion("6.0.0.0")]
|
||||||
[assembly: AssemblyFileVersion("6.0.3.17414")]
|
[assembly: AssemblyFileVersion("6.0.3.17415")]
|
||||||
|
|||||||
@@ -1187,12 +1187,18 @@ namespace Newtonsoft.Json
|
|||||||
{
|
{
|
||||||
string number = _stringReference.ToString();
|
string number = _stringReference.ToString();
|
||||||
|
|
||||||
// decimal.Parse doesn't support parsing hexadecimal values
|
try
|
||||||
int integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
|
{
|
||||||
? Convert.ToInt32(number, 16)
|
int integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
|
||||||
: Convert.ToInt32(number, 8);
|
? Convert.ToInt32(number, 16)
|
||||||
|
: Convert.ToInt32(number, 8);
|
||||||
|
|
||||||
numberValue = integer;
|
numberValue = integer;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw JsonReaderException.Create(this, "Input string '{0}' is not a valid integer.".FormatWith(CultureInfo.InvariantCulture, number), ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -1219,12 +1225,19 @@ namespace Newtonsoft.Json
|
|||||||
{
|
{
|
||||||
string number = _stringReference.ToString();
|
string number = _stringReference.ToString();
|
||||||
|
|
||||||
// decimal.Parse doesn't support parsing hexadecimal values
|
try
|
||||||
long integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
|
{
|
||||||
? Convert.ToInt64(number, 16)
|
// decimal.Parse doesn't support parsing hexadecimal values
|
||||||
: Convert.ToInt64(number, 8);
|
long integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
|
||||||
|
? Convert.ToInt64(number, 16)
|
||||||
|
: Convert.ToInt64(number, 8);
|
||||||
|
|
||||||
numberValue = Convert.ToDecimal(integer);
|
numberValue = Convert.ToDecimal(integer);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw JsonReaderException.Create(this, "Input string '{0}' is not a valid decimal.".FormatWith(CultureInfo.InvariantCulture, number), ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -1251,9 +1264,17 @@ namespace Newtonsoft.Json
|
|||||||
{
|
{
|
||||||
string number = _stringReference.ToString();
|
string number = _stringReference.ToString();
|
||||||
|
|
||||||
numberValue = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
|
try
|
||||||
? Convert.ToInt64(number, 16)
|
{
|
||||||
: Convert.ToInt64(number, 8);
|
numberValue = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
|
||||||
|
? Convert.ToInt64(number, 16)
|
||||||
|
: Convert.ToInt64(number, 8);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw JsonReaderException.Create(this, "Input string '{0}' is not a valid number.".FormatWith(CultureInfo.InvariantCulture, number), ex);
|
||||||
|
}
|
||||||
|
|
||||||
numberType = JsonToken.Integer;
|
numberType = JsonToken.Integer;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -91,5 +91,5 @@ using System.Security;
|
|||||||
// by using the '*' as shown below:
|
// by using the '*' as shown below:
|
||||||
|
|
||||||
[assembly: AssemblyVersion("6.0.0.0")]
|
[assembly: AssemblyVersion("6.0.0.0")]
|
||||||
[assembly: AssemblyFileVersion("6.0.3.17414")]
|
[assembly: AssemblyFileVersion("6.0.3.17415")]
|
||||||
[assembly: CLSCompliant(true)]
|
[assembly: CLSCompliant(true)]
|
||||||
|
|||||||
@@ -2020,7 +2020,8 @@ To fix this error either change the environment to be fully trusted, change the
|
|||||||
if (property.PropertyContract == null)
|
if (property.PropertyContract == null)
|
||||||
property.PropertyContract = GetContractSafe(property.PropertyType);
|
property.PropertyContract = GetContractSafe(property.PropertyType);
|
||||||
|
|
||||||
if (HasFlag(property.DefaultValueHandling.GetValueOrDefault(Serializer._defaultValueHandling), DefaultValueHandling.Populate) && property.Writable && !property.Ignored)
|
if (HasFlag(property.DefaultValueHandling.GetValueOrDefault(Serializer._defaultValueHandling), DefaultValueHandling.Populate) && property.Writable && !property.Ignored
|
||||||
|
)
|
||||||
property.ValueProvider.SetValue(newObject, EnsureType(reader, property.GetResolvedDefaultValue(), CultureInfo.InvariantCulture, property.PropertyContract, property.PropertyType));
|
property.ValueProvider.SetValue(newObject, EnsureType(reader, property.GetResolvedDefaultValue(), CultureInfo.InvariantCulture, property.PropertyContract, property.PropertyType));
|
||||||
break;
|
break;
|
||||||
case PropertyPresence.Null:
|
case PropertyPresence.Null:
|
||||||
|
|||||||
Reference in New Issue
Block a user