List of usage examples for com.fasterxml.jackson.databind JsonNode iterator
public final Iterator<JsonNode> iterator()
From source file:es.bsc.amon.util.tree.TreeNodeFactory.java
public static TreeNode fromJson(JsonNode json) { TreeNode n = null;/*from w w w . j a va 2 s .com*/ if (json.isArray()) { n = new NodeArray(); Iterator<JsonNode> it = json.iterator(); while (it.hasNext()) { ((NodeArray) n).elements.add(fromJson(it.next())); } } else if (json.isNumber()) { n = new NumberValue(); ((NumberValue) n).value = json.numberValue(); } else if (json.isTextual()) { n = new StringValue(); ((StringValue) n).value = json.asText(); } else if (json.isObject()) { n = new ObjNode(); Iterator<Map.Entry<String, JsonNode>> it = json.fields(); while (it.hasNext()) { Map.Entry<String, JsonNode> e = it.next(); ((ObjNode) n).properties.put(e.getKey(), fromJson(e.getValue())); } } else throw new RuntimeException("You should not reach this"); return n; }
From source file:com.galenframework.ide.devices.tasks.DeviceTaskParser.java
private static List<DeviceCommand> parseCommands(ObjectMapper mapper, JsonNode commandsNode) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {/*from w w w .j a v a2s. c om*/ if (commandsNode.isArray()) { Iterator<JsonNode> it = commandsNode.iterator(); List<DeviceCommand> commands = new LinkedList<>(); while (it.hasNext()) { commands.add(parseCommand(mapper, it.next())); } return commands; } else { throw new RuntimeException("\"commands\" should be an array"); } }
From source file:com.vmware.admiral.closures.util.ClosureUtils.java
private static JsonElement toJsonElementArray(JsonNode node) { JsonArray jsObjArray = new JsonArray(); Iterator<JsonNode> iterator = node.iterator(); while (iterator.hasNext()) { JsonNode childNode = iterator.next(); JsonElement convertedValue = getJsonObjElement(childNode); jsObjArray.add(convertedValue);//from w ww. ja va 2s . co m } return jsObjArray; }
From source file:org.gravidence.gravifon.db.ViewQueryResultsExtractor.java
/** * Extracts all properties from view query results. * //www.j a v a 2 s . c o m * @param propertyType property object type * @param propertyName property name * @param json view query results * @return list of <code>propertyType</code> objects if any, or <code>null</code> otherwise */ private static <T> List<T> extractProperies(Class<T> propertyType, String propertyName, InputStream json) { List<T> values = null; JsonNode rows = extractRows(json); if (rows.isArray() && rows.size() > 0) { values = new ArrayList<>(rows.size()); Iterator<JsonNode> it = rows.iterator(); while (it.hasNext()) { JsonNode row = it.next(); values.add(extractProperty(propertyType, propertyName, row)); } } return values; }
From source file:com.flipkart.zjsonpatch.JsonPatch.java
private static void process(JsonNode patch, JsonPatchProcessor processor, EnumSet<CompatibilityFlags> flags) throws InvalidJsonPatchException { if (!patch.isArray()) throw new InvalidJsonPatchException("Invalid JSON Patch payload (not an array)"); Iterator<JsonNode> operations = patch.iterator(); while (operations.hasNext()) { JsonNode jsonNode = operations.next(); if (!jsonNode.isObject()) throw new InvalidJsonPatchException("Invalid JSON Patch payload (not an object)"); Operation operation = Operation .fromRfcName(getPatchAttr(jsonNode, Constants.OP).toString().replaceAll("\"", "")); List<String> path = getPath(getPatchAttr(jsonNode, Constants.PATH)); switch (operation) { case REMOVE: { processor.remove(path);//from ww w . j a va 2 s. c o m break; } case ADD: { JsonNode value; if (!flags.contains(CompatibilityFlags.MISSING_VALUES_AS_NULLS)) value = getPatchAttr(jsonNode, Constants.VALUE); else value = getPatchAttrWithDefault(jsonNode, Constants.VALUE, NullNode.getInstance()); processor.add(path, value); break; } case REPLACE: { JsonNode value; if (!flags.contains(CompatibilityFlags.MISSING_VALUES_AS_NULLS)) value = getPatchAttr(jsonNode, Constants.VALUE); else value = getPatchAttrWithDefault(jsonNode, Constants.VALUE, NullNode.getInstance()); processor.replace(path, value); break; } case MOVE: { List<String> fromPath = getPath(getPatchAttr(jsonNode, Constants.FROM)); processor.move(fromPath, path); break; } } } }
From source file:de.unikonstanz.winter.crossref.node.doi.CrossrefDoiNodeModel.java
private static DataCell partialDateNodeToIntListCell(final JsonNode node) { if (CrossrefUtil.isNull(node)) { return new MissingCell(null); }//from w w w . j a v a 2 s . c o m JsonNode dateParts = node.get("date-parts"); if (CrossrefUtil.isNull(dateParts)) { return new MissingCell(null); } return CrossrefUtil.nodeToIntListCell(dateParts.iterator().next()); }
From source file:org.primaldev.ppm.util.ChartTypeGenerator.java
public static ChartTypeComponent generateChart(byte[] reportData) { // Convert json to pojo JsonNode jsonNode = convert(reportData); // Title/*w w w . j av a 2s .c o m*/ JsonNode titleNode = jsonNode.get("title"); String title = null; if (titleNode != null) { title = titleNode.textValue(); } ChartTypeComponent chartComponent = new ChartTypeComponent(title); // Retrieve data sets JsonNode datasetsNode = jsonNode.get("datasets"); // If no data was returned if (datasetsNode.size() == 0) { chartComponent.addChart(null, null, "Not enough data"); return chartComponent; } if (datasetsNode != null && datasetsNode.isArray()) { Iterator<JsonNode> dataIterator = datasetsNode.iterator(); while (dataIterator.hasNext()) { JsonNode datasetNode = dataIterator.next(); JsonNode descriptionNode = datasetNode.get("description"); String description = null; if (descriptionNode != null) { description = descriptionNode.textValue(); } JsonNode dataNode = datasetNode.get("data"); if (dataNode == null || dataNode.size() == 0) { chartComponent.addChart(description, null, "Not enough data"); } else { String[] names = new String[dataNode.size()]; Number[] values = new Number[dataNode.size()]; int index = 0; Iterator<String> fieldIterator = dataNode.fieldNames(); while (fieldIterator.hasNext()) { String field = fieldIterator.next(); names[index] = field; values[index] = dataNode.get(field).numberValue(); index++; } // Generate chart (or 'no data' message) if (names.length > 0) { Component chart = createChart(datasetNode, names, values); chartComponent.addChart(description, chart, null); } else { chartComponent.addChart(description, null, "Not enough data"); } } } } return chartComponent; }
From source file:org.forgerock.openig.migrate.action.EmptyConfigRemovalAction.java
@Override public ObjectNode migrate(final ObjectNode configuration) { ArrayNode heap = (ArrayNode) configuration.get("heap"); for (JsonNode node : heap) { ObjectNode object = (ObjectNode) node; JsonNode config = object.get("config"); if ((config != null) && config.isContainerNode() && !config.iterator().hasNext()) { object.remove("config"); }/*from w w w.j ava2s .c o m*/ } return configuration; }
From source file:org.agorava.twitter.jackson.LocalTrendsDeserializer.java
@Override public LocalTrendsHolder deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonNode tree = jp.readValueAsTree(); Iterator<JsonNode> dayIt = tree.iterator(); if (dayIt.hasNext()) { JsonNode day = dayIt.next();/*from ww w.j a v a 2 s .c o m*/ Date createdAt = toDate(day.get("created_at").asText()); JsonNode trendNodes = day.get("trends"); List<Trend> trends = new ArrayList<Trend>(); for (Iterator<JsonNode> trendsIt = trendNodes.iterator(); trendsIt.hasNext();) { JsonNode trendNode = trendsIt.next(); trends.add(new Trend(trendNode.get("name").asText(), trendNode.get("query").textValue())); } jp.skipChildren(); return new LocalTrendsHolder(new Trends(createdAt, trends)); } throw ctxt.mappingException(LocalTrendsHolder.class); }
From source file:org.springframework.social.twitter.api.impl.LocalTrendsDeserializer.java
@Override public LocalTrendsHolder deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonNode node = jp.readValueAs(JsonNode.class); Iterator<JsonNode> dayIt = node.iterator(); if (dayIt.hasNext()) { JsonNode day = dayIt.next();// ww w. ja v a2 s.c o m Date createdAt = toDate(day.get("created_at").asText()); JsonNode trendNodes = day.get("trends"); List<Trend> trends = new ArrayList<Trend>(); for (Iterator<JsonNode> trendsIt = trendNodes.iterator(); trendsIt.hasNext();) { JsonNode trendNode = trendsIt.next(); trends.add(new Trend(trendNode.get("name").asText(), trendNode.get("query").asText())); } jp.skipChildren(); return new LocalTrendsHolder(new Trends(createdAt, trends)); } throw ctxt.mappingException(LocalTrendsHolder.class); }