@Override public List<JsonNode> findParents(String fieldName, List<JsonNode> foundSoFar) { for (JsonNode node : _children) { foundSoFar = node.findParents(fieldName, foundSoFar); } return foundSoFar; }
/** * Method for finding a JSON Object that contains specified field, * within this node or its descendants. * If no matching field is found in this node or its descendants, returns null. * * @param fieldName Name of field to look for * * @return Value of first matching node found, if any; null if none */ public final List<JsonNode> findParents(String fieldName) { List<JsonNode> result = findParents(fieldName, null); if (result == null) { return Collections.emptyList(); } return result; }
@Override public List<JsonNode> findParents(String fieldName, List<JsonNode> foundSoFar) { for (Map.Entry<String, JsonNode> entry : _children.entrySet()) { if (fieldName.equals(entry.getKey())) { if (foundSoFar == null) { foundSoFar = new ArrayList<JsonNode>(); } foundSoFar.add(this); } else { // only add children if parent not added foundSoFar = entry.getValue() .findParents(fieldName, foundSoFar); } } return foundSoFar; }
@Override public List<JsonNode> findParents(String fieldName, List<JsonNode> foundSoFar) { if (_children != null) { for (JsonNode node : _children) { foundSoFar = node.findParents(fieldName, foundSoFar); } } return foundSoFar; }
@Override public List<JsonNode> findParents(String fieldName, List<JsonNode> foundSoFar) { for (JsonNode node : _children) { foundSoFar = node.findParents(fieldName, foundSoFar); } return foundSoFar; }
@Override public List<JsonNode> findParents(String fieldName, List<JsonNode> foundSoFar) { for (JsonNode node : _children) { foundSoFar = node.findParents(fieldName, foundSoFar); } return foundSoFar; }
@Override public List<JsonNode> findParents(String fieldName, List<JsonNode> foundSoFar) { if (_children != null) { for (JsonNode node : _children) { foundSoFar = node.findParents(fieldName, foundSoFar); } } return foundSoFar; }
@Override public List<JsonNode> findParents(String fieldName, List<JsonNode> foundSoFar) { for (JsonNode node : _children) { foundSoFar = node.findParents(fieldName, foundSoFar); } return foundSoFar; }
@Override public List<JsonNode> findParents(String fieldName, List<JsonNode> foundSoFar) { for (JsonNode node : _children) { foundSoFar = node.findParents(fieldName, foundSoFar); } return foundSoFar; }
JsonNode root = mapper.readTree(SCHEMA); List<JsonNode> required = root.findParents("required"); for (JsonNode node: required) { Object prettyOutput = mapper.readValue(node, Object.class); System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(prettyOutput)); }
private void productIdInLineItems(final JsonNode resourceDraftNode) { final JsonNode lineItems = resourceDraftNode.get("lineItems"); if (lineItems != null && lineItems.isArray()) { final List<JsonNode> parents = resourceDraftNode.findParents(PRODUCT_ID_FIELD_NAME); parents.forEach(node -> { final String newId = keyToIdMap.get(node.get(PRODUCT_ID_FIELD_NAME).asText()); ((ObjectNode) node).replace(PRODUCT_ID_FIELD_NAME, new TextNode(newId)); }); } }
private static void setupNullableEnums(final JsonNode schemaObject) { // If the node is marked as nullable, and this node is an enum, then we // need to extend the set of enumerated values to include null, so that // it will be properly handled by the JSON-schema validation routine. schemaObject .findParents(ENUM_KEY) .stream() .filter(jsonNode -> jsonNode.path(NULLABLE_KEY).asBoolean(false)) .filter(jsonNode -> !alreadySupportsNullEnum(jsonNode)) .forEach(jsonNode -> ((ArrayNode) jsonNode.get(ENUM_KEY)).addNull()); }
/** * Verify that JSON media node contains valid table payload. */ private void validateMedia(JsonNode data) throws InvalidInputException { for (JsonNode node : data.findParents(INDEX)) { JsonNode text = node.path(TEXT); if (!text.isMissingNode() && (!text.isArray() || !text.get(0).isArray())) { throw new InvalidInputException(String.format("Invalid table payload: %s (index: %s)", text.asText(), node.get(INDEX))); } } }
private void productIdInLineItems(final JsonNode resourceDraftNode) { final JsonNode lineItems = resourceDraftNode.get("lineItems"); if (lineItems != null && lineItems.isArray()) { final List<JsonNode> parents = resourceDraftNode.findParents(PRODUCT_ID_FIELD_NAME); parents.forEach(node -> { final String newId = keyToIdMap.get(node.get(PRODUCT_ID_FIELD_NAME).asText()); ((ObjectNode) node).replace(PRODUCT_ID_FIELD_NAME, new TextNode(newId)); }); } }
private void customerIdInOrders(final JsonNode resourceDraftNode) { final String fieldName = "customerId"; final List<JsonNode> nodesContainingCustomerId = resourceDraftNode.findParents(fieldName); nodesContainingCustomerId.forEach(node -> { if (node != null && node.isObject()) { final String newId = keyToIdMap.get(node.get(fieldName).asText()); ((ObjectNode) node).replace(fieldName, new TextNode(newId)); } }); }
private void customerIdInOrders(final JsonNode resourceDraftNode) { final String fieldName = "customerId"; final List<JsonNode> nodesContainingCustomerId = resourceDraftNode.findParents(fieldName); nodesContainingCustomerId.forEach(node -> { if (node != null && node.isObject()) { final String newId = keyToIdMap.get(node.get(fieldName).asText()); ((ObjectNode) node).replace(fieldName, new TextNode(newId)); } }); }
private static void modifyNode(JsonNode schema) { List<JsonNode> parents = schema.findParents("items"); for (JsonNode parent : parents) { if (parent.hasNonNull("type") && parent.get("type").asText().equals("array")) { if (parent.get("items").hasNonNull("oneOf")) { ((ObjectNode) parent).put("anyOf", parent.get("items").get("oneOf")); ((ObjectNode) parent).remove("items"); } } } } }
private void references(final JsonNode resourceDraftNode) { resourceDraftNode.findParents("typeId") .stream() .filter(node -> node.isObject()) .map(node -> (ObjectNode)node) .filter(node -> node.size() == 2 && node.has("id")) .forEach(node -> { final String id = keyToIdMap.get(node.get("id").asText()); node.replace("id", new TextNode(id)); }); } }
private void references(final JsonNode resourceDraftNode) { resourceDraftNode.findParents("typeId") .stream() .filter(node -> node.isObject()) .map(node -> (ObjectNode)node) .filter(node -> node.size() == 2 && node.has("id")) .forEach(node -> { final String id = keyToIdMap.get(node.get("id").asText()); node.replace("id", new TextNode(id)); }); } }
public static void transformIdToUuidInJson(final JsonNode json) { final List<JsonNode> matches = json.findParents(ID_FIELD_NAME); for (final JsonNode match : matches) { final long id = match.get(ID_FIELD_NAME).asLong(); ((ObjectNode) match).remove(ID_FIELD_NAME); ((ObjectNode) match).put(UUID_FIELD_NAME, "" + id); } }