-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
@@ -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()
{