Jackson Mixins Common Uses
Jackson mixins is a mechanism to add Jackson annotations to classes without modifying the actual class. It was created for those cases where we can’t modify a class such as when working with third-party classes.
We can use any Jackson annotation but we don’t add them directly to the class. We use them in a mixin class instead that can be either an abstract class or an interface. They can be used for both Jackson serialization and deserialization and they have to be added to the ObjectMapper configuration.
In the next sections, we’re gonna be quite practical and we’ll show some common cases where it’s useful to use Jackson mixins.
Making a Third-Party Class Jackson Serializable
The first case we’re showing is when we need to use third-party classes that can’t be serialized or deserialized by Jackson because they aren’t following the Jackson conventions. Since we can’t modify these classes we have to use mixins to add all the necessary pieces that Jackson needs for serialization purposes.
Let’s suppose that we want to serialize this third-party class:
We can’t serialize this class with Jackson because the properties are private and there aren’t getters and setters. Hence, Jackson won’t recognize any property and will throw an exception :
Let’s create now a mixin to solve this problem! In our mixin we’ll add the properties that we want to serialize:
In this case, we´ve created an abstract class because later we’ll need to add a constructor.
After that, we need to tell Jackson to use our Mixin. To do so, we need to set this up in our ObjectMapper:
Now, Jackson will be able to serialize our Person and will serialize both the firstName and lastName.
On the other hand, if we also want to use our Person class for deserialization we need to add a jackson-friendly constructor to our mixin class:
Otherwise, Jackson would throw an exception.
Ignoring Properties
We’ll consider now a different scenario when serializing third-party classes. Let’s assume now that our Person class has all the getters, setters and constructors needed and all its fields will be serialized:
However, we’d like to serialize only the firstName, and as before we can’t modify this class. Again, we can work around this by using a mixin class:
This way, Jackson will ignore this property and will only serialize the firstName.
Changing Property Names
Following our previous example, we’d also like to change the name of some properties when they’re serialized.
Let’s modify our mixin class to rename the property lastName:
Now, our lastName property will be serialized as surname.
Overriding Custom Serializers and Deserializers
There are other scenarios where we find classes that are using custom serializers and deserializers but we want to override them. And of course, we can’t or we don’t want to modify these classes.
Let’s extend our Person class to include a custom serializer and deserializer:
As we can see, the Person class is now serialized by concatenating the firstName and lastName.
However, in some cases we’d like to serialize this class differently. Let’s create a different serializer and deserializer for our class:
Now, our Person will be serialized as “lastName, firstName”.
As we did before, to use this new serializer and deserializer without modifying Person we need to specify it in our mixin class:
Now, Jackson will use these serializers and will ignore the ones specified in Person.
It would be the same if we wanted to do this just for a specific property.
Conclusion
In this short tutorial, we’ve briefly introduced the Jackson mixins and then focused on some cases where they may be useful. We’ve also shown some examples to illustrate these cases and show how these mixins work.
The source code of the examples can be found at Github.
Leave a comment