-Added generic JsonConvert.SerializeObject methods

-Fixed JToken.ToObject not using StringEnumConverter on enums
This commit is contained in:
James Newton-King
2015-12-09 08:45:36 +13:00
parent 45936d58a2
commit 2c7a51c50a
8 changed files with 222 additions and 9 deletions
+3 -2
View File
@@ -1948,13 +1948,14 @@ namespace Newtonsoft.Json.Linq
{
if (Type == JTokenType.String)
{
Type enumType = objectType.IsEnum() ? objectType : Nullable.GetUnderlyingType(objectType);
try
{
return Enum.Parse(enumType, (string)this, true);
// use serializer so JsonConverter(typeof(StringEnumConverter)) + EnumMemberAttributes are respected
return ToObject(objectType, JsonSerializer.CreateDefault());
}
catch (Exception ex)
{
Type enumType = objectType.IsEnum() ? objectType : Nullable.GetUnderlyingType(objectType);
throw new ArgumentException("Could not convert '{0}' to {1}.".FormatWith(CultureInfo.InvariantCulture, (string)this, enumType.Name), ex);
}
}