相关疑难解决方法(0)

使用枚举数组反序列化 json

使用枚举:

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

我想为我的 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; }
    }
}
Run Code Online (Sandbox Code Playgroud)

哪里GameBoard应该序列化为 JSON 作为字符串数组,其名称由EnumMember属性指定。这种方法改编自Deserialize json character as enumeration。然而,它不起作用。如果我将枚举更改为:

    [JsonConverter(typeof(JsonStringEnumConverter))]
    public enum …
Run Code Online (Sandbox Code Playgroud)

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

12
推荐指数
1
解决办法
1万
查看次数

使用System.Text.Json的JsonConverter等效项

我开始将.net Core 3.0应用程序中的一些代码从迁移Newtonsoft.JsonSystem.Text.Json

我从迁移了属性

[JsonProperty("id")][JsonPropertyName("id")]

但是我有一些属性装饰有如下JsonConverter属性:

[JsonConverter(typeof(DateTimeConverter))] [JsonPropertyName("birth_date")] DateTime BirthDate{ get; set; }

但是我在System.Text.Json“ 牛顿软件”转换器中找不到与之等效的东西。有人知道如何在.net Core 3.0中实现吗?

谢谢!

c# json .net-core .net-core-3.0 system.text.json

11
推荐指数
1
解决办法
3230
查看次数

使用 AspNet [FromQuery] 模型绑定中的 EnumMember 值反序列化枚举

我在 .NET 6 项目中有一个端点Microsoft.NET.Sdk.Web,它使用标准 [FromQuery] 将查询字符串反序列化为 .NET 对象

[Route("[controller]")]
public class SamplesController
    : ControllerBase
{
    [HttpGet]
    public IActionResult Get([FromQuery]QueryModel queryModel)
    {
        if (!queryModel.Status.HasValue)
        {
            return BadRequest("Problem in deserialization");
        }
        
        return Ok(queryModel.Status.Value.GetEnumDisplayName());
    }
}
Run Code Online (Sandbox Code Playgroud)

该模型包含一个枚举

public class QueryModel
{
    /// <summary>
    /// The foo parameter
    /// </summary>
    /// <example>bar</example>
    public string? Foo { get; init; } = null;
    
    /// <summary>
    /// The status
    /// </summary>
    /// <example>on-hold</example>
    public Status? Status { get; set; } = null;
}
Run Code Online (Sandbox Code Playgroud)

枚举具有EnumMember …

enums jsonserializer custom-model-binder asp.net-core system.text.json

7
推荐指数
1
解决办法
2213
查看次数

从带有空格的 JSON 字符串反序列化枚举

我将用这样的事实作为序言:我试图避免使用Newtonsoft.Json,因为表面上看,System.Text.Json它已经准备好在 .NET 6 中迎来黄金时段。

因此,我有两个来自 API 的枚举,我想使用此测试方法对它们进行反序列化:

[Theory]
[ClassData(typeof(TestDataGenerator))]
public void CanDeserialiseEnumsWithCustomJsonStrings(Enum expected, string jsonName)
{
    jsonName.ShouldNotBeNullOrEmpty();
    ReadOnlySpan<char> json = $"{{\"TestEnum\":\"{jsonName}\"}}";

    Type constructed = typeof(TestEnumWrapper<>).MakeGenericType(expected.GetType());
        
    var res = JsonSerializer.Deserialize(json, constructed);

    constructed.GetProperty("TestEnum").GetValue(res).ShouldBe(expected);
}

private class TestEnumWrapper<T> where T: struct
{
    public T TestEnum { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

(是的,我知道这可以通过 来完成JsonSerializer.Deserialize<T>(),我希望能够通过此测试测试许多类型,所以我需要反射 AFAICT)。

第一个,工作正常:

[JsonConverter(typeof(JsonStringEnumConverter))]
public enum RecordType
{
        [JsonPropertyName("country")]
        Country = 1,

        [JsonPropertyName("destinationOrbit")]
        DestinationOrbit = 2,

        [JsonPropertyName("engine")]
        Engine = 3,
        //etc...

}
Run Code Online (Sandbox Code Playgroud)

第二个,反序列化失败,这似乎是由于名称中的空格造成的。

[JsonConverter(typeof(JsonStringEnumConverter))]
public enum …
Run Code Online (Sandbox Code Playgroud)

c# enums json deserialization system.text.json

3
推荐指数
1
解决办法
4253
查看次数