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

[API Proposal]: Add option to specify JsonNamingPolicy for enum serialization on JsonSourceGenerationOptions #92828

Open
ithline opened this issue Sep 29, 2023 · 5 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Text.Json
Milestone

Comments

@ithline
Copy link

ithline commented Sep 29, 2023

Background and motivation

Recently there has been added UseStringEnumConverter flag to JsonSourceGenerationOptions, but there is no way to configure naming policy for these converters.

The Converters property accepts Type[]? array, so it cannot be used either.

API Proposal

namespace System.Text.Json.Serialization;

public partial class JsonSourceGenerationOptionsAttribute : JsonAttribute
{
    public JsonKnownNamingPolicy EnumNamingPolicy { get; set; }
}

API Usage

[JsonSourceGenerationOptions(
    UseStringEnumConverter = true,
    EnumNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
internal sealed partial class MyJsonContext : JsonSerializerContext
{
}

Alternative Designs

No response

Risks

No response

@ithline ithline added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Sep 29, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Sep 29, 2023
@ghost
Copy link

ghost commented Sep 29, 2023

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

Background and motivation

Recently there has been added UseStringEnumConverter flag to JsonSourceGenerationOptions, but there is no way to configure naming policy for these converters.

The Converters property accepts Type[]? array, so it cannot be used either.

API Proposal

namespace System.Text.Json.Serialization;

public partial class JsonSourceGenerationOptionsAttribute : JsonAttribute
{
    public JsonKnownNamingPolicy EnumNamingPolicy { get; set; }
}

API Usage

[JsonSourceGenerationOptions(
    UseStringEnumConverter = true,
    EnumNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
internal sealed partial class MyJsonContext : JsonSerializerContext
{
}

Alternative Designs

No response

Risks

No response

Author: ithline
Assignees: -
Labels:

api-suggestion, area-System.Text.Json

Milestone: -

@eiriktsarpalis eiriktsarpalis removed the untriaged New issue has not been triaged by the area owner label Sep 29, 2023
@eiriktsarpalis eiriktsarpalis added this to the Future milestone Sep 29, 2023
@eiriktsarpalis
Copy link
Member

Sounds reasonable.

@eiriktsarpalis
Copy link
Member

eiriktsarpalis commented Sep 29, 2023

The Converters property accepts Type[]? array, so it cannot be used either.

You can always try extending the converter class with a custom naming policy and specify that in the Converters property:

public enum MyEnum { A, B, C };

[JsonSourceGenerationOptions(Converters = new[] { typeof(CamelCaseJsonStringEnumConverter<MyEnum>)})]
[JsonSerializable(typeof(MyEnum))]
public partial class MyContext : JsonSerializerContext { }

public class CamelCaseJsonStringEnumConverter<TEnum>() 
    : JsonStringEnumConverter<TEnum>(JsonNamingPolicy.CamelCase) where TEnum : struct, Enum;

@alexanderdibenedetto
Copy link

This would solve a lot of issues with the fact that the JsonStringEnumConverter locks you into CamelCase, and some of us need snake or kebob case instead for certain enumerations.

@Pentadome
Copy link

The Converters property accepts Type[]? array, so it cannot be used either.

You can always try extending the converter class with a custom naming policy and specify that in the Converters property:

public enum MyEnum { A, B, C };

[JsonSourceGenerationOptions(Converters = new[] { typeof(CamelCaseJsonStringEnumConverter<MyEnum>)})]
[JsonSerializable(typeof(MyEnum))]
public partial class MyContext : JsonSerializerContext { }

public class CamelCaseJsonStringEnumConverter<TEnum>() 
    : JsonStringEnumConverter<TEnum>(JsonNamingPolicy.CamelCase) where TEnum : struct, Enum;

Or you can make a factory to support all enums.

internal sealed class JsonStringEnumCamelCaseConverter : JsonConverterFactory
{
    public override bool CanConvert(Type typeToConvert)
    {
        return typeToConvert.IsEnum;
    }

    public override JsonConverter? CreateConverter(
        Type typeToConvert,
        JsonSerializerOptions options
    )
    {
        if (!typeToConvert.IsEnum)
            throw new ArgumentException("Type should be enum.", nameof(typeToConvert));

        var constructedType = typeof(JsonStringEnumConverter<>).MakeGenericType(typeToConvert);
        return (JsonConverter)
            Activator.CreateInstance(
                constructedType,
                BindingFlags.Default,
                null,
                [JsonNamingPolicy.CamelCase, true],
                null
            )!;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Text.Json
Projects
None yet
Development

No branches or pull requests

4 participants