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 ignored when serializing a plain list

See original GitHub issue

JsonTypeInfo seems to be ignored entirely if the serialized value is a list (and not embedded in another bean). This is present with the most recent 2.8.9.

Repro:

public class TypeSerializationTest {
  @JsonTypeInfo(
      use = JsonTypeInfo.Id.NAME,
      property = "type")
  @JsonSubTypes({
    @JsonSubTypes.Type(name = "sub1", value = Sub1.class),
    @JsonSubTypes.Type(name = "sub2", value = Sub2.class)})
  public static class Superclass {}
  
  public static class Sub1 extends Superclass {}
  public static class Sub2 extends Superclass {}
  
  class ListHolder {
    @JsonProperty
    ArrayList<Superclass> list;
  }

  @Test
  public void testRoundtripClassName() throws IOException {
    ArrayList<Superclass> list = new ArrayList<>(Arrays.asList(
        new Sub1(),
        new Sub2()));

    
    ListHolder foo = new ListHolder();
    foo.list = list;

    ObjectMapper mapper = new ObjectMapper();
    System.out.println(mapper.writeValueAsString(foo));
    System.out.println(mapper.writeValueAsString(foo.list));
  }
}

Produces this:

{"list":[{"type":"sub1"},{"type":"sub2"}]}
[{},{}]

I’d expect the second line to have the ‘type’ property information as well?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
cowtowncodercommented, Nov 8, 2017

Ok, this needs to go in FAQ. It gets asked on monthly basis. 😃

Yes, this is as expected: the only type that Jackson can see (due to Java Type Erasure) is ArrayList<?>. Since ? is about same as Object, and has no polymorphic type indicators, type is not listed. Same applies to all generic types, but not all Lists (or Maps etc).

There are couple of work-arounds here:

  1. Remove generic-ness by sub-typing, using:

    public class MyFooList extends ArrayList<Superclass> { }

  2. Specifying type information on write call:

    mapper.writerFor(new TypeReference<List<Superclass>>() { }) .writeValueAsString();

both of which work. But in general it may be best to simply avoid use of generic types as root value if possible.

0reactions
cowtowncodercommented, Nov 8, 2017

@dweiss No problem at all. I can see how this is surprising and sub-optimal. I just would like to make it easier to figure this out since it is quite intricate. And I may be one of few people who know it fully, having implemented the system… took a while and is partly complicated due to configurability of various aspect.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does Jackson polymorphic serialization not work in lists?
The parent class configures @JsonTypeInfo to use an existing property as the marker to break the ambiguity of the sub types
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 >
Jackson-js: Powerful JavaScript decorators to serialize ...
@JsonIgnoreProperties can be used as a class-level decorator that marks a property or a list of properties that will be ignored during serialization...
Read more >
Jackson Annotation Examples - Baeldung
@JsonIgnoreProperties is a class-level annotation that marks a property or a list of properties that Jackson will ignore. Let's look at a quick ......
Read more >
FasterXML/jackson-databind - Gitter
public class Wrapper { @JsonTypeInfo(...) Object value; }. and so you would construct non-generic Wrapper with polymorphic value to serialize, deserialize ...
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