question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JsonTypeInfo does not work instance based in collections

See original GitHub issue

Using:

@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:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
wetnebcommented, Jun 14, 2019

Here is a workaround to force Jackson to serialize the class name at all times:

@JsonTypeInfo(
        use=JsonTypeInfo.Id.CLASS,
        include=JsonTypeInfo.As.EXISTING_PROPERTY,
        property="class")
public interface MyInterface {
	
	@JsonProperty("class")
	public default String getClassName() {
		return this.getClass().getName();
	}
}

Note the use of EXISTING_PROPERTY to avoid adding duplicate "class" attributes if serializing the instance as a root object.

0reactions
cowtowncodercommented, Jul 23, 2016

@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, roughly Object. So information is simply not available when considering serializer for List 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 telling ObjectMapper/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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found