@Override public Metric deserialize( JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { JsonObject jsonObject = jsonElement.getAsJsonObject(); String name = null; if (jsonObject.get("name") != null) name = jsonObject.get("name").getAsString(); boolean exclude_tags = false; if (jsonObject.get("exclude_tags") != null) exclude_tags = jsonObject.get("exclude_tags").getAsBoolean(); TreeMultimap<String, String> tags = TreeMultimap.create(); JsonElement jeTags = jsonObject.get("tags"); if (jeTags != null) { JsonObject joTags = jeTags.getAsJsonObject(); int count = 0; for (Map.Entry<String, JsonElement> tagEntry : joTags.entrySet()) { String context = "tags[" + count + "]"; if (tagEntry.getKey().isEmpty()) throw new ContextualJsonSyntaxException(context, "name must not be empty"); if (tagEntry.getValue().isJsonArray()) { for (JsonElement element : tagEntry.getValue().getAsJsonArray()) { if (element.isJsonNull() || element.getAsString().isEmpty()) throw new ContextualJsonSyntaxException( context + "." + tagEntry.getKey(), "value must not be null or empty"); tags.put(tagEntry.getKey(), element.getAsString()); } } else { if (tagEntry.getValue().isJsonNull() || tagEntry.getValue().getAsString().isEmpty()) throw new ContextualJsonSyntaxException( context + "." + tagEntry.getKey(), "value must not be null or empty"); tags.put(tagEntry.getKey(), tagEntry.getValue().getAsString()); } count++; } } Metric ret = new Metric(name, exclude_tags, tags); JsonElement limit = jsonObject.get("limit"); if (limit != null) ret.setLimit(limit.getAsInt()); return (ret); }
private List<QueryMetric> parseQueryMetric(JsonObject obj, String contextPrefix) throws QueryException, BeanValidationException { List<QueryMetric> ret = new ArrayList<QueryMetric>(); Query query; try { query = m_gson.fromJson(obj, Query.class); validateObject(query); } catch (ContextualJsonSyntaxException e) { throw new BeanValidationException( new SimpleConstraintViolation(e.getContext(), e.getMessage()), "query"); } JsonArray metricsArray = obj.getAsJsonArray("metrics"); if (metricsArray == null) { throw new BeanValidationException( new SimpleConstraintViolation("metric[]", "must have a size of at least 1"), contextPrefix + "query"); } for (int I = 0; I < metricsArray.size(); I++) { String context = (!contextPrefix.isEmpty() ? contextPrefix + "." : contextPrefix) + "query.metric[" + I + "]"; try { Metric metric = m_gson.fromJson(metricsArray.get(I), Metric.class); validateObject(metric, context); long startTime = getStartTime(query, context); QueryMetric queryMetric = new QueryMetric(startTime, query.getCacheTime(), metric.getName()); queryMetric.setExcludeTags(metric.isExcludeTags()); queryMetric.setLimit(metric.getLimit()); long endTime = getEndTime(query); if (endTime > -1) queryMetric.setEndTime(endTime); if (queryMetric.getEndTime() < startTime) throw new BeanValidationException( new SimpleConstraintViolation("end_time", "must be greater than the start time"), context); queryMetric.setCacheString(query.getCacheString() + metric.getCacheString()); JsonObject jsMetric = metricsArray.get(I).getAsJsonObject(); JsonElement group_by = jsMetric.get("group_by"); if (group_by != null) { JsonArray groupBys = group_by.getAsJsonArray(); parseGroupBy(context, queryMetric, groupBys); } JsonElement aggregators = jsMetric.get("aggregators"); if (aggregators != null) { JsonArray asJsonArray = aggregators.getAsJsonArray(); if (asJsonArray.size() > 0) parseAggregators(context, queryMetric, asJsonArray, query.getTimeZone()); } JsonElement plugins = jsMetric.get("plugins"); if (plugins != null) { JsonArray pluginArray = plugins.getAsJsonArray(); if (pluginArray.size() > 0) parsePlugins(context, queryMetric, pluginArray); } JsonElement order = jsMetric.get("order"); if (order != null) queryMetric.setOrder(Order.fromString(order.getAsString(), context)); queryMetric.setTags(metric.getTags()); ret.add(queryMetric); } catch (ContextualJsonSyntaxException e) { throw new BeanValidationException( new SimpleConstraintViolation(e.getContext(), e.getMessage()), context); } } return (ret); }