title: Json dynamic deserialization with jackson
body: <p>I've already have a look at the question "<a href="https://stackoverflow.com/questions/12134231/jackson-dynamic-property-names">Jackson dynamic property names</a>" but it does not really answer to my question.</p>
  
  <p>I want to deserialize something like this : </p>
  
  <pre><code>public class Response&lt;T&gt; {
      private String status;
      private Error error;
      private T data;
  }
  </code></pre>
  
  <p><strong>but data can have different names</strong> since different services exist and return the same structure with some different data. For example 'user' and 'contract' :</p>
  
  <pre><code>{
    response: {
      status: "success",
      user: {
          ...
      }
    }
  }
  </code></pre>
  
  <p>or </p>
  
  <pre><code>{
    response: {
      status: "failure",
      error : {
          code : 212, 
          message : "Unable to retrieve contract"
      }
      contract: {
          ...
      }
    }
  }
  </code></pre>
  
  <p>I'd like genericize my responses objects like this :</p>
  
  <pre><code>public class UserResponse extends Response&lt;User&gt; {}
  </code></pre>
  
  <p>I've tried the following but i'm not sure it is my use case or if don't use it in the good way :</p>
  
  <pre><code> @JsonTypeInfo(include = As.WRAPPER_OBJECT, use = Id.CLASS)
   @JsonSubTypes({@Type(value = User.class, name = "user"),
                      @Type(value = Contract.class, name = "contract")})
  </code></pre>
  
  <p>Finally, i've created a custom Deserializer. It works but i'm not satisfied:</p>
  
  <pre><code>public class ResponseDeserializer extends JsonDeserializer&lt;Response&gt; {
  @Override
  public Response deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
      ObjectMapper mapper = new ObjectMapper();
      Response responseData = new Response();
      Object data = null;
  
      for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
          String propName = jp.getCurrentName();
          // Skip field name:
          jp.nextToken();
  
          if ("contract".equals(propName)) {
              data = mapper.readValue(jp, Contract.class);
          } else if ("user".equals(propName)) {
              data = mapper.readValue(jp, User.class);
          } else if ("status".equals(propName)) {
              responseData.setStatus(jp.getText());
          } else if ("error".equals(propName)) {
              responseData.setError(mapper.readValue(jp, com.ingdirect.dg.business.object.community.api.common.Error.class));
          }
      }
  
      if (data instanceof Contract) {
          Response&lt;Contract&gt; response = new Response&lt;Ranking&gt;(responseData);
          return response;
      }
  
      if (data instanceof User) {
          Response&lt;User&gt; response = new Response&lt;User&gt;(responseData);
          return response;
      }
  
      // in all other cases, the type is not yet managed, add it when needed
      throw new JsonParseException("Cannot parse this Response", jp.getCurrentLocation());
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Any idea to do this clean with annotations ? Thanks in advance !</p>