title: JSON Jackson - exception when serializing a polymorphic class with custom serializer
body: <p>I'm currently migrating some code from Jackson 1.x to Jackson 2.5 json mapper and came a long a problem that wasn't there in 1.x.</p>
  
  <p>This is the setup (see code below):</p>
  
  <ul>
  <li>interface IPet</li>
  <li>class Dog implements IPet</li>
  <li>IPet is annotated with @JsonTypeInfo and @JsonSubTypes</li>
  <li>class Human has a property of type IPet that is annotated with @JsonSerialize(using=CustomPetSerializer.class) </li>
  </ul>
  
  <p><strong>The problem:</strong>
  If I serialize an instance of Dog it works as expected (also the type info is added to the json string by Jackson).
  However when I serialize an instance of the Human class an exception is thrown saying:</p>
  
  <blockquote>
    <p>com.fasterxml.jackson.databind.JsonMappingException: Type id handling
    not implemented for type com.pet.Dog (through reference chain:
    com.Human["pet"])</p>
  </blockquote>
  
  <p>The serialize(...) method of the CustomPetSerializer class is not invoked (tested using a breakpoint).</p>
  
  <p><strong>The code:</strong></p>
  
  <p>IPet implementation:</p>
  
  <pre><code>@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="type")
  @JsonSubTypes({
       @JsonSubTypes.Type(value=Dog.class,    name="dog")
      //,@JsonSubTypes.Type(value=Cat.class,  name="cat")
      //more subtypes here...
  })
  public interface IPet
  {
      public Long getId();
      public String getPetMakes();
  }
  </code></pre>
  
  <p>Dog implementation:</p>
  
  <pre><code>public class Dog implements IPet
  {
      @Override
      public String getPetMakes()
      {
          return "Wuff!";
      }
  
      @Override
      public Long getId()
      {
          return 777L;
      }
  }
  </code></pre>
  
  <p>Human who owns a dog:</p>
  
  <pre><code>public class Human
  {
      private IPet pet = new Dog();
  
      @JsonSerialize(using=CustomPetSerializer.class)
      public IPet getPet()
      {
          return pet;
      }
  }
  </code></pre>
  
  <p>CustomPetSerializer implementation:</p>
  
  <pre><code>public class CustomPetSerializer extends JsonSerializer&lt;IPet&gt;
  {
      @Override
      public void serialize(IPet value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException
      {
          if(value != null &amp;&amp; value.getId() != null)
          {
              Map&lt;String,Object&gt; style = new HashMap&lt;String,Object&gt;();
              style.put("age", "7");
              gen.writeObject(style);
          }
      }
  }
  </code></pre>
  
  <p>JUnit test method:</p>
  
  <pre><code>@Test
  public void testPet() throws JsonProcessingException
  {
      ObjectMapper mapper = new ObjectMapper();
  
      Human human = new Human();
  
      //works as expcected
      String json = mapper.writeValueAsString(human.getPet());
      Assert.assertNotNull(json);
      Assert.assertTrue(json.equals("{\"type\":\"dog\",\"id\":777,\"petMakes\":\"Wuff!\"}"));
  
      //throws exception: Type id handling not implemented for type com.pet.Dog (through reference chain: com.Human["pet"])
      json = mapper.writeValueAsString(human);    //exception is thrown here
      Assert.assertNotNull(json);
      Assert.assertTrue(json.contains("\"age\":\"7\""));
  }
  </code></pre>