List of usage examples for com.google.gson JsonElement getAsJsonArray
public JsonArray getAsJsonArray()
From source file:com.couchbase.cbadmin.client.CouchbaseAdmin.java
License:Open Source License
@Override public List<Node> getNodes() throws RestApiException { List<Node> ret = new ArrayList<Node>(); JsonElement e; try {/*www.j av a 2s.c om*/ e = getJson(P_POOL_NODES); } catch (IOException ex) { throw new RestApiException(ex); } if (!e.isJsonObject()) { throw new RestApiException("Expected JsonObject", e); } JsonObject obj = e.getAsJsonObject(); JsonArray nodesArr; e = obj.get("nodes"); if (e == null) { throw new RestApiException("Expected 'nodes' array", obj); } nodesArr = e.getAsJsonArray(); for (int i = 0; i < nodesArr.size(); i++) { e = nodesArr.get(i); JsonObject nObj; if (!e.isJsonObject()) { throw new RestApiException("Malformed node entry", e); } nObj = e.getAsJsonObject(); Node n = new Node(nObj); ret.add(n); } return ret; }
From source file:com.couchbase.plugin.basement.api.Client.java
License:Open Source License
private void addJsonElement(String key, JsonElement value, HashMap<String, Object> map) { if (value.isJsonArray()) { JsonArray jsonArray = value.getAsJsonArray(); ArrayList listAsValue = new ArrayList<HashMap<String, Object>>(); HashMap<String, Object> listElementMap = new HashMap<String, Object>(); Iterator<JsonElement> jsonArrayIterator = jsonArray.iterator(); while (jsonArrayIterator.hasNext()) { JsonElement jsonElement = jsonArrayIterator.next(); listAsValue.add(parse(jsonElement.toString())); }/*from w ww .jav a 2 s. co m*/ map.put(key, listAsValue); } else if (value.isJsonPrimitive()) { // check the type using JsonPrimitive // TODO: check all types JsonPrimitive jsonPrimitive = value.getAsJsonPrimitive(); if (jsonPrimitive.isNumber()) { map.put(key, jsonPrimitive.getAsInt()); } else { map.put(key, jsonPrimitive.getAsString()); } } else { map.put(key, parse(value.toString())); } }
From source file:com.crowdmap.java.sdk.json.UsersDeserializer.java
License:Open Source License
@Override public List<User> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { List<User> users = new ArrayList<User>(); if (json.isJsonArray()) { for (JsonElement e : json.getAsJsonArray()) { users.add((User) context.deserialize(e, User.class)); }/*from ww w .j a v a 2 s . c o m*/ } else if (json.isJsonObject()) { users.add((User) context.deserialize(json, User.class)); } else { throw new CrowdmapException("Unexpected JSON type" + json.getClass()); } return users; }
From source file:com.crypticbit.diff.demo.swing.contacts.JsonJTreeNode.java
License:Apache License
private void populateChildren(JsonElement myJsonElement) { switch (dataType) { case ARRAY:/*from w w w . j ava2s . c o m*/ int index = 0; Iterator<JsonElement> it = myJsonElement.getAsJsonArray().iterator(); while (it.hasNext()) { JsonElement element = it.next(); JsonJTreeNode childNode = new JsonJTreeNode(null, index, element); this.add(childNode); index++; } break; case OBJECT: for (Entry<String, JsonElement> entry : myJsonElement.getAsJsonObject().entrySet()) { JsonJTreeNode childNode = new JsonJTreeNode(entry.getKey(), -1, entry.getValue()); this.add(childNode); } break; default: throw new IllegalStateException("Internal coding error this should never happen."); } }
From source file:com.crypticbit.diff.demo.swing.JSONJTreeNode.java
License:Apache License
private void populateChildren(JsonElement myJsonElement) { switch (dataType) { case ARRAY:/* w w w .j a v a2 s .c om*/ int index = 0; Iterator<JsonElement> it = myJsonElement.getAsJsonArray().iterator(); while (it.hasNext()) { JsonElement element = it.next(); JSONJTreeNode childNode = new JSONJTreeNode(null, index, element); this.add(childNode); index++; } break; case OBJECT: for (Entry<String, JsonElement> entry : myJsonElement.getAsJsonObject().entrySet()) { JSONJTreeNode childNode = new JSONJTreeNode(entry.getKey(), -1, entry.getValue()); this.add(childNode); } break; default: throw new IllegalStateException("Internal coding error this should never happen."); } }
From source file:com.cybozu.kintone.database.JsonParser.java
License:Apache License
/** * Converts json element to the sub table object. * @param element json element/*from w w w . ja va 2 s . c om*/ * @return sub table object * @throws IOException */ private List<Record> jsonToSubtable(JsonElement element) throws IOException { List<Record> rs = new ArrayList<Record>(); if (!element.isJsonArray()) return null; JsonArray records = element.getAsJsonArray(); for (JsonElement elem : records) { if (elem.isJsonObject()) { JsonObject obj = elem.getAsJsonObject(); String id = obj.get("id").getAsString(); JsonElement value = obj.get("value"); Record record = readRecord(value); if (record != null) { try { record.setId(Long.valueOf(id)); } catch (NumberFormatException e) { } rs.add(record); } } } return rs; }
From source file:com.devamatre.core.JSONHelper.java
License:Open Source License
/** * //from w w w . j a v a 2 s. c o m * @param jsonString * @return */ public static JsonArray toJSONArray(String jsonString) { JsonArray array = null; JsonElement jsonElement = jsonElement(jsonString); if (jsonElement.isJsonArray()) { array = jsonElement.getAsJsonArray(); } return array; }
From source file:com.driver733.vkuploader.wallpost.attachment.support.fields.AttachmentArrays.java
License:Open Source License
@Override public List<String> attachmentsFields() throws Exception { final List<AbstractQueryBuilder> queries = new QueriesFromAttachments(this.attachments).queries(); final JsonElement root = new VkApiClient( new TransportClientExecuteBatchCached(new QueryResultsBasic(new QueriesSafeCached(queries)))) .execute().batch(this.actor, new QueriesSafeNonCached(queries).queries()).execute(); try {/*w ww .jav a2s . c om*/ return new AttachmentsFromResults(root.getAsJsonArray(), this.group).attachmentStrings(); } catch (final IOException ex) { throw new IOException("Could not map query results to attachments", ex); } }
From source file:com.driver733.vkuploader.wallpost.attachment.support.fields.AttachmentArraysWithProps.java
License:Open Source License
@Override public List<String> attachmentsFields() throws Exception { final List<AbstractQueryBuilder> queries = new QueriesFromAttachments(this.attachments).queries(); final IdsMap ids = new IdsMap(queries); final JsonElement root = new VkApiClient( new TransportClientExecuteBatchCached(new QueryResultsBasic(new QueriesSafeCached(queries)))) .execute().batch(this.actor, new QueriesSafeNonCached(queries).queries()).execute(); new PropertiesUpdate(this.properties, ids.value(), root.getAsJsonArray()).run(); try {/*from ww w . j a v a 2s .c om*/ return new AttachmentsFromResults(root.getAsJsonArray(), this.group).attachmentStrings(); } catch (final IOException ex) { throw new IOException("Could not map idsMap queriesResults to attachments", ex); } }
From source file:com.ericsson.eiffel.remrem.publish.service.MessageServiceRMQImpl.java
License:Apache License
@Override public SendResult send(JsonElement json, MsgService msgService, String userDomainSuffix, String tag, String routingKey) {/*from w ww . j av a 2 s.com*/ Map<String, String> map = new HashMap<>(); Map<String, String> routingKeyMap = new HashMap<>(); SendResult result; resultList = new ArrayList<PublishResultItem>(); if (json == null) { createFailureResult(resultList); } if (json.isJsonArray()) { statusCodes = new ArrayList<Integer>(); checkEventStatus = true; JsonArray bodyJson = json.getAsJsonArray(); for (JsonElement obj : bodyJson) { String eventId = msgService.getEventId(obj.getAsJsonObject()); if (StringUtils.isNotEmpty(eventId) && checkEventStatus) { String routing_key = getAndCheckEvent(msgService, map, resultList, obj, routingKeyMap, userDomainSuffix, tag, routingKey); if (StringUtils.isNotBlank(routing_key)) { result = send(obj.toString(), msgService, userDomainSuffix, tag, routing_key); resultList.addAll(result.getEvents()); int statusCode = result.getEvents().get(0).getStatusCode(); if (!statusCodes.contains(statusCode)) statusCodes.add(statusCode); } else if (routing_key == null) { routingKeyGenerationFailure(resultList); errorItems = new ArrayList<JsonElement>(); int statusCode = resultList.get(0).getStatusCode(); statusCodes.add(statusCode); errorItems.add(obj); checkEventStatus = false; } else { PublishResultItem resultItem = rabbitmqConfigurationNotFound(msgService); resultList.add(resultItem); int statusCode = resultItem.getStatusCode(); statusCodes.add(statusCode); break; } } else { if (!checkEventStatus) { addUnsuccessfulResultItem(obj); int statusCode = resultList.get(0).getStatusCode(); statusCodes.add(statusCode); } else { createFailureResult(resultList); errorItems = new ArrayList<JsonElement>(); int statusCode = resultList.get(0).getStatusCode(); statusCodes.add(statusCode); errorItems.add(obj); checkEventStatus = false; } } } } else { statusCodes = new ArrayList<Integer>(); result = send(json.toString(), msgService, userDomainSuffix, tag, routingKey); resultList.addAll(result.getEvents()); int statusCode = result.getEvents().get(0).getStatusCode(); if (!statusCodes.contains(statusCode)) statusCodes.add(statusCode); } result = new SendResult(); result.setEvents(resultList); return result; }