title: Jackson deserialize nulls to empty list with no access to entity
body: <p>What I am trying to achieve:</p>
  
  <p><strong>to deserialize null values to empty lists</strong> </p>
  
  <p><strong>Note</strong>: I cannot change/annotate the entities, because they do come from a jar.</p>
  
  <p>I have the following code:</p>
  
  <pre><code>import com.fasterxml.jackson.annotation.JsonInclude;
  import com.fasterxml.jackson.annotation.JsonSetter;
  import com.fasterxml.jackson.annotation.Nulls;
  import com.fasterxml.jackson.core.JsonParser;
  import com.fasterxml.jackson.core.JsonProcessingException;
  import com.fasterxml.jackson.core.type.TypeReference;
  import com.fasterxml.jackson.databind.*;
  import com.fasterxml.jackson.databind.module.SimpleModule;
  
  import java.io.IOException;
  import java.util.Collections;
  import java.util.List;
  
  public class MyJsonMapperList extends ObjectMapper{
  
  private static final MyJsonMapperList jsonMapper = new MyJsonMapperList();
  
  public MyJsonMapperList() {
      SimpleModule sm =  new SimpleModule();
      sm.addDeserializer(List.class, new NullToEmptyListDeserializer());
      super.registerModule(sm);
      // setSerializationInclusion(JsonInclude.Include.NON_NULL);
      // disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
  }
  
  private static class NullToEmptyListDeserializer extends JsonDeserializer&lt;List&gt; {
  
      @Override
      public List deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
          ObjectMapper mapper = new ObjectMapper();
          mapper.configOverride(List.class).setSetterInfo(JsonSetter.Value.forContentNulls(Nulls.AS_EMPTY));
          mapper.configOverride(List.class).setSetterInfo(JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY));
          mapper.configOverride(List.class).setSetterInfo(JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY, Nulls.AS_EMPTY));
          return mapper.readValue(jsonParser, new TypeReference&lt;List&gt;(){});
      }
  
      @Override
      public List getNullValue(DeserializationContext ctx) {
          return Collections.emptyList();
      }
  }
  
  public static &lt;T&gt; T fromJson(String json, Class&lt;T&gt; tClass) throws IOException {
      if (json == null) { return null; }
      return  jsonMapper.readerFor(tClass).readValue(json);
  
  }
  
  public static String toJson(Object bean) throws JsonProcessingException {
      if (bean == null) { return null; }
      return jsonMapper.writer().writeValueAsString(bean);
  }
  </code></pre>
  
  <p>The source for this code is from <a href="https://github.com/FasterXML/jackson-databind/issues/347" rel="nofollow noreferrer">here</a>.
  Did I interpret something incorrectly from the Kotlin code provided by <strong>shyiko</strong>?</p>
  
  <p>*Note I've tried also directly with <strong>setDeserializerModifier</strong> like:</p>
  
  <pre><code> public MyJsonMapperList () {
          SimpleModule sm = new SimpleModule();
          // sm.addDeserializer(CollectionType.class, new NullToEmptyListDeserializer ());
          sm.setDeserializerModifier(new BeanDeserializerModifier() {
              @Override
              public JsonDeserializer&lt;?&gt; modifyCollectionDeserializer(DeserializationConfig config, CollectionType type, BeanDescription beanDesc, JsonDeserializer&lt;?&gt; deserializer) {
                  deserializer = new JsonDeserializer&lt;List&lt;?&gt;&gt;() {
                      @Override
                      public List&lt;?&gt; deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
                          ObjectMapper mapper = new ObjectMapper();
                          return mapper.readValue(jsonParser, new TypeReference&lt;List&gt;(){});
                      }
                      @Override
                      public List getNullValue(DeserializationContext ctx) {
                          return Collections.emptyList();
                      }
                  };
                  return deserializer;
              }
          });
          super.registerModule(sm);
  //        setSerializationInclusion(JsonInclude.Include.NON_NULL);
  //        disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
  
      }
  </code></pre>
  
  <p>The error which I get:</p>
  
  <pre><code>java.lang.IllegalArgumentException: Cannot handle managed/back reference 'defaultReference': type: value deserializer of type MyJsonMapperList$NullToEmptyListDeserializer does not support them
  
      at com.fasterxml.jackson.databind.JsonDeserializer.findBackReference(JsonDeserializer.java:365)
      at com.fasterxml.jackson.databind.deser.BeanDeserializerBase._resolveManagedReferenceProperty(BeanDeserializerBase.java:764)
      at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.resolve(BeanDeserializerBase.java:474)
      at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:293)
      at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
      at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
      at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:477)
      at com.fasterxml.jackson.databind.ObjectReader._prefetchRootDeserializer(ObjectReader.java:1935)
      at com.fasterxml.jackson.databind.ObjectReader.&lt;init&gt;(ObjectReader.java:189)
      at com.fasterxml.jackson.databind.ObjectMapper._newReader(ObjectMapper.java:658)
      at com.fasterxml.jackson.databind.ObjectMapper.readerFor(ObjectMapper.java:3517)
      at ...MyJsonMapperList.fromJson(MyJsonMapperList.java:47)
  </code></pre>
  
  <p>Entities do not have any @JsonManagedReference and @JsonBackReference annotations (even if they had, could not modify them)</p>
  
  <p>Jackson dependency:</p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
          &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
          &lt;version&gt;2.9.0&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>What am I missing?</p>
  
  <p>Are there other possibilities which I ommitted?</p>
  
  <p><a href="https://stackoverflow.com/a/51442640/4791483">This answer</a> sounded promising, but you don't have access to objectMapperBuilder because of the protected access.</p>