title: Mapping JSON to polymorphic POJOs with single- and multi-properties
body: <p>I am currently trying to develop a REST client that communicates with a certain online service. This online service returns some JSON responses which I wish to map to Java objects using <strong>Jackson</strong>.</p>
  
  <p>An example of a JSON response would be:</p>
  
  <pre><code>{
      "id" : 1,
      "fields" : [ {
        "type" : "anniversary",
        "value" : {
          "day" : 1,
          "month" : 1,
          "year" : 1970
        }
      }, {
        "type" : "birthday",
        "value" : {
          "day" : 1,
          "month" : 1,
          "year" : 1970
        }
      }, {
        "type" : "simple",
        "value" : "simple string"
      },{
         "type": "name",
         "value": {
             "firstName": "Joe",
             "lastName": "Brown"
         }
      } ]
  }
  </code></pre>
  
  <p>NOTE the following:</p>
  
  <ul>
  <li>this structure contains a simple id, and a collection of Field instances, each having a <em>type</em> and a <em>value</em></li>
  <li>the <em>value</em> structure is determined by the external property <em>type</em></li>
  <li>in the example given, there are 3 types -> date, name, and single value string</li>
  <li>the <em>birthday</em> and <em>anniversary</em> types both match the <em>date</em> structure</li>
  <li>there are several types which map to a single value string, like <em>email</em> type, <em>twitterId</em> type, <em>company</em> type etc.</li>
  </ul>
  
  <blockquote>
    <blockquote>
      <p>My problem is that I do not seem to be able to correctly map this structure to the Java objects</p>
    </blockquote>
  </blockquote>
  
  <p><strong>Here's what I've done so far. The following are the classes and their Jackson annotations</strong>(the getters and setters are omitted).</p>
  
  <pre><code>public class Contact {
      private int id;
      private List&lt;Field&gt; fields;
  }
  
  public class Field {
      private FieldType type;
      private FieldValue value;
  }
  
  public enum FieldType {
      EMAIL, NICKNAME, NAME, ADDRESS, BIRTHDAY, ANNIVERSARY
  }
  
  @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "type",
          defaultImpl = SingleFieldValue.class)
  @JsonSubTypes({
          @JsonSubTypes.Type(value = NameFieldValue.class, name = "name"),
          @JsonSubTypes.Type(value = DateFieldValue.class, name = "anniversary"),
          @JsonSubTypes.Type(value = DateFieldValue.class, name = "birthday"),
          @JsonSubTypes.Type(value = SingleFieldValue.class, name = "nickname"),
          @JsonSubTypes.Type(value = SingleFieldValue.class, name = "email"),
          //other types that map to SingleFieldValue
  })
  public abstract FieldValue {
  }
  
  
  public class NameFieldValue extends FieldValue {
  
      private String firstName;
      private String lastName;
   }
  
  public class DateFieldValue extends FieldValue {
  
      private int day;
      private int month;
      private int year;
  }
  
  public class SingleFieldValue extends FieldValue {
  
      private String value;
  }
  </code></pre>
  
  <p>The <strong>ObjectMapper</strong> does not contain any configuration, the default configuration is used.</p>
  
  <p>What suggestions do you have to correctly map these? I would like to avoid making custom deserializers and just traverse Json objects, like JsonNode.</p>
  
  <p><strong>NOTE</strong>: I apologize in advance for any lack of information to make this issue clear enough. Please state any problems with my formulation. </p>