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 is ignored when serializing a list of annotated object

See original GitHub issue

It seems that JsonTypeInfo is somehow ignored when serializing a list of annotated object. The following is some piece of code describing the situation.

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "clazz")
public class ProjectElement extends BaseObject {
    private Long id = 10L;

    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
}

public abstract class AbstractTask extends ProjectElement {
}

public class Task extends AbstractTask {
}

The following two pieces of code produce the results shown:

Task aTask = new Task();
objectMapper.writeValueAsString(aTask)

Outputs: {“id”:10, “clazz”:“Task”}

Task aTask = new Task();
List aList = new ArrayList();
aList.add(aTask);
objectMapper.writeValueAsString(aList);

Outputs: [{“id”:10}]

“clazz” is not produced in the second code. why?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:22 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
cowtowncodercommented, Oct 28, 2013

Yes and no: this is due Java Type Erasure. It is missing because element type information is not available to Jackson; all it sees is List<?>, and from that base type (used for finding @JsonTypeInfo) is not available. Base type must be statically accessed, to use uniforma settings, unlike actual content serializer.

So question then is how to pass the extra information needed to detect the type. There are two main alternatives:

  1. Use a helper class like class TaskList extends ArrayList<Task> { } to “save” the type; if so, generic element type is available from super-class (one of oddities of type erasure)
  2. Construct ObjectWriter with specific type: mapper.writerWithType(listTypeConstructedViaTypeFactory).writeValueAsString()

Note that type erasure is only problematic for root values (value directly passed to ObjectMapper). Because of this, I recommend not using Lists or Maps (or any generic types) as root values. They can be made to work, but are more hassle.

2reactions
jgribonvaldcommented, Jun 6, 2016

I think I’m having the same problem, but how to solve it ? here are the details of my problem : http://stackoverflow.com/questions/37663404/jackson-xml-problems-on-serializing

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does Jackson polymorphic serialization not work in lists?
First, you can create a class that implements List<Cat> , instantiate it appropriately and serialize the instance.
Read more >
@JsonTypeInfo doesn't serialize property value in a List
I want to serialize value with `@JsonTypeName`. When serialize with `List`, the property value is not shown. public class Main {.
Read more >
Using @JsonTypeInfo annotation to handle polymorphic types
Jackson JSON - Using @JsonTypeInfo annotation to handle polymorphic types ... Let's serialize and then deserialize a View object:
Read more >
Package com.fasterxml.jackson.annotation - Javadoc.io
Definition of API used for constructing Object Identifiers (as annotated ... names for a property, accepted during deserialization as alternative to the ...
Read more >
Serialization with Jackson - Documentation - Akka
The deny list of possible serialization gadget classes defined by ... type must be listed with @JsonTypeInfo and @JsonSubTypes annotations.
Read more >

github_iconTop Related Medium Post

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