`@JsonValue` with integer for enum does not deserialize correctly
See original GitHub issueThe Javadoc for @JsonValue
states that it is the only required annotation to both serialize and deserialize enums as something other than their name or ordinal:
when use for Java enums, one additional feature is that value returned by annotated method is also considered to be the value to deserialize from, not just JSON String to serialize as. This is possible since set of Enum values is constant and it is possible to define mapping, but can not be done in general for POJO types; as such, this is not used for POJO deserialization.
The Javadoc also states that it can be applied to any scalar type:
Usually value will be of a simple scalar type (String or Number), but it can be any serializable type (Collection, Map or Bean).
However, annotating an enum like below will fail to deserialize – Jackson appears to interpret the integer as the ordinal of the enum.
public enum Type {
A(2),
B(3);
private final int value;
Type(final int value) { this.value = value; }
@JsonValue public int value() { return this.value; }
}
When attempting to deserialize an enum like the above example, on Jackson 2.9.2, I receive the following stack trace: (slightly anonymized)
com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `com.example.Type` from number 3: index value outside legal index range [0..1]
at [Source: (InputStreamReader); line: 1, column: 60] (through reference chain: java.util.ArrayList[0]->com.example.Pojo["type"])
at com.fasterxml.jackson.databind.DeserializationContext.weirdNumberException(DeserializationContext.java:1563)
at com.fasterxml.jackson.databind.DeserializationContext.handleWeirdNumberValue(DeserializationContext.java:953)
at com.fasterxml.jackson.databind.deser.std.EnumDeserializer.deserialize(EnumDeserializer.java:200)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeSetAndReturn(MethodProperty.java:149)
If I add a @JsonCreator
to a static method that matches the value to the internal field, the enum can be deserialized correctly. However that is more code I would rather not maintain if possible.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:34
- Comments:25 (10 by maintainers)
Top GitHub Comments
Hi, I had same issue today, I managed to resolve that using this 2 annotations implemented within my int coded enum:
Hope that helps someone else too, I did spent quite some time to find the variant that works… Cheers 😉
Hi. My issue is exactly the same as the one described originally.