-Fixed StringEnumConverter.AllowIntegerValues to reject integer strings
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#region License
|
||||
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
@@ -21,6 +22,7 @@
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
@@ -134,7 +136,8 @@ namespace Newtonsoft.Json.Converters
|
||||
if (reader.TokenType == JsonToken.String)
|
||||
{
|
||||
string enumText = reader.Value.ToString();
|
||||
return EnumUtils.ParseEnumName(enumText, isNullable, t);
|
||||
|
||||
return EnumUtils.ParseEnumName(enumText, isNullable, !AllowIntegerValues, t);
|
||||
}
|
||||
|
||||
if (reader.TokenType == JsonToken.Integer)
|
||||
|
||||
@@ -190,7 +190,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
return values;
|
||||
}
|
||||
|
||||
public static object ParseEnumName(string enumText, bool isNullable, Type t)
|
||||
public static object ParseEnumName(string enumText, bool isNullable, bool disallowValue, Type t)
|
||||
{
|
||||
if (enumText == string.Empty && isNullable)
|
||||
{
|
||||
@@ -222,6 +222,24 @@ namespace Newtonsoft.Json.Utilities
|
||||
else
|
||||
{
|
||||
finalEnumText = enumText;
|
||||
|
||||
if (disallowValue)
|
||||
{
|
||||
bool isNumber = true;
|
||||
for (int i = 0; i < finalEnumText.Length; i++)
|
||||
{
|
||||
if (!char.IsNumber(finalEnumText[i]))
|
||||
{
|
||||
isNumber = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isNumber)
|
||||
{
|
||||
throw new FormatException("Integer string '{0}' is not allowed.".FormatWith(CultureInfo.InvariantCulture, enumText));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Enum.Parse(t, finalEnumText, true);
|
||||
|
||||
Reference in New Issue
Block a user