-Fixed StringEnumConverter when EnumMember defines members that differ only by case
This commit is contained in:
@@ -617,6 +617,73 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
|
||||
Assert.AreEqual("Integer string '1' is not allowed.", ex.InnerException.Message);
|
||||
}
|
||||
|
||||
public enum EnumWithDifferentCases
|
||||
{
|
||||
M,
|
||||
m
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeEnumWithDifferentCases()
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(EnumWithDifferentCases.M, new StringEnumConverter());
|
||||
|
||||
Assert.AreEqual(@"""M""", json);
|
||||
|
||||
json = JsonConvert.SerializeObject(EnumWithDifferentCases.m, new StringEnumConverter());
|
||||
|
||||
Assert.AreEqual(@"""m""", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeEnumWithDifferentCases()
|
||||
{
|
||||
EnumWithDifferentCases e = JsonConvert.DeserializeObject<EnumWithDifferentCases>(@"""M""", new StringEnumConverter());
|
||||
|
||||
Assert.AreEqual(EnumWithDifferentCases.M, e);
|
||||
|
||||
e = JsonConvert.DeserializeObject<EnumWithDifferentCases>(@"""m""", new StringEnumConverter());
|
||||
|
||||
// unfortunatly Enum.Parse with ignoreCase will find the first match rather than the best match
|
||||
// it would be great to find a way around this
|
||||
Assert.AreEqual(EnumWithDifferentCases.M, e);
|
||||
}
|
||||
|
||||
#if !NET20
|
||||
[DataContract(Name = "DateFormats")]
|
||||
public enum EnumMemberWithDifferentCases
|
||||
{
|
||||
[EnumMember(Value = "M")]
|
||||
Month,
|
||||
[EnumMember(Value = "m")]
|
||||
Minute
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeEnumMemberWithDifferentCases()
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(EnumMemberWithDifferentCases.Month, new StringEnumConverter());
|
||||
|
||||
Assert.AreEqual(@"""M""", json);
|
||||
|
||||
json = JsonConvert.SerializeObject(EnumMemberWithDifferentCases.Minute, new StringEnumConverter());
|
||||
|
||||
Assert.AreEqual(@"""m""", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeEnumMemberWithDifferentCases()
|
||||
{
|
||||
EnumMemberWithDifferentCases e = JsonConvert.DeserializeObject<EnumMemberWithDifferentCases>(@"""M""", new StringEnumConverter());
|
||||
|
||||
Assert.AreEqual(EnumMemberWithDifferentCases.Month, e);
|
||||
|
||||
e = JsonConvert.DeserializeObject<EnumMemberWithDifferentCases>(@"""m""", new StringEnumConverter());
|
||||
|
||||
Assert.AreEqual(EnumMemberWithDifferentCases.Minute, e);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if !NET20
|
||||
|
||||
@@ -43,10 +43,10 @@ namespace Newtonsoft.Json.Utilities
|
||||
private static BidirectionalDictionary<string, string> InitializeEnumType(Type type)
|
||||
{
|
||||
BidirectionalDictionary<string, string> map = new BidirectionalDictionary<string, string>(
|
||||
StringComparer.OrdinalIgnoreCase,
|
||||
StringComparer.OrdinalIgnoreCase);
|
||||
StringComparer.Ordinal,
|
||||
StringComparer.Ordinal);
|
||||
|
||||
foreach (FieldInfo f in type.GetFields())
|
||||
foreach (FieldInfo f in type.GetFields(BindingFlags.Public | BindingFlags.Static))
|
||||
{
|
||||
string n1 = f.Name;
|
||||
string n2;
|
||||
|
||||
Reference in New Issue
Block a user