On Sat, Dec 29, 2018 at 3:01 PM Mike Summers <[email protected]> wrote:
>
> I have a Spring Boot (2.1.1) app that is modeling ActivityStreams objects and 
> for the most part Jackson's (2.9.7) Polymorphic Deserialization works well.
>
>
> There are 'objects' in the JSON which are references (links) and not JSON 
> objects with type information. For instance
>
>
>  "actor":"https://some.actors.href/
>
>
> rather than
>
>
> "actor":{
>    "type":"Actor",
>    "name":"SomeActor"
>  }
>
>
> I've written custom deserializers and and placed them on the fields to deal 
> with this
>
>
> @JsonDeserialize (using = ActorOrLinkDeserializer.class)
> private Actor actor;
>
>
> However my ActorOrLinkDeserializer is instantiated but never called and 
> Jackson complains with
>
>
>  Missing type id when trying to resolve subtype of [simple type, class 
> org.w3.activity.streams.Actor]: missing type id property 'type' (for POJO 
> property 'actor')
>
>
>
> which is from the polymorphic deserializer.
>
>
> It appears that the polymorphic deserialization code takes precedence over my 
> local @JsonDeserialize annotation and I need a way to force my code to have 
> precedence when a @JsonDeserializer annotated field is encounted.
>
>
> I've tried using my own ObjectMapper with a SimpleModule and get the same 
> result.
>
>
> I'd appreciate pointers and suggestions.

I think what is probably missing here is that to support polymorphic
handling, both serializer and deserializer must do bit more than just
handle content since Type Ids are configurable (both by what is used
as id and by how that id is included in json).
So it is not enough to simply specify one `deserialize()` method in
custom deserializer.

If serialization is always as JSON String, this may be solved simply
by extending `StdScalarDeserializer` instead of `StdDeserializer`
(there is rarely any need to extend base `JsonDeserializer` directly),
as it defines `deserializeWithType()` properly knowing that
serialization is as JSON scalar type (that is, not as JSON Object or
Array).

Similar handling is needed on serializer side too, see
`StdScalarSerializer`. If json serialization requires different shape,
they can still work as an example of kind of work that is needed to
support handling of polymorphic type ids.

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 [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