使用自定义 JSON.NET JsonConverter 从 DescriptionAttribute 反序列化枚举停止工作
Deserializing Enum from DescriptionAttribute using custom JSON.NET JsonConverter stopped working
在 asp.net 核心 2.2 上寻求有关 Newtonsoft Json 的帮助。
我有一个 JsonEnumConverter<T>
负责来自枚举类型的 DescriptionAttribute 的 serializing/deserializing 值。直到大约 2 周前它工作正常,现在它已经完全停止工作。
这是我拥有的:
//From PerformersController:
public async Task<ActionResult<PagedPerformers>> GetPagedPerformersAsync([FromQuery] PerformerRequest performerRequest) { ... }
[JsonObject]
public class PerformerRequest : PageRequest
{
[FromQuery(Name = "performer_id")]
[JsonProperty(PropertyName = "performer_id", Order = 1)]
public override string Id { get; set; }
....
}
[JsonConverter(typeof(JsonEnumConverter<SortDirectionType>))]
public enum SortDirectionType
{
[Description("asc")]
ASCENDING,
[Description("desc")]
DESCENDING
}
public abstract class PageRequest
{
[FromQuery(Name = "page")]
[JsonProperty("page")]
public int Page { get; set; }
[FromQuery(Name = "limit")]
[JsonProperty("limit")]
public int PageSize { get; set; } = 100;
[FromQuery(Name = "sort_field")]
[JsonProperty("sort_field")]
public string SortField { get; set; } //= "Id";
[FromQuery(Name = "sort_dir")] [JsonConverter(typeof(JsonEnumConverter<SortDirectionType>))]
[JsonProperty("sort_dir")]
public SortDirectionType SortDirection { get; set; }
[FromQuery(Name = "id")]
[JsonProperty("id")]
public virtual string Id { get; set; }
}
public class JsonEnumConverter<T> : JsonConverter where T : struct, IComparable, IConvertible, IFormattable
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(T);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var type = typeof(T);
if (!type.IsEnum) throw new InvalidOperationException();
var enumDescription = (string)reader.Value;
return enumDescription.GetEnumValueFromDescription<T>();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var type = typeof(T);
if (!type.IsEnum) throw new InvalidOperationException();
if (value != null)
{
if (value is Enum sourceEnum)
{
writer.WriteValue(sourceEnum.GetDescriptionFromEnumValue());
}
}
}
}
public static class EnumExtensions
{
public static string GetDescriptionFromEnumValue(this Enum @enum)
{
FieldInfo fi = @enum.GetType().GetField(@enum.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute),
false);
if (attributes != null &&
attributes.Length > 0)
return attributes[0].Description;
else
return @enum.ToString();
}
public static T GetEnumValueFromDescription<T>(this string description)
{
var type = typeof(T);
if (!type.IsEnum)
throw new InvalidOperationException();
foreach (var field in type.GetFields())
{
if (Attribute.GetCustomAttribute(field,
typeof(DescriptionAttribute)) is DescriptionAttribute attribute)
{
if (attribute.Description == description)
return (T)field.GetValue(null);
}
else
{
if (field.Name == description)
return (T)field.GetValue(null);
}
}
throw new ArgumentException($"No matching value for enum {nameof(T)} found from {description}.",$"{nameof(description)}"); // or return default(T);
}
}
直到最近,它都运行得非常好。现在我不确定发生了什么,我立即收到 ValidationProblemDetails 响应。如果我抑制 asp.net 核心 2.2 模型状态无效过滤器,那么 modelState.IsValid 仍然会有错误。如果我在我的 JsonEnumConverter 的 ReadJson 中放置一个断点,它甚至不会命中。甚至尝试在启动时设置 JsonSerializerSettings 但没有成功或运气。已经尝试用 EnumMember 和 StringEnumConverter 替换 Description。还是一样的问题。似乎 ModelBinder 和 Json.NET 之间存在一些问题。
注意:此问题发生在 ASP.NET Core 2.2 上。建议 3.0 的解决方案没有帮助!!
如果您使用的是 aspnet core 3 / netstandard 2.1,您可以试试这个库 https://github.com/StefH/System.Text.Json.EnumExtensions,它定义了 JsonStringEnumConverter 的一些扩展以支持 EnumMember、Display 和 Description 等属性。
在 asp.net 核心 2.2 上寻求有关 Newtonsoft Json 的帮助。
我有一个 JsonEnumConverter<T>
负责来自枚举类型的 DescriptionAttribute 的 serializing/deserializing 值。直到大约 2 周前它工作正常,现在它已经完全停止工作。
这是我拥有的:
//From PerformersController:
public async Task<ActionResult<PagedPerformers>> GetPagedPerformersAsync([FromQuery] PerformerRequest performerRequest) { ... }
[JsonObject]
public class PerformerRequest : PageRequest
{
[FromQuery(Name = "performer_id")]
[JsonProperty(PropertyName = "performer_id", Order = 1)]
public override string Id { get; set; }
....
}
[JsonConverter(typeof(JsonEnumConverter<SortDirectionType>))]
public enum SortDirectionType
{
[Description("asc")]
ASCENDING,
[Description("desc")]
DESCENDING
}
public abstract class PageRequest
{
[FromQuery(Name = "page")]
[JsonProperty("page")]
public int Page { get; set; }
[FromQuery(Name = "limit")]
[JsonProperty("limit")]
public int PageSize { get; set; } = 100;
[FromQuery(Name = "sort_field")]
[JsonProperty("sort_field")]
public string SortField { get; set; } //= "Id";
[FromQuery(Name = "sort_dir")] [JsonConverter(typeof(JsonEnumConverter<SortDirectionType>))]
[JsonProperty("sort_dir")]
public SortDirectionType SortDirection { get; set; }
[FromQuery(Name = "id")]
[JsonProperty("id")]
public virtual string Id { get; set; }
}
public class JsonEnumConverter<T> : JsonConverter where T : struct, IComparable, IConvertible, IFormattable
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(T);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var type = typeof(T);
if (!type.IsEnum) throw new InvalidOperationException();
var enumDescription = (string)reader.Value;
return enumDescription.GetEnumValueFromDescription<T>();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var type = typeof(T);
if (!type.IsEnum) throw new InvalidOperationException();
if (value != null)
{
if (value is Enum sourceEnum)
{
writer.WriteValue(sourceEnum.GetDescriptionFromEnumValue());
}
}
}
}
public static class EnumExtensions
{
public static string GetDescriptionFromEnumValue(this Enum @enum)
{
FieldInfo fi = @enum.GetType().GetField(@enum.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute),
false);
if (attributes != null &&
attributes.Length > 0)
return attributes[0].Description;
else
return @enum.ToString();
}
public static T GetEnumValueFromDescription<T>(this string description)
{
var type = typeof(T);
if (!type.IsEnum)
throw new InvalidOperationException();
foreach (var field in type.GetFields())
{
if (Attribute.GetCustomAttribute(field,
typeof(DescriptionAttribute)) is DescriptionAttribute attribute)
{
if (attribute.Description == description)
return (T)field.GetValue(null);
}
else
{
if (field.Name == description)
return (T)field.GetValue(null);
}
}
throw new ArgumentException($"No matching value for enum {nameof(T)} found from {description}.",$"{nameof(description)}"); // or return default(T);
}
}
直到最近,它都运行得非常好。现在我不确定发生了什么,我立即收到 ValidationProblemDetails 响应。如果我抑制 asp.net 核心 2.2 模型状态无效过滤器,那么 modelState.IsValid 仍然会有错误。如果我在我的 JsonEnumConverter 的 ReadJson 中放置一个断点,它甚至不会命中。甚至尝试在启动时设置 JsonSerializerSettings 但没有成功或运气。已经尝试用 EnumMember 和 StringEnumConverter 替换 Description。还是一样的问题。似乎 ModelBinder 和 Json.NET 之间存在一些问题。
注意:此问题发生在 ASP.NET Core 2.2 上。建议 3.0 的解决方案没有帮助!!
如果您使用的是 aspnet core 3 / netstandard 2.1,您可以试试这个库 https://github.com/StefH/System.Text.Json.EnumExtensions,它定义了 JsonStringEnumConverter 的一些扩展以支持 EnumMember、Display 和 Description 等属性。