Skip to content

Apply DictionaryKeyPolicy to enum keys (and other primitive key types?) #47765

Closed
@trueromanus

Description

@trueromanus

Description

Example for reproducing issue:

    public enum ETestEnum {
        TestValue1 = 1,
        TestValue2 = 2
    };

    public class Info
    {
        public Dictionary<ETestEnum, int> Queue { get; set; }
    }

    public void Main(string[] args) {
            string jsonString = JsonSerializer.Serialize(new Info
            {
                Queue = new Dictionary<ETestEnum, int> { [ETestEnum.TestValue1] = 1 },
            }, new JsonSerializerOptions {
                PropertyNameCaseInsensitive = true,
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
                DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
                Converters = {
                    new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)
                }
            } );
    }

jsonString in result: {"queue":{"TestValue1":1}}
jsonString in expected result: {"queue":{"testValue1":1}}

Key name in dictionary not in camel case.

I investigated little bit and find out that DictionaryKeyPolicy and Converters (in my case JsonStringEnumConverter(JsonNamingPolicy.CamelCase)) don't executing while serialization process.

Configuration

  • Which version of .NET is the code running on? net 5.0.102
  • What OS and version, and what distro if applicable? Windows 10 2004
  • What is the architecture (x64, x86, ARM, ARM64)? x64
  • Do you know whether it is specific to that configuration? I don't think so.

Regression?

I don't know because used Newtonsoft.Json.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions