【问题标题】:deserialize json with array of enum使用枚举数组反序列化 json
【发布时间】:2020-01-31 06:11:43
【问题描述】:

使用枚举:

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

我想为我的 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 属性指定。此方法改编自Deserialize json character as enumeration。但是,它不起作用。如果我将枚举更改为:

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

但我显然达到了“空”枚举的限制。我该怎么做?

更新 2:

我的创业公司没有 AddNewtonsoftJson(),完全转换为 Newtonsoft。现在我的错误可能更具可操作性:

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

这是有道理的,给我的解决方案here 指定了一个 JsonConverterFactory .. 我只需要原始 JsonConverter 来代替我的用例。

【问题讨论】:

  • 你没有使用json.net你正在使用system.text.json。 System.Text.Json 不支持[EnumMember()] 重命名枚举值,你必须编写自己的自定义转换器。要做到这一点,请参阅System.Text.Json: How do I specify a custom name for an enum value?。或者,您可以切换回 Json.NET,如 Where did IMvcBuilder AddJsonOptions go in .Net Core 3.0? 所示。这可能是其中一个或两个的副本,具体取决于您首选的序列化程序。
  • 您打算使用 Json.NET 还是 System.Text.Json? JsonStringEnumConverter 是 System.Text.Json 的一部分。 Newtonsoft 使用StringEnumConverter
  • 实际上,我认为切换回 Json.Net 的链接太旧了.. 我的 Newtsonsoft 导入正在工作,但是像 services.AddControllers() .AddNewtonsoftJson(); 这样我收到一个错误,表明 AddNewtonsoftJson 不是会员IMVCBuilder。这是一个值得写出来的问题,以回应一个真正的答案,你能提供一个吗?
  • @dbc 重新提出您的第二个建议。谢谢。我尝试使用 Newtonsoft.Json.Converters 和 StringEnumConverter .. 遗憾的是,这仍然失败
  • 我根据我的建议使用CustomJsonStringEnumConverter,编辑了中间更新,从而简化了您的问题。该转换器在这里不起作用。我更新了对System.Text.Json: How do I specify a custom name for an enum value? 的回答,表明我的CustomJsonStringEnumConverter 仅适用于序列化,并为反序列化提供一些替代建议。如果不合适,请随时恢复我的编辑。

标签: c# asp.net json.net asp.net-core-webapi asp.net-core-3.1


【解决方案1】:

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

  1. .NET Core 3.0+ 有一个 new built-in JSON serializer System.Text.Json,而您正在混淆这个新的序列化程序和 Json.NET 之间的属性和类。当两者都安装时,这很容易做到,因为它们共享一些类名,例如JsonSerializerJsonConverter

  2. 默认使用新的序列化程序,但尚不支持将枚举序列化为具有自定义值名称的字符串;详情请参阅System.Text.Json: How do I specify a custom name for an enum value?

解决您的问题的最简单方法是切换回 Json.NET,如 here 所示,并仅使用此序列化程序中的属性、转换器和命名空间。

首先我们来分解一下这两个序列化器的异同:

  1. System.Text.Json:

  2. Json.NET:

考虑到这一点,您在代码中使用了哪个序列化程序?由于您在问题中包含了命名空间,因此我们可以检查:

using System.Text.Json.Serialization; // System.Text.Json
using Newtonsoft.Json;                // Json.NET

namespace Assignment_1
{
    public class MyRequest
    {
//...
        [JsonProperty(                                         // JsonProperty from Newtonsoft
            "changeTypes", 
            ItemConverterType = typeof(JsonStringEnumConverter)// JsonStringEnumConverter from System.Text.Json
        )]
        public AppGlobals.BoardSymbols[] GameBoard { get; set; }
    }
}

如您所见,您将 Newtonsoft 的属性与 System.Text.Json 的转换器混合在一起,这是行不通的。 (也许您从 Visual Studio 中的“Resolve -> using ...”右键单击中选择了命名空间?)

那么,如何解决这个问题呢?由于 Json.NET 支持开箱即用的枚举值重命名,因此解决问题的最简单方法是使用此序列化程序。虽然性能可能不如System.Text.Json,但它更完整、功能更全面。

为此,删除命名空间 System.Text.Json.SerializationSystem.Text.Json 以及对类型 JsonStringEnumConverter 的引用,然后修改 MyRequestBoardSymbols,如下所示:

using System.Runtime.Serialization;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json;

namespace Assignment_1
{
    public class MyRequest
    {
//...
        [Required]
        [MinLength(9)]
        [MaxLength(9)]
        [JsonProperty("changeTypes")] // No need to add StringEnumConverter here since it's already applied to the enum itself
        public AppGlobals.BoardSymbols[] GameBoard { get; set; }
    }
}

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

然后 NuGet Microsoft.AspNetCore.Mvc.NewtonsoftJson 并在 Startup.ConfigureServices 中调用 AddNewtonsoftJson()

services.AddMvc()
    .AddNewtonsoftJson();

或者,如果您更喜欢全局使用 StringEnumConverter

services.AddMvc()
    .AddNewtonsoftJson(o => o.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter()));

请注意docs的以下评论

注意:如果AddNewtonsoftJson 方法不可用,请确保您安装了Microsoft.AspNetCore.Mvc.NewtonsoftJson 包。一个常见的错误是安装Newtonsoft.Json 包而不是Microsoft.AspNetCore.Mvc.NewtonsoftJson 包。

样机小提琴here.

【讨论】:

    【解决方案2】:

    您可以创建自己的 JsonStringEnumAttribute 并用它装饰您的枚举。

    using System.Text.Json.Serialization;
    
    class JsonStringEnumAttribute : JsonConverterAttribute
    {
        public JsonStringEnumAttribute() : base(typeof(JsonStringEnumConverter))
        {
    
        }
    }
    

    然后把它放在你的枚举上:

    [JsonStringEnum]
    enum MyEnum
    {
        Value1,
        Value2
    }
    

    然后您可以像这样使用字符串值反序列化 JSON:

    {
        "MyEnumProperty1": "Value1",
        "MyEnumProperty2": ["Value2", "Value1"]
    }
    

    进入这样的类:

    class MyClass
    {
        MyEnum MyEnumProperty1 { get; set; }
        MyEnum[] MyEnumProperty2 { get; set; }
    }
    

    例如使用System.Net.Http.Json:

    using HttpClient client = new();
    var myObjects = await client.GetFromJsonAsync<MyClass>("/some-endpoint");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-29
      • 1970-01-01
      • 2013-09-01
      • 2014-06-15
      • 1970-01-01
      • 2017-04-10
      • 1970-01-01
      • 2015-10-19
      相关资源
      最近更新 更多