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]> 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]. > To post to this group, send email to [email protected]. > 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.
