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:
1. Document has: @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS,
include=As.WRAPPER_OBJECT)
2. Document has: @JsonSerialize(using = JsonDocumentSerializer.class)
3. Document has: @JsonDeserialize(using =
JsonDocumentDeserializer.class)
4. Resource initially had no specific annotations
5. Next, tried Resource with: @JsonTypeInfo(use =
JsonTypeInfo.Id.CLASS, include=As.WRAPPER_OBJECT), also @JsonSubTypes({
@Type=Message.class) })
6. Message has @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS,
include=As.WRAPPER_OBJECT)
7. The JsonDocumentSerializer extends JsonSerializer<Document> and has
custom logic for serializing a Document.
8. The JsonDocumentDeserializer extends JsonDeserializer<Document> and
has custom logic for deserializing a Document.
9. 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:
1. objectMapper.enableDefaultTyping()
2. In JsonDocumentDeserializer:
@Overridepublic 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.