用枚举数组反序列化json [英] deserialize json with array of enum

查看:100
本文介绍了用枚举数组反序列化json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用枚举:

namespace AppGlobals
{
    [JsonConverter(typeof(JsonStringEnumConverter))]
    public enum BoardSymbols
    {
        [EnumMember(Value = "X")]
        First = 'X',
        [EnumMember(Value = "O")]
        Second = 'O',
        [EnumMember(Value = "?")]
        EMPTY = '?'
    }
}

我想为我的api定义一个模型:

I would like to define a model for my api:

using System;
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using Newtonsoft.Json;

namespace Assignment_1
{
    public class MyRequest
    {
//...
        [Required]
        [MinLength(9)]
        [MaxLength(9)]
        [JsonProperty("changeTypes", ItemConverterType = typeof(JsonStringEnumConverter))]
        public AppGlobals.BoardSymbols[] GameBoard { get; set; }
    }
}

GameBoard应该以字符串数组的形式序列化为JSON,该字符串的名称由EnumMember属性指定.此方法改编自将json字符反序列化为枚举.但是,它不起作用.如果我将枚举更改为:

Where GameBoard should serialize to JSON as an array of strings with names specified by the EnumMember attributes. This approach is adapted from Deserialize json character as enumeration. However, it does not work. This does works if I change the enum to:

    [JsonConverter(typeof(JsonStringEnumConverter))]
    public enum BoardSymbols
    {
      X='X',
      Y='Y'
    }

但是我显然在空"枚举上达到了极限.我该怎么办?

But I obviously hit a limit on the 'empty' enumeration. How can I do this?

我的启动中没有AddNewtonsoftJson(),完全转换为Newtonsoft.现在,我的错误也许更具可执行性:

I did not have AddNewtonsoftJson() in my startup, converting over fully to Newtonsoft. Now my error is perhaps more actionable:

System.InvalidCastException: Unable to cast object of type 'CustomJsonStringEnumConverter' to type 'Newtonsoft.Json.JsonConverter'.
   at Newtonsoft.Json.Serialization.JsonTypeReflector.CreateJsonConverterInstance(Type converterType, Object[] args)

这很有意义,在此为我指定的解决方案指定了一个JsonConverterFactory.我只需要原始的JsonConverter用例代替.

This makes sense, the solution prescribed to me here specified a JsonConverterFactory .. I just need the raw JsonConverter for my use case instead.

推荐答案

TL/DR:您在这里有两个基本问题:

TL/DR: You have two basic problems here:

  1. .NET Core 3.0+具有

  1. .NET Core 3.0+ has a new built-in JSON serializer System.Text.Json, and you are mixing up attributes and classes between this new serializer and Json.NET. This is very easy to do when both are installed because they share some class names, such as JsonSerializer and JsonConverter.

默认情况下使用新的序列化器,但尚不支持将枚举序列化为具有自定义值名称的字符串;参见 System.Text.Json:如何为枚举值指定自定义名称? 有关详细信息.

The new serializer is used by default but does not yet support serialization of enums as strings with custom value names; see System.Text.Json: How do I specify a custom name for an enum value? for details.

解决问题的最简单方法是切换到Json.NET,如此处所示,并使用属性,转换器以及只能从此序列化程序获得的名称空间.

The easiest way to solve your problem is to switch back to Json.NET as shown here and use attributes, converters and namespaces exclusively from this serializer.

首先让我们分解两个序列化器之间的差异和相似之处:

First let's break down the differences and similarities between the two serializers:

  1. System.Text.Json:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆