-Fixed not preventing negative integers with StringEnumConverter.AllowIntegerValues

This commit is contained in:
James Newton-King
2017-06-11 15:26:25 +12:00
parent 64a092ee64
commit 4467bdb224
2 changed files with 23 additions and 10 deletions
@@ -586,6 +586,10 @@ namespace Newtonsoft.Json.Tests.Converters
e = JsonConvert.DeserializeObject<NumberNamesEnum>("\"3\"", converter);
Assert.AreEqual(NumberNamesEnum.third, e);
e = JsonConvert.DeserializeObject<NumberNamesEnum>("\"-4\"", converter);
Assert.AreEqual(NumberNamesEnum.fourth, e);
}
[Test]
@@ -604,6 +608,10 @@ namespace Newtonsoft.Json.Tests.Converters
e = JsonConvert.DeserializeObject<NumberNamesEnum>("\"3\"", converter);
Assert.AreEqual(NumberNamesEnum.third, e);
e = JsonConvert.DeserializeObject<NumberNamesEnum>("\"-4\"", converter);
Assert.AreEqual(NumberNamesEnum.fourth, e);
}
#endif
@@ -618,6 +626,17 @@ namespace Newtonsoft.Json.Tests.Converters
Assert.AreEqual("Integer string '1' is not allowed.", ex.InnerException.Message);
}
[Test]
public void AllowIntegerValueAndNegativeStringNumber()
{
JsonSerializationException ex = ExceptionAssert.Throws<JsonSerializationException>(() =>
{
JsonConvert.DeserializeObject<StoreColor>("\"-1\"", new StringEnumConverter { AllowIntegerValues = false });
});
Assert.AreEqual("Integer string '-1' is not allowed.", ex.InnerException.Message);
}
[Test]
public void AllowIntegerValueAndNonNamedValue()
{
@@ -714,7 +733,9 @@ namespace Newtonsoft.Json.Tests.Converters
[EnumMember(Value = "1")]
second,
[EnumMember(Value = "3")]
third
third,
[EnumMember(Value = "-4")]
fourth
}
[DataContract]
+1 -9
View File
@@ -221,15 +221,7 @@ namespace Newtonsoft.Json.Utilities
if (disallowValue)
{
bool isNumber = true;
for (int i = 0; i < finalEnumText.Length; i++)
{
if (!char.IsNumber(finalEnumText[i]))
{
isNumber = false;
break;
}
}
bool isNumber = int.TryParse(finalEnumText, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out int _);
if (isNumber)
{