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

JsonSourceGenerationOptionsAttribute should provide an option for enabling string enum serialization globally #87195

Closed
eiriktsarpalis opened this issue Jun 6, 2023 · 3 comments · Fixed by #88984
Assignees
Labels
api-approved API was approved in API review, it can be implemented area-System.Text.Json
Milestone

Comments

@eiriktsarpalis
Copy link
Member

eiriktsarpalis commented Jun 6, 2023

  • While we can register the convert globally, there is no good way to ensure all enums are registered

Originally posted by @terrajobst in #79311 (comment)

API Proposal

namespace System.Text.Json.Serialization;

public partial class JsonSourceGenerationOptionsAttribute
{
     public bool UseStringEnumConverter { get; set; } = false;
}

API Usage

JsonSerializer.Serialize(new MyPoco(), MyContext.Default.MyPoco); // {"Property1":"Public","Property2":"Static","Property3":"Instance"}

public class MyPoco
{
    public BindingFlags Property1 { get; set; } = BindingFlags.Public;
    public BindingFlags Property2 { get; set; } = BindingFlags.Static;
    public BindingFlags Property3 { get; set; } = BindingFlags.Instance;
}

[JsonSourceGenerationOptions(UseStringEnumConverter = true)]
[JsonSerializable(typeof(MyPoco))]
public partial class MyContext { }

Related to #81985 which tracks support for compile-time control of JsonSerializerDefaults.Web.

cc @halter73

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jun 6, 2023
@ghost
Copy link

ghost commented Jun 6, 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
  • While we can register the convert globally, there is no good way to ensure all enums are registered

Originally posted by @terrajobst in #79311 (comment)

Author: eiriktsarpalis
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@eiriktsarpalis eiriktsarpalis added this to the 8.0.0 milestone Jun 6, 2023
@eiriktsarpalis eiriktsarpalis self-assigned this Jun 6, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Jun 6, 2023
@eiriktsarpalis eiriktsarpalis added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Jun 6, 2023
@eiriktsarpalis eiriktsarpalis removed their assignment Jun 6, 2023
@eiriktsarpalis
Copy link
Member Author

Related to #57321.

@eiriktsarpalis eiriktsarpalis self-assigned this Jul 7, 2023
@eiriktsarpalis eiriktsarpalis added blocking Marks issues that we want to fast track in order to unblock other important work api-ready-for-review API is ready for review, it is NOT ready for implementation and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Jul 7, 2023
@terrajobst
Copy link
Member

terrajobst commented Jul 13, 2023

Video

  • Looks good as proposed
  • We decided to add a converter that lets people serialize enums as numbers as a way to offset the global switch
namespace System.Text.Json.Serialization;

public partial class JsonSourceGenerationOptionsAttribute
{
     public bool UseStringEnumConverter { get; set; } = false;
}

public partial class JsonNumberEnumConverter<TEnum> : JsonConverterFactory
    where TEnum : struct, Enum
{
    public JsonNumberEnumConverter();

    public sealed override bool CanConvert(System.Type typeToConvert);
    public sealed override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options);
}

@terrajobst terrajobst added api-approved API was approved in API review, it can be implemented and removed api-ready-for-review API is ready for review, it is NOT ready for implementation labels Jul 13, 2023
@eiriktsarpalis eiriktsarpalis removed the blocking Marks issues that we want to fast track in order to unblock other important work label Jul 17, 2023
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jul 17, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jul 17, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Aug 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-System.Text.Json
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants