title: Jackson's polymorphic serialization / deserialization AND custom serializer / deserializer
body: <p>I'va an OID interface that can be implemented by many concrete types:</p>
  
  <pre class="lang-java prettyprint-override"><code>public interface OID {
  }
  public abstract class OIDBase
             implements OID {
     private String _id;
  }
  public class MyOID 
       extends OIDBase {
      public MyOID() {
          // default no-args constructor
      }
  }
  public class MyOtherOID 
       extends OIDBase {
      public MyOtherOID() {
          // default no-args constructor
      }
  }
  </code></pre>
  
  <p>Now I've an object with two fields, one defined using the abstract interface type (OID) and the other defined using a concrete type (MyOID)</p>
  
  <pre class="lang-java prettyprint-override"><code>public class MyBean {
      private OID _absOid;
      private MyOID _concreteOid;
  
      public MyBean(final OID absOid,final MyOID concreteOid) {
          _absOid = absOid;
          _concreteOid = concreteOid;
      }
  }
  </code></pre>
  
  <p>I want to use jackson to serialize / deserialize the fields differently whether they're defined using the abstract interface type or the concrete type:</p>
  
  <pre class="lang-java prettyprint-override"><code>{
      "_absOid": {
                      "type" : "MyOtherOID",
                      "id" : "__an_id__"
                 },
      "_concreteOid" : "__another_id___"
  }
  </code></pre>
  
  <p>Beware that <code>_absOid</code> is serialized including type information (polymorphic serialization) and <code>_concreteOid</code> is serialized as text</p>
  
  <p>To do so, I've annotated the OID interface as:</p>
  
  <pre class="lang-java prettyprint-override"><code>@JsonSubTypes({
      @JsonSubTypes.Type(MyOID.class),
      @JsonSubTypes.Type(MyOtherOID.class)
  })
  public interface OID {
  }
  </code></pre>
  
  <p>and assigned a type id to each concrete types:</p>
  
  <pre class="lang-java prettyprint-override"><code>@JsonTypeName("MyOID")
  public class MyOID 
       extends OIDBase {
      ...
  }
  @JsonTypeName("MyOtherOID")
  public class MyOtherOID 
       extends OIDBase {
      ...
  }
  </code></pre>
  
  <p>finally the abstract-defined field at the container bean is annotated to make jackson include the type info:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class MyBean {
      @JsonTypeInfo(include = JsonTypeInfo.As.PROPERTY,
                    use = JsonTypeInfo.Id.NAME,
                    property = "type")
      private OID _absOid;
  
      private MyOID _concreteOid;
  }
  </code></pre>
  
  <p>so far, so good, BUT in order to serialize differently if the field is defined using the abstract type (OID) and if the field is defined using the concrete type (MyOID), I have to create a custom serializer:</p>
  
  <p>First annotate the concrete types to use the serializer / deserializer:</p>
  
  <pre class="lang-java prettyprint-override"><code>@JsonTypeName("MyOID")
  @JsonSerialize(using=OIDSerializer.class) @JsonDeserialize(using=OIDDeSerializer.class)
  public class MyOID 
       extends OIDBase {
      ...
  }
  @JsonTypeName("MyOtherOID")
  @JsonSerialize(using=OIDSerializer.class) @JsonDeserialize(using=OIDDeSerializer.class)
  public class MyOtherOID 
       extends OIDBase {
      ...
  }
  </code></pre>
  
  <p>... the serializer / deserializer code:</p>
  
  <pre class="lang-java prettyprint-override"><code>public static class OIDSerializer 
              extends JsonSerializer&lt;OID&gt; {
      @Override
      public void serialize(final OID value,
                            final JsonGenerator jgen,
                            final SerializerProvider provider) throws IOException,JsonProcessingException {
          // **** used when serializing a concrete type
          jgen.writeString(value.toString());
      }
      @Override
      public void serializeWithType(final OID value, 
                                    final JsonGenerator jgen,
                                    final SerializerProvider provider,
                                    final TypeSerializer typeSer) throws IOException {
          // **** used when serializing a polymorphic type
          // guess the type id
          WritableTypeId typeId = typeSer.typeId(value,JsonToken.START_OBJECT);
  
          // type prefix
          typeSer.writeTypePrefix(jgen,
                                  typeId);
  
          // object
          jgen.writeFieldName("id");
          jgen.writeString(value.toString());
  
          // type suffix 
          typeId.wrapperWritten = !jgen.canWriteTypeId();
          typeSer.writeTypeSuffix(jgen,
                                  typeId);
      }
  }
  </code></pre>
  
  <p>The <strong>PROBLEM</strong> arises <strong>when de-serializing</strong> the json string, I've used the following custom deserializer:</p>
  
  <pre class="lang-java prettyprint-override"><code>public static class OIDDeSerializer 
              extends StdDeserializer&lt;OID&gt; { 
  
      public MyOIDDeSerializer() { 
          super(MyOID.class); 
      } 
      @Override
      public OID deserialize(final JsonParser jp,
                             final DeserializationContext ctxt) throws IOException,JsonProcessingException {            
          JsonNode node = jp.getCodec().readTree(jp); 
  
          // [1] - Read the id depending on the serialized format
          String oidStr = null;           
          // text node &gt; concrete oid impl serialized as [value]
          if (node.getNodeType() == JsonNodeType.STRING) {
              oidStr = ((TextNode)node).asText();
          }
          // oid=value &gt; abstract oid impl serialized as {typeId=[type],oid={value]} 
          else {
              ObjectNode objNode = (ObjectNode)node;
              oidStr = objNode.findValue("id").asText();
          }
  
          // [2] - Read tye type id 
          String typeId = objNode.findValue("type").asText()
  
          // PROBLEM!!!!!!
          // how get the type from the typeId in order to create the concrete instance
          // how access the type resolver????
          Class&lt;? extends OID&gt; oidType = RESOLVE TYPE FROM THE ID
          return oidType.newInstance();
      }
  }
  </code></pre>
  
  <p>So the <strong>problem</strong> is <strong>how to access the type from the type id resolving</strong> at the custom <strong>deserializer</strong>???</p>