@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); }
public String getCacheString() { StringBuilder sb = new StringBuilder(); sb.append(name).append(":"); for (Map.Entry<String, String> tagEntry : tags.entries()) { sb.append(tagEntry.getKey()).append("="); sb.append(tagEntry.getValue()).append(":"); } return (sb.toString()); }
// Consider return map in future. private static List<String> fetchPartialKey(Map<String, String> map, String key) { List<String> keywords = Arrays.asList(key.split("\\{INDEX\\}")); List<String> listStr = new ArrayList<>(); for (Map.Entry<String, String> e : map.entrySet()) { String numStr = e.getKey().toUpperCase(); for (String keyword : keywords) { numStr = StringUtils.remove(numStr, keyword); } if (NumberUtils.isDigits(numStr)) { listStr.add(e.getValue()); } } return listStr; }
protected Object[] getArgumentsForConstraint( String objectName, String field, ConstraintDescriptor<?> descriptor) { List<Object> arguments = new LinkedList<>(); String[] codes = new String[] {objectName + Errors.NESTED_PATH_SEPARATOR + field, field}; arguments.add(new DefaultMessageSourceResolvable(codes, field)); // Using a TreeMap for alphabetical ordering of attribute names Map<String, Object> attributesToExpose = new TreeMap<>(); for (Map.Entry<String, Object> entry : descriptor.getAttributes().entrySet()) { String attributeName = entry.getKey(); Object attributeValue = entry.getValue(); if (!internalAnnotationAttributes.contains(attributeName)) { attributesToExpose.put(attributeName, attributeValue); } } arguments.addAll(attributesToExpose.values()); return arguments.toArray(new Object[arguments.size()]); }
private void deserializeProperties( String context, JsonObject jsonObject, String name, Object object) throws QueryException, BeanValidationException { Set<Map.Entry<String, JsonElement>> props = jsonObject.entrySet(); for (Map.Entry<String, JsonElement> prop : props) { String property = prop.getKey(); if (property.equals("name")) continue; PropertyDescriptor pd = null; try { pd = getPropertyDescriptor(object.getClass(), property); } catch (IntrospectionException e) { logger.error("Introspection error on " + object.getClass(), e); } if (pd == null) { String msg = "Property '" + property + "' was specified for object '" + name + "' but no matching setter was found on '" + object.getClass() + "'"; throw new QueryException(msg); } Class propClass = pd.getPropertyType(); Object propValue; try { propValue = m_gson.fromJson(prop.getValue(), propClass); validateObject(propValue, context + "." + property); } catch (ContextualJsonSyntaxException e) { throw new BeanValidationException( new SimpleConstraintViolation(e.getContext(), e.getMessage()), context); } catch (NumberFormatException e) { throw new BeanValidationException( new SimpleConstraintViolation(property, e.getMessage()), context); } Method method = pd.getWriteMethod(); if (method == null) { String msg = "Property '" + property + "' was specified for object '" + name + "' but no matching setter was found on '" + object.getClass().getName() + "'"; throw new QueryException(msg); } try { method.invoke(object, propValue); } catch (Exception e) { logger.error("Invocation error: ", e); String msg = "Call to " + object.getClass().getName() + ":" + method.getName() + " failed with message: " + e.getMessage(); throw new QueryException(msg); } } }