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

Allow more than one value map to the same enum value with @JsonValue #1380

Open
mateusfccp opened this issue Dec 7, 2023 · 4 comments
Open

Comments

@mateusfccp
Copy link

Currently, we can use @JsonValue in an enum to define what value maps to it.

enum CustomType {
  @JsonValue('a')
  valueA,
  @JsonValue('b')
  valueB,
  @JsonValue('c')
  valueC,
}

Now, I want to map more than one value to the same enum value. I cannot do this unless I write a custom JsonConverter for the enum.

This issue proposes that we have a way to tell json_serializable that we want more than one value to be mapped to our enum value.

For instance:

enum CustomType {
  @JsonValue('valueA')
  @JsonValue('a')
  valueA,
  @JsonValue('valueB')
  @JsonValue('b')
  valueB,
  @JsonValue('valueC')
  @JsonValue('c')
  valueC,
}
@mateusfccp mateusfccp changed the title Allow more than one values map to the same enum value with @JsonValue Allow more than one value map to the same enum value with @JsonValue Dec 7, 2023
@Xazin
Copy link

Xazin commented Dec 21, 2023

I would assume a new decorator eg. @MultiJsonValues(['valueA', 'a']) to be more proper, or an argument on the existing decorator like so @JsonValue('a', aliases: ['valueA', 'A']).

I wouldn't know if this is possible since I have not looked deeply at the code.

@mateusfccp
Copy link
Author

mateusfccp commented Jan 4, 2024

Maybe the aliases approach would be better, as we should have a "canonical" reference to use in toJson. While fromJson could map from many different values to a single value, when converting this value to JSON, we can only have a single option, which would be the non-alias one.

@alvarogabrielgomez
Copy link

Some updates to this?

@JLabeda
Copy link

JLabeda commented Nov 26, 2024

Hi, any update on this issue? 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants