Skip to content

System.Text.Json: Naming policies are not applied consistently to [Flags] enum values by JsonStringEnumConverter. #31622

Closed

Description

@dbc2

When a JsonStringEnumConverter with a naming policy serializes a [Flags] enum, the naming policy does not result in consistent results for single composite values. E.g. FlagEnum.FlagTwo will appear differently in the JSON when serialized as part of FlagEnum.FlagOne | FlagEnum.FlagTwo than when serialized by itself. This is inconsistent with Newtonsoft, and seems undesirable. Details follow.

Say I have a [Flags] enum as follows:

[Flags]
public enum FlagEnum : ushort
{
    Default = 0,
    FlagOne = (1 << 1),
    FlagTwo = (1 << 2),
}

And I serialize a composite value to JSON using camel case for enum values as follows:

        var values = new[] { FlagEnum.FlagOne, FlagEnum.FlagTwo, FlagEnum.FlagOne | FlagEnum.FlagTwo };

        JsonNamingPolicy policy = JsonNamingPolicy.CamelCase;
        var options = new JsonSerializerOptions
        {
            Converters = { new JsonStringEnumConverter(policy) },
            WriteIndented = true,
        };

        var json = JsonSerializer.Serialize(values, options);

Then the naming policy will not be applied consistently to composite enum values, as shown by the JSON results:

[
  "flagOne",
  "flagTwo",
  "flagOne, FlagTwo"
]

The value FlagEnum.FlagTwo is serialized with camel-casing: "flagTwo", but when the same value is combined with another value it is not camel cased; FlagEnum.FlagOne | FlagEnum.FlagTwo maps to "flagOne, FlagTwo". That seems wrong.

This is even more apparent when using a snake case naming strategy:

public class SnakeCaseNamingPolicy : JsonNamingPolicy
{
    public override string ConvertName(string name)
    {
        return string.Concat(name.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + Char.ToLowerInvariant(x).ToString() : Char.ToLowerInvariant(x).ToString()));
    }
}

With JsonNamingPolicy policy = new SnakeCaseNamingPolicy(); the serialized array becomes:

[
  "flag_one",
  "flag_two",
  "flag_one, _flag_two"
]

It's apparent that JsonConverterEnum<T>.Write() is calling the naming policy's ConvertName() method on the overall, comma-separate flag value string rather than each component of the comma-separated string.

This contrasts with Json.NET which applies its naming strategy to each individual component, resulting in "flagOne, flagTwo" and "flag_one, flag_two".

System.Text.Json should do the same so that naming policies are applied consistently to single and composite flag enums values. It seems more correct to do so, and will facilitate conversion from Newtonsoft to System.Text.Json.

Demo fiddle here showing that System.Text.Json and Newtonsoft serialize composite enum values inconsistently.

Activity

  1. added
    untriaged
    on Feb 2, 2020
  2. added and removed
    untriaged
    on Feb 2, 2020
  3. added this to the 5.0 milestone on Feb 2, 2020
  4. illialarka commented on Apr 8, 2020

    @illialarka
    Contributor

    Hi
    I'm new to the codebase. I spent some time researching this bug. Can I take this as my first contribution to the repository?

  5. added 3 commits that reference this issue on Apr 11, 2020
    013c6d9
    d3ce81a
    5119a6b
  6. added
    help wanted
    and removed
    help wanted
    on May 13, 2020
  7. self-assigned this
    on May 19, 2020
  8. ghost locked as resolved and limited conversation to collaborators on Dec 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions