List of usage examples for com.google.gson JsonElement isJsonArray
public boolean isJsonArray()
From source file:com.crypticbit.diff.demo.swing.JSONJTreeNode.java
License:Apache License
/** * @param fieldName/*from w w w . j a v a 2 s. com*/ * - name of field if applicable or null * @param index * - index of element in the array or -1 if not part of an array * @param jsonElement * - element to represent */ public JSONJTreeNode(String fieldName, int index, JsonElement jsonElement) { this.index = index; this.fieldName = fieldName; if (jsonElement.isJsonArray()) { this.dataType = DataType.ARRAY; this.value = jsonElement.toString(); populateChildren(jsonElement); } else if (jsonElement.isJsonObject()) { this.dataType = DataType.OBJECT; this.value = jsonElement.toString(); populateChildren(jsonElement); } else if (jsonElement.isJsonPrimitive()) { this.dataType = DataType.VALUE; this.value = jsonElement.toString(); } else if (jsonElement.isJsonNull()) { this.dataType = DataType.VALUE; this.value = jsonElement.toString(); } else { throw new IllegalArgumentException("jsonElement is an unknown element type."); } }
From source file:com.cybozu.kintone.database.JsonParser.java
License:Apache License
/** * Converts json element to the sub table object. * @param element json element//w w w . ja v a2 s .c o m * @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.cybozu.kintone.database.JsonParser.java
License:Apache License
/** * Converts json element to the string array object. * @param element json element//w ww. ja va 2 s .c om * @return string array object * @throws IOException */ private List<String> jsonToStringArray(JsonElement element) { if (!element.isJsonArray()) return null; Type collectionType = new TypeToken<Collection<String>>() { }.getType(); Gson gson = new Gson(); return gson.fromJson(element, collectionType); }
From source file:com.cybozu.kintone.database.JsonParser.java
License:Apache License
/** * Converts json element to the user array object. * @param element json element/*from w w w . jav a 2 s. c o m*/ * @return user array object * @throws IOException */ private List<UserDto> jsonToUserArray(JsonElement element) { if (!element.isJsonArray()) return null; Type collectionType = new TypeToken<Collection<UserDto>>() { }.getType(); Gson gson = new Gson(); return gson.fromJson(element, collectionType); }
From source file:com.cybozu.kintone.database.JsonParser.java
License:Apache License
/** * Converts json element to the file array object. * @param element json element//from w ww .jav a2 s . com * @return file array object * @throws IOException */ private List<FileDto> jsonToFileArray(JsonElement element) { if (!element.isJsonArray()) return null; Type collectionType = new TypeToken<Collection<FileDto>>() { }.getType(); Gson gson = new Gson(); return gson.fromJson(element, collectionType); }
From source file:com.devamatre.core.JSONHelper.java
License:Open Source License
/** * //from w ww . j ava 2s. 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.diversityarrays.dalclient.DalUtil.java
License:Open Source License
static public DalResponseRecord createFrom(String requestUrl, String tagName, JsonObject input) { DalResponseRecord result = new DalResponseRecord(requestUrl, tagName); for (Map.Entry<String, JsonElement> entry : input.entrySet()) { String key = entry.getKey(); JsonElement value = entry.getValue(); if (value == null || value.isJsonNull()) { result.rowdata.put(key, ""); } else if (value.isJsonArray()) { JsonArray array = (JsonArray) value; int count = 0; for (JsonElement elt : array) { if (elt != null && elt.isJsonObject()) { JsonObject child = (JsonObject) elt; Map<String, String> childMap = asRowdata(child); if (childMap != null) { result.addNestedData(key, childMap); }//from w ww .j ava2s . c om } else { result.warnings.add(String.format("unexpected value-type for '%s'[%d] :%s", //$NON-NLS-1$ key, count, elt == null ? "null" : elt.getClass().getName())); } ++count; } } else if (value.isJsonPrimitive()) { JsonPrimitive prim = (JsonPrimitive) value; result.rowdata.put(key, prim.getAsString()); } else if (value.isJsonObject()) { // ?? perhaps Map<String, String> childMap = asRowdata((JsonObject) value); if (childMap != null) { result.addNestedData(key, childMap); } } else { result.warnings.add(String.format("unexpected value-type for '%s' :%s", //$NON-NLS-1$ key, value.getClass().getName())); } } return result; }
From source file:com.edduarte.argus.reader.JsonReader.java
License:Apache License
private void readRecursive(JsonElement root, MutableString collector) { if (root.isJsonObject()) { JsonObject object = (JsonObject) root; object.entrySet().forEach(e -> { collector.append(e.getKey()); collector.append(' '); readRecursive(e.getValue(), collector); collector.append(' '); });//from w w w .j a va 2s . c o m } else if (root.isJsonArray()) { JsonArray array = (JsonArray) root; array.forEach(child -> { readRecursive(child, collector); collector.append(' '); }); } else if (root.isJsonPrimitive()) { JsonPrimitive primitive = (JsonPrimitive) root; collector.append(primitive.getAsString()); collector.append(' '); } }
From source file:com.ericsson.eiffel.remrem.publish.service.MessageServiceRMQImpl.java
License:Apache License
@Override public SendResult send(String jsonContent, MsgService msgService, String userDomainSuffix, String tag, String routingKey) {// ww w. j ava 2 s . c o m JsonParser parser = new JsonParser(); try { JsonElement json = parser.parse(jsonContent); if (json.isJsonArray()) { return send(json, msgService, userDomainSuffix, tag, routingKey); } else { Map<String, String> map = new HashMap<>(); Map<String, String> routingKeyMap = new HashMap<>(); String eventId = msgService.getEventId(json.getAsJsonObject()); if (StringUtils.isNotBlank(eventId)) { String routing_key = PublishUtils.getRoutingKey(msgService, json.getAsJsonObject(), rmqHelper, userDomainSuffix, tag, routingKey); if (StringUtils.isNotBlank(routing_key)) { map.put(eventId, json.toString()); routingKeyMap.put(eventId, routing_key); } else if (routing_key == null) { List<PublishResultItem> resultItemList = new ArrayList<>(); routingKeyGenerationFailure(resultItemList); return new SendResult(resultItemList); } else { List<PublishResultItem> resultItemList = new ArrayList<>(); PublishResultItem resultItem = rabbitmqConfigurationNotFound(msgService); resultItemList.add(resultItem); return new SendResult(resultItemList); } } else { List<PublishResultItem> resultItemList = new ArrayList<>(); createFailureResult(resultItemList); return new SendResult(resultItemList); } return send(routingKeyMap, map, msgService); } } catch (final JsonSyntaxException e) { String resultMsg = "Could not parse JSON."; if (e.getCause() != null) { resultMsg = resultMsg + " Cause: " + e.getCause().getMessage(); } log.error(resultMsg, e.getMessage()); List<PublishResultItem> resultItemList = new ArrayList<>(); createFailureResult(resultItemList); return new SendResult(resultItemList); } }
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 ww w . j a v a 2s . c om 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; }