Exception when trying to @JsonSerialized a nested polymorphic field annotated with @JsonTypeInfo
See original GitHub issueI have this field that is annotated as @StoredAsJson
and it’s type is interface FilterValue
. In FilterValue
, I have @JsonTypeInfo
and @JsonSubTypes
configured properly, also having like classes StringValue
and NumberValue
implementing that interface.
The serialization works fine, but it’s failing to deserialize the values back from the database. When I try to fetch the object from the database with my DAO I get the following error:
com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (VALUE_STRING), expected START_ARRAY: need JSON Array to contain As.WRAPPER_ARRAY type information for class io.github.rscarvalho.rosettatest.data.FilterValue
at [Source: {"id":0,"name":"filter1","filterValue":"{\"@type\":\"StringValue\",\"value\":\"my value\"}"}; line: 1, column: 25] (through reference chain: io.github.rscarvalho.rosettatest.data.Filter["filterValue"])
I’ve set up a repository with a small test to reproduce the issue. You can see the original error message into the Travis build that failed the same way as my local tests.
Issue Analytics
- State:
- Created 7 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Jackson deserializing nested polymorphic type - Stack Overflow
Your definitions are messed up -- you are trying to use two type identifiers, type name AND class. This does not make any...
Read more >Using @JsonTypeInfo annotation to handle polymorphic types
In cases where polymorphic types are persisted to JSON, there's no way for Jackson ... Let's fix about exception by using this annotation:...
Read more >Serialization with Jackson - Documentation - Akka
When nested fields or collections are of polymorphic type the ... the type must be listed with @JsonTypeInfo and @JsonSubTypes annotations.
Read more >JSON serialization - Immutables
You can use @JsonProperty to customize JSON field names. ... in the corresponding type (directly annotated and all nested immutable values) or package....
Read more >Serialization for Nested Polymorphic Types with FasterXML ...
I have polymorphic types defined as below: import com.fasterxml.jackson.annotation._ @JsonTypeInfo(use = JsonTypeInfo.Id.NA…
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@ldriscoll when you configure
use = JsonTypeInfo.Id.NAME
on@JsonTypeInfo
it does only records the simple class name.From what I understand Jackson should be picking up which class to use based on the configured
@JsonSubTypes
Confirmed that this is fixed by 3.11.8, although some of the tests seemed to have logical errors. With this patch the tests pass: