I have an EntityId class that servers as a simple wrapper class to database identifiers. The class already has methods for converting to and from a string representation. I’d like to use this string representation of the EntityId in my JSON web resources.
What’s the simplest to implement custom serialization for this simple type in Jackson? I know I can write a custom serializer and deserializer, but I wondered if there might be an even simpler solution.
Thanks!
If there is a method to serialize type as String, you can just add
@JsonValueannotation like so:Conversely, if there is a single-arg constructor that takes a
String,intorlong(or some Java type that Jackson can convert to from JSON Scalar type), you can add@JsonCreatorannotation next to that constructor: