[ACCEPTED]-Jackson How to retrieve parent bean in a custom Serializer/Deserializer-jackson

Accepted answer
Score: 14

If you are using Jackson 2.5, it is possible 4 to access parent object via JsonGenerator.getCurrentValue(). Or, further 3 up the hierarchy, going via getOutputContext() (which has 2 getParent() as well as getCurrentValue() method). This is also available 1 through JsonParser for custom deserializer.

Score: 5

For deserialization, where you don't have 2 access to the JsonGenerator object. The following worked 1 for me:

JsonStreamContext parsingContext = jsonParser.getParsingContext();
JsonStreamContext parent = parsingContext.getParent();
Object currentValue = parent.getCurrentValue();
Score: 0

Note: getCurrentValue will be null if you 5 are using custom serialization

I worked around 4 this by setting the parent object into the 3 child's serializer instance and then accessing 2 it when the child's serializer was called 1 by jackson.

More Related questions