List of usage examples for com.google.gson JsonArray get
public JsonElement get(int i)
From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java
License:Open Source License
/** * A recursive merge of two json elements. * * @param source1 the first json element * @param source2 the second json element * @param mergeArray the flag to denote if arrays should be merged * @return the recursively merged json element *///from www .ja v a 2 s . c o m public JsonElement merge(JsonElement source1, JsonElement source2, Boolean mergeArray) { mergeArray = null == mergeArray ? Boolean.FALSE : mergeArray; JsonElement result = JsonNull.INSTANCE; source1 = asJsonElement(source1, JsonNull.INSTANCE); source2 = asJsonElement(source2, JsonNull.INSTANCE); if (source1.getClass().equals(source2.getClass())) { if (source1.isJsonObject()) { JsonObject obj1 = asJsonObject(source1); JsonObject obj2 = asJsonObject(source2); result = obj1; JsonObject resultObj = result.getAsJsonObject(); for (Entry<String, JsonElement> entry : obj1.entrySet()) { String key = entry.getKey(); JsonElement value1 = entry.getValue(); JsonElement value2 = obj2.get(key); JsonElement merge = merge(value1, value2, mergeArray); resultObj.add(key, merge); } for (Entry<String, JsonElement> entry : obj2.entrySet()) { String key = entry.getKey(); if (!resultObj.has(key)) { resultObj.add(key, entry.getValue()); } } } else if (source1.isJsonArray()) { result = new JsonArray(); JsonArray resultArray = result.getAsJsonArray(); JsonArray array1 = asJsonArray(source1); JsonArray array2 = asJsonArray(source2); int index = 0; int a1size = array1.size(); int a2size = array2.size(); if (!mergeArray) { for (; index < a1size && index < a2size; index++) { resultArray.add(merge(array1.get(index), array2.get(index), mergeArray)); } } for (; index < a1size; index++) { resultArray.add(array1.get(index)); } index = mergeArray ? 0 : index; for (; index < a2size; index++) { resultArray.add(array2.get(index)); } } else { result = source1 != null ? source1 : source2; } } else { result = isNotNull(source1) ? source1 : source2; } return result; }
From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java
License:Open Source License
/** * Replete array./*from ww w . j a v a 2s . c o m*/ * * @param source the source * @param index the index * @param type the type * @return the json array */ private JsonArray repleteArray(JsonArray source, Integer index, Class<? extends JsonElement> type) { source = isNull(source) ? new JsonArray() : source; for (int i = 0; i <= index; i++) { try { source.get(i); } catch (IndexOutOfBoundsException e) { Class<? extends JsonElement> newType = JsonNull.class; if (i == index) { newType = type; } source.add(getNewElement(newType)); } } if (!source.get(index).getClass().equals(type)) { log.warn(String.format( "The element at index %s in the source %s, does not match %s. Creating a new element.", index, source, type)); source.add(getNewElement(type)); } return source; }
From source file:com.basho.riakcs.client.impl.RiakCSClientImpl.java
License:Open Source License
private void addAdditionalACLImpl(String bucketName, String objectKey, JsonObject acl) throws Exception { StringBuilder aclText = new StringBuilder(); aclText.append("<AccessControlPolicy>"); aclText.append("<Owner><ID>").append(acl.getAsJsonObject("owner").get("id").getAsString()).append("</ID>"); aclText.append("<DisplayName>").append(acl.getAsJsonObject("owner").get("displayName").getAsString()) .append("</DisplayName></Owner>"); aclText.append("<AccessControlList>"); JsonArray grantsList = acl.getAsJsonArray("grantsList"); for (int pt = 0; pt < grantsList.size(); pt++) { JsonObject grant = grantsList.get(pt).getAsJsonObject(); aclText.append("<Grant><Permission>").append(grant.getAsJsonObject("permission").getAsString()) .append("</Permission>"); aclText.append("<Grantee"); aclText.append(" ").append("xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"); if (grant.getAsJsonObject("grantee").has("id")) { aclText.append(" xsi:type='CanonicalUser'>"); aclText.append("<ID>").append(grant.getAsJsonObject("grantee").get("id").getAsString()) .append("</ID>"); } else {//from w ww. ja v a2 s.c o m aclText.append(" xsi:type='AmazonCustomerByEmail'>"); aclText.append("<EmailAddress>") .append(grant.getAsJsonObject("grantee").get("emailAddress").getAsString()) .append("</EmailAddress>"); } aclText.append("</Grantee>"); aclText.append("</Grant>"); } aclText.append("</AccessControlList>"); aclText.append("</AccessControlPolicy>"); Map<String, String> parameters = new HashMap<String, String>(); parameters.put("acl", null); InputStream dataInputStream = new ByteArrayInputStream(aclText.toString().getBytes("UTF-8")); Map<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "application/xml"); CommunicationLayer comLayer = getCommunicationLayer(); URL url = comLayer.generateCSUrl(bucketName, objectKey, parameters); comLayer.makeCall(CommunicationLayer.HttpMethod.PUT, url, dataInputStream, headers); }
From source file:com.basho.riakcs.client.impl.RiakCSClientImpl.java
License:Open Source License
public void removeContentOfBucket(String bucketName) throws RiakCSException { JsonObject response = listObjects(bucketName, false); JsonArray resultList = response.getAsJsonArray("objectList"); // if (debugModeEnabled) System.out.println("Number of Objects to delete: "+ resultList.length() + "\n"); System.out.println("Number of Objects to delete: " + resultList.size() + "\n"); for (int pt = 0; pt < resultList.size(); pt++) { String key = resultList.get(pt).getAsJsonObject().get("key").getAsString(); deleteObject(bucketName, key);/*from w ww . j a v a 2 s.com*/ } }
From source file:com.basho.riakcs.client.impl.RiakCSClientImpl.java
License:Open Source License
public static void copyBucketBetweenSystems(RiakCSClient fromSystem, String fromBucket, RiakCSClient toSystem, String toBucket) throws RiakCSException { try {/*from w ww. j a v a 2 s. com*/ JsonObject response = fromSystem.listObjectNames(fromBucket); JsonArray resultList = response.getAsJsonArray("objectList"); System.out.println("Number of Objects to transfer: " + resultList.size() + "\n"); for (int pt = 0; pt < resultList.size(); pt++) { String key = resultList.get(pt).getAsJsonObject().get("key").getAsString(); File tempFile = File.createTempFile("cscopy-", ".bin"); //Retrieve Object FileOutputStream outputStream = new FileOutputStream(tempFile); JsonObject objectData = fromSystem.getObject(fromBucket, key, outputStream); outputStream.close(); //Upload Object Map<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", objectData.get("contenttype").getAsString()); Map<String, String> metadata = null; if (objectData.has("metadata") && !objectData.getAsJsonObject("metadata").entrySet().isEmpty()) { metadata = new HashMap<String, String>(); Set<Map.Entry<String, JsonElement>> metadataRaw = objectData.getAsJsonObject("metadata") .entrySet(); for (Map.Entry<String, JsonElement> entry : metadataRaw) { metadata.put(entry.getKey(), entry.getValue().getAsString()); } } FileInputStream inputStream = new FileInputStream(tempFile); toSystem.createObject(toBucket, key, inputStream, headers, metadata); inputStream.close(); tempFile.delete(); } } catch (Exception e) { throw new RiakCSException(e); } }
From source file:com.bbk.cgac.controller.pagecontroller.ax.SampleAjaxPageController.java
@Override public void execute() throws IOException { //Sample Code //Test Request JsonElement jel = mJson;//from w ww . j av a2s . c om if (jel.isJsonArray()) { JsonArray jarr = jel.getAsJsonArray(); if (jarr.get(0).isJsonObject()) { JsonObject jobj = jarr.get(0).getAsJsonObject(); System.out.println("JsonObject ."); System.out.println("a: " + jobj.get("a")); } if (jarr.get(1).isJsonArray()) { JsonArray jarr2 = jarr.get(1).getAsJsonArray(); System.out.println("jarr[1]? JsonArray ."); } } //Test Response (Example) TempObj obj = new TempObj("Kim"); String arg01 = JSON.parseToString(obj); TempObj obj1 = new TempObj("Lee"); String arg02 = JSON.parseToString(obj1); mResponse.setContentType("text/html;charset=UTF-8"); mResponse.getWriter().write("[" + arg01 + "," + arg02 + "]"); }
From source file:com.bedatadriven.rebar.appcache.server.DefaultSelectionServlet.java
License:Apache License
private String computePermutation(HttpServletRequest req, JsonArray permutationMap) throws ServletException { Map<String, String> properties = computeProperties(req); Set<String> matches = new HashSet<>(); for (int i = 0; i != permutationMap.size(); ++i) { JsonObject permutation = (JsonObject) permutationMap.get(i); if (matches(properties, permutation)) { matches.add(permutation.get("permutation").getAsString()); }/* w w w .ja v a2 s. c om*/ } if (matches.size() == 1) { return matches.iterator().next(); } else { return null; } }
From source file:com.birbit.jsonapi.JsonApiDeserializer.java
License:Apache License
private List<JsonApiError> parserErrors(JsonDeserializationContext context, JsonObject jsonObject) { JsonElement errors = jsonObject.get("errors"); if (errors == null || !errors.isJsonArray()) { return null; }/* w w w . jav a2s .c o m*/ JsonArray asJsonArray = errors.getAsJsonArray(); int size = asJsonArray.size(); List<JsonApiError> result = new ArrayList<JsonApiError>(size); for (int i = 0; i < size; i++) { result.add(context.<JsonApiError>deserialize(asJsonArray.get(i), JsonApiError.class)); } return result; }
From source file:com.birbit.jsonapi.JsonApiDeserializer.java
License:Apache License
private Map<String, Map<String, Object>> parseIncluded(JsonDeserializationContext context, JsonObject jsonObject) {//w w w .j ava2 s.c o m JsonElement includedElm = jsonObject.get("included"); Map<String, Map<String, Object>> included; if (includedElm != null && includedElm.isJsonArray()) { included = new HashMap<String, Map<String, Object>>(); JsonArray includedArray = includedElm.getAsJsonArray(); final int size = includedArray.size(); for (int i = 0; i < size; i++) { ResourceWithIdAndType parsed = parseResource(includedArray.get(i), context); if (parsed.resource != null) { Map<String, Object> itemMap = included.get(parsed.apiType); if (itemMap == null) { itemMap = new HashMap<String, Object>(); included.put(parsed.apiType, itemMap); } itemMap.put(parsed.id, parsed.resource); } } } else { included = Collections.emptyMap(); } return included; }
From source file:com.birbit.jsonapi.JsonApiDeserializer.java
License:Apache License
private T[] parseData(JsonDeserializationContext context, ParameterizedType parameterizedType, JsonObject jsonObject) {/*from ww w . j a va 2 s . c om*/ JsonElement dataElm = jsonObject.get("data"); if (dataElm != null) { Type typeArg = parameterizedType.getActualTypeArguments()[0]; if (dataElm.isJsonArray()) { JsonArray jsonArray = dataElm.getAsJsonArray(); final int size = jsonArray.size(); // boolean isArray = typeArg instanceof GenericArrayType; // if (isArray) { TypeToken<?> typeToken = TypeToken.get(typeArg); T[] result = (T[]) Array.newInstance(typeToken.getRawType().getComponentType(), size); for (int i = 0; i < size; i++) { ResourceWithIdAndType<T> resourceWithIdAndType = parseResource(jsonArray.get(i), context); result[i] = resourceWithIdAndType.resource; } return result; // } else { // TypeToken<?> typeToken = TypeToken.get(typeArg); // T[] result = (T[]) Array.newInstance(typeToken.getRawType().getComponentType(), size); // for (int i = 0; i < size; i ++) { // ResourceWithIdAndType<T> resourceWithIdAndType = parseResource(jsonArray.get(i), context); // //noinspection unchecked // result[i] = resourceWithIdAndType.resource; // } // return result; // } } else if (dataElm.isJsonObject()) { T resource = parseResource(dataElm, context).resource; Object[] result = (Object[]) Array.newInstance(resource.getClass(), 1); result[0] = resource; return (T[]) result; } } return null; }