JsonTypeInfo does not work instance based in collections
See original GitHub issueUsing:
@JsonTypeInfo(
use = Id.NAME,
include = As.PROPERTY,
property = "_class"
)
On a Hibernate entity class (this could make a difference, as Hibernate tends to proxy objects), when serializing a single object, I correctly get the _class
field:
new ObjectMapper().writeValueAsString(base.getSubScopes().get(0));
// Returns: {"_class":"Institute","id":36}
But for the entire collection it returns the JSON without _class
data:
new ObjectMapper().writeValueAsString(base.getSubScopes());
// Returns: [{"id":36}]
The collection is of the Hibernate type PersistentBag
. If I wrap the samething in an Arraylist though, I get the same result:
new ObjectMapper().writeValueAsString(Lists.newArrayList(base.getSubScopes()));
// Returns: [{"id":36}]
I see this behaviour on both 2.7.x and 2.8.0 versions.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:7 (3 by maintainers)
Top Results From Across the Web
JsonTypeInfo not written for an object in a collection
You need to instruct Jackson that you need given collection with reading abstract type annotation. See example:
Read more >Type info is not serialized when a @JsonTypeInfo element is a ...
I saw https://github.com/FasterXML/jackson-databind/issues/336 and in ... will not be serialized when you serialize the collection (and same ...
Read more >Using @JsonTypeInfo annotation to handle polymorphic types
Jackson JSON - Using @JsonTypeInfo annotation to handle polymorphic types ... For non-collection types there is no difference.
Read more >Inheritance in Jackson | Baeldung
This tutorial will demonstrate how to handle inclusion of subtype metadata and ignoring properties inherited from superclasses with Jackson.
Read more >JsonTypeInfo (Jackson-annotations 2.5.0 API) - FasterXML
If POJO itself has a property with same name, value of property will be set with type id metadata: if no such property...
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 FreeTop 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
Top GitHub Comments
Here is a workaround to force Jackson to serialize the class name at all times:
Note the use of
EXISTING_PROPERTY
to avoid adding duplicate"class"
attributes if serializing the instance as a root object.@JWGmeligMeyling super-class annotations are considered, that is not the problem. Problem is that type information for root value (
List
) has element type of?
, that is, roughlyObject
. So information is simply not available when considering serializer forList
value; unless explicitly passed. This is how 2 work-arounds work: they make type information available for root values too, either by sub-classing (in which case generic types are available from class definition), or by tellingObjectMapper
/ObjectWriter
generic type.And yes, third work around would indeed be to have non-generic POJO as root value; in that case full type is available. That is probably the cleanest approach.