title: How to serialize nested ObjectId to String with Jackson?
body: <p>There are <a href="https://stackoverflow.com/questions/14389198/jackson-mapper-serialize-deserialize-objectid">many</a> <a href="https://stackoverflow.com/questions/24230526/how-to-serialize-objectid-to-json">questions</a> concerning conversion from <code>ObjectId</code> to <code>String</code> with jackson. All answers suggest either creating own <code>JsonSerializer&lt;ObjectId&gt;</code> or annotating the <code>ObjectId</code> field with <code>@JsonSerialize(using = ToStringSerializer.class)</code>.</p>
  
  <p>However, I have a map that <em>sometimes</em> contains <code>ObjectIds</code>, i.e.:</p>
  
  <pre><code>class Whatever {
    private Map&lt;String, Object&gt; parameters = new HashMap&lt;&gt;();
    Whatever() {
      parameters.put("tom", "Cat");
      parameters.put("jerry", new ObjectId());
    }
  }
  </code></pre>
  
  <p>I want jackson to convert it to:</p>
  
  <pre><code>{
    "parameters": {
      "tom": "cat",
      "jerry": "57076a6ed1c5d61930a238c5"
    }
  }
  </code></pre>
  
  <p>But I get:</p>
  
  <pre><code>{
    "parameters": {
      "tom": "cat",
      "jerry": {
        "date": 1460103790000,
        "machineIdentifier": 13747670,
        "processIdentifier": 6448,
        "counter": 10631365,
        "time": 1460103790000,
        "timestamp": 1460103790,
        "timeSecond": 1460103790
      }
    }
  }
  </code></pre>
  
  <p>I have registered the conversion (in Spring) with </p>
  
  <pre><code>public class WebappConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configureMessageConverters(List&lt;HttpMessageConverter&lt;?&gt;&gt; converters) {
      Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
      builder
          .serializerByType(ObjectId.class, new ToStringSerializer());
      MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(builder.build());
      converters.add(converter);
    }
  }
  </code></pre>
  
  <p>And the first-level <code>ObjectIds</code> are converted correctly. How to make jackson convert also the nested ones? Do I have to write custom converter for this map?</p>
  
  <p>Keep in mind that this <code>Map</code> can be nested multiple times (i.e. contain another maps). I just want to convert <code>ObjectId</code> to <code>String</code> whenever jackson sees it.</p>