List of usage examples for com.google.gson JsonElement isJsonObject
public boolean isJsonObject()
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()); }/* w w w .j a v a 2 s . com*/ } } 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 . ja va2 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; }
From source file:com.ikanow.infinit.e.data_model.index.document.DocumentPojoIndexMap.java
License:Apache License
private static boolean enforceTypeNamingPolicy(JsonElement je, int nDepth) { if (je.isJsonPrimitive()) { return false; // Done } else if (je.isJsonArray()) { JsonArray ja = je.getAsJsonArray(); if (0 == ja.size()) { return false; // No idea, carry on }// w w w .j a v a2 s . c o m JsonElement jaje = ja.get(0); return enforceTypeNamingPolicy(jaje, nDepth + 1); // keep going until you find primitive/object } else if (je.isJsonObject()) { JsonObject jo = je.getAsJsonObject(); // Nested variables: Iterator<Entry<String, JsonElement>> it = jo.entrySet().iterator(); StringBuffer newName = null; Map<String, JsonElement> toFixList = null; while (it.hasNext()) { boolean bFix = false; Entry<String, JsonElement> el = it.next(); String currKey = el.getKey(); if ((currKey.indexOf('.') >= 0) || (currKey.indexOf('%') >= 0)) { it.remove(); currKey = currKey.replace("%", "%25").replace(".", "%2e"); bFix = true; } if (null == el.getValue()) { if (!bFix) it.remove(); // nice easy case, just get rid of it (if bFix, it's already removed) bFix = false; } else if (enforceTypeNamingPolicy(el.getValue(), nDepth + 1)) { // rename! if (currKey.indexOf("__") < 0) { // unless it's an es type if (!bFix) it.remove(); // (if bFix, it's already removed) if (null == newName) { newName = new StringBuffer(); } else { newName.setLength(0); } currKey = newName.append(currKey).append("__obj").toString(); bFix = true; } } // (end check if need to rename) if (bFix) { if (null == toFixList) { toFixList = new HashMap<String, JsonElement>(); } toFixList.put(currKey, el.getValue()); } } // (end loop over params) if (null != toFixList) { for (Entry<String, JsonElement> el : toFixList.entrySet()) { jo.add(el.getKey(), el.getValue()); } } return true; // (in any case, I get renamed by calling parent) } return false; }
From source file:com.ikanow.infinit.e.data_model.store.MongoDbUtil.java
License:Apache License
public static Object encodeUnknown(JsonElement from) { if (from.isJsonArray()) { // Array return encodeArray(from.getAsJsonArray()); } //TESTED/* www. j a v a 2 s .c om*/ else if (from.isJsonObject()) { // Object JsonObject obj = from.getAsJsonObject(); // Check for OID/Date: if (1 == obj.entrySet().size()) { if (obj.has("$date")) { try { return _format.parse(obj.get("$date").getAsString()); } catch (ParseException e) { try { return _format2.parse(obj.get("$date").getAsString()); } catch (ParseException e2) { return null; } } } //TESTED else if (obj.has("$oid")) { return new ObjectId(obj.get("$oid").getAsString()); } //TESTED } return encode(obj); } //TESTED else if (from.isJsonPrimitive()) { // Primitive JsonPrimitive val = from.getAsJsonPrimitive(); if (val.isNumber()) { return val.getAsNumber(); } //TESTED else if (val.isBoolean()) { return val.getAsBoolean(); } //TESTED else if (val.isString()) { return val.getAsString(); } //TESTED } //TESTED return null; }
From source file:com.imaginea.kodebeagle.base.util.ESUtils.java
License:Apache License
protected final JsonObject getJsonElements(final String esResultJson) { JsonReader reader = new JsonReader(new StringReader(esResultJson)); reader.setLenient(true);/* w w w. j av a 2 s . c om*/ JsonElement jsonElement = new JsonParser().parse(reader); if (jsonElement.isJsonObject()) { return jsonElement.getAsJsonObject().getAsJsonObject(HITS); } else { return null; } }
From source file:com.imaginea.kodebeagle.base.util.SearchUtils.java
License:Apache License
protected final JsonObject getJsonElements(final String esResultJson) { JsonReader reader = new JsonReader(new StringReader(esResultJson)); reader.setLenient(true);/*from w w w . j a va2s.com*/ JsonElement jsonElement = new JsonParser().parse(reader); if (jsonElement.isJsonObject()) { return jsonElement.getAsJsonObject(); } else { return null; } }
From source file:com.indragie.cmput301as1.JSONHelpers.java
License:Open Source License
/** * Gets the value of the JSON element as a JSON object if possible. * @param element The JSON element.//from ww w .j a v a 2 s . com * @return A string if the JSON element was a JSON object, or * null otherwise. */ public static JsonObject getJsonObjectIfPossible(JsonElement element) { if (element == null) return null; if (element.isJsonObject()) { return element.getAsJsonObject(); } return null; }
From source file:com.intellij.plugins.haxe.haxelib.HaxelibMetadata.java
License:Apache License
private JsonObject parse(String jsonData) { if (null == jsonData || jsonData.isEmpty()) { LOG.debug("Empty json metadata."); return null; }/*from www. ja v a 2 s. c o m*/ JsonParser parser = new JsonParser(); JsonElement root = parser.parse(jsonData); if (!root.isJsonObject()) { LOG.debug("Unexpected JSON type (expected JsonObject)."); return null; } return root.getAsJsonObject(); }
From source file:com.javacreed.examples.gson.part3.AuthorDeserializer.java
License:Apache License
@Override public Author deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { // Only the ID is available if (json.isJsonPrimitive()) { final JsonPrimitive primitive = json.getAsJsonPrimitive(); return getOrCreate(primitive.getAsInt()); }// w w w . java 2s . c om // The whole object is available if (json.isJsonObject()) { final JsonObject jsonObject = json.getAsJsonObject(); final Author author = getOrCreate(jsonObject.get("id").getAsInt()); author.setName(jsonObject.get("name").getAsString()); return author; } throw new JsonParseException("Unexpected JSON type: " + json.getClass().getSimpleName()); }