title: (Jackson) How to deserialize JSON to POJO with polymorphism using another object's property?
body: <p>Let's say I have a json with 2 objects, "header" and "data". Both of them have equivalent models, only Data has variable attributes and Header properties are always the same, though. 
  The properties on "data" are variable and should be deserilized according to a field from "header", such as:</p>
  
  <pre><code>// this should be deserialized to SuperPojo
  {
    "header": {
      "type": "y" // this should be used as a rule for deserializing object "data"
    },
    "data": { // this should be deserialized into any Data subclass
      // variable data
    }
  }
  </code></pre>
  
  <p>These are the beans I want to deserialize the above JSON to:</p>
  
  <pre><code>// wrapper object
  @JsonIgnoreProperties(ignoreUnknown = true)
  public class SuperPojo&lt;T extends Data&gt; {
      private Header header;
      private T data;
      // getters &amp; setters
  }
  
  @JsonIgnoreProperties(ignoreUnknown = true)
  public class Header implements Serializable {
      private String type;
      // getters &amp; setters
  }
  
  // Marker class without fields. Used for inheritance and polymorphism purposes only
  // the issue here is that Jackson tries to find a field named "type"
  // within "Data" subclasses. What I want is for it to search the "type"
  // attribute in class "Header".
  @JsonTypeInfo(
      use = JsonTypeInfo.Id.CLASS,
      include = JsonTypeInfo.As.EXTERNAL_PROPERTY,
      property = "type")
  @JsonSubTypes({
      @JsonSubTypes.Type(value = DataX.class, name="x"), // how do I make this refer to field "type" in object "Header"?
      @JsonSubTypes.Type(value = DataY.class, name="y") 
  })
  public class Data implements Serializable { }
  
  // should be deserialized into my SuperPojo when header.type equals "x"
  @JsonTypeName("x")
  public class DataX extends Data {
      private long id;
      private String name;
      // getters &amp; setters
  }
  
  // should be deserialized into my SuperPojo when header.type equals "y"
  @JsonTypeName("y")
  public class DataY extends Data {
      @JsonProperty("registration_date")
      private LocalDateTime registrationDate;
      private Country country; // enum
      // getters &amp; setters
  }
  </code></pre>
  
  <p>Finally, this is how I'm trying to perform the deserialization:</p>
  
  <pre><code>// JSON X
  {
    "header": {
      "type": "x"
    },
    "data": {
      "id": "12345",
      "name":"Jackson"
    }
  }
  
  // JSON Y
  {
    "header": {
      "type": "y"
    },
    "data": {
      "registration_date": "2018-05-01T18:10:00",
      "country":"BRAZIL"
    }
  }
  
  @Service
  public class MyTest {
  
      @Autowired
      private ObjectMapper mapper;
  
      void doStuff() {
          String jsonX = getJsonX(); // JSON X
          String jsonY = getJsonY(); // JSON Y
          SuperPojo&lt;? extends Data&gt; bigPojoX = mapper.readValue(jsonX, new TypeReference&lt;SuperPojo&lt;? extends Data&gt;&gt;() { });
          SuperPojo&lt;? extends Data&gt; bigPojoY = mapper.readValue(jsonY, new TypeReference&lt;SuperPojo&lt;? extends Data&gt;&gt;() { });
          DataX subPojoX = (DataX) bigPojoX.getData(); // throws ClassCastException
          DataY subPojoY = (DataY) bigPojoY.getData(); // throws ClassCastException
      }
  }
  </code></pre>
  
  <p>What I'm doing right now, since I couldn't figure out a way to make this work is: I'm deserializing my json into a wrapper class with a single field of type "Header", then getting and checking the "type" from it and deserializing the json again into a subclass of the above mentioned wrapper class with an attribute of the correct Data implementation. </p>
  
  <p>I'm sure this isn't the best way to do this, nor a good design choice, but it was the only way I could solve this issue.</p>
  
  <p>I've already searched some possible solutions, but the problems were kind of different from mine, so they didn't work as well:</p>
  
  <ul>
  <li><p><a href="https://stackoverflow.com/questions/11798394/polymorphism-in-jackson-annotations-jsontypeinfo-usage?noredirect=1&amp;lq=1">Polymorphism in jackson annotations: @JsonTypeInfo usage</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/34079050/add-subtype-information-at-runtime-using-jackson-for-polymorphism?noredirect=1&amp;lq=1">Add SubType information at runtime using Jackson for polymorphism</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/15027628/cannot-with-deserialize-a-list-with-polymorphic-items-in-jackson?rq=1">Cannot with (de)serialize a List with polymorphic items in Jackson</a></p></li>
  <li><p><a href="http://www.baeldung.com/jackson-annotations" rel="nofollow noreferrer">http://www.baeldung.com/jackson-annotations</a></p></li>
  </ul>