/** * Returns a Java object for a json value node based on the node type. */ public static Object valueFromJson(ValueNode node) { if (node instanceof NullNode) { return null; } else { if(node instanceof TextNode) { return node.textValue(); } else if(node instanceof BooleanNode) { return node.booleanValue(); } else if(node instanceof NumericNode) { return node.numberValue(); } else { throw new RuntimeException("Unsupported node type:"+node.getClass().getName()); } } }
/** * Returns a Java object for a json value node based on the node type. */ public static Object valueFromJson(ValueNode node) { if (node instanceof NullNode) { return null; } else { if(node instanceof TextNode) { return node.textValue(); } else if(node instanceof BooleanNode) { return node.booleanValue(); } else if(node instanceof NumericNode) { return node.numberValue(); } else { throw new RuntimeException("Unsupported node type:"+node.getClass().getName()); } } }
public void flattenJsonIntoMap(String currentPath, JsonNode jsonNode, Map<String, Object> map) { if (jsonNode.isObject()) { ObjectNode objectNode = (ObjectNode) jsonNode; Iterator<Map.Entry<String, JsonNode>> iter = objectNode.fields(); String pathPrefix = currentPath.isEmpty() ? "" : currentPath + "."; while (iter.hasNext()) { Map.Entry<String, JsonNode> entry = iter.next(); flattenJsonIntoMap(pathPrefix + entry.getKey(), entry.getValue(), map); } } else if (jsonNode.isArray()) { ArrayNode arrayNode = (ArrayNode) jsonNode; for (int i = 0; i < arrayNode.size(); i++) { flattenJsonIntoMap(currentPath + "[" + i + "]", arrayNode.get(i), map); } } else if (jsonNode.isValueNode()) { ValueNode valueNode = (ValueNode) jsonNode; Object value = null; if (valueNode.isNumber()) { value = valueNode.numberValue(); } else if (valueNode.isBoolean()) { value = valueNode.asBoolean(); } else if (valueNode.isTextual()){ value = valueNode.asText(); } map.put(currentPath, value); } } }
switch (nodeType) { case NUMBER: return new JsonPrimitive(vn.numberValue()); case BOOLEAN: return new JsonPrimitive(vn.asBoolean());