title: Deserialization of map with integer keys using Jackson
body: <p>I have to serialize a simple integer-to-string map as JSON and then read it back. 
  The serialization is pretty simple, however since JSON keys must be strings the resulting JSON looks like:</p>
  
  <pre><code>{
    "123" : "hello",
    "456" : "bye",
  }
  </code></pre>
  
  <p>When I read it using code like:</p>
  
  <pre><code>new ObjectMapper().readValue(json, Map.class)
  </code></pre>
  
  <p>I get <code>Map&lt;String, String&gt;</code> instead of <code>Map&lt;Integer, String&gt;</code> that I need. </p>
  
  <p>I tried to add key deserializer as following:</p>
  
  <pre><code>    Map&lt;Integer, String&gt; map1 = new HashMap&lt;&gt;();
      map1.put(1, "foo");
      map1.put(2, "bar");
  
  
      ObjectMapper mapper = new ObjectMapper();
      SimpleModule module = new SimpleModule();
      module.addKeyDeserializer(Integer.class, new KeyDeserializer() {
          @Override
          public Object deserializeKey(String key, DeserializationContext ctxt) throws IOException, JsonProcessingException {
              System.out.println("deserialize " + key);
              return Integer.parseInt(key);
          }
      });
  
      mapper.registerModule(module);
      String json = mapper.writeValueAsString(map1);
  
  
      Map map2 = mapper.readValue(json, Map.class);
      System.out.println(map2);
      System.out.println(map2.keySet().iterator().next().getClass());
  </code></pre>
  
  <p>Unfortunately my key deserialzier is never called and <code>map2</code> is in fact <code>Map&lt;String, String&gt;</code>, so my example prints:</p>
  
  <pre><code>{1=foo, 2=bar}
  class java.lang.String
  </code></pre>
  
  <p>What am I doing wrong and how to fix the problem?</p>