@JsonTypeInfo annotation ObjectMapper equivalent

52 views
Skip to first unread message

Pierre

unread,
Jul 2, 2022, 6:05:53 PM7/2/22
to jackson-user
Hello,

What is the equivalent of @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY) in the ObjectMapper without using annotations (or mixins) ?

I'm looking for something like ObjectMapper().registerTypeInfo(MyBaseClass::class, Id.CLASS, As.PROPERTY)

Also, I don't want to activateDefaultTyping(), I just want typing for class I specified (like @JsonTypeInfo behavior)

Tatu Saloranta

unread,
Jul 2, 2022, 6:16:41 PM7/2/22
to jackso...@googlegroups.com
There is nothing else that I know of, specifically, although you can
probably achieve this by sub-classing `JacksonAnnotationIntrospector`
(or implementing `AnnotationIntrospector`), and defining/overriding
method that looks for `@JsonTypeInfo`.

But I think the `activateDefaultTyping()` route would make most sense:
you would need to specific custom applicability, but otherwise.

Also note that the use of `As.PROPERTY` is discouraged for polymorphic
handling: for types that do not serialize as JSON Objects it must use
`As.WRAPPER_ARRAY` logic anyway. So it is usually better to use either
`As.WRAPPER_ARRAY` or `As.WRAPPER_OBJECT`.
Technically you can of course try `As.PROPERTY` and perhaps it works
for your case; this is just a warning that it may be problematic.

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