title: Why does Jackson polymorphic serialization not work in lists?
body: <p>Jackson is doing something truly bizarre and I cannot find any explanation for it. I'm doing polymorphic serialization and it works perfectly when an object is on its own. But if you put the same object into a list and serialize the list instead, it erases the type information. </p>
  
  <p>The fact that it's losing type info would lead one to suspect type erasure. But this is happening during serialization of the <em>contents</em> of the list; all Jackson has to do is inspect the current object it's serializing to determine its type.</p>
  
  <p>I've created an example using Jackson 2.5.1:</p>
  
  <pre><code>import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
  import com.fasterxml.jackson.annotation.JsonSubTypes;
  import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
  import com.fasterxml.jackson.annotation.JsonTypeInfo;
  import com.fasterxml.jackson.annotation.JsonTypeName;
  import com.fasterxml.jackson.core.JsonProcessingException;
  import com.fasterxml.jackson.databind.ObjectMapper;
  
  import java.util.ArrayList;
  import java.util.List;
  
  public class Test {
  
    @JsonIgnoreProperties(ignoreUnknown = true)
    @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY)
    @JsonSubTypes({
      @Type(value = Dog.class, name = "dog"),
      @Type(value = Cat.class, name = "cat")})
    public interface Animal {}
  
    @JsonTypeName("dog")
    public static class Dog implements Animal {
      private String name;
  
      public String getName() {
        return name;
      }
  
      public void setName(String name) {
        this.name = name;
      }
    }
  
    @JsonTypeName("cat")
    public static class Cat implements Animal {
      private String name;
  
      public String getName() {
        return name;
      }
  
      public void setName(String name) {
        this.name = name;
      }
    }
  
    public static void main(String[] args) throws JsonProcessingException {
      List&lt;Cat&gt; list = new ArrayList&lt;&gt;();
      list.add(new Cat());
      System.out.println(new ObjectMapper().writeValueAsString(list));
      System.out.println(new ObjectMapper().writeValueAsString(list.get(0)));
    }
  }
  </code></pre>
  
  <p>Here's the output:</p>
  
  <pre><code>[{"name":null}]
  {"@type":"cat","name":null}
  </code></pre>
  
  <p>As you can see, Jackson is not adding the type information when the object is in a list. Does anyone know why this is happening?</p>