-Fixed StringEnumConverter reading specified enum names that contain a comma
This commit is contained in:
@@ -96,6 +96,21 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
Second,
|
||||
Third
|
||||
}
|
||||
|
||||
public enum NamedEnumWithComma
|
||||
{
|
||||
[EnumMember(Value = "@first")]
|
||||
First,
|
||||
|
||||
[EnumMember(Value = "@second")]
|
||||
Second,
|
||||
|
||||
[EnumMember(Value = ",third")]
|
||||
Third,
|
||||
|
||||
[EnumMember(Value = ",")]
|
||||
JustComma
|
||||
}
|
||||
#endif
|
||||
|
||||
public class NegativeEnumClass
|
||||
@@ -165,6 +180,40 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NamedEnumCommaTest()
|
||||
{
|
||||
EnumContainer<NamedEnumWithComma> c = new EnumContainer<NamedEnumWithComma>
|
||||
{
|
||||
Enum = NamedEnumWithComma.Third
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(c, Formatting.Indented, new StringEnumConverter());
|
||||
Assert.AreEqual(@"{
|
||||
""Enum"": "",third""
|
||||
}", json);
|
||||
|
||||
EnumContainer<NamedEnumWithComma> c2 = JsonConvert.DeserializeObject<EnumContainer<NamedEnumWithComma>>(json, new StringEnumConverter());
|
||||
Assert.AreEqual(NamedEnumWithComma.Third, c2.Enum);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NamedEnumCommaTest2()
|
||||
{
|
||||
EnumContainer<NamedEnumWithComma> c = new EnumContainer<NamedEnumWithComma>
|
||||
{
|
||||
Enum = NamedEnumWithComma.JustComma
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(c, Formatting.Indented, new StringEnumConverter());
|
||||
Assert.AreEqual(@"{
|
||||
""Enum"": "",""
|
||||
}", json);
|
||||
|
||||
EnumContainer<NamedEnumWithComma> c2 = JsonConvert.DeserializeObject<EnumContainer<NamedEnumWithComma>>(json, new StringEnumConverter());
|
||||
Assert.AreEqual(NamedEnumWithComma.JustComma, c2.Enum);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeNameEnumTest()
|
||||
{
|
||||
|
||||
@@ -200,21 +200,28 @@ namespace Newtonsoft.Json.Utilities
|
||||
string finalEnumText;
|
||||
|
||||
BidirectionalDictionary<string, string> map = EnumMemberNamesPerType.Get(t);
|
||||
if (enumText.IndexOf(',') != -1)
|
||||
string resolvedEnumName;
|
||||
if (TryResolvedEnumName(map, enumText, out resolvedEnumName))
|
||||
{
|
||||
finalEnumText = resolvedEnumName;
|
||||
}
|
||||
else if (enumText.IndexOf(',') != -1)
|
||||
{
|
||||
string[] names = enumText.Split(',');
|
||||
for (int i = 0; i < names.Length; i++)
|
||||
{
|
||||
string name = names[i].Trim();
|
||||
|
||||
names[i] = ResolvedEnumName(map, name);
|
||||
names[i] = TryResolvedEnumName(map, name, out resolvedEnumName)
|
||||
? resolvedEnumName
|
||||
: name;
|
||||
}
|
||||
|
||||
finalEnumText = string.Join(", ", names);
|
||||
}
|
||||
else
|
||||
{
|
||||
finalEnumText = ResolvedEnumName(map, enumText);
|
||||
finalEnumText = enumText;
|
||||
}
|
||||
|
||||
return Enum.Parse(t, finalEnumText, true);
|
||||
@@ -246,12 +253,15 @@ namespace Newtonsoft.Json.Utilities
|
||||
return finalName;
|
||||
}
|
||||
|
||||
private static string ResolvedEnumName(BidirectionalDictionary<string, string> map, string enumText)
|
||||
private static bool TryResolvedEnumName(BidirectionalDictionary<string, string> map, string enumText, out string resolvedEnumName)
|
||||
{
|
||||
string resolvedEnumName;
|
||||
map.TryGetBySecond(enumText, out resolvedEnumName);
|
||||
resolvedEnumName = resolvedEnumName ?? enumText;
|
||||
return resolvedEnumName;
|
||||
if (map.TryGetBySecond(enumText, out resolvedEnumName))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
resolvedEnumName = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user