On Wed, Apr 26, 2017 at 2:24 AM, Johan L <[email protected]> wrote:
> We're having problems figuring out how to configure jackson to match how
> we'd like to have things. We have a bunch of legacy java classes (that we
> cannot annotate) that we'd like de-serialize from json. We have something

This is the main case use case that mix-ins were designed for you know.
But assuming that won't work for some other reason, let's see.

> similar to the following structure.
> interface Animal { ... }
>
> interface Dog implements Animal { ... }
> class DogImpl implements Dog { ... }
>
> interface Cat implements Animal { ... }
> class CatImpl implements Cat { ... }
>
> class Zoo {
>   List<Animal> animals;
>   String zooName;
> }
> Preferably we'd like to see jackson understand and convert the following
> json
> {
>   "zooName": "Carl's Zoo",
>   "animals": [
>     { "@class": "com.acme.zoo.DogImpl", ... },
>     { "@class": "com.acme.zoo.DogImpl", ... },
>     { "@class": "com.acme.zoo.CatImpl", ... },
>   ]
> }
> But we also like to be able to send Lists as root objects, like:
> [
>   { "@class": "com.acme.zoo.DogImpl", ... },
>   { "@class": "com.acme.zoo.DogImpl", ... },
>   { "@class": "com.acme.zoo.CatImpl", ... },
> ]
>
> Our current approach is to have a SimpleModule that uses
> addAbstractTypeMapping between Dog/Cat and DogImpl/CatImpl. And
> addValueInstantiator to add custom instatiators for CatImpl/DogImpl, because
> we need to use factories to create the legacy classes.

That's not really what this facility is meant for: type information
should indicate type, and not be subject to further resolution.
It works well for non-polymorphic cases, but overlaps with PM
handling. So I would not expect that to work here.

> And we add type info with As.PROPERTY, but none of the different
> DefaultTyping seems to give us the desired  result. The whole thing is then
> hooked into Spring via a Converter to map jackson to expected target class
> (using objectMapper.readValue(.., targetClass)).

DefaulTyping really is just a way to indicate behavior similar to
`@JsonTypeInfo`, but applied sort of similar to how mix-ins work.
But inclusion done using matching.

> Is the above behavior something that jackson can be configured for? Or do we
> have to implement some custom things and hook into jackson, if so, what
> would that be? We're running jackson v. 2.5.1 at the moment (it was used in
> another part of the product).

Instead of trying to use class names -- which really are meant to be
1-to-1 physical matches, similar to JDK serialization -- it would seem
better to use Type Id (use = Id.NAME). You can then handle translation
between id and actual class in configurable manner (not necessarily
super easily, but you can register handlers).

But I think I am missing something on your use case: who is producing
the JSON? Why wouldn't class name included be the one to use? What are
you trying to change?

-+ Tatu +-

ps. Even if sticking to 2.5.x, you should definitely consider latest
patch version (2.5.5 I think, if not 2.5.6) -- patch versions are safe
upgrades and contain many bug fixes.

-- 
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 [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to