You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
System.Text.Json: Naming policies are not applied consistently to [Flags] enum values by JsonStringEnumConverter. #31622
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.
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
illialarka commentedon Apr 8, 2020
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?
Support combined flag enum converting
Add unit tests
Improve naming and move cache