title: Gson to parse a recursive json structure
body: <p>I am using gson to build Java objects from json strings. The json structure builds a "query" that can contain either </p>
  
  <p>a) rules, combined by a condition (AND or OR)</p>
  
  <p>b) one ore multiple queries again, combined by a condition (AND or OR)</p>
  
  <p>(a) is simple, but how can a implement (b)?</p>
  
  <p>here comes a sample of the json:</p>
  
  <pre><code>{
    "condition": "AND",
    "rules": [
      {
        "id": "amount",
        "field": "amount",
        "type": "double",
        "input": "text",
        "operator": "greater",
        "value": "80"
      },
      {
        "condition": "OR",
        "rules": [
          {
            "id": "text",
            "field": "text",
            "type": "string",
            "input": "text",
            "operator": "contains",
            "value": "rewe"
          },
          {
            "id": "text",
            "field": "text",
            "type": "string",
            "input": "text",
            "operator": "contains",
            "value": "aldi"
          }
        ]
      }
    ]
  }
  </code></pre>
  
  <p>and this my java class, representing the json:</p>
  
  <pre><code>public class QueryBuilder {
      public String condition;
      private List&lt;Rule&gt; rules;
      private List&lt;QueryBuilder&gt; children;  // &lt;-- should I do it like this?
  
      public static QueryBuilder buildFromJson(String json) {
  //      Gson g = new Gson();
          Gson g = new GsonBuilder()
                  //.registerTypeAdapter(Rule.class, new JsonCustomSerializer ())
                  .create(); 
  
          return g.fromJson(json, QueryBuilder.class);
      }
  
      public String toJson() {
          Gson g = new Gson();
          return g.toJson(this);
      }
  
      public String getCondition() {
          return condition;
      }
  
      public void setCondition(String condition) {
          this.condition = condition;
      }
  
      public List&lt;Rule&gt; getRules() {
          return rules;
      }
  
      public void setRules(List&lt;Rule&gt; rules) {
          this.rules = rules;
      }
  
  
  
      public List&lt;QueryBuilder&gt; getChildren() {
          return children;
      }
  
      public void setChildren(List&lt;QueryBuilder&gt; children) {
          this.children = children;
      }
  
  }
  </code></pre>