0

I am using Gson to parse JSON (generated by the jquery query builder) to a corresponding Java Object. My issue is that the attribute "value" can be either an array or a single value:

{
  "condition": "AND",
  "rules": [
    {
      "id": "date",
      "field": "date",
      "type": "date",
      "input": "text",
      "operator": "between",
      "value": [
        "01.01.2016",
        "20.01.2016"
      ]
    }
  ]
}

or

{
  "condition": "AND",
  "rules": [
    {
      "id": "date",
      "field": "date",
      "type": "date",
      "input": "text",
      "operator": "equal",
      "value": "01.01.2016"
    }
  ]
}

Apparently Gson can build the object for the first JSON if my class has a field like private String[] value and for the second, a field like private String value — but not both JSON inputs with either Java field option.

So my question: Is the a way to handle both, either a single "value" or an array of "values"?

pydsigner
  • 2,779
  • 1
  • 20
  • 33
tobi
  • 753
  • 1
  • 14
  • 25
  • If you have the option to edit the json that would probably be easier. I would modify the json such that it always contains an array. – bhspencer Apr 05 '16 at 20:33

1 Answers1

1

I don't know what is the structure of a class that you are using to deserialize this JSON, but I'd like to sugest that there is a class like this

public class Rule {

    private String id;
    private String field;
    private String type;
    private String input;
    private String operator;
    private List<String> value;

    // constructors, getters and setters
}

As you can see the value property is defined as a list of java.lang.String objects. This approach will help us to handle both cases: when value is an array and when it's a simple string value.

What can we do with the Gson? We can create custom deserializer like this

public class CustomSerializer implements JsonDeserializer<Rule> {
    public Rule deserialize(JsonElement jsonElement, Type type,
                             JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {

        JsonObject obj = (JsonObject) jsonElement;
        JsonElement value = obj.get("value");
        List<String> values = new ArrayList();
        if (value.isJsonArray()) {

            for (JsonElement jsonElement1 : value.getAsJsonArray()) {
                String str = jsonElement1.getAsString();
                values.add(str);
            }
        } else {
            values.add(value.getAsString());
        }
        // deserialize other properties


        Rule rule = new Rule();
        rule.setValue(values);
        // set other properties
        return rule;
    }
}

After that you need to register deserializer with

Gson gson = new GsonBuilder()
        .registerTypeAdapter(Rule.class, new CustomSerializer ())
        .create(); 

and you can deserialize JSON object like

{
  "id": "date",
  "field": "date",
  "type": "date",
  "input": "text",
  "operator": "equal",
  "value": "01.01.2016"
}

that is a part of your whole data structure.

Kamil
  • 2,712
  • 32
  • 39