-Fixed not preventing negative integers with StringEnumConverter.AllowIntegerValues
This commit is contained in:
@@ -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]
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user