List of usage examples for com.fasterxml.jackson.databind JsonNode elements
public Iterator<JsonNode> elements()
From source file:org.jberet.support.io.MongoItemReaderTest.java
static void addTestData(final String dataResource, final String mongoCollection, final int minSizeIfExists) throws Exception { final DBCollection collection = db.getCollection(mongoCollection); if (collection.find().count() >= minSizeIfExists) { System.out.printf("The readCollection %s already contains 100 items, skip adding test data.%n", mongoCollection);/*from w w w.j a va 2 s .co m*/ return; } InputStream inputStream = MongoItemReaderTest.class.getClassLoader().getResourceAsStream(dataResource); if (inputStream == null) { try { final URL url = new URI(dataResource).toURL(); inputStream = url.openStream(); } catch (final Exception e) { System.out.printf("Failed to convert dataResource %s to URL: %s%n", dataResource, e); } } if (inputStream == null) { throw new IllegalStateException("The inputStream for the test data is null"); } final JsonFactory jsonFactory = new MappingJsonFactory(); final JsonParser parser = jsonFactory.createParser(inputStream); final JsonNode arrayNode = parser.readValueAs(ArrayNode.class); final Iterator<JsonNode> elements = arrayNode.elements(); final List<DBObject> dbObjects = new ArrayList<DBObject>(); while (elements.hasNext()) { final DBObject dbObject = (DBObject) JSON.parse(elements.next().toString()); dbObjects.add(dbObject); } collection.insert(dbObjects); }
From source file:system.controllers.KeywordApp.java
/** * //ww w . ja va 2s . c o m * @Title: keyWordExcel * @Description: ?excel * @param type * @param startDate * @param endDate ? * @param searchText * @return */ public static Result keyWordExcel(String type, String startDate, String endDate, String searchText) { String result = getKeyWordCount(searchText, type, startDate, endDate, "-1"); JsonNode json = play.libs.Json.parse(result); json = json.findPath("data"); Iterator<JsonNode> iterator = json.elements(); List<KeyWordVO> list = new ArrayList<KeyWordVO>(); while (iterator.hasNext()) { json = iterator.next(); KeyWordVO vo = new KeyWordVO(); vo.setWord(json.findPath("word").textValue()); vo.setSearchNum(json.findPath("searchNum").asInt()); list.add(vo); } try { Long time = System.currentTimeMillis(); play.mvc.Http.Response response = response(); response.setContentType("application/x-msdownload;"); response.setHeader("Content-disposition", "attachment; filename=" + java.net.URLEncoder .encode("_" + getExcelName(type, startDate, endDate) + ".xls", "UTF-8")); ByteArrayOutputStream os = new ByteArrayOutputStream(); ExcelUtil.export(KeyWordVO.class, list, os); byte[] excel = os.toByteArray(); os.close(); response.setHeader("Content-Length", String.valueOf(excel.length)); return ok(excel); } catch (Exception e) { e.printStackTrace(); } return ok(); }
From source file:org.fcrepo.camel.processor.EventProcessor.java
private static Optional<List<String>> getValues(final JsonNode node, final String fieldName) { if (node.has(fieldName)) { final JsonNode field = node.get(fieldName); if (field.isArray()) { final List<String> elements = new ArrayList<>(); field.elements().forEachRemaining(elem -> { if (elem.isTextual()) { elements.add(elem.asText()); }/*from w w w .j a va2s. c o m*/ }); return of(elements); } else if (field.isTextual()) { return of(singletonList(field.asText())); } } return empty(); }
From source file:utils.SampleData.java
public static String convertToHexString(JsonNode node) { String result = null;/*from w w w . j ava2 s . c o m*/ if (node != null) { if (node.isArray()) { Iterator<JsonNode> arrayIterator = node.elements(); StringBuilder sb = new StringBuilder(); while (arrayIterator.hasNext()) { JsonNode elementNode = arrayIterator.next(); if (elementNode != null) { String text = elementNode.asText(); if (StringUtils.isNotBlank(text)) { byte[] bytes = text.getBytes(); if (bytes != null && bytes.length > 0) { sb.append(String.format("%02x", text.getBytes()[0])); } } } } result = sb.toString(); } else { result = node.asText(); } } return result; }
From source file:com.ikanow.aleph2.data_model.utils.JsonUtils.java
/** Converts (possibly recursively) a JsonNode to its Java equivalent * @param to_convert - the JsonNode to convert to... * @return - ...a Java primitive, or Map<String, Object>, or List<Object> (where Object is a java type) *//*from w ww. jav a 2s .c o m*/ public static Object jacksonToJava(final JsonNode to_convert) { final JsonNodeType type = to_convert.getNodeType(); // (we'll go old school for this...) switch (type) { case ARRAY: return Optionals.streamOf(to_convert.elements(), false).map(j -> jacksonToJava(j)) .collect(Collectors.toList()); case BINARY: try { return to_convert.binaryValue(); } catch (IOException e) { return null; } case BOOLEAN: return to_convert.asBoolean(); case NUMBER: if (to_convert.isFloatingPointNumber()) { return to_convert.asDouble(); } else { return to_convert.asLong(); } case OBJECT: return _mapper.convertValue(to_convert, Map.class); case POJO: return ((POJONode) to_convert).getPojo(); case STRING: return to_convert.asText(); default: // (MISSING, NULL) return null; } }
From source file:org.neo4j.ogm.drivers.http.request.HttpRequest.java
private static String parseError(String responseBody) { try {//from w ww . j a v a 2s. c o m final JsonNode responseNode = OBJECT_MAPPER.readTree(responseBody); final JsonNode errors = responseNode.findValue("errors"); if (errors.elements().hasNext()) { final JsonNode errorNode = errors.elements().next(); return errorNode.findValue("message").asText(); } else { return responseBody; } } catch (JsonParseException e) { // Don't return the responseBody here as it is logged in #execute // See: https://www.owasp.org/index.php/Log_Injection, returning // it above should be rethought as well. return JSON_PARSE_ERROR_EXCEPTION_MESSAGE; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:models.service.reminder.RemindService.java
/** * ????// www . j av a2 s .co m * * @param cfg json * @param items ??item * * @return true:, false: */ public static boolean verifyCfg(JsonNode cfg, Item[] items) { if (null == cfg) { return false; } boolean isSuccess = true; int count = 0; for (Item item : items) { if (!item.isEnable) { continue; } String itemVal = item.getVal(); // ?? if (cfg.hasNonNull(itemVal) && cfg.get(itemVal).isArray()) { JsonNode optionsNode = cfg.get(itemVal); Iterator<JsonNode> optionsIt = optionsNode.elements(); // ?? Set<String> optionSet = new HashSet<>(); while (optionsIt.hasNext()) { String optionString = optionsIt.next().asText(); if (!Option.isContainVal(optionString)) { isSuccess = false; break; } optionSet.add(optionString); } if (!isSuccess) { break; } // ????? if (optionSet.size() != optionsNode.size()) { isSuccess = false; break; } count++; } else { isSuccess = false; break; } } // ? if (cfg.size() > count) { isSuccess = false; } return isSuccess; }
From source file:io.gravitee.policy.oauth2.Oauth2Policy.java
static boolean hasRequiredScopes(JsonNode oauthResponseNode, List<String> requiredScopes, String scopeSeparator) {/*from w w w .j a v a 2 s . c o m*/ if (requiredScopes == null) { return true; } JsonNode scopesNode = oauthResponseNode.path(OAUTH_PAYLOAD_SCOPE_NODE); List<String> scopes; if (scopesNode instanceof ArrayNode) { Iterator<JsonNode> scopeIterator = scopesNode.elements(); scopes = new ArrayList<>(scopesNode.size()); List<String> finalScopes = scopes; scopeIterator.forEachRemaining(jsonNode -> finalScopes.add(jsonNode.asText())); } else { scopes = Arrays.asList(scopesNode.asText().split(scopeSeparator)); } return scopes.containsAll(requiredScopes); }
From source file:com.msopentech.odatajclient.engine.data.json.DOMTreeUtils.java
/** * Recursively builds DOM content out of JSON subtree rooted at given node. * * @param document root of the DOM document being built * @param parent parent of the nodes being generated during this step * @param node JSON node to be used as source for DOM elements *//*w w w. j av a2 s. co m*/ public static void buildSubtree(final Element parent, final JsonNode node) { final Iterator<String> fieldNameItor = node.fieldNames(); final Iterator<JsonNode> nodeItor = node.elements(); while (nodeItor.hasNext()) { final JsonNode child = nodeItor.next(); final String name = fieldNameItor.hasNext() ? fieldNameItor.next() : ""; // no name? array item if (name.isEmpty()) { final Element element = parent.getOwnerDocument().createElementNS(ODataConstants.NS_DATASERVICES, ODataConstants.PREFIX_DATASERVICES + ODataConstants.ELEM_ELEMENT); parent.appendChild(element); if (child.isValueNode()) { if (child.isNull()) { element.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_NULL, Boolean.toString(true)); } else { element.appendChild(parent.getOwnerDocument().createTextNode(child.asText())); } } if (child.isContainerNode()) { buildSubtree(element, child); } } else if (!name.contains("@") && !ODataConstants.JSON_TYPE.equals(name)) { final Element property = parent.getOwnerDocument().createElementNS(ODataConstants.NS_DATASERVICES, ODataConstants.PREFIX_DATASERVICES + name); parent.appendChild(property); boolean typeSet = false; if (node.hasNonNull(name + "@" + ODataConstants.JSON_TYPE)) { property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE, node.get(name + "@" + ODataConstants.JSON_TYPE).textValue()); typeSet = true; } if (child.isNull()) { property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_NULL, Boolean.toString(true)); } else if (child.isValueNode()) { if (!typeSet) { if (child.isInt()) { property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE, EdmSimpleType.Int32.toString()); } if (child.isLong()) { property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE, EdmSimpleType.Int64.toString()); } if (child.isBigDecimal()) { property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE, EdmSimpleType.Decimal.toString()); } if (child.isDouble()) { property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE, EdmSimpleType.Double.toString()); } if (child.isBoolean()) { property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE, EdmSimpleType.Boolean.toString()); } if (child.isTextual()) { property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE, EdmSimpleType.String.toString()); } } property.appendChild(parent.getOwnerDocument().createTextNode(child.asText())); } else if (child.isContainerNode()) { if (!typeSet && child.hasNonNull(ODataConstants.JSON_TYPE)) { property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE, child.get(ODataConstants.JSON_TYPE).textValue()); } final String type = property.getAttribute(ODataConstants.ATTR_M_TYPE); if (StringUtils.isNotBlank(type) && EdmSimpleType.isGeospatial(type)) { if (EdmSimpleType.Geography.toString().equals(type) || EdmSimpleType.Geometry.toString().equals(type)) { final String geoType = child.get(ODataConstants.ATTR_TYPE).textValue(); property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE, geoType.startsWith("Geo") ? EdmSimpleType.namespace() + "." + geoType : type + geoType); } if (child.has(ODataConstants.JSON_COORDINATES) || child.has(ODataConstants.JSON_GEOMETRIES)) { GeospatialJSONHandler.deserialize(child, property, property.getAttribute(ODataConstants.ATTR_M_TYPE)); } } else { buildSubtree(property, child); } } } } }
From source file:com.msopentech.odatajclient.engine.data.json.DOMTreeUtilsV4.java
public static void buildSubtree(final Element parent, final JsonNode node) { final String v4AnnotationPrefix = "@"; final Iterator<String> fieldNameItor = node.fieldNames(); final Iterator<JsonNode> nodeItor = node.elements(); while (nodeItor.hasNext()) { final JsonNode child = nodeItor.next(); final String name = fieldNameItor.hasNext() ? fieldNameItor.next() : ""; // no name? array item if (name.isEmpty()) { final Element element = parent.getOwnerDocument().createElementNS(ODataConstants.NS_DATASERVICES, ODataConstants.PREFIX_DATASERVICES + ODataConstants.ELEM_ELEMENT); parent.appendChild(element); if (child.isValueNode()) { if (child.isNull()) { element.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_NULL, Boolean.toString(true)); } else { element.appendChild(parent.getOwnerDocument().createTextNode(child.asText())); }/* w w w . j ava 2 s . c o m*/ } if (child.isContainerNode()) { buildSubtree(element, child); } } else if (!name.contains("@") && !ODataConstants.JSON_TYPE.equals(name)) { final Element property = parent.getOwnerDocument().createElementNS(ODataConstants.NS_DATASERVICES, ODataConstants.PREFIX_DATASERVICES + name); parent.appendChild(property); boolean typeSet = false; if (node.hasNonNull(name + "@" + ODataConstants.JSON_TYPE)) { property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE, node.get(name + "@" + ODataConstants.JSON_TYPE).textValue()); typeSet = true; } if (child.isNull()) { property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_NULL, Boolean.toString(true)); } else if (child.isValueNode()) { if (!typeSet) { if (child.isInt()) { property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE, EdmSimpleType.Int32.toString()); } if (child.isLong()) { property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE, EdmSimpleType.Int64.toString()); } if (child.isBigDecimal()) { property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE, EdmSimpleType.Decimal.toString()); } if (child.isDouble()) { property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE, EdmSimpleType.Double.toString()); } if (child.isBoolean()) { property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE, EdmSimpleType.Boolean.toString()); } if (child.isTextual()) { property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE, EdmSimpleType.String.toString()); } } property.appendChild(parent.getOwnerDocument().createTextNode(child.asText())); } else if (child.isContainerNode()) { if (!typeSet && child.hasNonNull(v4AnnotationPrefix + ODataConstants.JSON_TYPE)) { property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE, child.get(v4AnnotationPrefix + ODataConstants.JSON_TYPE).textValue()); } final String type = property.getAttribute(ODataConstants.ATTR_M_TYPE); if (StringUtils.isNotBlank(type) && EdmSimpleType.isGeospatial(type)) { if (EdmSimpleType.Geography.toString().equals(type) || EdmSimpleType.Geometry.toString().equals(type)) { final String geoType = child.get(ODataConstants.ATTR_TYPE).textValue(); property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE, geoType.startsWith("Geo") ? EdmSimpleType.namespace() + "." + geoType : type + geoType); } if (child.has(ODataConstants.JSON_COORDINATES) || child.has(ODataConstants.JSON_GEOMETRIES)) { GeospatialJSONHandler.deserialize(child, property, property.getAttribute(ODataConstants.ATTR_M_TYPE)); } } else { buildSubtree(property, child); } } } } }