-Refactor enum parsing

This commit is contained in:
James Newton-King
2017-12-28 10:48:56 +13:00
parent e5e901c6d0
commit 51c9547dbc
11 changed files with 330 additions and 205 deletions
@@ -72,6 +72,18 @@ namespace Newtonsoft.Json.Tests.Converters
Positive = 1
}
[Flags]
public enum NegativeFlagsEnum
{
NegativeFour = -4,
NegativeTwo = -2,
NegativeOne = -1,
Zero = 0,
One = 1,
Two = 2,
Four = 4
}
#if !NET20
public enum NamedEnum
{
@@ -115,6 +127,12 @@ namespace Newtonsoft.Json.Tests.Converters
public NegativeEnum Value2 { get; set; }
}
public class NegativeFlagsEnumClass
{
public NegativeFlagsEnum Value1 { get; set; }
public NegativeFlagsEnum Value2 { get; set; }
}
[JsonConverter(typeof(StringEnumConverter), true)]
public enum CamelCaseEnum
{
@@ -210,6 +228,13 @@ namespace Newtonsoft.Json.Tests.Converters
Assert.AreEqual(NamedEnumWithComma.JustComma, c2.Enum);
}
[Test]
public void NamedEnumCommaCaseInsensitiveTest()
{
EnumContainer<NamedEnumWithComma> c2 = JsonConvert.DeserializeObject<EnumContainer<NamedEnumWithComma>>(@"{""Enum"":"",THIRD""}", new StringEnumConverter());
Assert.AreEqual(NamedEnumWithComma.Third, c2.Enum);
}
[Test]
public void DeserializeNameEnumTest()
{
@@ -297,6 +322,35 @@ namespace Newtonsoft.Json.Tests.Converters
}", json);
}
[Test]
public void SerializeNegativeFlagsEnum()
{
NegativeFlagsEnumClass negativeEnumClass = new NegativeFlagsEnumClass();
negativeEnumClass.Value1 = NegativeFlagsEnum.NegativeFour | NegativeFlagsEnum.NegativeTwo;
negativeEnumClass.Value2 = NegativeFlagsEnum.Two | NegativeFlagsEnum.Four;
string json = JsonConvert.SerializeObject(negativeEnumClass, Formatting.Indented, new StringEnumConverter());
StringAssert.AreEqual(@"{
""Value1"": ""NegativeTwo"",
""Value2"": ""Two, Four""
}", json);
}
[Test]
public void DeserializeNegativeFlagsEnum()
{
string json = @"{
""Value1"": ""NegativeFour,NegativeTwo"",
""Value2"": ""NegativeFour,Four""
}";
NegativeFlagsEnumClass negativeEnumClass = JsonConvert.DeserializeObject<NegativeFlagsEnumClass>(json, new StringEnumConverter());
Assert.AreEqual(NegativeFlagsEnum.NegativeFour | NegativeFlagsEnum.NegativeTwo, negativeEnumClass.Value1);
Assert.AreEqual(NegativeFlagsEnum.NegativeFour | NegativeFlagsEnum.Four, negativeEnumClass.Value2);
}
[Test]
public void SerializeNegativeEnum()
{
@@ -695,9 +749,7 @@ namespace Newtonsoft.Json.Tests.Converters
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);
Assert.AreEqual(EnumWithDifferentCases.m, e);
}
#if !NET20
@@ -44,6 +44,7 @@ using NUnit.Framework;
#endif
using Newtonsoft.Json;
using System.IO;
using System.Reflection;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Tests.TestObjects;
@@ -1213,7 +1214,7 @@ _____'propertyName': NaN,
var valueStates = JsonWriter.StateArrayTempate[7];
foreach (JsonToken valueToken in EnumUtils.GetValues(typeof(JsonToken)))
foreach (JsonToken valueToken in GetValues(typeof(JsonToken)))
{
switch (valueToken)
{
@@ -1231,6 +1232,24 @@ _____'propertyName': NaN,
}
}
private static IList<object> GetValues(Type enumType)
{
if (!enumType.IsEnum())
{
throw new ArgumentException("Type {0} is not an enum.".FormatWith(CultureInfo.InvariantCulture, enumType.Name), nameof(enumType));
}
List<object> values = new List<object>();
foreach (FieldInfo field in enumType.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static))
{
object value = field.GetValue(enumType);
values.Add(value);
}
return values;
}
[Test]
public void DateTimeZoneHandling()
{
@@ -8,6 +8,7 @@
<PropertyGroup>
<TargetFrameworks Condition="'$(TestFrameworks)'==''">net46;net451;net452;net40;net35;net20;netcoreapp2.0;netcoreapp1.1;netcoreapp1.0</TargetFrameworks>
<TargetFrameworks Condition="'$(TestFrameworks)'!=''">$(TestFrameworks)</TargetFrameworks>
<LangVersion>latest</LangVersion>
<VersionPrefix>1.0</VersionPrefix>
<Authors>James Newton-King</Authors>
<Company>Newtonsoft</Company>
@@ -1,3 +1,4 @@
using System;
using System.Reflection;
using Newtonsoft.Json.Utilities;
#if DNXCORE50