How to register a custom deserializer for an entire type hierarchy

20 views
Skip to first unread message

Bojan Tomic

unread,
Dec 20, 2017, 2:05:01 PM12/20/17
to jackson-user
If I register a custom deserializer using simpleModule.addDeserializer(Map.class, ...) it gets invoked literally for Map only, not for its subtypes like a LinkedHashMap.
I understand the reasoning behind it, but I'm guessing there must be a way to have it triggered for the entire hierarchy. Yet, I can't seem to find anything. I looked into the Module, but it's very abstract and, unlike SimpleModule, has no mention of deserializers.
Can you please help?

Tatu Saloranta

unread,
Dec 20, 2017, 2:15:35 PM12/20/17
to jackson-user
Correct, `SimpleModule` uses `SimpleDeserializers` implementations
(`Deserializers` is the callback definition modules can register),
which does basic matching by
class name only.
In general that is how it has to be, since you can not assume that
subtypes can be handled by base type deserializer (things are bit
different for serializers).
It may be the case for specific cases that a general deserializer can
be used; this is just not guaranteed.
Also worth pointing out is that `Map`, `Collection` and array
deserializers would not work well with `SimpleDeserializers`
regardless of this -- any generic types
will need access to type (key, content) parameters, and these are not
passed by "simple" variants.

So if you want more flexible matching, you need to implement
`Deserializers` callback method(s) (in this case,
`findMapDeserializer()`) yourself, register that with `Module`.
You can have a look at `SimpleModule` to see how that works; it's not
complicated.
Note that callbacks vary between groups of types (Map vs Collection vs
Array and so on).

I hope this helps,

-+ Tatu +-
> --
> You received this message because you are subscribed to the Google Groups
> "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to jackson-user...@googlegroups.com.
> To post to this group, send email to jackso...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Bojan Tomic

unread,
Dec 21, 2017, 4:39:39 AM12/21/17
to jackson-user
Thanks for the advice! I did as you said and managed to achieve what I was after.

Tatu Saloranta

unread,
Dec 21, 2017, 2:06:57 PM12/21/17
to jackson-user
Good!

-+ Tatu +-

On Thu, Dec 21, 2017 at 1:39 AM, Bojan Tomic <veg...@gmail.com> wrote:
> Thanks for the advice! I did as you said and managed to achieve what I was after.
>
Reply all
Reply to author
Forward
0 new messages