Jackson 3 Migration: Accessing the configured mapper from a custom StdDeserializer #6160
Replies: 0 comments 1 reply
|
The fastest way to get answers these days is with Gen AI. And it is faster for you to do that than for me to do it for you. Jackson 3.x has an explicit, supported replacement for this pattern — it's just moved from What changedIn Jackson 3.x, The key point: The mechanismInstead of: // Jackson 2.x
ObjectMapper mapper = (ObjectMapper) jsonParser.getCodec();
Block child = mapper.readValue(jsonParser, resolvedPluginType);use the context to do the nested read: // Jackson 3.x
@Override
public Block deserialize(JsonParser p, DeserializationContext ctxt) {
Class<? extends Block> resolvedPluginType = resolveTypeFromPluginRegistry(p, ctxt);
return ctxt.readValue(p, resolvedPluginType);
}
If you need annotation-aware nested reads for a bean property (rather than a "loose" root-style value), there's also: ctxt.readPropertyValue(p, beanProperty, resolvedPluginType);which additionally takes into account contextual annotations on the enclosing property — closer to what happens for normal nested POJO fields. If you're working off a buffered
|
Uh oh!
There was an error while loading. Please reload this page.
Hi,
Subject: Jackson 3 Migration: Accessing the configured mapper from a custom StdDeserializer
While migrating from Jackson 2.x to Jackson 3.x, we ran into a limitation that I am not sure how to handle.
We have a recursive polymorphic model where concrete block implementations are discovered dynamically through plugins. The core application does not know all possible block types at compile time.
With Jackson 2.x, we used a custom StdDeserializer to perform the dispatch. Inside the deserializer, we could retrieve the configured mapper via:
ObjectMapper mapper = (ObjectMapper) jsonParser.getCodec();And then use that mapper to deserialize nested plugin-provided blocks while preserving all registered modules and configuration.
In Jackson 3.x, this approach no longer seems possible. As a result, we do not have access to the fully configured mapper from within the deserializer.
The obvious alternative would be to inject the ObjectMapper into the deserializer, but that creates a circular dependency:
The ObjectMapper must be built with all modules.
The modules register the custom deserializers.
The deserializers would need a reference to the ObjectMapper.
Therefore, neither side can be constructed first without depending on the other.
Creating a new ObjectMapper inside the deserializer is not an option either because it would not contain the dynamically registered plugin modules and configuration.
What is the recommended Jackson 3 approach for this pattern? Is there a supported way to access the active mapper (or equivalent deserialization context) from within a custom deserializer, or is there a different mechanism that should be used for recursive plugin-based polymorphic deserialization?
All reactions