-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Feature request: Enum (de)serialization in conjunction with JsonFormat.Shape.NUMBER_INT #3580
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
Comments
so basically the combination of |
Yes, @yawkat, this is would be my first idea that would use existing annotations. I could also imagine some other annotation or argument to |
@kistlers what version of Jackson were you using? |
@yihtserns I suspect at this time we were using jackson within Spring Boot version ~ 2.7.0, so I suspect ~ jackson 2.13.3, as this version was bundled at this time. |
ContextFirst of all, I just want to note that the best answer for https://stackoverflow.com/questions/37833557 should've been https://stackoverflow.com/a/56285491/229654, because the question author wrote:
As mentioned by the (supposed to be best) answer, to make Jackson de/serialize an enum value as its ordinal number, either annotate the enum class like this:
...or annotate the property like this:
...both will result in:
Knowing the above, I re-tested using Jackson Is that not already the behaviour this issue wants? Unless what you want is NOT the ordinal number, but your own custom number? |
Yes, @yihtserns, you are indeed correct. I want it to be any number, not just it's ordinal value. I know the functionality you suggested that would use the ordinal value. I realise now that my given example unfortunately suggests that, please check my edit to the original issue. |
OK after digging through the history a bit, here's what I understand:
For the 2nd one, the commit message said:
Now knowing the above, I tested this:
Seems like a JsonCreator factory method is not necessary at all, which I believe is the main motivation for opening this issue? (I did find some complaints about needing JsonCreator factory method to make things work (#1850), but it was a (fixed) bug.) |
Oh, this is new to me. I suppose I never fully read the JavaDoc of @JsonValue to find this behaviour (Probably because I simply would not have expected it). Thanks for digging it up! I think this does exactly what I wanted this Issue to achieve since the @JsonCreator method in the first example exactly reverses the mapping of the @JsonValue method. I just tested this and it then also again plays nicely with
as one would expect @jsonformat to work anyway. And I also realized at some point in the last 8 months that the getter is also not required, one can also just annotate the field directly and all the tests still pass:
My original idea would have also removed the need for the private field and the @JsonValue annotation. But not requiring the @JsonCreator method is already a large improvement in my opinion. |
Yeah, handling of |
As far as I understand it now, using
However, they do not:
On the first enum, the annotation is ignored, as Jackson does not coerce the |
Coercions/conversions between Numbers and Strings are tricky... and in a way I wish there was no default support for (de)serializing Enums by index (without some sort of explicit configuration). But I will note one thing: Conversely intent with I just don't know if:
since everything is sort of cobbled together from separate pieces of functionality. |
Thanks for the detailed answer, I appreciate it. I expected something along those lines to explain the observed behaviour. I suppose we can close this issue then? |
Is your feature request related to a problem? Please describe.
Not a problem, as there is a relatively clear and simple way to achieve the same, albeit slightly more limited, functionality. See below:
Describe the solution you'd like
I would like to see the functionality of the
@JsonProperty
annotation on enum values be extended to output the value as integers (and potentially other types) in the JSON output (and also deserialized with the same logic). I imagine a@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT)
annotation on the property in the POJO would define the output format of the enum value given to@JsonProperty
as a string. Hence, see the example below.Usage example
The following example is taken from StackOverflow, an answer by user SomethingSomething:
The enum
State
in the following POJO would be serialized as{"state": "1"}
, all good so far.However, consider the following case:
I would like to see this case result in JSON such as
{"state": 1}
, with the value being a number (int in that case) instead of a string.Additional context
I know I can achieve the same functionality using
@JsonValue
and@JsonCreator
annotations:The proposed feature would allow us to shortcut these methods. Before Jackson supported the
@JsonProperty
annotation on enum values, the above was also the way to (de)serialize enums to other strings, this feature would extend the functionality of the@JsonProperty
annotation to integers and potentially other types.I originally posted this idea in the Ideas section of the main Jackson repo, where cowtowncoder suggested filing a feature request here.
I would like to see something like that be supported in some way or form.
Edit:
I'd like to emphasize that I want this feature to work even if the integer values from the example would be some values other than the ordinal values, i.e. the following would also work:
And the enum, when serialized as integers, would be serialized with integers 17, 31 and 99.
The text was updated successfully, but these errors were encountered: