使用自定义JSON.NET JsonConverter从DescriptionAttribute反序列化Enum停止工作

时间:2019-12-19 04:27:54

标签: asp.net json.net core converters

在asp.net core 2.2上寻求有关Newtonsoft Json的帮助。 我有一个JsonEnumConverter<T>,它负责对Enum类型的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);
        }
}

直到最近,它的工作情况还算不错。现在,我不确定发生了什么,我是否立即收到ValidationProblemDetails响应。如果我抑制asp.net core 2.2模型状态无效过滤器,则modelState.IsValid仍为false。如果我在JsonEnumConverter的ReadJson中放置一个断点,它甚至不会命中。甚至尝试在启动时设置JsonSerializerSettings,但没有成功或运气。已经尝试用EnumMember和StringEnumConverter替换Description。还是一样的问题。似乎ModelBinder和Json.NET不能很好地配合使用会出现问题。

1 个答案:

答案 0 :(得分:1)

如果您使用的是aspnet core 3 / netstandard 2.1,可以尝试使用该库https://github.com/StefH/System.Text.Json.EnumExtensions,该库定义了JsonStringEnumConverter的某些扩展以支持EnumMember,Display和Description等属性。