title: Custom JSON Deserialization in Jackson, exclude invalide array
body: <p>I receive data in JSON</p>
  
  <pre><code>{
  "status": "INVALID_DATA",
  "errors":{ "invalid_id": "Id isn't available",
             ...
             "wrong_address": "Address error msg"
           }
  }
  </code></pre>
  
  <p>Keys and their quantity in structure "errors" are unknown for me. I'm trying to map this with class</p>
  
  <pre><code>@JsonIgnoreProperties(ignoreUnknown = true)
  public class StatusErrors
  {
      private String status;
      private HashMap&lt;String, String&gt; errors = new HashMap&lt;String, String&gt;();
  
      public String getStatus() {
          return status;
      }
  
      public void setStatus(String status) {
          this.status = status;
      }
  
      public HashMap&lt;String, String&gt; getErrors() {
          return errors;
      }
  
      public void setErrors(HashMap&lt;String, String&gt; errors) {
          this.errors = errors;
      }
  }
  </code></pre>
  
  <p>It works fine if I have "errors", but when server say "OK" and has no errors it send me </p>
  
  <pre><code>{
  "status": "OK",
  "errors":[]
  }
  </code></pre>
  
  <p>(Don't ask me who write the server)
  So mapper crashes.
  I'm trying to write a custom JsonDeserializer (generic way)</p>
  
  <pre><code>public abstract class ExcludeEmptyArrayDeserializer&lt;T&gt; extends JsonDeserializer&lt;T&gt; {
  
      private final Class&lt;T&gt; clazz;
  
      protected ExcludeEmptyArrayDeserializer(Class&lt;T&gt; clazz) {
          this.clazz = clazz;
      }
  
      @Override
      public T deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
          ObjectCodec oc = jp.getCodec();
          JsonNode node = oc.readTree(jp);
  
          if(node.has("errors")) {
              if (node.get("errors").isArray() &amp;&amp; !node.get("errors").getElements().hasNext())
                  ((ObjectNode)node).remove("errors");
          }
  
          ObjectMapper objectMapper = new ObjectMapper();
          return objectMapper.readValue(node, clazz);   // doesn't work
          //return oc.treeToValue(node, clazz);   // doesn't work too
      }
  }
  
  public class StatusErrorsDeserializer extends ExcludeEmptyArrayDeserializer&lt;StatusErrors&gt; {
      public StatusErrorsDeserializer() {
          super(StatusErrors.class);
      }
  }
  </code></pre>
  
  <p>The result usage code has a view</p>
  
  <pre><code>    SimpleModule module = new SimpleModule("", Version.unknownVersion());
      module.addDeserializer(StatusErrors.class, new StatusErrorsDeserializer());
  
      ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, JsonAutoDetect.Visibility.ANY).withModule(module); 
      MappingJacksonHttpMessageConverter messageConverter = new MappingJacksonHttpMessageConverter();
      messageConverter.setObjectMapper(mapper);
  
      getRestTemplate().getMessageConverters().clear();
      getRestTemplate().getMessageConverters().add(messageConverter);
  </code></pre>
  
  <p>The "errors" node deleted correctly but this solution still doesn't work.
  I suppose I make a mistake in JsonDeserializer.deserialize method but don't get an idea.</p>
  
  <p>BTW StatusErrors class can be a base class for other complicated messages from server.</p>