List of usage examples for com.google.gson JsonElement getAsJsonArray
public JsonArray getAsJsonArray()
From source file:com.haulmont.cuba.core.app.importexport.EntityImportViewBuilder.java
License:Apache License
protected EntityImportView buildFromJsonObject(JsonObject jsonObject, MetaClass metaClass) { EntityImportView view = new EntityImportView(metaClass.getJavaClass()); for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) { String propertyName = entry.getKey(); MetaProperty metaProperty = metaClass.getProperty(propertyName); if (metaProperty != null) { Range propertyRange = metaProperty.getRange(); Class<?> propertyType = metaProperty.getJavaType(); if (propertyRange.isDatatype() || propertyRange.isEnum()) { if (security.isEntityAttrUpdatePermitted(metaClass, propertyName)) view.addLocalProperty(propertyName); } else if (propertyRange.isClass()) { if (Entity.class.isAssignableFrom(propertyType)) { if (metadataTools.isEmbedded(metaProperty)) { MetaClass propertyMetaClass = metadata.getClass(propertyType); JsonElement propertyJsonObject = entry.getValue(); if (!propertyJsonObject.isJsonObject()) { throw new RuntimeException("JsonObject was expected for property " + propertyName); }//from w w w . jav a 2 s. com if (security.isEntityAttrUpdatePermitted(metaClass, propertyName)) { EntityImportView propertyImportView = buildFromJsonObject( propertyJsonObject.getAsJsonObject(), propertyMetaClass); view.addEmbeddedProperty(propertyName, propertyImportView); } } else { MetaClass propertyMetaClass = metadata.getClass(propertyType); if (metaProperty.getType() == MetaProperty.Type.COMPOSITION) { JsonElement propertyJsonObject = entry.getValue(); if (security.isEntityAttrUpdatePermitted(metaClass, propertyName)) { if (propertyJsonObject.isJsonNull()) { //in case of null we must add such import behavior to update the reference with null value later if (metaProperty.getRange() .getCardinality() == Range.Cardinality.MANY_TO_ONE) { view.addManyToOneProperty(propertyName, ReferenceImportBehaviour.IGNORE_MISSING); } else { view.addOneToOneProperty(propertyName, ReferenceImportBehaviour.IGNORE_MISSING); } } else { if (!propertyJsonObject.isJsonObject()) { throw new RuntimeException( "JsonObject was expected for property " + propertyName); } EntityImportView propertyImportView = buildFromJsonObject( propertyJsonObject.getAsJsonObject(), propertyMetaClass); if (metaProperty.getRange() .getCardinality() == Range.Cardinality.MANY_TO_ONE) { view.addManyToOneProperty(propertyName, propertyImportView); } else { view.addOneToOneProperty(propertyName, propertyImportView); } } } } else { if (security.isEntityAttrUpdatePermitted(metaClass, propertyName)) if (metaProperty.getRange().getCardinality() == Range.Cardinality.MANY_TO_ONE) { view.addManyToOneProperty(propertyName, ReferenceImportBehaviour.ERROR_ON_MISSING); } else { view.addOneToOneProperty(propertyName, ReferenceImportBehaviour.ERROR_ON_MISSING); } } } } else if (Collection.class.isAssignableFrom(propertyType)) { MetaClass propertyMetaClass = metaProperty.getRange().asClass(); switch (metaProperty.getRange().getCardinality()) { case MANY_TO_MANY: if (security.isEntityAttrUpdatePermitted(metaClass, propertyName)) view.addManyToManyProperty(propertyName, ReferenceImportBehaviour.ERROR_ON_MISSING, CollectionImportPolicy.REMOVE_ABSENT_ITEMS); break; case ONE_TO_MANY: if (metaProperty.getType() == MetaProperty.Type.COMPOSITION) { JsonElement compositionJsonArray = entry.getValue(); if (!compositionJsonArray.isJsonArray()) { throw new RuntimeException( "JsonArray was expected for property " + propertyName); } EntityImportView propertyImportView = buildFromJsonArray( compositionJsonArray.getAsJsonArray(), propertyMetaClass); if (security.isEntityAttrUpdatePermitted(metaClass, propertyName)) view.addOneToManyProperty(propertyName, propertyImportView, CollectionImportPolicy.REMOVE_ABSENT_ITEMS); } break; } } } } } return view; }
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 w w w .ja v a 2 s.co 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"); }//w ww. jav a2 s. co m 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.helion3.prism.util.DataUtil.java
License:MIT License
/** * Attempts to convert a JsonElement to an a known type. * * @param element JsonElement/*from w w w .jav a2 s. com*/ * @return Optional<Object> */ private static Optional<Object> jsonElementToObject(JsonElement element) { Object result = null; if (element.isJsonObject()) { result = dataViewFromJson(element.getAsJsonObject()); } else if (element.isJsonPrimitive()) { JsonPrimitive prim = element.getAsJsonPrimitive(); if (prim.isBoolean()) { result = prim.getAsBoolean(); } else if (prim.isNumber()) { result = prim.getAsNumber().intValue(); } else if (prim.isString()) { result = prim.getAsString(); } } else if (element.isJsonArray()) { List<Object> list = new ArrayList<Object>(); JsonArray arr = element.getAsJsonArray(); arr.forEach(new Consumer<JsonElement>() { @Override public void accept(JsonElement t) { Optional<Object> translated = jsonElementToObject(t); if (translated.isPresent()) { list.add(translated.get()); } } }); result = list; } return Optional.ofNullable(result); }
From source file:com.hi3project.dandelion.gip.codec.json.JSONGIPCodec.java
License:Open Source License
private ArrayList<FuzzyVariable> decodeInteractionHints(JsonElement interactionHints) throws GIPEventCodeDecodeErrorException { try {/*w w w .j a v a2s .c o m*/ JsonArray ihsValue = interactionHints.getAsJsonArray(); ArrayList<FuzzyVariable> interactionHintsResult = new ArrayList<FuzzyVariable>(ihsValue.size()); JsonObject ihObject; String name, label, value; for (JsonElement ihv : ihsValue) { ihObject = ihv.getAsJsonObject(); name = jsonValueAsString(ihObject.get(IH_NAME)); label = jsonValueAsString(ihObject.get(IH_LABEL)); value = jsonValueAsString(ihObject.get(IH_VALUE)); interactionHintsResult.add(new FuzzyVariable(name, label, Double.valueOf(value))); } return interactionHintsResult; } catch (UnsupportedOperationException ex) { throw new GIPEventCodeDecodeErrorException(ex); } }
From source file:com.hi3project.dandelion.gip.codec.json.JSONGIPCodec.java
License:Open Source License
private ArrayList<Property> decodePropertySet(JsonElement propertySet) throws GIPEventCodeDecodeErrorException { try {//from w ww. j a v a 2 s . co m JsonArray psValues = propertySet.getAsJsonArray(); ArrayList<Property> propertySetResult = new ArrayList<Property>(psValues.size()); for (JsonElement psV : psValues) { propertySetResult.add(decodeProperty(psV.getAsJsonObject())); } return propertySetResult; } catch (IllegalArgumentException ex) { throw new GIPEventCodeDecodeErrorException(ex); } catch (UnsupportedOperationException ex) { throw new GIPEventCodeDecodeErrorException(ex); } }
From source file:com.hkm.disqus.api.gson.ApplicationsUsageDeserializer.java
License:Apache License
@Override public Usage deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { // JSON element should be an array if (json.isJsonArray()) { // Create usage Usage usage = new Usage(); // Iterate the array for (JsonElement element : json.getAsJsonArray()) { // Each element should be an array containing a date and int if (element.isJsonArray() && element.getAsJsonArray().size() == 2) { JsonArray jsonArray = element.getAsJsonArray(); Date date = context.deserialize(jsonArray.get(0), Date.class); int count = jsonArray.get(1).getAsInt(); usage.put(date, count);/* ww w .j av a 2 s. c om*/ } } return usage; } return null; }
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 ww w . j ava 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 ww . j a v a 2 s . c o 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.hybris.datahub.service.impl.DefaultJsonService.java
License:Open Source License
@Override public List<Map<String, String>> parse(final String json) { final List<Map<String, String>> result = new LinkedList<Map<String, String>>(); final JsonElement jsonElement = new JsonParser().parse(json); if (jsonElement.isJsonArray()) { final Iterator<JsonElement> iterator = jsonElement.getAsJsonArray().iterator(); while (iterator.hasNext()) { final JsonElement jsonElementInArray = iterator.next(); result.add(convertJsonObject(jsonElementInArray.getAsJsonObject())); }/*from w w w.j a va 2 s .c o m*/ } else if (jsonElement.isJsonObject()) { result.add(convertJsonObject(jsonElement.getAsJsonObject())); } return result; }