Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonStringEnumConverter没有对Enum的自定义JsonPropertyNameAttribute处理 #79903

Closed
bunnyi116 opened this issue Dec 22, 2022 · 2 comments
Closed

Comments

@bunnyi116
Copy link

Description

问题相关类
System.Text.Json.JsonSerializer
System.Text.Json.Serialization.JsonStringEnumConverter

无法读取通过JsonStringEnumConverter转换器读取到枚举JsonPropertyNameAttribute特性属性名

Reproduction Steps

using System.Runtime.Serialization;
using System.Text.Json;
using System.Text.Json.Serialization;



Console.WriteLine(JsonSerializer.Serialize(new Test(), new JsonSerializerOptions() { WriteIndented = true })); ;


public class Test
{
    [JsonPropertyName("data1")]
    public DataType Days1 => DataType.TestData1;

    [JsonPropertyName("data2")]
    public DataType Days2 => DataType.TestData2;

    [JsonPropertyName("data3")]
    public DataType Days3 => DataType.TestData3;
}

[JsonConverter(typeof(JsonStringEnumConverter))]
public enum DataType
{
    [JsonPropertyName("testData1")]
    [EnumMember(Value = "testData1")]
    TestData1,

    [JsonPropertyName("testData2")]
    [EnumMember(Value = "testData2")]
    TestData2,

    TestData3,

};

Expected behavior

源 JSON 结果

{
  "data1": "TestData1",
  "data2": "TestData2",
  "data3": "TestData3"
}

预期 JSON 结果

{
  "data1": "testData1",
  "data2": "testData2",
  "data3": "TestData3"
}

Actual behavior

略...

Regression?

No response

Known Workarounds

添加对 JsonStringEnumConverter 的 JsonPropertyNameAttribute 的读取支持

Configuration

image

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Dec 22, 2022
@ghost
Copy link

ghost commented Dec 22, 2022

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

问题相关类
System.Text.Json.JsonSerializer
System.Text.Json.Serialization.JsonStringEnumConverter

无法读取通过JsonStringEnumConverter转换器读取到枚举JsonPropertyNameAttribute特性属性名

Reproduction Steps

using System.Runtime.Serialization;
using System.Text.Json;
using System.Text.Json.Serialization;



Console.WriteLine(JsonSerializer.Serialize(new Test(), new JsonSerializerOptions() { WriteIndented = true })); ;


public class Test
{
    [JsonPropertyName("data1")]
    public DataType Days1 => DataType.TestData1;

    [JsonPropertyName("data2")]
    public DataType Days2 => DataType.TestData2;

    [JsonPropertyName("data3")]
    public DataType Days3 => DataType.TestData3;
}

[JsonConverter(typeof(JsonStringEnumConverter))]
public enum DataType
{
    [JsonPropertyName("testData1")]
    [EnumMember(Value = "testData1")]
    TestData1,

    [JsonPropertyName("testData2")]
    [EnumMember(Value = "testData2")]
    TestData2,

    TestData3,

};

Expected behavior

源 JSON 结果

{
  "data1": "TestData1",
  "data2": "TestData2",
  "data3": "TestData3"
}

预期 JSON 结果

{
  "data1": "testData1",
  "data2": "testData2",
  "data3": "TestData3"
}

Actual behavior

略...

Regression?

No response

Known Workarounds

添加对 JsonStringEnumConverter 的 JsonPropertyNameAttribute 的读取支持

Configuration

image

Other information

No response

Author: Bunnui
Assignees: -
Labels:

area-System.Text.Json

Milestone: -

@eiriktsarpalis
Copy link
Member

Duplicate of #74385.

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Dec 22, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Jan 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants