List of usage examples for com.google.gson JsonElement getAsString
public String getAsString()
From source file:com.ibm.streamsx.topology.generator.spl.SPLGenerator.java
License:Open Source License
/** * Add a single value of a known type.// w ww . j a v a2 s .co m */ static void value(StringBuilder sb, String type, JsonElement value) { switch (type) { case "UINT8": case "UINT16": case "UINT32": case "UINT64": case "INT8": case "INT16": case "INT32": case "INT64": case "FLOAT32": case "FLOAT64": numberLiteral(sb, value.getAsJsonPrimitive(), type); break; case "RSTRING": stringLiteral(sb, value.getAsString()); break; case "USTRING": stringLiteral(sb, value.getAsString()); sb.append("u"); break; case "BOOLEAN": sb.append(value.getAsBoolean()); break; default: case JParamTypes.TYPE_ENUM: case JParamTypes.TYPE_SPLTYPE: case JParamTypes.TYPE_ATTRIBUTE: case JParamTypes.TYPE_SPL_EXPRESSION: sb.append(value.getAsString()); break; } }
From source file:com.ibm.streamsx.topology.internal.core.PlacementInfo.java
License:Open Source License
private static void addToSet(Set<String> set, JsonArray array) { for (JsonElement item : array) set.add(item.getAsString()); }
From source file:com.ibm.streamsx.topology.internal.gson.GsonUtilities.java
License:Open Source License
/** * Returns a property as a String./* w w w . ja va 2 s . co m*/ * @param object * @param property * @return Value or null if it is not set. */ public static String jstring(JsonObject object, String property) { if (object.has(property)) { JsonElement je = object.get(property); if (je.isJsonNull()) return null; return je.getAsString(); } return null; }
From source file:com.ibm.streamsx.topology.internal.streaminganalytics.VcapServices.java
License:Open Source License
/** * Get the top-level VCAP services object. * /*w w w.ja va 2s.c om*/ * Object can be one of the following: * <ul> * <li>JsonObject - assumed to contain VCAP_SERVICES </li> * <li>String - assumed to contain serialized VCAP_SERVICES JSON, or the * location of a file containing the serialized VCAP_SERVICES JSON</li> * <li>null - assumed to be in the environment variable VCAP_SERVICES</li> * </ul> */ public static JsonObject getVCAPServices(JsonElement rawServices) throws IOException { JsonParser parser = new JsonParser(); String vcapString; String vcapContents = null; if (rawServices == null || rawServices.isJsonNull()) { // if rawServices is null, then pull from the environment vcapString = System.getenv("VCAP_SERVICES"); if (vcapString == null) { throw new IllegalStateException( "VCAP_SERVICES are not defined, please set environment variable VCAP_SERVICES or configuration property: " + VCAP_SERVICES); } // resulting string can be either the serialized JSON or filename if (Files.isRegularFile(Paths.get(vcapString))) { Path vcapFile = Paths.get(vcapString); vcapContents = new String(Files.readAllBytes(vcapFile), StandardCharsets.UTF_8); } else { vcapContents = vcapString; } } else if (rawServices.isJsonObject()) { return rawServices.getAsJsonObject(); } else if (rawServices.isJsonPrimitive()) { // String can be either the serialized JSON or filename String rawString = rawServices.getAsString(); if (Files.isRegularFile(Paths.get(rawString))) { Path vcapFile = Paths.get(rawString); vcapContents = new String(Files.readAllBytes(vcapFile), StandardCharsets.UTF_8); } else vcapContents = rawString; } else { throw new IllegalArgumentException("Unknown VCAP_SERVICES object class: " + rawServices.getClass()); } return parser.parse(vcapContents).getAsJsonObject(); }
From source file:com.ibm.watson.developer_cloud.util.DateDeserializer.java
License:Open Source License
@Override public synchronized Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonNull() || json.getAsString().isEmpty()) { return null; }/* ww w . j a v a 2 s . c o m*/ String dateAsString = json.getAsJsonPrimitive().getAsString().replaceAll("Z$", "+0000"); ParseException e = null; for (SimpleDateFormat format : dateFormatters) { try { return format.parse(dateAsString); } catch (ParseException e1) { e = e1; } } Pattern isJustNumber = Pattern.compile("^\\d+$"); Matcher foundMatch = isJustNumber.matcher(dateAsString); if (foundMatch.find()) { Long timeAsLong = Long.parseLong(dateAsString); Long msCheck = 100000000000L; // are we ms or seconds maybe? if (timeAsLong < msCheck) { // assuming in seconds timeAsLong = timeAsLong * 1000; } return new Date(timeAsLong); } LOG.log(Level.SEVERE, "Error parsing: " + dateAsString, e); return null; }
From source file:com.ibm.watson.movieapp.dialog.fvt.rest.RestAPI.java
License:Open Source License
/** * getJSONArray - read JSON and find the values associated with the element param * @param jsonBody = JSON to read in string form * @param element - element looking for/* w ww .j a va 2 s. c o m*/ * @return - String value of element */ public ArrayList<String> getJSONArrayValue(String jsonBody, String field) { ArrayList<String> values = new ArrayList<String>(); try { JsonElement je = new JsonParser().parse(jsonBody); JsonArray inner = je.getAsJsonObject().getAsJsonArray(field); Iterator<JsonElement> innerIter = inner.iterator(); while (innerIter.hasNext()) { JsonElement innerEntry = innerIter.next(); values.add(innerEntry.getAsString()); } } catch (JsonIOException | JsonSyntaxException e) { e.printStackTrace(); } return values; }
From source file:com.iheart.quickio.client.QuickIo.java
License:Open Source License
private void routeCallback(final String evPath, final QuickIoCallback cb, final JsonElement json) { if (!json.isJsonObject()) { return;// ww w .jav a2 s . co m } long cbId = Long.parseLong(evPath.substring(QuickIo.EV_CALLBACK.length())); QuickIoCallback evCb = null; synchronized (this) { evCb = this.cbs.remove(cbId); } if (evCb == null) { return; } JsonObject obj = json.getAsJsonObject(); int code = obj.get("code").getAsInt(); String errMsg = ""; if (obj.has("err_msg")) { JsonElement errObj = obj.get("err_msg"); errMsg = errObj.isJsonNull() ? null : errObj.getAsString(); } evCb.onCallback(obj.get("data"), cb, code, errMsg); }
From source file:com.ikanow.infinit.e.data_model.custom.InfiniteFileInputJsonParser.java
License:Apache License
private String getKey(JsonElement meta, String key, boolean bPrimitiveOnly) { try {//from w w w. jav a 2s. c om String[] components = key.split("\\."); JsonObject metaObj = meta.getAsJsonObject(); for (String comp : components) { meta = metaObj.get(comp); if (null == meta) { return null; } //TESTED else if (meta.isJsonObject()) { metaObj = meta.getAsJsonObject(); } //TESTED else if (meta.isJsonPrimitive()) { return meta.getAsString(); } //TESTED else if (bPrimitiveOnly) { // (meta isn't allowed to be an array, then you'd have too many primary keys!) return null; } //TOTEST (? - see JsonToMetadataParser) else { // Check with first instance JsonArray array = meta.getAsJsonArray(); meta = array.get(0); if (meta.isJsonObject()) { metaObj = meta.getAsJsonObject(); } } //TESTED } if (!bPrimitiveOnly) { // allow objects, we just care if the field exists... if (null != metaObj) { return "[Object]"; } } //TESTED } catch (Exception e) { } // no primary key 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 w ww . j a v a2 s. co m*/ } } if (list.size() > 0) { return list; } return null; }
From source file:com.ikanow.infinit.e.data_model.custom.InfiniteFileInputJsonParser.java
License:Apache License
static private Object[] handleJsonArray(JsonArray jarray, boolean bHtmlUnescape) { Object o[] = new Object[jarray.size()]; for (int i = 0; i < jarray.size(); i++) { JsonElement jsonEl = jarray.get(i); if (jsonEl.isJsonObject()) { o[i] = convertJsonObjectToBson(jsonEl.getAsJsonObject(), bHtmlUnescape); }/*from w w w .j a v a2 s .co m*/ if (jsonEl.isJsonArray()) { o[i] = handleJsonArray(jsonEl.getAsJsonArray(), bHtmlUnescape); } else if (jsonEl.isJsonPrimitive()) { if (bHtmlUnescape) { o[i] = StringEscapeUtils.unescapeHtml(jsonEl.getAsString()); } else { o[i] = jsonEl.getAsString(); } } } return o; }