Jackson Post Deserialization Annotation Hook
Overview
This code provides a simple annotation that may be placed upon a method (even a private method) which will be run upon completion of the deserialization.
This has been inspired by several stackoverflow posts:
- Jackson Mapper post-construct
- How do I call the default deserializer from a custom deserializer in Jackson
- How to make @JsonBackReference and a custom deserializer work at the same time?
and by the following Jackson issues
Usage
In order to use this annotation the ObjectMapper
instance must be correctly configured. For example:
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule( org.goots.jackson.deserializer.JacksonPostHookDeserializer.getSimpleModule() );
Then, place the annotation upon a method to run after deserialization e.g.
@JsonPostDeserialize
public void postDeserialize()
{
myObjects.forEach( { obj -> obj.postInit()})
}
By default the annotation will not call private methods. To force access set the parameter as follows:
@JsonPostDeserialize(forceAccess=true)
private void postDeserialize()
{
myObjects.forEach( { obj -> obj.postInit()})
}