Jackson: Serialize and deserialize enum values as integers

It should work by specifying JsonValue mapper.

public enum State {
    OFF,
    ON,
    UNKNOWN;

    @JsonValue
    public int toValue() {
        return ordinal();
    }
}  

This works for deserialization also, as noted in Javadoc of @JsonValue annotation:

NOTE: 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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)