title: Jackson: how to deserialize null elements in JSON array
body: <p><strong>EDIT:</strong> by request of some of you I'm posting my real data structures and classes. I failed in trying to make it easier for you to understand the problem.</p>
  
  <p>I have a problem in resolving with Jackson <code>null</code> JSON array elements, ending up with an exception that I'll report later in this post.</p>
  
  <p>I have a long-winded JSON string coming from a 3rd party provider:</p>
  
  <pre><code>[
    null,
    null,
    [
      {
        "t" : {
          "id" : 0,
          "it" : "Attuazione"
        },
        "c" : "DM.10.0.2",
        "l" : "Macro 2",
        "v0" : 0,
        "scid" : "1",
        "m" : "10",
        "sam" : 0,
        "v1" : 100,
        "smcm" : true,
        "w" : "OD.10.4",
        "st" : {
          "auto_formula" : {
            "ia" : "($x &gt; 0 ? 1 : 0)",
            "id" : "($x &gt; 0 ? 1 : 0)",
            "bit" : "($x &gt; 0 ? 1 : 0)",
            "oa" : "($x &gt; 0 ? 1 : 0)",
            "od" : "($x &gt; 0 ? 1 : 0)",
            "dm" : "($x &gt; 0 ? 1 : 0)"
          },
          "id" : 0,
          "css_rule" : "containerTypeOnOff",
          "default_measure_unit" : null,
          "it" : "On \/ Off",
          "widget" : {
            "id" : 0,
            "caption" : {
              "it" : "Etichetta di testo"
            }
          }
        }
      }
    ],
    null,
    null,
    null,
    null,
    null
  ]
  </code></pre>
  
  <p>My Java classes ensemble is the following.</p>
  
  <p>This is the outmost container:</p>
  
  <pre><code>public class TinyMap {
      ArrayList&lt;TinyMapModule&gt; modules;
  
      public TinyMap() { ...  }
  
      public ArrayList&lt;TinyMapModule&gt; getModules() { ... }
  
      /**
       * @param modules the modules to set
       */
      @SuppressWarnings("unchecked")
      public void setModules(ArrayList&lt;TinyMapModule&gt; modules) { ... }
  
      public void appendNewMacro(TinyMapModule m) { ... }
  
  }
  </code></pre>
  
  <p>And this is the first-level container:</p>
  
  <pre><code>public class TinyMapModule {
      private ArrayList&lt;TinyMapMacro&gt; macros;
  
      /**
       * C'tor 
       */
      public TinyMapModule() { ... }
  
      /**
       * @return ArrayList&lt;TinyMapMacro&gt; 
       */
      public ArrayList&lt;TinyMapMacro&gt; getMacros() { ... }
  
      /**
       * @param macros the macros to set
       */
      @SuppressWarnings("unchecked")
      public void setMacros(ArrayList&lt;TinyMapMacro&gt; macros) { ... }
  
  }
  </code></pre>
  
  <p>And this is the innermost level:</p>
  
  <pre><code>public class TinyMapMacro {
      private String c; 
      private String l;
      private String m;
      private MacroType t;
      private String w;
  
      private static Logger logger;
  
      static {
          logger = Logger.getLogger(TinyMapMacro.class);
          DOMConfigurator.configure("log4j.xml");
      }
  
      /**
       * C'tor 
       */
      public TinyMapMacro() { ... }
  
      public static class MacroType {
          private int id;
          private String it; // Italian
  
          public int getId() { ... }
          public void setId(int id) { ... }
          public String getIt() { ... }
          public void setIt(String it) { ... }
      }
  
      public String getC() { ... }
  
      public void setC(String c) { ... }
  
      public String getL() { ... }
  
      public void setL(String l) { ... }
  
      public String getM() { ... }
  
      public void setM(String m) { ... }
  
      public MacroType getT() { ... }
  
      public void setT(MacroType t) { ... }
  
      public String getW() { ... }
  
      public void setW(String w) { ... }
  
      public int getTypeId() { ... }
  
      public String getLocalizedTypeString() { ... }
  
  }
  </code></pre>
  
  <p>Finally, this is my deserialization code:</p>
  
  <pre><code>ObjectMapper mapper = new ObjectMapper();
  mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
  mapper.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
  ObjectReader reader = mapper.reader(TinyMapModule.class);
  try {
      logger.info("Instantiating ObjectMapper");
      TinyMap tm = new TinyMap();
      tm.buildTinyMap(reader.readValues(jsonString.replaceAll("null", "[{\"name\":\"\"}]")));
      for (int i = 0; i &lt; tm.getModules().size(); i++) {
          if (tm.getModules().get(i) == null) 
              continue;
          for (int j = 0; j &lt; tm.getModules().get(i).getMacros().size(); j++) {
              if (tm.getModules().get(i).getMacros().get(j) == null) 
                  continue;
              System.out.println(String.format("c: %s", tm.getModules().get(i).getMacros().get(i).getC()));
              System.out.println(String.format("l: %s", tm.getModules().get(i).getMacros().get(i).getL()));
              System.out.println(String.format("m: %s", tm.getModules().get(i).getMacros().get(i).getM()));
              System.out.println(String.format("t.id: %d", tm.getModules().get(i).getMacros().get(i).getTypeId()));
              System.out.println(String.format("t.it: %s", tm.getModules().get(i).getMacros().get(i).getLocalizedTypeString()));
              System.out.println(String.format("w: %s", tm.getModules().get(i).getMacros().get(i).getW()));
          }
      }
      logger.info("Execution terminated");
  } 
  ... 
  </code></pre>
  
  <p>In extreme synthesis, I tried to schematize the problem as follows:</p>
  
  <ol>
  <li><code>TinyMap</code> class has a modules member, which is an <code>ArrayList</code> of <code>TinyMapModule</code> objects</li>
  <li><code>TinyMapModule</code> class has a macros member, which is an <code>ArrayList</code> of <code>TinyMapMacro</code> objects</li>
  <li><code>TinyMapMacro</code> class maps objects with the only attributes I strictly need.</li>
  </ol>
  
  <p>Now, when I try to deserialize the above JSON string into my objects I get an exception telling me that JSON can't be deserialized:</p>
  
  <pre><code>Exception in thread "main" com.fasterxml.jackson.databind.RuntimeJsonMappingException: Can not deserialize instance of it.asystelsrl.hcsbridge.communication.TinyMapModule out of START_ARRAY token
   at [Source: java.io.StringReader@3c50507; line: 1, column: 2]
      at com.fasterxml.jackson.databind.MappingIterator.next(MappingIterator.java:122)
      at it.asystelsrl.hcsbridge.communication.TinyMap.buildTinyMap(TinyMap.java:47)
      at it.asystelsrl.hcsbridge.tests.TestTinyMap.main(TestTinyMap.java:69)
  Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of it.asystelsrl.hcsbridge.communication.TinyMapModule out of START_ARRAY token
   at [Source: java.io.StringReader@3c50507; line: 1, column: 2]
      at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
      at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:599)
      at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:593)
      at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromArray(BeanDeserializer.java:531)
      at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:141)
      at com.fasterxml.jackson.databind.MappingIterator.nextValue(MappingIterator.java:188)
      at com.fasterxml.jackson.databind.MappingIterator.next(MappingIterator.java:120)
      ... 2 more
  </code></pre>
  
  <p>I already read a lot about Jackson annotations, custom deserializers, etc. but the only solution I seem to come up with is to convert the <code>TinyMap.modules</code> member from <code>ArrayList&lt;TinyMapModules&gt;</code> to <code>ArrayList&lt;Object&gt;</code> and use a custom deserializer to handle NullType objects.</p>
  
  <p>By this choice (if feasible, because I have not tested it yet) I would lose semantic value of the <code>TinyMap.modules</code> member, and I wouldn't be forced to.</p>
  
  <p>Is there another way I can resolve the null elements issue?</p>
  
  <p>EDIT:</p>
  
  <p>It is important I can preserve ordinal position of non-null objects within this structure, so I should definitely at least replace null elements with something usable as a placeholder.</p>