List of usage examples for com.google.gson JsonObject entrySet
public Set<Map.Entry<String, JsonElement>> entrySet()
From source file:com.ibm.watson.apis.conversation_enhanced.retrieve_and_rank.Client.java
License:Open Source License
/** * Helper Method to include highlighting information along with the retrieve and rank response so * the final payload includes id,title,highlight,body,sourceUrl as json key value pairs. * /*from w w w . ja v a2 s. com*/ * @param input The user's query sent to the retrieve and rank service * @param results The results obtained from a call to the retrieve and rank service with * <code>input</code> as the query * @param highlights SOLR highlighting information obtained from a call to the retrieve and rank * service * @return A list of DocumentPayload objects, each representing a single document the retrieve and * rank service believes is a possible answer to the user's query */ private List<DocumentPayload> createPayload(String input, String results, String highlights) { logger.info(Messages.getString("Service.CREATING_RNR_PAYLOAD")); //$NON-NLS-1$ List<DocumentPayload> payload = new ArrayList<DocumentPayload>(); HashMap<String, Integer> hm = new HashMap<String, Integer>(); JsonElement jelement = new JsonParser().parse(results); JsonArray jarray = jelement.getAsJsonArray(); for (int i = 0; i < jarray.size(); i++) { DocumentPayload documentPayload = new DocumentPayload(); String id = jarray.get(i).getAsJsonObject().get(Constants.SCHEMA_FIELD_ID).toString().replaceAll("\"", //$NON-NLS-1$ ""); //$NON-NLS-1$ documentPayload.setId(id); documentPayload.setTitle(jarray.get(i).getAsJsonObject().get(Constants.SCHEMA_FIELD_TITLE).toString() .replaceAll("\"", "")); //$NON-NLS-1$ //$NON-NLS-2$ if (jarray.get(i).getAsJsonObject().get(Constants.SCHEMA_FIELD_BODY) != null) { documentPayload.setBody( // This method limits the response text in this sample app to two paragraphs. limitParagraph(jarray.get(i).getAsJsonObject().get(Constants.SCHEMA_FIELD_BODY).toString() .replaceAll("\"", ""))); //$NON-NLS-1$ //$NON-NLS-2$ } else { documentPayload.setBody("empty"); //$NON-NLS-1$ } if (jarray.get(i).getAsJsonObject().get(Constants.SCHEMA_FIELD_SOURCE_URL) == null) { documentPayload.setSourceUrl("empty"); //$NON-NLS-1$ } else { documentPayload.setSourceUrl(jarray.get(i).getAsJsonObject().get(Constants.SCHEMA_FIELD_SOURCE_URL) .toString().replaceAll("\"", "")); //$NON-NLS-1$ //$NON-NLS-2$ } if (jarray.get(i).getAsJsonObject().get(Constants.SCHEMA_FIELD_CONFIDENCE) != null) { documentPayload.setConfidence(jarray.get(i).getAsJsonObject().get(Constants.SCHEMA_FIELD_CONFIDENCE) .toString().replaceAll("\"", "")); //$NON-NLS-1$ //$NON-NLS-2$ } else { documentPayload.setConfidence("0.0"); //$NON-NLS-1$ } payload.add(i, documentPayload); hm.put(id, i); } jelement = new JsonParser().parse(highlights); JsonObject highlight = jelement.getAsJsonObject(); // Add highlighting information Set<Map.Entry<String, JsonElement>> entrySet = highlight.entrySet(); for (Map.Entry<String, JsonElement> entry : entrySet) { String docid = entry.getKey(); String highlighted = ""; //$NON-NLS-1$ if (entry.getValue().getAsJsonObject().get(Constants.SCHEMA_FIELD_BODY) != null) { highlighted = entry.getValue().getAsJsonObject().get(Constants.SCHEMA_FIELD_BODY).getAsJsonArray() .get(0).toString(); } if (hm.get(docid) != null) payload.get(hm.get(docid)).setHighlight(highlighted); } return payload; }
From source file:com.ibm.watson.developer_cloud.document_conversion.v1.DocumentConversion.java
License:Open Source License
/** * Converts a document and returns an {@link InputStream}. * * @param document The file to convert/*from ww w. j a v a 2 s .com*/ * @param mediaType Internet media type of the file * @param conversionTarget The conversion target to use * @param customConfig The configuration parameters * @return Converted document in the specified format * @see HttpMediaType HttpMediaType for available media types */ private Request createConversionRequest(final File document, final String mediaType, final ConversionTarget conversionTarget, final JsonObject customConfig) { if ((document == null) || !document.exists()) { throw new IllegalArgumentException("document cannot be null and must exist"); } JsonObject config = null; if (customConfig != null) { config = customConfig; } else { config = EMPTY_CONFIG; } final JsonObject configJson = new JsonObject(); // Do this since we shouldn't mutate customConfig for (Map.Entry<String, JsonElement> entry : config.entrySet()) { configJson.add(entry.getKey(), entry.getValue()); } // Add or override the conversion target configJson.addProperty(CONVERSION_TARGET, conversionTarget.toString()); final MediaType mType = parseMediaType(document, mediaType); final RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM) .addPart(Headers.of(HttpHeaders.CONTENT_DISPOSITION, "form-data; name=\"config\""), RequestBody.create(HttpMediaType.JSON, configJson.toString())) .addPart(Headers.of(HttpHeaders.CONTENT_DISPOSITION, "form-data; name=\"file\""), RequestBody.create(mType, document)) .build(); return RequestBuilder.post(CONVERT_DOCUMENT_PATH).query(VERSION, versionDate).body(body).build(); }
From source file:com.ibm.watson.developer_cloud.util.BluemixUtils.java
License:Open Source License
/** * Returns the apiKey from the VCAP_SERVICES or null if doesn't exists. If plan is specified, then * only credentials for the given plan will be returned. * //from www . j a v a2 s .co m * @param serviceName the service name * @param plan the service plan: standard, free or experimental * @return the API key */ public static String getAPIKey(String serviceName, String plan) { if (serviceName == null || serviceName.isEmpty()) return null; final JsonObject services = getVCAPServices(); if (services == null) return null; for (final Entry<String, JsonElement> entry : services.entrySet()) { final String key = entry.getKey(); if (key.startsWith(serviceName)) { final JsonArray servInstances = services.getAsJsonArray(key); for (final JsonElement instance : servInstances) { final JsonObject service = instance.getAsJsonObject(); final String instancePlan = service.get(PLAN).getAsString(); if (plan == null || plan.equalsIgnoreCase(instancePlan)) { final JsonObject credentials = instance.getAsJsonObject().getAsJsonObject(CREDENTIALS); if (serviceName.equalsIgnoreCase(ALCHEMY_API)) { return credentials.get(APIKEY).getAsString(); } else { final String username = credentials.get(USERNAME).getAsString(); final String password = credentials.get(PASSWORD).getAsString(); return Credentials.basic(username, password); } } } } } return null; }
From source file:com.ibm.watson.developer_cloud.util.CredentialUtils.java
License:Open Source License
/** * A helper method to retrieve the appropriate 'credentials' JSON property value from the VCAP_SERVICES. * * @param vcapServices JSON object representing the VCAP_SERVICES * @param serviceName the name of the service whose credentials are sought * @param plan the name of the plan for which the credentials are sought, e.g. 'standard', 'beta' etc, may be null * @return the first set of credentials that match the search criteria, service name and plan. May return null *//*from w w w. j a v a 2 s . com*/ private static JsonObject getCredentialsObject(JsonObject vcapServices, String serviceName, String plan) { for (final Entry<String, JsonElement> entry : vcapServices.entrySet()) { final String key = entry.getKey(); if (key.startsWith(serviceName)) { final JsonArray servInstances = vcapServices.getAsJsonArray(key); for (final JsonElement instance : servInstances) { final JsonObject service = instance.getAsJsonObject(); final String instancePlan = service.get(PLAN).getAsString(); if ((plan == null) || plan.equalsIgnoreCase(instancePlan)) { return instance.getAsJsonObject().getAsJsonObject(CREDENTIALS); } } } } return null; }
From source file:com.ibm.watson.WatsonVRTraining.util.PatchedCredentialUtils.java
License:Open Source License
/** * Returns the apiKey from the VCAP_SERVICES or null if doesn't exists. If * plan is specified, then only credentials for the given plan will be * returned./*from w w w . jav a2 s. c om*/ * * @param serviceName * the service name * @param plan * the service plan: standard, free or experimental * @return the API key */ public static String getVRAPIKey(String plan) { final JsonObject services = getVCAPServices(); if (services == null) return null; for (final Entry<String, JsonElement> entry : services.entrySet()) { final String key = entry.getKey(); if (key.startsWith(WATSON_VISUAL_RECOGNITION)) { final JsonArray servInstances = services.getAsJsonArray(key); for (final JsonElement instance : servInstances) { final JsonObject service = instance.getAsJsonObject(); final String instancePlan = service.get(PLAN).getAsString(); if (plan == null || plan.equalsIgnoreCase(instancePlan)) { final JsonObject credentials = instance.getAsJsonObject().getAsJsonObject(CREDENTIALS); return credentials.get(APIKEY).getAsString(); } } } } return null; }
From source file:com.ibm.watson.WatsonVRTraining.util.PatchedCredentialUtils.java
License:Open Source License
public static String getVRurl(String plan) { final JsonObject services = getVCAPServices(); if (services == null) return null; for (final Entry<String, JsonElement> entry : services.entrySet()) { final String key = entry.getKey(); if (key.startsWith(WATSON_VISUAL_RECOGNITION)) { final JsonArray servInstances = services.getAsJsonArray(key); for (final JsonElement instance : servInstances) { final JsonObject service = instance.getAsJsonObject(); final String instancePlan = service.get(PLAN).getAsString(); if (plan == null || plan.equalsIgnoreCase(instancePlan)) { final JsonObject credentials = instance.getAsJsonObject().getAsJsonObject(CREDENTIALS); return credentials.get(vr_url_key).getAsString(); }/*ww w. j a v a 2 s . c o m*/ } } } return null; }
From source file:com.ibm.watson.WatsonVRTraining.util.PatchedCredentialUtils.java
License:Open Source License
public static String getDBuname(String plan) { final JsonObject services = getVCAPServices(); if (services == null) return null; for (final Entry<String, JsonElement> entry : services.entrySet()) { final String key = entry.getKey(); if (key.startsWith(WATSON_CLOUDANT_DB)) { final JsonArray servInstances = services.getAsJsonArray(key); for (final JsonElement instance : servInstances) { final JsonObject service = instance.getAsJsonObject(); final String instancePlan = service.get(PLAN).getAsString(); if (plan == null || plan.equalsIgnoreCase(instancePlan)) { final JsonObject credentials = instance.getAsJsonObject().getAsJsonObject(CREDENTIALS); return credentials.get(db_uname_key).getAsString(); }/*from w w w .j ava 2s. co m*/ } } } return null; }
From source file:com.ibm.watson.WatsonVRTraining.util.PatchedCredentialUtils.java
License:Open Source License
public static String getDBpass(String plan) { final JsonObject services = getVCAPServices(); if (services == null) return null; for (final Entry<String, JsonElement> entry : services.entrySet()) { final String key = entry.getKey(); if (key.startsWith(WATSON_CLOUDANT_DB)) { final JsonArray servInstances = services.getAsJsonArray(key); for (final JsonElement instance : servInstances) { final JsonObject service = instance.getAsJsonObject(); final String instancePlan = service.get(PLAN).getAsString(); if (plan == null || plan.equalsIgnoreCase(instancePlan)) { final JsonObject credentials = instance.getAsJsonObject().getAsJsonObject(CREDENTIALS); return credentials.get(db_pass_key).getAsString(); }//from w ww . jav a 2 s . c om } } } return null; }
From source file:com.ibm.watson.WatsonVRTraining.util.PatchedCredentialUtils.java
License:Open Source License
public static String getDBurl(String plan) { final JsonObject services = getVCAPServices(); if (services == null) return null; for (final Entry<String, JsonElement> entry : services.entrySet()) { final String key = entry.getKey(); if (key.startsWith(WATSON_CLOUDANT_DB)) { final JsonArray servInstances = services.getAsJsonArray(key); for (final JsonElement instance : servInstances) { final JsonObject service = instance.getAsJsonObject(); final String instancePlan = service.get(PLAN).getAsString(); if (plan == null || plan.equalsIgnoreCase(instancePlan)) { final JsonObject credentials = instance.getAsJsonObject().getAsJsonObject(CREDENTIALS); return credentials.get(db_url_key).getAsString(); }//from ww w .j a v a2s . co m } } } return null; }
From source file:com.ikanow.infinit.e.data_model.custom.InfiniteFileInputJsonParser.java
License:Apache License
static public BasicDBObject convertJsonObjectToBson(JsonObject json, boolean bHtmlUnescape) { int length = json.entrySet().size(); BasicDBObject list = new BasicDBObject(capacity(length)); for (Map.Entry<String, JsonElement> jsonKeyEl : json.entrySet()) { JsonElement jsonEl = jsonKeyEl.getValue(); if (jsonEl.isJsonArray()) { list.put(jsonKeyEl.getKey(), handleJsonArray(jsonEl.getAsJsonArray(), bHtmlUnescape)); } else if (jsonEl.isJsonObject()) { list.put(jsonKeyEl.getKey(), convertJsonObjectToBson(jsonEl.getAsJsonObject(), bHtmlUnescape)); } else if (jsonEl.isJsonPrimitive()) { if (bHtmlUnescape) { list.put(jsonKeyEl.getKey(), StringEscapeUtils.unescapeHtml(jsonEl.getAsString())); } else { list.put(jsonKeyEl.getKey(), jsonEl.getAsString()); }//from www. j av a 2 s .c o m } } if (list.size() > 0) { return list; } return null; }