Gleeb
Gleeb

Reputation: 11289

Jackson polymorphic type conversion deletes the property after using it

I am trying to use Jackson to automatically parse my JSON payload to subtypes

All is working as intended and the object is being parsed to the right subtype. but the property used for discriminating is deleted at the end of the process.

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    property = "type")
@JsonSubTypes({
    @Type(value = MySubClass.class, name = "type1") })

In this case the property "type" is null in the MySubClass instance.

How do i tell jackson to leave the data intact.

Thanks.

Upvotes: 0

Views: 121

Answers (1)

StaxMan
StaxMan

Reputation: 116472

Yes; by default type information is considered to be metadata, and not data to expose to POJOs; similar to how Java type information is distinct from actual properties (albeit accessible via getClass()).

But you can expose type discriminator if you want to by using @JsonTypeInfo(visible=true).

Upvotes: 1

Related Questions