c# - 使用枚举数组反序列化 json

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

使用枚举:

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 来代替我的用例。

最佳答案

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 中的“解析 -> 使用...”右键单击选择了命名空间?)

那么,如何解决这个问题呢?由于 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 中的以下评论

Note: If the AddNewtonsoftJson method isn't available, make sure that you installed the Microsoft.AspNetCore.Mvc.NewtonsoftJson package. A common error is to install the Newtonsoft.Json package instead of the Microsoft.AspNetCore.Mvc.NewtonsoftJson package.

样机 fiddle here .

关于c# - 使用枚举数组反序列化 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59998747/

相关文章:

c# - GDI+ 中出现一般性错误

c# - 准备好的语句与用户变量冲突

asp.net - Active Directory LDS 异常

c# - 在 Debug模式下发布了一个 asp.net Web 应用程序?

c# - Json.NET:反序列化对象列表

c# - 使用 Json.Net 序列化对象会导致内存不足异常

c# - ASP.NET API 无法在 Azure 上发布

c# - 是否可以结合两个条件

.net - 有什么简单的方法可以将 Criteria 转换为 HQL?

c# - 使用 JSON.Net 读取具有 NULL 处理的 JSON 属性