title: How to convert Java class to Map<String, String> and convert non-string members to json using jackson?
body: <p>I have some class in Java that I want to convert to a <code>Map&lt;String, String&gt;</code>. The catch is that any fields of my java class that don't have an obvious <code>String</code> representation (collections, other classes) should be converted to json strings.</p>
  
  <p>Here's an example:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Data
  @AllArgsConstructor
  class MyClass {
      String field1;
      Long field2;
      Set&lt;String&gt; field3;
      OtherClass field4;
  }
  
  @Data
  @AllArgsConstructor
  class OtherClass {
      String field1;
      String field2;
  }
  
  ObjectMapper mapper = new ObjectMapper();
  MyClass myClass = new MyClass("value", 
                                123L, 
                                Sets.newHashSet("item1", "item2"),
                                new OtherClass("value1", "value2"));
  Map&lt;String, String&gt; converted =
          mapper.convertValue(myClass, new TypeReference&lt;Map&lt;String, String&gt;&gt;(){});
  </code></pre>
  
  <p>At this point, <code>converted</code> should look like the following:</p>
  
  <pre><code>"field1" -&gt; "value"
  "field2" -&gt; "123"
  "field3" -&gt; "[\"item1\", \"item2\"]"
  "field4" -&gt; "{\"field1\":\"value1\",\"field2\":\"value2\"}"
  </code></pre>
  
  <p>Instead, the call to <code>mapper.convertValue</code> fails when trying to deserizlize the Set with the exception <code>java.lang.IllegalArgumentException: Can not deserialize instance of java.lang.String out of START_ARRAY token</code>.</p>
  
  <p>Are there any special configurations I can annotate <code>MyClass</code> with or ways to configure the <code>ObjectMapper</code> to make this work the way I want it to?</p>