title: Deserializing JSON with multiple types in one field
body: <p>I would like to deserialize JSON (with Jackson 1.9.11 and RestTemplate 1.0.1), in which one field may have more type meanings, for example:</p>
  
  <pre><code>    {"responseId":123,"response":"error"}
  </code></pre>
  
  <p>or</p>
  
  <pre><code>    {"responseId":123,"response":{"foo":"bar", ... }}
  </code></pre>
  
  <p>Either one or other case works correctly with one setter of specific type (String od custom Response class), but when I put into my entity bean overriden setter to be able to handle both cases, exception is thrown:</p>
  
  <pre><code>Caused by: org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [xxxx.templates.ExportResponse] and content type [application/json;charset=utf-8]
  </code></pre>
  
  <p>I was thinking about three solutions, but I did not get any of them working:</p>
  
  <ul>
  <li>using only String setter and inside use ObjectMapper to unmarshall that string, if it is not equal to "error", but when that JS Array comes, it's not string so no String setter is used :(.</li>
  <li>use polymorphic type handling (@JsonTypeInfo annotation) with own JsonDeserializer extension - I'm still trying to understand this and implement.</li>
  <li>create list of HttpMessageConverter and put inside all message converters, I can use. But I thing this step is unnecessary, because only MappingJacksonHttpMessageConverter is used, am I right?</li>
  </ul>
  
  <h1>EDIT: how it works now</h1>
  
  <p>Setter in entity bean:</p>
  
  <pre><code>@JsonDeserialize(using = ResponseDeserializer.class)
  public void setResponse(Object responseObject) {
      if(responseObject instanceof Response)
          response = (Response) responseObject;
  }
  </code></pre>
  
  <p>Deserialize method in ResponseDeserializer:</p>
  
  <pre><code>public Response deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException {
      Response response = new Response();
  
      if(JsonToken.START_OBJECT.equals(parser.getCurrentToken())) {
          ObjectMapper mapper = new ObjectMapper();
          response = mapper.readValue(parser, Response.class);
      } else
          throw new JsonMappingException("Unexpected token received.");
  
      return response;
  }
  </code></pre>