-Improved number parsing error message

This commit is contained in:
James Newton-King
2013-12-26 22:04:56 +13:00
parent 40c24307de
commit cc730cc860
6 changed files with 136 additions and 42 deletions
+33 -15
View File
@@ -1195,7 +1195,14 @@ namespace Newtonsoft.Json
}
else
{
numberValue = ConvertUtils.Int32Parse(_stringReference.Chars, _stringReference.StartIndex, _stringReference.Length);
int value;
ParseResult parseResult = ConvertUtils.Int32TryParse(_stringReference.Chars, _stringReference.StartIndex, _stringReference.Length, out value);
if (parseResult == ParseResult.Success)
numberValue = value;
else if (parseResult == ParseResult.Overflow)
throw JsonReaderException.Create(this, "JSON integer {0} is too large or small for an Int32.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString()));
else
throw JsonReaderException.Create(this, "Input string '{0}' is not a valid integer.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString()));
}
numberType = JsonToken.Integer;
@@ -1222,7 +1229,11 @@ namespace Newtonsoft.Json
{
string number = _stringReference.ToString();
numberValue = decimal.Parse(number, NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture);
decimal value;
if (decimal.TryParse(number, NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out value))
numberValue = value;
else
throw JsonReaderException.Create(this, "Input string '{0}' is not a valid decimal.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString()));
}
numberType = JsonToken.Float;
@@ -1253,17 +1264,6 @@ namespace Newtonsoft.Json
numberValue = value;
numberType = JsonToken.Integer;
}
else if (parseResult == ParseResult.Invalid)
{
string number = _stringReference.ToString();
if (_floatParseHandling == FloatParseHandling.Decimal)
numberValue = decimal.Parse(number, NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture);
else
numberValue = Convert.ToDouble(number, CultureInfo.InvariantCulture);
numberType = JsonToken.Float;
}
else if (parseResult == ParseResult.Overflow)
{
#if !(NET20 || NET35 || PORTABLE40 || PORTABLE)
@@ -1271,13 +1271,31 @@ namespace Newtonsoft.Json
numberValue = BigInteger.Parse(number, CultureInfo.InvariantCulture);
numberType = JsonToken.Integer;
#else
// todo - validate number was a valid integer to make sure overflow was the reason for failure
throw JsonReaderException.Create(this, "JSON integer {0} is too large or small for an Int64.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString()));
#endif
}
else
{
throw JsonReaderException.Create(this, "Unknown error parsing integer.");
string number = _stringReference.ToString();
if (_floatParseHandling == FloatParseHandling.Decimal)
{
decimal d;
if (decimal.TryParse(number, NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out d))
numberValue = d;
else
throw JsonReaderException.Create(this, "Input string '{0}' is not a valid decimal.".FormatWith(CultureInfo.InvariantCulture, number));
}
else
{
double d;
if (double.TryParse(number, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo, out d))
numberValue = d;
else
throw JsonReaderException.Create(this, "Input string '{0}' is not a valid number.".FormatWith(CultureInfo.InvariantCulture, number));
}
numberType = JsonToken.Float;
}
}
}
@@ -91,5 +91,5 @@ using System.Security;
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.5.0.0")]
[assembly: AssemblyFileVersion("5.0.8.16823")]
[assembly: AssemblyFileVersion("5.0.8.16826")]
[assembly: CLSCompliant(true)]
+15 -14
View File
@@ -565,10 +565,12 @@ namespace Newtonsoft.Json.Utilities
}
}
public static int Int32Parse(char[] chars, int start, int length)
public static ParseResult Int32TryParse(char[] chars, int start, int length, out int value)
{
value = 0;
if (length == 0)
throw new FormatException("Input string was not in a correct format.");
return ParseResult.Invalid;
bool isNegative = (chars[start] == '-');
@@ -576,13 +578,12 @@ namespace Newtonsoft.Json.Utilities
{
// text just a negative sign
if (length == 1)
throw new FormatException("Input string was not in a correct format.");
return ParseResult.Invalid;
start++;
length--;
}
int result = 0;
int end = start + length;
for (int i = start; i < end; i++)
@@ -590,12 +591,12 @@ namespace Newtonsoft.Json.Utilities
int c = chars[i] - '0';
if (c < 0 || c > 9)
throw new FormatException("Input string was not in a correct format.");
return ParseResult.Invalid;
int newValue = (10 * result) - c;
int newValue = (10 * value) - c;
// overflow has caused the number to loop around
if (newValue > result)
if (newValue > value)
{
i++;
@@ -606,13 +607,13 @@ namespace Newtonsoft.Json.Utilities
c = chars[i] - '0';
if (c < 0 || c > 9)
throw new FormatException("Input string was not in a correct format.");
return ParseResult.Invalid;
}
throw new OverflowException();
return ParseResult.Overflow;
}
result = newValue;
value = newValue;
}
// go from negative to positive to avoids overflow
@@ -620,13 +621,13 @@ namespace Newtonsoft.Json.Utilities
if (!isNegative)
{
// negative integer can be one bigger than positive
if (result == int.MinValue)
throw new OverflowException();
if (value == int.MinValue)
return ParseResult.Overflow;
result = -result;
value = -value;
}
return result;
return ParseResult.Success;
}
public static ParseResult Int64TryParse(char[] chars, int start, int length, out long value)