Jackson Enum
Serialization
Deserialization
Java Programming
Data Processing

Jackson enum serialization and deserialization

Codemia Knowledge Hub ·

By default Jackson serializes an enum as the name of its constant and deserializes by matching a JSON string to a constant name. Use @JsonValue to change the serialized value, @JsonCreator on a static factory method to control deserialization, and a custom serializer or deserializer when the enum carries extra data.

Default enum serialization

Jackson writes each constant's declared name. Given this enum:

java
public enum Color {
    RED, GREEN, BLUE;
}

Jackson produces:

json
"RED"
"GREEN"
"BLUE"

Changing the output with @JsonValue

@JsonValue marks a method whose return value is used instead of the constant name:

java
1public enum Color {
2    RED, GREEN, BLUE;
3    
4    @JsonValue
5    public String toLower() {
6        return name().toLowerCase();
7    }
8}

The serialized output becomes:

json
"red"
"green"
"blue"

Default enum deserialization

When reading JSON, Jackson matches string values against the enum constant names. If a value matches no constant, Jackson throws an exception unless you add specific handling for that case.

Custom deserialization with @JsonCreator

@JsonCreator can annotate a static factory method that takes a string and returns an enum constant:

java
1public enum Color {
2    RED, GREEN, BLUE;
3    
4    @JsonCreator
5    public static Color fromString(String s) {
6        return Color.valueOf(s.toUpperCase());
7    }
8}

This gives more flexibility, such as ignoring case during deserialization.

Enums with extra fields: custom serializers and deserializers

When an enum has additional fields or needs custom logic, @JsonCreator and @JsonValue may not be enough, and custom serializers and deserializers are the robust option. For example, an enum that carries more data:

java
1public enum Color {
2    RED(1), GREEN(2), BLUE(3);
3    
4    private final int code;
5    
6    private Color(int code) {
7        this.code = code;
8    }
9    
10    @JsonValue
11    public int getCode() {
12        return code;
13    }
14}

A custom deserializer for it:

java
1public class ColorDeserializer extends StdDeserializer<Color> {
2
3    public ColorDeserializer() {
4        super(Color.class);
5    }
6
7    @Override
8    public Color deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
9        int code = p.getIntValue();
10        for (Color color : Color.values()) {
11            if (color.getCode() == code) {
12                return color;
13            }
14        }
15        return null; // or throw exception
16    }
17}

This handles JSON that contains integer values instead of the string representation of the enum.

Which tool to use

FeatureAnnotationUsage
Custom serialization@JsonValueCustomize how the enum is serialized to JSON.
Custom deserialization@JsonCreatorCustomize how JSON is deserialized to an enum.
Handling additional dataCustom classesUtilize custom serializers/deserializers.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms