Dynamic deserialization without any parent/class interface based on a common field

17 views
Skip to first unread message

Matthieu Ghilain

unread,
Jul 1, 2019, 10:44:26 AM7/1/19
to jackson-user
Is it possible for jackson to deserialize an unknown payload based on some type mapping information provided via a field which is present in all the payloads?

Constraint: the payload should not have any parent class/interface.

I'd like to just do mapper.read(bytes, Object.class) and have jackson deserialize it to the right class based on a common property (like messageType).

Thx a lot for your help!

Tatu Saloranta

unread,
Jul 18, 2019, 1:39:52 AM7/18/19
to jackson-user
Yes, sort of: for structures like:

{ "messageType" : "typeIdentifier",
"value" : { .... }
}

and result of, say

public class Wrapper {
@JsonTypeInfo(use = Id.CLASS, // or possibly NAME but then need to
map them explicitly
property = "value"
include = As.EXTERNAL_PROPERTY
)
public value Object;
}

it is possible.

Alternatively use of so-called "default typing" can achieve it too,
but it can lead to security concerns (see
https://medium.com/@cowtowncoder/on-jackson-cves-dont-panic-here-is-what-you-need-to-know-54cd0d6e8062).

But what that does (via `ObjectMapper.enableDefaultTyping(...)`) is
equivalent to enabled `@JsonTypeInfo` on categories of types (classes)
without having to add actual annotation.

-+ Tatu +-
Reply all
Reply to author
Forward
0 new messages