使用自定义 JSON.NET JsonConverter 从 DescriptionAttribute 反序列化枚举停止



寻求有关Newtonsoft Json asp.net 核心2.2的帮助。 我有一个JsonEnumConverter<T>负责从枚举类型序列化/反序列化来自 DescriptionAttribute 的值。直到大约 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);
}
}

直到最近,这绝对可以正常工作。现在我不确定发生了什么,我立即得到验证问题详细信息响应。如果我抑制 asp.net 核心 2.2 模型状态无效过滤器,则 modelState.IsValid 仍将具有 false。如果我在我的 JsonEnumConverter 的 ReadJson 中放置一个断点,它甚至不会命中。甚至试图在启动中设置JsonSerializerSettings,但没有成功或运气。已经尝试将描述替换为EnumMember和StringEnumConverter。还是同样的问题。似乎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等属性。

最新更新