Thank you, Tatu, that makes a lot of sense.

So basically let me restate the issue; let's forget @JsonTypeInfo for the 
moment and assume it's not set on the classes.

Document - concrete class, has @JsonSerialize(using = 
JsonDocumentSerializer.class); @JsonDeserialize(using = 
JsonDocumentSerializer.class).
abstract Resource extends Document - no annotations at the moment
Message extends Resource (concrete class) - no annotations at the moment

What we want to be able to do is this:

1) Create an instance of Document, serialize it to JSON, then be able to 
deserialize:
Document doc = om.readValue(strJson, Document.class);

2) Create an instance of Message, serialize it to JSON, then be able to 
deserialize as follows:
// at this point, we don't know we've got a Message coming in; we only know 
it's a Document
Document doc = om.readValue(strJson, Document.class);
// cast later
Message message = (Message) doc;
or
if (doc instanceof Message) { ... } else { ... }

i.e. we can't do this immediately:
Message message = om.readValue(strJson, Message.class); 


In other words, we want to preserve the type information for the original 
subclass and carry it over the serialization, then deserialization.
We want to keep the serializer/deserializer defined on Document.  It sounds 
like we're missing the "defining our fully custom (de)serializer for 
Document, which handles any polymorphic needs".
I was thinking that JsonDocumentSerializer needs to, perhaps, override the 
serializeWithType method and do
    typeSer.writeTypePrefixForObject(value, gen);
    serialize(value, gen, serializers);
    typeSer.writeTypeSuffixForObject(value, gen);
Is that correct and would that include the class info as a property or as a 
wrapper object? Or perhaps this is to be coded differently?

Also then on the JsonDocumentDeserializer side, would we override 
deserializeWithType?.. and what would that need to do?  Right now, the 
deserializer is strictly plain implementation of JsonDeserializer<Document> 
with no additional logic for the polymorphic needs.

Thanks again for your help,
- Dmitry


On Wednesday, April 19, 2017 at 8:26:11 PM UTC-4, Tatu Saloranta wrote:
>
> What you try to do does not make sense to me because you try to do both: 
>
> 1. Specify that `Document` should use polymorphic handling (via 
> `@JsonTypeInfo`) 
> 2. Define exact serializer/deserializer to use 
>
> Either you should do (1), and let actual (de)serializers be created 
> for or defined by subtypes, or define your fully custom (de)serializer 
> for Document, which handles any polymorphic needs you have. 
> But trying to combine the two will not work. 
>
> Put another way: what are you trying to achieve here? From that it is 
> easier to see how to go about it -- at this point all I can see what 
> you are trying do but not really why. :) 
>
> -+ Tatu +- 
>
>
>
> On Wed, Apr 19, 2017 at 2:48 PM,  <[email protected] <javascript:>> 
> wrote: 
> > Hi, 
> > 
> > We have the following object hierarchy: 
> > 
> > - Document 
> > - abstract Resource extends Document 
> > - Message extends Resource 
> > 
> > We want to be able to serialize Message to JSON and then deserialize it 
> > without necessarily referencing Message.class, i.e. we want to go 
> > polymorphic: 
> > 
> > 1) Message msg = om.readValue(strJson, Message.class); 
> > and 
> > 2) Resource res = om.readValue(strJson, Resource.class); 
> > 
> > This is working: 
> > 3) Document doc = om.readValue(strJson, Document.class) but not the 
> other 
> > two ways (1 and 2). 
> > 
> > Currently, the serialization is specified as follows: 
> > 
> > Document has:  @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, 
> > include=As.WRAPPER_OBJECT) 
> > Document has:  @JsonSerialize(using = JsonDocumentSerializer.class) 
> > Document has:  @JsonDeserialize(using = JsonDocumentDeserializer.class) 
> > Resource initially had no specific annotations 
> > Next, tried Resource with:  @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, 
> > include=As.WRAPPER_OBJECT), also @JsonSubTypes({ @Type=Message.class) }) 
> > Message has @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, 
> > include=As.WRAPPER_OBJECT) 
> > The JsonDocumentSerializer extends JsonSerializer<Document> and has 
> custom 
> > logic for serializing a Document. 
> > The JsonDocumentDeserializer extends JsonDeserializer<Document> and has 
> > custom logic for deserializing a Document. 
> > We would very much like not to have @JsonSubTypes on Document (it's in a 
> > higher level module) 
> > 
> > Message seems to be getting serialized properly, with 
> > { 
> >     "com.myco.Message": 
> >         { "id": "id goes here" 
> > ........................................ 
> > } 
> > For that, we had to override the serializeWithType method in 
> > JsonDocumentSerializer. 
> > 
> > However, I can't get a Resource, or a Message returned from the 
> readValue 
> > method. 
> > 
> > We have tried: 
> > 
> > objectMapper.enableDefaultTyping() 
> > In JsonDocumentDeserializer: 
> > 
> > @Override 
> > public Object deserializeWithType(JsonParser jp, DeserializationContext 
> > ctxt, 
> >         TypeDeserializer typeDeserializer) 
> >     throws IOException, JsonProcessingException 
> > { 
> >     return typeDeserializer.deserializeTypedFromObject(jp, ctxt); 
> > } 
> > 
> > but none of that seems to have worked. 
> > 
> > Any other recommendations? 
> > Thanks, 
> > - Dmitry 
> > 
> > 
> > 
> > -- 
> > 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] <javascript:>. 
> > To post to this group, send email to [email protected] 
> <javascript:>. 
> > For more options, visit https://groups.google.com/d/optout. 
>

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