runtime icon indicating copy to clipboard operation
runtime copied to clipboard

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

Open trueromanus opened this issue 5 years ago • 11 comments

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.

trueromanus avatar Feb 02 '21 15:02 trueromanus

Tagging subscribers to this area: @eiriktsarpalis, @layomia See info in area-owners.md if you want to be subscribed.

Issue Details

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.

Author: trueromanus
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

ghost avatar Feb 02 '21 15:02 ghost

That seems like a bug to me. For example using the same serializer options I get the following behaviour:

var dict = new Dictionary<ETestEnum, ETestEnum> { [ETestEnum.TestValue1] = ETestEnum.TestValue1 };
JsonSerializer.Serialize(dict, options); // "{\"TestValue1\":\"testValue1\"}"

@layomia thoughts?

eiriktsarpalis avatar Feb 02 '21 16:02 eiriktsarpalis

cc @Jozkee

When we designed the feature to support non-string primitives as dictionary keys https://github.com/dotnet/runtime/pull/32676, we elected to keep applying DictionaryKeyPolicy to only string keys, as it wasn't clear whether doing so for other types with JSON string representations (enums, Guids, DateTime etc.) is a first-class scenario (https://github.com/dotnet/runtime/pull/32909#discussion_r386662860). It's good to see some feedback around this. Enums seem like a reasonable type to apply the policy to. I'm curious what thoughts are around which other types to include.

layomia avatar Feb 02 '21 18:02 layomia

@layomia I made a quick research. In my opinion enum is the only type to include.

SkiFoD avatar Feb 09 '21 15:02 SkiFoD

@layomia

I'm curious what thoughts are around which other types to include.

E.g. we use Guid for dictionary key and with Json.net it was serialized using Guid converter (we use .ToString("N") in converter), but System.Text.Json just call ToString on guid value.

MaceWindu avatar Mar 23 '21 17:03 MaceWindu

Even if you decide that DictionaryKeyPolicy doesn't apply to enum dictionary keys, JsonStringEnumConverter with naming policy set to JsonNamingPolicy.CamelCase and added to JsonSerializerOptions.Converters shall be used for serialization anyway, and it is currently not the case, which is wrong.

yuretz avatar Mar 25 '21 09:03 yuretz

but System.Text.Json just call ToString on guid value.

I think this issue is not about what format we write data types with, but about whether to apply the dictionary naming policy to instances of those types when they are dictionary keys. It doesn't seem meaningful to apply something like camelCase policy (as a concrete example) to a guid.

JsonStringEnumConverter with naming policy set to JsonNamingPolicy.CamelCase and added to JsonSerializerOptions.Converters shall be used for serialization anyway, and it is currently not the case, which is wrong.

From the example from @eiriktsarpalis above, the policy is being applied to the dictionary value as expected.


I do think it makes sense to apply the policy to enum keys. It would be a breaking change to do this however, but it might be worth it since the workaround of having a custom converter for the parent dictionary could be cumbersome.

[I'd normally push something like this to future to wait for more customer feedback, but since it would be a breaking change we should address 6.0 before more folks depend on behavior that we want to change.]

layomia avatar Jun 01 '21 14:06 layomia

Marking this as up-for-grabs. The task is to apply DictionaryKeyPolicy to enum keys on serialization. I can help with any questions.

layomia avatar Jun 09 '21 16:06 layomia

@layomia Hey, I would like to work on this one.

SkiFoD avatar Jun 09 '21 16:06 SkiFoD

We have an open PR to fix this https://github.com/dotnet/runtime/pull/54429, but it won't block the release if we don't get this functionality in. I'll mark as .NET 7.

layomia avatar Jul 23 '21 01:07 layomia

Fixed by https://github.com/dotnet/runtime/pull/54429.

layomia avatar Aug 03 '21 03:08 layomia