On Tue, Apr 16, 2019 at 1:05 PM Chris C <yahoono...@gmail.com> wrote:

> I have a map of type
>
> Map<Key1, Map<Key2, Integer>>
>
>  that I want to serialize to json to store in as a field in a database
> record.
>
> Key1 and Key2 are my own classes with getters defined and the constructors
> annotated as JsonCreators.
>
> I have been able to process single level maps for the same purpose by
> configuring the type factory for the object mapper, but I am unsure how to
> go about it for nested maps with different keys.
>

There are 2 challenges:

1. Expressing `JavaType` to use for deserialization (serialization is
usually fine, although if you try to serialize Map as root value [Strongly
Discouraged practice!], then there too)
2. Allowing custom Map key types -- may need to register Key deserializers,
key serializers (common types like `String`, `Number` supported out of the
box)
     - note: Map keys must be scalar types; read from String, written as
Strings

First one is done using `TypeFactory`, either with TypeReference:

   JavaType mapRef = typeFactory.constructType(new TypeReference<Map<Key1,
Map<Key2, Integer>>>) {} ())

Or constructing it in two parts, something like

   JavaType mapRef = typeFactory.constructMapType(Map.class,
typeFactory.constructType(Key1.class),
       typeFactory.constructMapType(Map.class,
typeFactory.constructType(Key2.class),
typeFactory.constructType(Integer.class)));

Second part may be more involved, but if you have already handled
non-nested maps with custom types, you probably know how that works.

-+ 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+unsubscr...@googlegroups.com.
To post to this group, send email to jackson-user@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to