title: How to deserialize a Map<?, ?> with Jackson?
body: <p>I'm trying to serialize/deserialize a <code>Map&lt;?, ?&gt;</code> with arbitrary object as keys with Jackson version 2.8. The JSON counterpart should be an array of couples, i.e. given</p>
  
  <pre><code>public class Foo {
      public String foo;
      public Foo(String foo) {
          this.foo = foo;
      }
  }
  
  public class Bar {
      public String bar;
      public Bar(String bar) {
          this.bar = bar;
      }
  }
  </code></pre>
  
  <p>then</p>
  
  <pre><code>Map&lt;Foo, Bar&gt; map;
  map.put(new Foo("foo1"), new Bar("bar1"));
  map.put(new Foo("foo2"), new Bar("bar2"));
  </code></pre>
  
  <p>should be represented by this JSON</p>
  
  <pre><code>[
      [ { "foo": "foo1" }, { "bar": "bar1" } ],
      [ { "foo": "foo2" }, { "bar": "bar2" } ]
  ]
  </code></pre>
  
  <p>So I did the serializer part as</p>
  
  <pre><code>public class MapToArraySerializer extends JsonSerializer&lt;Map&lt;?, ?&gt;&gt; {
  
      @Override
      public void serialize(Map&lt;?, ?&gt; value, JsonGenerator gen, SerializerProvider serializers)
          throws IOException, JsonProcessingException {
          gen.writeStartArray();
          for (Map.Entry&lt;?, ?&gt; entry : value.entrySet()) {
              gen.writeStartArray();
              gen.writeObject(entry.getKey());
              gen.writeObject(entry.getValue());
              gen.writeEndArray();
          }
          gen.writeEndArray();
      }
  
  }
  </code></pre>
  
  <p>but I have no idea how to write a <code>JsonDeserializer</code> to do the inverse job. Any suggestions?</p>
  
  <p>Note: I need the <code>[ [ "key1", "value1" ], [ "key2", "value2" ] ]</code> notation to be able to consume that JSON in JavaScript a <code>new Map( ... )</code> and <code>JSON.stringify(map)</code> would produce that notation too (see <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map" rel="nofollow">https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map</a>).</p>
  
  <p>To clarify, such a map would be a field of other classes, e.g.</p>
  
  <pre><code>public class Baz {
  
      @JsonSerialize(using = MapToArraySerializer.class)
      @JsonDeserialize(using = ArrayToMapDeserializer.class, keyAs = Foo.class, contentAs = Bar.class)
      Map&lt;Foo, Bar&gt; map;
  
  }
  </code></pre>
  
  <p>and <code>ArrayToMapDeserializer extends JsonDeserializer&lt;Map&lt;?, ?&gt;&gt;</code> is where I'm asking for help.</p>