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:
Jackson produces:
Changing the output with @JsonValue
@JsonValue marks a method whose return value is used instead of the constant name:
The serialized output becomes:
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:
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:
A custom deserializer for it:
This handles JSON that contains integer values instead of the string representation of the enum.
Which tool to use
| Feature | Annotation | Usage |
| Custom serialization | @JsonValue | Customize how the enum is serialized to JSON. |
| Custom deserialization | @JsonCreator | Customize how JSON is deserialized to an enum. |
| Handling additional data | Custom classes | Utilize custom serializers/deserializers. |
Related reading
- Jackson how to prevent field serialization
- Jackson JsonFormat set date with one day day less
- Jackson serialization ignore empty values or null
- Jackson serializes a ZonedDateTime wrongly in Spring Boot
- Jackson Vs. Gson
- Jackson with JSON Unrecognized field, not marked as ignorable
- Java-R integration?
- Java8 Why is it forbidden to define a default method for a method from java.lang.Object
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.