Simple(Des|S)erializers not a good fit for handling generic types: what is a good fit?

35 views
Skip to first unread message

gapag

unread,
Aug 31, 2023, 11:18:07 AM8/31/23
to jackson-user
Hi all

Time and again whenever I want to configure a mapper to do something more than reading/writing POJOS, I end up bumping here:


Where I read:


/**
 * Simple implementation {@link Serializers} which allows registration of
 * serializers based on raw (type erased class).
 * It can work well for basic bean and scalar type serializers, but is not
 * a good fit for handling generic types (like {@link Map}s and {@link Collection}s).
[...]
 */


Then I am puzzled because I have no idea on what a 'good fit' could be:

* I usually end up hammering a solution that does not look like a good fit (because it still uses SimpleSerializers & SimpleDeserializers)
* and it feels I am repeating work that might be already done in the library.

Could you give examples/pointers to e.g. create a module with  'good fits' to handle generic types, both serializing and deserializing them?

E.g. where should I look to define a module that 
* does not use `SimpleSerializers`, because...
* has custom serializer/deserializer for
     * `Collection<Integer>`
     * `Collection<BigDecimal>`
     * `Map<Integer, List<String>>`
* and uses defaults for all other types

Many thanks in advance.
-gapag

Gabriele Paganelli

unread,
Aug 31, 2023, 3:59:13 PM8/31/23
to jackso...@googlegroups.com

By module here I mean a Jackson module.

Tatu Saloranta

unread,
Sep 1, 2023, 11:47:36 PM9/1/23
to jackso...@googlegroups.com
Correct, `SimpleSerializers`/`-Deserializers` won't work for generic
types in general, and structured types specifically.

On implementations that do support these, `jackson-datatype-guava`
from https://github.com/FasterXML/jackson-datatypes-collections/
might be a good example: it supports some of Guava's Map etc types.

-+ 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 view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/CA%2BtOLyERexUhi79jfjtow3qRAYzjBQp%3D1uhxx9GndrQY9B%2BB2Q%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages