Ich benutze Gson, um Java-Objekte aus JSON-Strings zu erstellen. Die json Struktur baut eine "Abfrage", die entwederGson, um eine rekursive JSON-Struktur zu analysieren
a) Regeln enthalten kann, durch eine Bedingung kombiniert (AND oder OR)
b) ein oder mehrere Abfragen wieder zusammengeführt, durch eine Bedingung (AND oder OR)
(a) ist einfach, aber wie kann man (b) implementieren?
hier kommt eine Probe des json:
{
"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"
}
]
}
]
}
und diese meine Java-Klasse, die die json:
public class QueryBuilder {
public String condition;
private List<Rule> rules;
private List<QueryBuilder> children; // <-- 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<Rule> getRules() {
return rules;
}
public void setRules(List<Rule> rules) {
this.rules = rules;
}
public List<QueryBuilder> getChildren() {
return children;
}
public void setChildren(List<QueryBuilder> children) {
this.children = children;
}
}