title: How to parse json with recursive lists of objects with Gson?
body: <p>I need to parse incoming json messages to Java Objects using Gson.
  The class "MessageBody" should be used to feed <strong>Gson.fromJson(json, MessageBody.class);</strong></p>
  
  <p>The json message looks as follows.</p>
  
  <p>There are three static fields on the first level. The third field ("fields") is a list of "DataField"-objects.</p>
  
  <p>A DataField object has a type and a value field.
  It's value can be heterogeneous. Expected types are: "String", "int", "boolean" and "HashMap&lt;String, DataField&gt;".</p>
  
  <pre><code>{
      "eventId": "abc",
      "customerId": "abc",
      "fields": {
          "eventDateTime": {
              "type": "datetime",
              "value": "2019-05-03T10:15:30Z"
          },
          "eventCorrelationID": {
              "type": "string",
              "value": "abc"
          },
          "additionalAttributes": {
              "type": "collection",
              "value": {
                  "additionalAttribute1": {
                      "value": "abc",
                      "type": "string"
                  },
                  "additionalAttribute2": {
                      "value": "abc",
                      "type": "string"
                  }
              }
          }
      }
  }
  </code></pre>
  
  <pre><code>public class MessageBody {
      private String eventId;
      private String customerId;
      private HashMap&lt;String, DataField&gt; fields;
  
      public String getEventId() {
          return eventId;
      }
  
      public void setEventId(String eventId) {
          this.eventId = eventId;
      }
  
      public String getCustomerId() {
          return customerId;
      }
  
      public void setCustomerId(String customerId) {
          this.customerId = customerId;
      }
  
      public HashMap&lt;String, DataField&gt; getFields() {
          return fields;
      }
  
      public void setFields(HashMap&lt;String, DataField&gt; fields) {
          this.fields = fields;
      }
  
      public class DataField {
          private Object value;
          private String type;
  
          public Object getValue() {
              return value;
          }
  
          public void setValue(Object value) {
              this.value = value;
          }
  
          public String getType() {
              return type;
          }
  
          public void setType(String type) {
              this.type = type;
          }
      }
  }
  </code></pre>
  
  <p>The parsing using the class above works for all objects except of the nested DataField list under "additionalAttributes".</p>
  
  <p>They result as a LinkedHashTreeMap object. Unfortunately it's not possible to cast it into another HashMap of DataFields.</p>
  
  <p>How is it possible to handle nested/recursive lists in heterogenous objects?</p>
  
  <p>How do I get the following statement to work? :-)</p>
  
  <pre><code>HashMap&lt;String, DataField&gt; addAttrs = (HashMap&lt;String, DataField&gt;) messageBody.getFields().get("additionalAttributes").getValue();
  </code></pre>