Jackson Enum Serializing and Deserializing
To implement enum serialization and deserialization with Jackson, use the @JsonValue
and @JsonCreator
annotations, respectively. The @JsonValue
should be annotated on the getter method providing the serialized value in the enum, while the @JsonCreator
should be on a static method that reverts the serialized value back to an enum constant:
Running this will serialize Type.YES to "Y", and deserialize "Y" back to Type.YES.
Case Insensitivity, Failures, and More!
How about when your enums have complex mappings, or when the JSON input may not stick to case conventions? No worry! You can create a Map
and implement a method for case-insensitive deserialization.
Also consider handling "I don't know what this is!"
scenarios during serialization. You can return null
from the toValue()
method, or generate an exception.
Custom Serializers/Deserializers: Just Do It Your Way!
Need even more control over your serialization/deserialization process? Consider utilizing custom serializer/deserializer classes. This will allow you more complex logic and better flexibility handling non-standard enum representations.
Also, remember in Jackson 2.6 and above, you can use @JsonProperty
on each constant to directly declare its serialization name.
Edge Cases: The Enum Strikes Back!
When you have additional properties tied to enum constants, or you need to work with polymorphic deserialization, remember to tailor your solution in such a way that Jackson smiles and gives you the correct outputs.
You can also add a @JsonEnumDefaultValue
to handle unknown or deprecated enum values gracefully. Thus, the application continues running, regardless of the string that comes knocking.
Considerations: The Good, The Bad, The Ugly
Customize ObjectMapper Settings
ObjectMapper settings can be your ultimate game changer. If you'd like to take a global approach in how your application handles enums, use WRITE/READ_ENUMS_USING_TO_STRING
flag.
Deserialization Logic
Ensure your logic is bulletproof. Your deserialization logic must be able to handle as many inputs as possible while maintaining the integrity of your application.
Map It Right!
Padlock your (de)serializer classes for compatibility with the Java and Jackson versions in use. You wouldn't want to throw a NoSuchMethodError
party, would you?
Was this article helpful?