title: JsonSerialize with HashMap
body: <p>I have following java code</p>
  
  <pre><code> @XmlRootElement
  public class PreferenceEntity  {
      String type, name;
      int id;
      Map&lt;String, String&gt; attributes = new HashMap&lt;String, String&gt;();
      List&lt;PreferenceEntity&gt; children;
      PreferenceEntity parent;
  
      public PreferenceEntity() {
          parent = null;
      }
  
      public PreferenceEntity(PreferenceEntity parent) {
          this.parent = parent;
      }
  
      @XmlAnyElement
      public List&lt;PreferenceEntity&gt; getChildren() {
          return children;
      }
      public void setChildren(List&lt;PreferenceEntity&gt; children) {
          this.children = children;
      }
  
      public String getType() {
          return type;
      }
      public void setType(String type) {
          this.type = type;
      }
  
      @JsonSerialize(contentUsing=SubjectAutocompleteSerializer.class)
      public Map&lt;String, String&gt; getAttributes() {
          return attributes;
      }
  
      @XmlTransient
      public PreferenceEntity getParent() {
          return parent;
      }
  
  
      public void setName(String name) {
          this.name = name;
      }
  
      public void setId(int id) { 
          if (id &gt;= 0) {
              this.id = id;
          }
      }
  
      // Internal class
      public static class SubjectAutocompleteSerializer extends JsonSerializer&lt;Map&lt;String, String&gt;&gt; {
  
          @Override
          public void serialize(Map&lt;String, String&gt; map, JsonGenerator jgen, SerializerProvider provider)
                  throws IOException, JsonProcessingException {
              jgen.writeStartObject();
              for (String key:map.keySet()) {
                  jgen.writeStringField(key, map.get(key));
              }
              //jgen.writeFieldName(value.getType());
              //jgen.writeStringField(value.getType(), value.getType());
              // jgen.writeStartObject();
              //jgen.writeObject(value);
              //jgen.writeStartObject("XMY");
              //jgen.writeStringField("value", value.getName());
              // jgen.writeEndObject();
              jgen.writeEndObject();
          }
  
      }
  }
  </code></pre>
  
  <p>Output is :</p>
  
  <pre><code>{
  -preferences : [
  -{
  -children : [
      -{
      children : null
      type : "A1"
      -attributes : {
      ColumnId : "86"
      }
      }
      -{
      children : null
      type : "A2"
      -attributes : {
      ColumnId : "87"
      }
      }
      -{
      children : null
      type : "A1"
      -attributes : {
      ColumnId : "382"
      Selected : "true"
      }
  }
  </code></pre>
  
  <p>The desired output is following:</p>
  
  <pre><code>{
  -preferences : [
  -{
      -children : [
      -{
          children : null
          type : "A1"
          ColumnId : "86"
      }
      -{
          children : null
          type : "A2"
          ColumnId : "87"
      }
      -{
          children : null
          type : "A1"
          ColumnId : "382"
          Selected : "true"
      }
  }
  </code></pre>
  
  <p>even better If I can get it:</p>
  
  <pre><code>{
  -preferences : [
  -{
      -A1 : [
      -{
          children : null
          type : "A1"
          ColumnId : "86"
      }
      -A2{
          children : null
          ColumnId : "87"
      }
      -A3{
          children : null
          ColumnId : "382"
          Selected : "true"
      }
  }
  </code></pre>
  
  <p>I tried using JsonSerialize but it seems not to be working with HashMap. Any pointers will be a great help.</p>
  
  <p>Regards
  WhiteLotus</p>