I think what is needed here is a full class definition, expected json to match.
Depending on that, 2 main choices are:
1. Use `@JsonTypeInfo(include = As.WRAPPER_OBJECT)`, and then value
will be wrapped in JSON Object field name that is used as type id
2. Such usage is not supported and you will have to change usage or
write custom deserializer(s)
Former case would work like:
class Wrapper {
@JsonTypeInfo(include = As.WRAPPER_OBJECT, ....)
public Object value
}
to read/write json like
{ "value" :
{ "[TYPE-ID]" :
{ <actual properties of value object> }
}
}
-+ Tatu +-
On Mon, Feb 18, 2019 at 1:27 PM John Green <[email protected]> wrote:
>
> I have an attribute that may be one of 2 types and an abstract class for
> common things.
>
> I have another class that references this class as it's abstract type.
>
> The payload I am de-serializing uses name of the attribute to discriminate
> the type:
> I..E. it may be
> item : { stockProduct: {data..},lineNumber:2 }
> or
> item : { customProduct: {data..},lineNumber:2 }
> I cannot add a type attribute to the payload:
> Tried using a TypeResolver but could not get it access to the value name
> I want to use a Mapper to deSerialize the data elements, so...
>
> ObjectMapper mapper = new ObjectMapper();
>
> public JacksonProductDeserializer() {
> this(null);
> }
>
> public JacksonProductDeserializer(Class<?> vc) {
> super(vc);
> }
>
> @Override
> public ProductDto deserialize(JsonParser jp, DeserializationContext ctx)
> throws IOException, JsonProcessingException {
> String currentName = jp.getCurrentName();
> mapper.readValue(jp, StockProductDto.class);
> ProductDto dto;
> if ("stockProduct".equals(currentName)) {
> dto = mapper.readValue(jp, StockProductDto.class);
> return dto;
> }
>
> Etc...
>
>
> This works, but the parser apparently skips up when processing and tries to
> assign lineNumber to the StockProduct when it should be assigned to the Item.
>
> com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:
> Unrecognized field "lineNumber" (class mycorp.common.StockProductDto), not
> marked as ignorable (2 known properties: "id", "code"])
> at [Source: (String)"{
> "quantity": "1",
> "price": {
> "value": 1.99
> },
> "stockProduct": {
> "code": "2LDCOKE"
> },
> "lineNumber": "3"
> }"; line: 9, column: 18] (through reference chain:
> mycorp.events.common.OrderLineDto["stockProduct"]->
> mycorp.events.common.StockProductDto["lineNumber"])
>
> This seems like something that should be easy and I'm just missing something,
> any help would be appreciated.
>
>
> --
> 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.