1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | { op: 'or', data: [ { op: 'lessEqual', data: [ { op: 'number', data: 'colC', isCol: true }, { op: 'number', data: 89 } ] }, { op: 'not', data: [ { op: 'endWith', data: [ { op: 'string', data: 'colD', isCol: true }, { op: 'string', data: 'xyz' } ] } ] } ] } |
1 | interface FilterExpression{} |
1 2 3 4 | class FilterExpressionList implements FilterExpression{ String op; List<FilterExpression> data; } |
1 2 3 4 5 | class FilterExpressionColOrValue implments FilterExpression{ String op; String data; String isCol; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | class FilterExpressionParser implements JsonDeserializer<FilterExpression> { public FilterExpression deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { FilterExpression expr = null; if (json == null) { expr = null; } else { JsonElement data = json.getAsJsonObject().get("data"); if (data.isJsonArray()) { FilterExpressionList exprList = new FilterExpressionList(); Type type = new TypeToken<List<FilterExpression>>() { }.getType(); exprList.data = context.deserialize(data, type); exprList.op = json.getAsJsonObject().get("op").getAsString(); expr = exprList; } else if (data.isJsonPrimitive()) { FilterExpressionColOrValue exprDetail = new FilterExpressionColOrValue(); exprDetail.data = json.getAsJsonObject().get("data") .getAsString().trim(); exprDetail.op = json.getAsJsonObject().get("op").getAsString(); if (json.getAsJsonObject().get("isCol") != null) { exprDetail.isCol = json.getAsJsonObject().get("isCol") .getAsBoolean(); } expr = exprDetail; } else { expr = null; } } return expr; } } |
1 2 3 4 5 6 7 | GsonBuilder gb = new GsonBuilder(); gb.registerTypeAdapter(FilterExpression.class, new FilterExpressionParser()); Gson gson = gb.create(); FilterExpressionList filter = (FilterExpressionList)gson.fromJson(inputJsonString, FilterExpression.class); |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |