List of usage examples for com.google.gson JsonElement getAsString
public String getAsString()
From source file:com.haulmont.restapi.service.filter.RestFilterParser.java
License:Apache License
protected RestFilterGroupCondition parseGroupCondition(JsonObject conditionJsonObject, MetaClass metaClass) throws RestFilterParseException { JsonElement group = conditionJsonObject.get("group"); String groupName = group.getAsString(); RestFilterGroupCondition.Type type; try {/*from ww w .ja va 2 s .c o m*/ type = RestFilterGroupCondition.Type.valueOf(groupName.toUpperCase()); } catch (IllegalArgumentException e) { throw new RestFilterParseException("Invalid conditions group type: " + groupName); } RestFilterGroupCondition groupCondition = new RestFilterGroupCondition(); groupCondition.setType(type); JsonElement conditions = conditionJsonObject.get("conditions"); if (conditions != null) { for (JsonElement conditionElement : conditions.getAsJsonArray()) { RestFilterCondition childCondition = parseConditionObject(conditionElement.getAsJsonObject(), metaClass); groupCondition.getConditions().add(childCondition); } } return groupCondition; }
From source file:com.haulmont.restapi.service.filter.RestFilterParser.java
License:Apache License
protected RestFilterPropertyCondition parsePropertyCondition(JsonObject conditionJsonObject, MetaClass metaClass) throws RestFilterParseException { RestFilterPropertyCondition condition = new RestFilterPropertyCondition(); JsonElement propertyJsonElem = conditionJsonObject.get("property"); if (propertyJsonElem == null) { throw new RestFilterParseException("Field 'property' is not defined for filter condition"); }//from ww w. ja v a 2 s .com String propertyName = propertyJsonElem.getAsString(); JsonElement operatorJsonElem = conditionJsonObject.get("operator"); if (operatorJsonElem == null) { throw new RestFilterParseException("Field 'operator' is not defined for filter condition"); } String operator = operatorJsonElem.getAsString(); Op op = findOperator(operator); boolean isValueRequired = op != Op.NOT_EMPTY; JsonElement valueJsonElem = conditionJsonObject.get("value"); if (valueJsonElem == null && isValueRequired) { throw new RestFilterParseException("Field 'value' is not defined for filter condition"); } MetaPropertyPath propertyPath = metaClass.getPropertyPath(propertyName); if (propertyPath == null) { throw new RestFilterParseException( "Property for " + metaClass.getName() + " not found: " + propertyName); } MetaProperty metaProperty = propertyPath.getMetaProperty(); EnumSet<Op> opsAvailableForJavaType = opManager.availableOps(metaProperty.getJavaType()); if (!opsAvailableForJavaType.contains(op)) { throw new RestFilterParseException("Operator " + operator + " is not available for java type " + metaProperty.getJavaType().getCanonicalName()); } if (metaProperty.getRange().isClass()) { if (Entity.class.isAssignableFrom(metaProperty.getJavaType())) { MetaClass _metaClass = metadata.getClass(metaProperty.getJavaType()); MetaProperty primaryKeyProperty = metadata.getTools().getPrimaryKeyProperty(_metaClass); String pkName = primaryKeyProperty.getName(); propertyName += "." + pkName; propertyPath = metaClass.getPropertyPath(propertyName); if (propertyPath == null) { throw new RestFilterParseException( "Property " + propertyName + " for " + metaClass.getName() + " not found"); } metaProperty = propertyPath.getMetaProperty(); } } if (isValueRequired) { Object value = null; if (op == Op.IN || op == Op.NOT_IN) { if (!valueJsonElem.isJsonArray()) { throw new RestFilterParseException( "JSON array was expected as a value for condition with operator " + operator); } List<Object> parsedArrayValues = new ArrayList<>(); for (JsonElement arrayItemElem : valueJsonElem.getAsJsonArray()) { parsedArrayValues.add(parseValue(metaProperty, arrayItemElem.getAsString())); } value = parsedArrayValues; } else { value = parseValue(metaProperty, valueJsonElem.getAsString()); } condition.setValue(transformValue(value, op)); condition.setQueryParamName(generateQueryParamName()); } condition.setPropertyName(propertyName); condition.setOperator(op); return condition; }
From source file:com.haulmont.restapi.service.RestServiceInvoker.java
License:Apache License
private void parseParamsJson(String paramsJson, List<String> paramValuesStr, List<Class> paramTypes) { if (Strings.isNullOrEmpty(paramsJson)) return;//www . java2s . c o m JsonParser jsonParser = new JsonParser(); JsonObject jsonObject = jsonParser.parse(paramsJson).getAsJsonObject(); int idx = 0; while (true) { JsonElement nthParam = jsonObject.get("param" + idx); if (nthParam == null) break; if (nthParam.isJsonPrimitive()) { paramValuesStr.add(nthParam.getAsString()); } else { paramValuesStr.add(nthParam.toString()); } JsonElement nthParamType = jsonObject.get("param" + idx + "_type"); if (nthParamType != null) { try { paramTypes.add(ClassUtils.forName(nthParamType.getAsString(), null)); } catch (ClassNotFoundException e) { throw new RestAPIException("Error on evaluating parameter type", e.getMessage(), HttpStatus.BAD_REQUEST, e); } } idx++; } }
From source file:com.hazelcast.qasonar.utils.WhiteListBuilder.java
License:Open Source License
private static void populateWhiteList(WhiteList whiteList, JsonArray array) { for (JsonElement element : array) { JsonObject entry = element.getAsJsonObject(); String type = entry.get("type").getAsString(); String value = entry.get("value").getAsString(); JsonElement justificationElement = entry.get("justification"); String justification = (justificationElement == null) ? null : justificationElement.getAsString(); JsonElement commentElement = entry.get("comment"); String comment = (commentElement == null) ? null : commentElement.getAsString(); if ((justification == null) == (comment == null)) { throw new IllegalArgumentException("Whitelist has to contain a justification or a comment!"); }/* www .j av a 2 s .c o m*/ whiteList.addEntry(type, value, justification, comment); } }
From source file:com.heroiclabs.sdk.android.util.json.MessageJsonDeserializer.java
License:Apache License
/** {@inheritDoc} */ @Override//from w w w . j av a 2 s .com public Message deserialize(final @NonNull JsonElement json, final @NonNull Type typeOfT, final @NonNull JsonDeserializationContext context) throws JsonParseException { final JsonObject object = json.getAsJsonObject(); final String messageId = object.get("message_id").getAsString(); final List<String> tags; if (object.get("tags").isJsonNull()) { tags = null; } else { tags = new ArrayList<>(); for (final JsonElement tag : object.getAsJsonArray("tags")) { tags.add(tag.getAsString()); } } final String subject = object.get("subject").getAsString(); final long createdAt = object.get("created_at").getAsLong(); final long expiresAt = object.get("expires_at").getAsLong(); final Long readAt = JsonDeserializerUtils.extractAsLong(object, "read_at"); final String body = JsonDeserializerUtils.extractAsString(object, "body"); return new Message(messageId, tags, subject, createdAt, expiresAt, readAt, body, codec); }
From source file:com.hi3project.dandelion.gip.codec.json.JSONGIPCodec.java
License:Open Source License
private EventType decodeType(JsonElement typeValue) throws GIPEventCodeDecodeErrorException { try {/*from w ww .j a v a2s. c om*/ String type = typeValue.getAsString(); return EventType.valueOf(type); } catch (IllegalArgumentException ex) { throw new GIPEventCodeDecodeErrorException(ex); } catch (UnsupportedOperationException ex) { throw new GIPEventCodeDecodeErrorException(ex); } }
From source file:com.hisign.sso.common.gson.CustDateTypeAdapter.java
License:Apache License
/** * @param json/*from w w w . j av a2s.co m*/ * @return */ private Date deserializeToDate(JsonElement json) { try { String dateStr = json.getAsString(); if (dateStr.trim().equals("")) { return null; } else { return custFormat.parse(dateStr); } } catch (ParseException e) { throw new JsonSyntaxException(json.getAsString(), e); } }
From source file:com.hp.ov.sdk.adaptors.PortTelemetrySerializationAdapter.java
License:Apache License
@Override public PortTelemetry deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonPrimitive()) { return PortTelemetry.valueOf(json.getAsString()); } else if (json.isJsonArray()) { for (JsonElement element : json.getAsJsonArray()) { String elementAsString = element.getAsString(); if (PortTelemetry.contains(elementAsString)) { return PortTelemetry.valueOf(elementAsString); }//from w ww . j a va 2 s.co m } } throw new JsonParseException("Unknown port telemetry value type. " + "Expected value types are String or String[] (String array)"); }
From source file:com.hp.ov.sdk.adaptors.StorageCapabilitiesDeserializer.java
License:Apache License
@Override public StorageCapabilities deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonArray()) { StorageCapabilities storageCapabilities = new StorageCapabilities(); storageCapabilities.setControllerModes(Lists.newArrayList(ControllerMode.RAID)); storageCapabilities.setDriveTechnologies(new ArrayList<DriveTechnology>()); storageCapabilities.setRaidLevels(new ArrayList<RaidLevel>()); Iterator<JsonElement> iterator = json.getAsJsonArray().iterator(); while (iterator.hasNext()) { JsonElement element = iterator.next(); if (!element.isJsonPrimitive()) { throw new JsonParseException( "Expected a primitive (String) value but found " + element.toString()); }//from w w w. j a v a2 s. co m try { storageCapabilities.getRaidLevels().add(RaidLevel.valueOf(element.getAsString())); } catch (IllegalArgumentException e) { throw new JsonParseException("Unknown RAID Level", e); } } return storageCapabilities; } return new Gson().fromJson(json, StorageCapabilities.class); }
From source file:com.hp.ov.sdk.adaptors.StoragePoolSerializationAdapter.java
License:Apache License
@Override public StoragePool deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { Gson gson = new Gson(); JsonElement allocatedCapacity = json.getAsJsonObject().remove(StoragePool.ALLOCATED_CAPACITY_FIELD); StoragePool storagePool = gson.fromJson(json, StoragePool.class); if (allocatedCapacity.isJsonPrimitive()) { storagePool.setAllocatedCapacity(allocatedCapacity.getAsString()); } else {//from www . java 2 s . c o m storagePool.setAllocatedCapacityDetails(gson.fromJson(allocatedCapacity, AllocatedCapacity.class)); } return storagePool; }