List of usage examples for com.google.gson JsonElement getAsJsonArray
public JsonArray getAsJsonArray()
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 }//from w w w . j a v a2 s . com 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.index.ElasticSearchManager.java
License:Apache License
public BulkResponse bulkAddDocuments(JsonElement docsJson, String idFieldName, String sParentId, boolean bAllowOverwrite) { if (null != _multiIndex) { throw new RuntimeException("bulkAddDocuments not supported on multi-index manager"); }/* www. j av a 2 s .c om*/ if (!docsJson.isJsonArray()) { throw new RuntimeException("bulkAddDocuments - not a list"); } BulkRequestBuilder brb = _elasticClient.prepareBulk(); JsonArray docJsonArray = docsJson.getAsJsonArray(); for (JsonElement docJson : docJsonArray) { IndexRequest ir = new IndexRequest(_sIndexName); ir.type(_sIndexType); if (null != sParentId) { ir.parent(sParentId); } if (!bAllowOverwrite) { ir.opType(OpType.CREATE); } //TESTED // Some _id unpleasantness if (null != idFieldName) { String id = docJson.getAsJsonObject().get(idFieldName).getAsString(); ir.id(id); ir.source(docJson.toString()); } //TESTED brb.add(ir); } brb.setConsistencyLevel(WriteConsistencyLevel.ONE); return brb.execute().actionGet(); }
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/*from ww w . j av a 2 s . c o m*/ 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.SearchUtils.java
License:Apache License
public final List<Integer> getLineNumbers(final Collection<String> imports, final String tokens) { List<Integer> lineNumbers = new ArrayList<Integer>(); JsonReader reader = new JsonReader(new StringReader(tokens)); reader.setLenient(true);//from ww w . ja v a2s . c o m JsonArray tokensArray = new JsonParser().parse(reader).getAsJsonArray(); for (JsonElement token : tokensArray) { JsonObject jObject = token.getAsJsonObject(); String typeName = jObject.getAsJsonPrimitive(TYPE_EXACT_NAME).getAsString(); if (imports.contains(typeName)) { JsonArray propsArray = jObject.getAsJsonArray(PROPS); for (JsonElement propsInfo : propsArray) { JsonArray lineNumbersArray = propsInfo.getAsJsonObject().getAsJsonArray(LINES); for (JsonElement lineNumber : lineNumbersArray) { lineNumbers.add(lineNumber.getAsJsonArray().get(0).getAsInt()); } } } } return lineNumbers; }
From source file:com.impetus.client.couchdb.CouchDBClient.java
License:Apache License
@Override public <E> List<E> getColumnsById(String schemaName, String tableName, String pKeyColumnName, String inverseJoinColumnName, Object pKeyColumnValue, Class columnJavaType) { List<E> foreignKeys = new ArrayList<E>(); URI uri = null;/* ww w. j av a 2 s . co m*/ HttpResponse response = null; try { String q = "key=" + CouchDBUtils.appendQuotes(pKeyColumnValue); uri = new URI(CouchDBConstants.PROTOCOL, null, httpHost.getHostName(), httpHost.getPort(), CouchDBConstants.URL_SEPARATOR + schemaName.toLowerCase() + CouchDBConstants.URL_SEPARATOR + CouchDBConstants.DESIGN + tableName + CouchDBConstants.VIEW + pKeyColumnName, q, null); HttpGet get = new HttpGet(uri); get.addHeader("Accept", "application/json"); response = httpClient.execute(get); InputStream content = response.getEntity().getContent(); Reader reader = new InputStreamReader(content); JsonObject json = gson.fromJson(reader, JsonObject.class); JsonElement jsonElement = json.get("rows"); if (jsonElement == null) { return foreignKeys; } JsonArray array = jsonElement.getAsJsonArray(); for (JsonElement element : array) { JsonElement value = element.getAsJsonObject().get("value").getAsJsonObject() .get(inverseJoinColumnName); if (value != null) { foreignKeys.add((E) PropertyAccessorHelper.fromSourceToTargetClass(columnJavaType, String.class, value.getAsString())); } } } catch (Exception e) { log.error("Error while fetching column by id {}, Caused by {}.", pKeyColumnValue, e); throw new KunderaException(e); } finally { closeContent(response); } return foreignKeys; }
From source file:com.impetus.client.couchdb.CouchDBClient.java
License:Apache License
@Override public Object[] findIdsByColumn(String schemaName, String tableName, String pKeyName, String columnName, Object columnValue, Class entityClazz) { List foreignKeys = new ArrayList(); HttpResponse response = null;//from w w w. j a va 2s. c o m EntityMetadata m = KunderaMetadataManager.getEntityMetadata(kunderaMetadata, entityClazz); try { String q = "key=" + CouchDBUtils.appendQuotes(columnValue); URI uri = new URI(CouchDBConstants.PROTOCOL, null, httpHost.getHostName(), httpHost.getPort(), CouchDBConstants.URL_SEPARATOR + schemaName.toLowerCase() + CouchDBConstants.URL_SEPARATOR + CouchDBConstants.DESIGN + tableName + CouchDBConstants.VIEW + columnName, q, null); HttpGet get = new HttpGet(uri); get.addHeader("Accept", "application/json"); response = httpClient.execute(get); InputStream content = response.getEntity().getContent(); Reader reader = new InputStreamReader(content); JsonObject json = gson.fromJson(reader, JsonObject.class); JsonElement jsonElement = json.get("rows"); if (jsonElement == null) { return foreignKeys.toArray(); } JsonArray array = jsonElement.getAsJsonArray(); for (JsonElement element : array) { JsonElement value = element.getAsJsonObject().get("value").getAsJsonObject().get(pKeyName); if (value != null) { foreignKeys.add(PropertyAccessorHelper.fromSourceToTargetClass( m.getIdAttribute().getBindableJavaType(), String.class, value.getAsString())); } } } catch (Exception e) { log.error("Error while fetching ids for column where column name is" + columnName + " and column value is {} , Caused by {}.", columnValue, e); throw new KunderaException(e); } finally { closeContent(response); } return foreignKeys.toArray(); }
From source file:com.impetus.client.couchdb.CouchDBClient.java
License:Apache License
@Override public void deleteByColumn(String schemaName, String tableName, String columnName, Object columnValue) { URI uri = null;//from w w w. j a v a 2s . c om HttpResponse response = null; try { String q = "key=" + CouchDBUtils.appendQuotes(columnValue); uri = new URI(CouchDBConstants.PROTOCOL, null, httpHost.getHostName(), httpHost.getPort(), CouchDBConstants.URL_SEPARATOR + schemaName.toLowerCase() + CouchDBConstants.URL_SEPARATOR + CouchDBConstants.DESIGN + tableName + CouchDBConstants.VIEW + columnName, q, null); HttpGet get = new HttpGet(uri); get.addHeader("Accept", "application/json"); response = httpClient.execute(get); InputStream content = response.getEntity().getContent(); Reader reader = new InputStreamReader(content); JsonObject json = gson.fromJson(reader, JsonObject.class); JsonElement jsonElement = json.get("rows"); closeContent(response); JsonArray array = jsonElement.getAsJsonArray(); for (JsonElement element : array) { JsonObject jsonObject = element.getAsJsonObject().get("value").getAsJsonObject(); JsonElement pkey = jsonObject.get("_id"); onDelete(schemaName, pkey.getAsString(), response, jsonObject); } } catch (Exception e) { log.error("Error while deleting row by column where column name is " + columnName + " and column value is {}, Caused by {}.", columnValue, e); throw new KunderaException(e); } finally { closeContent(response); } }
From source file:com.impetus.client.couchdb.CouchDBClient.java
License:Apache License
/** * Gets the json from response.//from www . ja v a 2 s.c om * * @param response * the response * @return the json from response * @throws IOException * Signals that an I/O exception has occurred. */ private JsonArray getJsonFromResponse(HttpResponse response) throws IOException { InputStream content = response.getEntity().getContent(); Reader reader = new InputStreamReader(content); JsonObject json = gson.fromJson(reader, JsonObject.class); JsonElement jsonElement = json.get("rows"); return jsonElement == null ? null : jsonElement.getAsJsonArray(); }
From source file:com.intellij.openapi.updateSettings.impl.pluginsAdvertisement.PluginsAdvertiser.java
License:Apache License
public static List<PluginId> retrieve(UnknownFeature unknownFeature) { final String featureType = unknownFeature.getFeatureType(); final String implementationName = unknownFeature.getImplementationName(); final String buildNumber = ApplicationInfo.getInstance().getBuild().asString(); final String pluginRepositoryUrl = FEATURE_IMPLEMENTATIONS_URL + "featureType=" + featureType + "&implementationName=" + implementationName.replaceAll("#", "%23") + "&build=" + buildNumber; try {//ww w. j a va 2s . c o m HttpURLConnection connection = HttpConfigurable.getInstance().openHttpConnection(pluginRepositoryUrl); connection.connect(); final InputStreamReader streamReader = new InputStreamReader(connection.getInputStream()); try { final JsonReader jsonReader = new JsonReader(streamReader); jsonReader.setLenient(true); final JsonElement jsonRootElement = new JsonParser().parse(jsonReader); final List<PluginId> result = new ArrayList<PluginId>(); for (JsonElement jsonElement : jsonRootElement.getAsJsonArray()) { final JsonObject jsonObject = jsonElement.getAsJsonObject(); final JsonElement pluginId = jsonObject.get("pluginId"); result.add(PluginId.getId(StringUtil.unquoteString(pluginId.toString()))); } return result; } finally { streamReader.close(); } } catch (IOException e) { LOG.info(e); return null; } }
From source file:com.intellij.openapi.updateSettings.impl.pluginsAdvertisement.PluginsAdvertiser.java
License:Apache License
private static Map<String, Set<PluginId>> loadSupportedExtensions() { final String pluginRepositoryUrl = FEATURE_IMPLEMENTATIONS_URL + "featureType=" + FileTypeFactory.FILE_TYPE_FACTORY_EP.getName(); try {/*ww w .j a v a 2 s . c o m*/ HttpURLConnection connection = HttpConfigurable.getInstance().openHttpConnection(pluginRepositoryUrl); connection.connect(); final InputStreamReader streamReader = new InputStreamReader(connection.getInputStream()); try { final JsonReader jsonReader = new JsonReader(streamReader); jsonReader.setLenient(true); final JsonElement jsonRootElement = new JsonParser().parse(jsonReader); final Map<String, Set<PluginId>> result = new HashMap<String, Set<PluginId>>(); for (JsonElement jsonElement : jsonRootElement.getAsJsonArray()) { final JsonObject jsonObject = jsonElement.getAsJsonObject(); final JsonElement ext = jsonObject.get("implementationName"); final String extension = StringUtil.unquoteString(ext.toString()); Set<PluginId> pluginIds = result.get(extension); if (pluginIds == null) { pluginIds = new HashSet<PluginId>(); result.put(extension, pluginIds); } pluginIds.add(PluginId.getId(StringUtil.unquoteString(jsonObject.get("pluginId").toString()))); } saveExtensions(result); return result; } finally { streamReader.close(); } } catch (IOException e) { LOG.info(e); return null; } }