List of usage examples for com.google.gson JsonObject getAsJsonArray
public JsonArray getAsJsonArray(String memberName)
From source file:org.aksw.lucene.processor.IndexProcessor.java
License:Apache License
/** * @param json//from w ww . j a v a 2 s .co m * @return */ public static List<SparqlQueryResult> getResult(String json) { List<SparqlQueryResult> result = new ArrayList<SparqlQueryResult>(); if (json == null || json.isEmpty()) return result; Gson gson = new Gson(); JsonParser parser = new JsonParser(); JsonObject validJSON = parser.parse(json).getAsJsonObject(); JsonObject results = validJSON.get("results").getAsJsonObject(); JsonArray bindings = results.getAsJsonArray("bindings"); for (JsonElement obj : bindings) { SparqlQueryResult jsonElement = gson.fromJson(obj, SparqlQueryResult.class); result.add(jsonElement); } return result; }
From source file:org.ambraproject.amendment.AmendmentServiceImpl.java
License:Apache License
public List<ArticleAmendment> parseJsonFromAmbra(String json) { List<ArticleAmendment> amendments = new ArrayList<ArticleAmendment>(); // uncomment the json received from the ambra action if (json.startsWith("/*")) { json = json.substring(2);//from w w w . j ava 2s .c om } if (json.endsWith("*/")) { json = json.substring(0, json.length() - 2); } JsonParser parser = new JsonParser(); JsonObject obj = parser.parse(json).getAsJsonObject(); JsonArray amendmentArr = obj.getAsJsonArray("amendments"); for (JsonElement element : amendmentArr) { JsonObject amendment = element.getAsJsonObject(); amendments.add( ArticleAmendment.builder().setParentArticleURI(amendment.get("parentArticleURI").getAsString()) .setOtherArticleDoi(amendment.get("otherArticleDoi").getAsString()) .setRelationshipType(amendment.get("relationshipType").getAsString()).build()); } return amendments; }
From source file:org.ambraproject.category.CategoryServiceImpl.java
License:Apache License
/** * Extracts the categories from the JSON returned by the ambra server. * * @param json response from the ambra server to a get categories call * @return List of category Strings//from w w w . j av a 2 s .co m */ List<String> parseJsonFromAmbra(String json, String doi) { // The ambra action returns JSON wrapped in javascript comments, but Gson does not // like this. if (json.startsWith("/*")) { json = json.substring(2); } if (json.endsWith("*/")) { json = json.substring(0, json.length() - 2); } JsonParser parser = new JsonParser(); JsonObject obj = parser.parse(json).getAsJsonObject(); JsonArray categories; try { categories = obj.getAsJsonArray("categories"); } catch (ClassCastException cce) { log.error(String.format("Exception processing category JSON for %s. " + "This probably means that this is not a valid DOI.", doi), cce); return new ArrayList<String>(); } List<String> result = new ArrayList<String>(categories.size()); for (JsonElement category : categories) { result.add(category.getAsString()); } return result; }
From source file:org.ambraproject.service.crossref.CrossRefLookupServiceImpl.java
License:Apache License
/** * Parse the JSON into native types/*from ww w . j av a 2s.c om*/ * * @param json the JSON string to convert to a java native type * * @return a CrossRefResponse object */ private CrossRefResponse parseJSON(final String json) { return new CrossRefResponse() { { JsonParser parser = new JsonParser(); JsonObject responseObject = parser.parse(json).getAsJsonObject(); queryOK = (responseObject.getAsJsonPrimitive("query_ok")).getAsBoolean(); List<CrossRefResult> resultTemp = new ArrayList<CrossRefResult>(); for (final JsonElement resultElement : responseObject.getAsJsonArray("results")) { JsonObject resultObj = resultElement.getAsJsonObject(); CrossRefResult res = new CrossRefResult(); if (resultObj.getAsJsonPrimitive("text") != null) { res.text = resultObj.getAsJsonPrimitive("text").getAsString(); } if (resultObj.getAsJsonPrimitive("match") != null) { res.match = resultObj.getAsJsonPrimitive("match").getAsBoolean(); } if (resultObj.getAsJsonPrimitive("doi") != null) { res.doi = resultObj.getAsJsonPrimitive("doi").getAsString(); } if (resultObj.getAsJsonPrimitive("score") != null) { res.score = resultObj.getAsJsonPrimitive("score").getAsString(); } //Some results aren't actually valid if (res.doi != null) { resultTemp.add(res); } } this.results = resultTemp.toArray(new CrossRefResult[resultTemp.size()]); } }; }
From source file:org.apache.airavata.workflow.model.component.ws.WSComponentApplication.java
License:Apache License
public static WSComponentApplication parse(JsonObject applicationObject) { WSComponentApplication wsComponentApplication = new WSComponentApplication(); wsComponentApplication.description = applicationObject .getAsJsonPrimitive(WorkflowConstants.APPLICATION_COMPONENT_DESCRIPTION).getAsString(); wsComponentApplication.name = applicationObject .getAsJsonPrimitive(WorkflowConstants.APPLICATION_COMPONENT_NAME).getAsString(); wsComponentApplication.applicationId = applicationObject .getAsJsonPrimitive(WorkflowConstants.APPLICATION_COMPONENT_APPLICATION).getAsString(); if (applicationObject.get(WorkflowConstants.APPLICATION_INPUT) != null) { JsonArray inputArray = applicationObject.getAsJsonArray(WorkflowConstants.APPLICATION_INPUT); WSComponentApplicationParameter inputParameter; JsonObject inputObject;//from w w w .ja v a 2 s.c o m for (JsonElement jsonElement : inputArray) { if (jsonElement instanceof JsonObject) { inputObject = (JsonObject) jsonElement; inputParameter = new WSComponentApplicationParameter(); inputParameter.setDefaultValue(inputObject .getAsJsonPrimitive(WorkflowConstants.APPLICATION_DATA_DEFAULT_VALUE).getAsString()); inputParameter.setDescription(inputObject .getAsJsonPrimitive(WorkflowConstants.APPLICATION_DATA_DESCRIPTION).getAsString()); inputParameter.setName( inputObject.getAsJsonPrimitive(WorkflowConstants.APPLICATION_DATA_NAME).getAsString()); inputParameter.setType(DataType.valueOf(inputObject .getAsJsonPrimitive(WorkflowConstants.APPLICATION_DATA_DATA_TYPE).getAsString())); inputParameter.setInputOrder(inputObject .getAsJsonPrimitive(WorkflowConstants.APPLICATION_DATA_INPUT_ORDER).getAsInt()); if (inputObject.getAsJsonPrimitive(WorkflowConstants.APPLICATION_DATA_APP_ARGUMENT) != null) { inputParameter.setApplicationArgument(inputObject .getAsJsonPrimitive(WorkflowConstants.APPLICATION_DATA_APP_ARGUMENT).getAsString()); } wsComponentApplication.addInputParameter(inputParameter); } } } if (applicationObject.get(WorkflowConstants.APPLICATION_OUTPUT) != null) { JsonArray outputArray = applicationObject.getAsJsonArray(WorkflowConstants.APPLICATION_OUTPUT); WSComponentApplicationParameter outputParameter; JsonObject outputObject; for (JsonElement jsonElement : outputArray) { if (jsonElement instanceof JsonObject) { outputObject = (JsonObject) jsonElement; outputParameter = new WSComponentApplicationParameter(); outputParameter.setDescription(outputObject .getAsJsonPrimitive(WorkflowConstants.APPLICATION_DATA_DESCRIPTION).getAsString()); outputParameter.setName( outputObject.getAsJsonPrimitive(WorkflowConstants.APPLICATION_DATA_NAME).getAsString()); outputParameter.setType(DataType.valueOf(outputObject .getAsJsonPrimitive(WorkflowConstants.APPLICATION_DATA_DATA_TYPE).getAsString())); wsComponentApplication.addOutputParameter(outputParameter); } } } return wsComponentApplication; }
From source file:org.apache.airavata.workflow.model.graph.impl.GraphImpl.java
License:Apache License
protected void parse(JsonObject graphObject) throws GraphException { JsonPrimitive jsonPrimitive = graphObject.getAsJsonPrimitive(GraphSchema.GRAPH_ID_TAG); if (jsonPrimitive != null) { this.id = jsonPrimitive.getAsString(); }/* w w w . j a v a 2s . com*/ jsonPrimitive = graphObject.getAsJsonPrimitive(GraphSchema.GRAPH_NAME_TAG); if (jsonPrimitive != null) { this.name = jsonPrimitive.getAsString(); } jsonPrimitive = graphObject.getAsJsonPrimitive(GraphSchema.GRAPH_DESCRIPTION_TAG); if (jsonPrimitive != null) { this.description = jsonPrimitive.getAsString(); } JsonArray jArray = graphObject.getAsJsonArray(GraphSchema.NODE_TAG); for (JsonElement jsonElement : jArray) { addNode(this.factory.createNode((JsonObject) jsonElement)); } jArray = graphObject.getAsJsonArray(GraphSchema.PORT_TAG); for (JsonElement jsonElement : jArray) { addPort(this.factory.createPort((JsonObject) jsonElement)); } jArray = graphObject.getAsJsonArray(GraphSchema.EDGE_TAG); for (JsonElement jsonElement : jArray) { addEdge(this.factory.createEdge((JsonObject) jsonElement)); } indexToPointer(); }
From source file:org.apache.airavata.workflow.model.graph.impl.NodeImpl.java
License:Apache License
protected void parse(JsonObject nodeObject) { this.id = nodeObject.getAsJsonPrimitive(GraphSchema.NODE_ID_TAG).getAsString(); this.name = nodeObject.getAsJsonPrimitive(GraphSchema.NODE_NAME_TAG).getAsString(); JsonArray jArray;// ww w . j a va2 s .co m if (nodeObject.get(GraphSchema.NODE_INPUT_PORT_TAG) != null) { jArray = nodeObject.getAsJsonArray(GraphSchema.NODE_INPUT_PORT_TAG); for (JsonElement jsonElement : jArray) { this.inputPortIDs.add(jsonElement.getAsString()); } } if (nodeObject.get(GraphSchema.NODE_OUTPUT_PORT_TAG) != null) { jArray = nodeObject.getAsJsonArray(GraphSchema.NODE_OUTPUT_PORT_TAG); for (JsonElement jsonElement : jArray) { this.outputPortIDs.add(jsonElement.getAsString()); } } JsonElement jElement = nodeObject.get(GraphSchema.NODE_CONTROL_IN_PORT_TAG); if (jElement != null) { this.controlInPortID = jElement.getAsString(); } if (nodeObject.get(GraphSchema.NODE_CONTROL_OUT_PORT_TAG) != null) { jArray = nodeObject.getAsJsonArray(GraphSchema.NODE_CONTROL_OUT_PORT_TAG); for (JsonElement jsonElement : jArray) { this.controlOutPortIDs.add(jsonElement.getAsString()); } } jElement = nodeObject.get(GraphSchema.NODE_EPR_PORT_TAG); if (jElement != null) { this.eprPortID = jElement.getAsString(); } this.position.x = nodeObject.get(GraphSchema.NODE_X_LOCATION_TAG).getAsInt(); this.position.y = nodeObject.get(GraphSchema.NODE_Y_LOCATION_TAG).getAsInt(); // Parse config element not sure why we used it. // Parse component element. JsonObject configObject = nodeObject.getAsJsonObject(GraphSchema.NODE_CONFIG_TAG); if (configObject != null) { parseConfiguration(configObject); } }
From source file:org.apache.ambari.server.upgrade.UpgradeCatalog221.java
License:Apache License
protected String addCheckCommandTimeoutParam(String source) { JsonObject sourceJson = new JsonParser().parse(source).getAsJsonObject(); JsonArray parametersJson = sourceJson.getAsJsonArray("parameters"); boolean parameterExists = parametersJson != null && !parametersJson.isJsonNull(); if (parameterExists) { Iterator<JsonElement> jsonElementIterator = parametersJson.iterator(); while (jsonElementIterator.hasNext()) { JsonElement element = jsonElementIterator.next(); JsonElement name = element.getAsJsonObject().get("name"); if (name != null && !name.isJsonNull() && name.getAsString().equals("check.command.timeout")) { return sourceJson.toString(); }/* w w w .j ava2s. c o m*/ } } JsonObject checkCommandTimeoutParamJson = new JsonObject(); checkCommandTimeoutParamJson.add("name", new JsonPrimitive("check.command.timeout")); checkCommandTimeoutParamJson.add("display_name", new JsonPrimitive("Check command timeout")); checkCommandTimeoutParamJson.add("value", new JsonPrimitive(60.0)); checkCommandTimeoutParamJson.add("type", new JsonPrimitive("NUMERIC")); checkCommandTimeoutParamJson.add("description", new JsonPrimitive("The maximum time before check command will be killed by timeout")); checkCommandTimeoutParamJson.add("units", new JsonPrimitive("seconds")); if (!parameterExists) { parametersJson = new JsonArray(); parametersJson.add(checkCommandTimeoutParamJson); sourceJson.add("parameters", parametersJson); } else { parametersJson.add(checkCommandTimeoutParamJson); sourceJson.remove("parameters"); sourceJson.add("parameters", parametersJson); } return sourceJson.toString(); }
From source file:org.apache.edgent.runtime.jsoncontrol.JsonControlService.java
License:Apache License
/** * Handle a control operation./*from w ww . java 2s .c o m*/ * An operation maps to a {@code void} method. * @param request Request to be executed. * @return JSON boolean true if the request was executed, false if it was not. * @throws Exception Exception executing the control instruction. */ private JsonElement controlOperation(JsonObject request) throws Exception { final String type = request.get(TYPE_KEY).getAsString(); String alias = request.get(ALIAS_KEY).getAsString(); final String controlId = getControlId(type, null, alias); logger.trace("Operation - control id: {}", controlId); ControlMBean<?> mbean; synchronized (this) { mbean = mbeans.get(controlId); } if (mbean == null) { logger.warn("Unable to find mbean for control id: {}", controlId); return new JsonPrimitive(Boolean.FALSE); } String methodName = request.get(OP_KEY).getAsString(); logger.trace("Operation method - control id: {} method: {}", controlId, methodName); int argumentCount = 0; JsonArray args = null; if (request.has(ARGS_KEY)) { args = request.getAsJsonArray(ARGS_KEY); argumentCount = args.size(); } Method method = findMethod(mbean.getControlInterface(), methodName, argumentCount); if (method == null) { logger.warn("Unable to find method \"{}\" with {} args in {}", methodName, argumentCount, mbean.getControlInterface().getName()); return new JsonPrimitive(Boolean.FALSE); } logger.trace("Execute operation - control id: {} method: {}", controlId, methodName); executeMethod(method, mbean.getControl(), getArguments(method, args)); logger.trace("Execute completed - control id: {} method: {}", controlId, methodName); return new JsonPrimitive(Boolean.TRUE); }
From source file:org.apache.fineract.portfolio.loanaccount.service.LoanAssembler.java
License:Apache License
public Set<LoanDisbursementDetails> fetchDisbursementData(final JsonObject command) { final Locale locale = this.fromApiJsonHelper.extractLocaleParameter(command); final String dateFormat = this.fromApiJsonHelper.extractDateFormatParameter(command); Set<LoanDisbursementDetails> disbursementDatas = new HashSet<>(); if (command.has(LoanApiConstants.disbursementDataParameterName)) { final JsonArray disbursementDataArray = command .getAsJsonArray(LoanApiConstants.disbursementDataParameterName); if (disbursementDataArray != null && disbursementDataArray.size() > 0) { int i = 0; do {//from w w w . j a v a 2 s. co m final JsonObject jsonObject = disbursementDataArray.get(i).getAsJsonObject(); Date expectedDisbursementDate = null; Date actualDisbursementDate = null; BigDecimal principal = null; if (jsonObject.has(LoanApiConstants.disbursementDateParameterName)) { LocalDate date = this.fromApiJsonHelper.extractLocalDateNamed( LoanApiConstants.disbursementDateParameterName, jsonObject, dateFormat, locale); if (date != null) { expectedDisbursementDate = date.toDate(); } } if (jsonObject.has(LoanApiConstants.disbursementPrincipalParameterName) && jsonObject.get(LoanApiConstants.disbursementPrincipalParameterName).isJsonPrimitive() && StringUtils.isNotBlank((jsonObject .get(LoanApiConstants.disbursementPrincipalParameterName).getAsString()))) { principal = jsonObject .getAsJsonPrimitive(LoanApiConstants.disbursementPrincipalParameterName) .getAsBigDecimal(); } disbursementDatas.add(new LoanDisbursementDetails(expectedDisbursementDate, actualDisbursementDate, principal)); i++; } while (i < disbursementDataArray.size()); } } return disbursementDatas; }