List of usage examples for com.google.gson JsonArray JsonArray
public JsonArray()
From source file:com.claresco.tinman.servlet.XapiServletUtility.java
License:Open Source License
protected static String createJsonArray(Set<String> theData) { JsonArray theArray = new JsonArray(); for (String s : theData) { theArray.add(new JsonPrimitive(s)); }/*from w w w .ja va2s. c om*/ return theArray.toString(); }
From source file:com.cloopen.rest.sdk.CCPRestSDK.java
License:Open Source License
private HashMap<String, Object> jsonToMap(String result) { HashMap<String, Object> hashMap = new HashMap<String, Object>(); JsonParser parser = new JsonParser(); JsonObject asJsonObject = parser.parse(result).getAsJsonObject(); Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet(); HashMap<String, Object> hashMap2 = new HashMap<String, Object>(); for (Map.Entry<String, JsonElement> m : entrySet) { if ("statusCode".equals(m.getKey()) || "statusMsg".equals(m.getKey())) hashMap.put(m.getKey(), m.getValue().getAsString()); else {/*from w w w. ja v a 2 s . c o m*/ if ("SubAccount".equals(m.getKey()) || "totalCount".equals(m.getKey()) || "smsTemplateList".equals(m.getKey()) || "token".equals(m.getKey()) || "callSid".equals(m.getKey()) || "state".equals(m.getKey()) || "downUrl".equals(m.getKey())) { if (!"SubAccount".equals(m.getKey()) && !"smsTemplateList".equals(m.getKey())) hashMap2.put(m.getKey(), m.getValue().getAsString()); else { try { if ((m.getValue().toString().trim().length() <= 2) && !m.getValue().toString().contains("[")) { hashMap2.put(m.getKey(), m.getValue().getAsString()); hashMap.put("data", hashMap2); break; } if (m.getValue().toString().contains("[]")) { hashMap2.put(m.getKey(), new JsonArray()); hashMap.put("data", hashMap2); continue; } JsonArray asJsonArray = parser.parse(m.getValue().toString()).getAsJsonArray(); ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>(); for (JsonElement j : asJsonArray) { Set<Entry<String, JsonElement>> entrySet2 = j.getAsJsonObject().entrySet(); HashMap<String, Object> hashMap3 = new HashMap<String, Object>(); for (Map.Entry<String, JsonElement> m2 : entrySet2) { hashMap3.put(m2.getKey(), m2.getValue().getAsString()); } arrayList.add(hashMap3); } hashMap2.put(m.getKey(), arrayList); } catch (Exception e) { JsonObject asJsonObject2 = parser.parse(m.getValue().toString()).getAsJsonObject(); Set<Entry<String, JsonElement>> entrySet2 = asJsonObject2.entrySet(); HashMap<String, Object> hashMap3 = new HashMap<String, Object>(); for (Map.Entry<String, JsonElement> m2 : entrySet2) { hashMap3.put(m2.getKey(), m2.getValue().getAsString()); } hashMap2.put(m.getKey(), hashMap3); hashMap.put("data", hashMap2); } } hashMap.put("data", hashMap2); } else { JsonObject asJsonObject2 = parser.parse(m.getValue().toString()).getAsJsonObject(); Set<Entry<String, JsonElement>> entrySet2 = asJsonObject2.entrySet(); HashMap<String, Object> hashMap3 = new HashMap<String, Object>(); for (Map.Entry<String, JsonElement> m2 : entrySet2) { hashMap3.put(m2.getKey(), m2.getValue().getAsString()); } if (hashMap3.size() != 0) { hashMap2.put(m.getKey(), hashMap3); } else { hashMap2.put(m.getKey(), m.getValue().getAsString()); } hashMap.put("data", hashMap2); } } } return hashMap; }
From source file:com.cloud.agent.transport.ArrayTypeAdaptor.java
License:Apache License
@Override public JsonElement serialize(T[] src, Type typeOfSrc, JsonSerializationContext context) { JsonArray array = new JsonArray(); for (T cmd : src) { JsonObject obj = new JsonObject(); obj.add(cmd.getClass().getName().substring(s_pkg.length()), _gson.toJsonTree(cmd)); array.add(obj);/*from w w w .j a v a2 s . co m*/ } return array; }
From source file:com.cloudant.client.api.Database.java
License:Open Source License
/** * Set permissions for a user/apiKey on this database. * <p>/*from w w w . ja v a2 s . co m*/ * Note this method is only applicable to databases that support the * <a href="http://docs.cloudant.com/authorization.html"> * Cloudant authorization API * </a> such as Cloudant DBaaS. For unsupported databases consider using the /db/_security * endpoint. * </p> * * @param userNameorApikey * @param permissions permissions to grant * @throws UnsupportedOperationException if called on a database that does not provide the * Cloudant authorization API * @see <a href="http://docs.cloudant.com/authorization.html#roles">Roles</a> * @see <a href="http://docs.cloudant.com/authorization.html#modifying-permissions">Modifying * permissions</a> */ public void setPermissions(String userNameorApikey, EnumSet<Permissions> permissions) { assertNotEmpty(userNameorApikey, "userNameorApikey"); assertNotEmpty(permissions, "permissions"); final JsonArray jsonPermissions = new JsonArray(); for (Permissions s : permissions) { final JsonPrimitive permission = new JsonPrimitive(s.toString()); jsonPermissions.add(permission); } // get existing permissions JsonObject perms = getPermissionsObject(); // now set back JsonElement elem = perms.get("cloudant"); if (elem == null) { perms.addProperty("_id", "_security"); elem = new JsonObject(); perms.add("cloudant", elem); } elem.getAsJsonObject().add(userNameorApikey, jsonPermissions); HttpResponse response = null; HttpPut put = new HttpPut(apiV2DBSecurityURI); setEntity(put, client.getGson().toJson(perms), "application/json"); try { response = executeRequest(put); String ok = getAsString(response, "ok"); if (!ok.equalsIgnoreCase("true")) { //raise exception } } finally { close(response); } }
From source file:com.cloudant.client.api.DatabaseImpl.java
License:Open Source License
@Override public void setPermissions(String userNameorApikey, EnumSet<Permissions> permissions) { assertNotEmpty(userNameorApikey, "userNameorApikey"); assertNotEmpty(permissions, "permissions"); final JsonArray jsonPermissions = new JsonArray(); for (Permissions s : permissions) { final JsonPrimitive permission = new JsonPrimitive(s.toString()); jsonPermissions.add(permission); }// w ww. j av a2s . co m // get existing permissions JsonObject perms = getPermissionsObject(); // now set back JsonElement elem = perms.get("cloudant"); if (elem == null) { perms.addProperty("_id", "_security"); elem = new JsonObject(); perms.add("cloudant", elem); } elem.getAsJsonObject().add(userNameorApikey, jsonPermissions); InputStream response = null; HttpConnection put = Http.PUT(apiV2DBSecurityURI, "application/json"); put.setRequestBody(client.getGson().toJson(perms)); try { response = client.couchDbClient.executeToInputStream(put); String ok = getAsString(response, "ok"); if (!ok.equalsIgnoreCase("true")) { //raise exception } } finally { close(response); } }
From source file:com.cloudant.client.api.DatabaseImpl.java
License:Open Source License
/** * Form a create index json from parameters */// w w w . java2s.co m private JsonObject getIndexDefinition(String indexName, String designDocName, String indexType, IndexField[] fields) { assertNotEmpty(fields, "index fields"); JsonObject indexObject = new JsonObject(); if (!(indexName == null || indexName.isEmpty())) { indexObject.addProperty("name", indexName); } if (!(designDocName == null || designDocName.isEmpty())) { indexObject.addProperty("ddoc", designDocName); } if (!(indexType == null || indexType.isEmpty())) { indexObject.addProperty("type", indexType); } JsonArray fieldsArray = new JsonArray(); for (int i = 0; i < fields.length; i++) { JsonObject fieldObject = new JsonObject(); fieldObject.addProperty(fields[i].getName(), fields[i].getOrder().toString()); fieldsArray.add(fieldObject); } JsonObject arrayOfFields = new JsonObject(); arrayOfFields.add("fields", fieldsArray); indexObject.add("index", arrayOfFields); return indexObject; }
From source file:com.cloudant.client.api.DatabaseImpl.java
License:Open Source License
private JsonObject getFindByIndexBody(String selectorJson, FindByIndexOptions options) { JsonArray fieldsArray = new JsonArray(); if (options.getFields().size() > 0) { for (String field : options.getFields()) { JsonPrimitive jsonField = client.getGson().fromJson(field, JsonPrimitive.class); fieldsArray.add(jsonField);/*w w w .ja v a2 s. com*/ } } JsonArray sortArray = new JsonArray(); if (options.getSort().size() > 0) { for (IndexField sort : options.getSort()) { JsonObject sortObject = new JsonObject(); sortObject.addProperty(sort.getName(), sort.getOrder().toString()); sortArray.add(sortObject); } } JsonObject indexObject = new JsonObject(); //parse and find if valid json issue #28 JsonObject selectorObject = null; boolean isObject = true; try { selectorObject = getGson().fromJson(selectorJson, JsonObject.class); } catch (JsonParseException e) { isObject = false; } if (!isObject) { if (selectorJson.startsWith("\"selector\"")) { selectorJson = selectorJson.substring(selectorJson.indexOf(":") + 1, selectorJson.length()).trim(); selectorObject = getGson().fromJson(selectorJson, JsonObject.class); } else { throw new JsonParseException("selectorJson should be valid json or like " + "\"selector\": {...} "); } } if (selectorObject.has("selector")) { indexObject.add("selector", selectorObject.get("selector")); } else { indexObject.add("selector", selectorObject); } if (fieldsArray.size() > 0) { indexObject.add("fields", fieldsArray); } if (sortArray.size() > 0) { indexObject.add("sort", sortArray); } if (options.getLimit() != null) { indexObject.addProperty("limit", options.getLimit()); } if (options.getSkip() != null) { indexObject.addProperty("skip", options.getSkip()); } if (options.getReadQuorum() != null) { indexObject.addProperty("r", options.getReadQuorum()); } if (options.getUseIndex() != null) { indexObject.add("use_index", getGson().fromJson(options.getUseIndex(), JsonArray.class)); } return indexObject; }
From source file:com.cloudant.client.api.model.FindByIndexOptions.java
License:Open Source License
/** * Specify a specific index to run the query against * * @param designDocument set the design document to use * @param indexName set the index name to use * @return this to set additional options *//*from w w w . ja v a 2 s.com*/ public FindByIndexOptions useIndex(String designDocument, String indexName) { assertNotNull(designDocument, "designDocument"); assertNotNull(indexName, "indexName"); JsonArray index = new JsonArray(); index.add(new JsonPrimitive(designDocument)); index.add(new JsonPrimitive(indexName)); this.useIndex = index; return this; }
From source file:com.cloudant.client.internal.views.ViewMultipleRequester.java
License:Open Source License
public List<ViewResponse<K, V>> getViewResponses() throws IOException { //build the queries array of data to POST JsonArray queries = new JsonArray(); ViewQueryParameters<K, V> viewQueryParameters = null; for (ViewQueryParameters<K, V> params : requestParameters) { if (viewQueryParameters == null) { viewQueryParameters = params; }/* ww w .j ava 2s . c o m*/ queries.add(params.asJson()); } JsonObject queryJson = new JsonObject(); queryJson.add("queries", queries); //construct and execute the POST request HttpPost post = new HttpPost(viewQueryParameters.getViewURIBuilder().buildEncoded()); StringEntity entity = new StringEntity(queryJson.toString(), "UTF-8"); entity.setContentType("application/json"); post.setEntity(entity); JsonObject jsonResponse = ViewRequester.executeRequestWithResponseAsJson(viewQueryParameters, post); //loop the returned array creating the ViewResponse objects List<ViewResponse<K, V>> responses = new ArrayList<ViewResponse<K, V>>(); JsonArray jsonResponses = jsonResponse.getAsJsonArray("results"); if (jsonResponses != null) { int index = 0; for (ViewQueryParameters<K, V> params : requestParameters) { JsonObject response = jsonResponses.get(index).getAsJsonObject(); responses.add(new ViewResponseImpl<K, V>(params, response)); index++; } return responses; } else { return Collections.emptyList(); } }
From source file:com.codenvy.ide.ext.java.server.TypeBindingConvector.java
License:Open Source License
private static JsonElement toJsonMethods(MethodBinding[] methods) { if (methods == null) return JsonNull.INSTANCE; JsonArray jsonElements = new JsonArray(); for (MethodBinding method : methods) { jsonElements.add(toJsonMethod(method)); }//from w w w. j av a 2 s.com return jsonElements; }