Jackson Custom Serialisers, Deserialisers, and Mix-in Annotations: The Advanced Toolkit
There are two scenarios where Jackson’s built-in annotations are not enough: when you need to control exactly how a complex type is serialised, and when the class you need to annotate belongs to a third-party library whose source you cannot modify. For the first case, Jackson provides custom serialisers and deserialisers. For the second, it provides Mix-in Annotations — a mechanism that lets you attach annotations to any class without touching its source. Writing a Custom Serialiser Extend StdSerializer<T> and override serialize(). The method receives the value to write and a JsonGenerator you use to emit JSON tokens. Suppose you have a Money type that should serialise as a structured JSON object containing the amount and the currency code: public class Money { private final BigDecimal amount; private final String currencyCode; // Constructor and getters }