Query on JsonTypeInfo.defaultImpl

37 views
Skip to first unread message

Paulomi Mukherjee

unread,
Jun 19, 2019, 1:07:52 PM6/19/19
to jackson-user


Parent.java has

 

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        defaultImpl = com.common.model.Parent.
class,
        include = JsonTypeInfo.As.EXISTING_PROPERTY,
        property =
"@class"
)
@JsonSubTypes({
        @JsonSubTypes.Type(value = com.common.model.Child.
class, name = "com.common.model.Child"),
        @JsonSubTypes.Type(value = com.common.model.Grandchild.
class, name = "com.common.model.Grandchild")
})

 

Child.java has

 

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        defaultImpl = com.common.model.Parent.
class,
        include = JsonTypeInfo.As.EXISTING_PROPERTY,
        property =
"@class"
)
@JsonSubTypes(@JsonSubTypes.Type(value = com.common.model.Grandchild.
class, name = "com.common.model.Grandchild"))

 

 

Note: in Child.java I mentioned defaultImpl as the Parent class as Child class is abstract. My aim is to create a parent object if @class is missing in the payload.

 

But when I try to create the child object, I get the below error:

JSON parse error: Missing type id when trying to resolve subtype of [simple type, class com.common.model.Child]: missing type id property '@class'; nested exception is com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Missing type id when trying to resolve subtype of [simple type, class com.common.model.Child]: missing type id property '@class'\n at [Source: (PushbackInputStream); line: 3, column: 1]

 

I tried giving defaultImpl = Object.class in JsonTypeInfo of Child class. Still I get the same missing type id property '@class' error.

 

So, my question is, is it mandatory for defaultImpl to be on the same class and any of the children class?

Can I not give any random or the parent class in defaultImpl ?

Spring boot version is 2.1.5.

 

pic.jpg

Tatu Saloranta

unread,
Jul 1, 2019, 4:44:22 PM7/1/19
to jackson-user
No --- actually it is recommended that child classes do NOT add
`@JsonTypeInfo` at all, because doing that may lead to impression that
it would be used -- and, worse, this can lead to incompatible behavior
between serialization (during which sub-class info IS used as
available from runtype value) and deserialization (during which only
base class information is usually available).

>
> Can I not give any random or the parent class in defaultImpl ?
>
> Spring boot version is 2.1.5.

No, you must use type that is compatible with expected result base type.
That is, something that is assingment-compatible: if field with
polymorphic value is declared to be a `BaseType`, default
implementation must be `BaseType` or another type that
extends/implements it.

Things get more confusing if different base type is specified during
serialization and deserialization: ideally same type would be used,
but since root value type handling is fundamentally asymmetric (when
serializing there is an instance available, with subtype class; on
deserialization base type is usually given).

-+ Tatu +-

>
>
>
> --
> 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 jackson-user...@googlegroups.com.
> To post to this group, send email to jackso...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/95b7a575-b70f-4420-8766-86f0db975c01%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages