Java Code Examples for com.fasterxml.jackson.core.JsonPointer#head()

The following examples show how to use com.fasterxml.jackson.core.JsonPointer#head() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonCacheImpl.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Override
public void delete(JsonPointer ptr) {
    JsonPointer head = ptr.head();
    if (head == null) {
        root = null;
    } else if (root != null) {
        JsonNode parent = root.at(head);
        if (parent.isArray()) {
            ((ArrayNode) parent).remove(Integer.parseInt(ptr.last().getMatchingProperty()));
        } else if (parent.isObject()) {
            ((ObjectNode) parent).remove(ptr.last().getMatchingProperty());
        } else {
            throw new IllegalArgumentException(ptr + " does not identify a deletable node");
        }
    }
    setDirty();
}
 
Example 2
Source File: JsonPatchOperation.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
private static JsonNode ensureParent(JsonNode node, JsonPointer path, String typeName) {
    /*
     * Check the parent node: it must exist and be a container (ie an array
     * or an object) for the add operation to work.
     */
    final JsonPointer parentPath = path.head();
    final JsonNode parentNode = node.at(parentPath);
    if (parentNode.isMissingNode()) {
        throw new JsonPatchException("non-existent " + typeName + " parent: " + parentPath);
    }
    if (!parentNode.isContainerNode()) {
        throw new JsonPatchException(typeName + " parent is not a container: " + parentPath +
                                     " (" + parentNode.getNodeType() + ')');
    }
    return parentNode;
}
 
Example 3
Source File: JSONUtils.java    From SkaETL with Apache License 2.0 5 votes vote down vote up
public void put(JsonNode jsonNode, String path, JsonNode value) {
    JsonPointer valueNodePointer = JsonPointer.compile(asJsonPath(path));
    JsonPointer parentPointer = valueNodePointer.head();
    addMissingNodeIfNecessary(jsonNode, path);
    JsonNode parentNode = jsonNode.at(parentPointer);

    if (parentNode.getNodeType() == JsonNodeType.OBJECT) {
        ObjectNode parentNodeToUpdate = (ObjectNode) parentNode;
        parentNodeToUpdate.set(StringUtils.replace(valueNodePointer.last().toString(),"/", ""), value);
    }
}
 
Example 4
Source File: JSONUtils.java    From SkaETL with Apache License 2.0 5 votes vote down vote up
public void remove(JsonNode jsonNode, String path) {
    JsonPointer valueNodePointer = JsonPointer.compile(asJsonPath(path));
    JsonPointer parentPointer = valueNodePointer.head();
    JsonNode parentNode = jsonNode.at(parentPointer);
    if (parentNode.getNodeType() == JsonNodeType.OBJECT) {
        ObjectNode parentNodeToUpdate = (ObjectNode) parentNode;
        parentNodeToUpdate.remove(StringUtils.replace(valueNodePointer.last().toString(),"/",""));
    }

}
 
Example 5
Source File: JsonCacheImpl.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
/**
 * Ensures that all ancestor containers exist for the specified JSON pointer.
 *
 * @param ptr        A <a href="https://tools.ietf.org/html/rfc6901">JSON Pointer</a> to the property to set.
 * @param forceArray <code>true</code> to create an array for the last segment of the pointer if it is non-integral.
 * @return The container that owns the property identified by <code>path</code>.
 */
protected ContainerNode<?> ensureContainerExists(JsonPointer ptr, boolean forceArray) {
    if (root == null) {
        root = isInteger(ptr.getMatchingProperty()) // split
                ? JsonNodeFactory.instance.arrayNode()
                : JsonNodeFactory.instance.objectNode();
    }
    String lastProperty = ptr.last().getMatchingProperty();
    Deque<String> stack = new ArrayDeque<>();
    JsonPointer ancestorPtr = forceArray && !isInteger(lastProperty) ? ptr : ptr.head();
    JsonNode ancestor = root.at(ancestorPtr);
    while (ancestor.isMissingNode()) {
        stack.push(ancestorPtr.last().getMatchingProperty());
        ancestorPtr = ancestorPtr.head();
        ancestor = root.at(ancestorPtr);
    }
    if (!ancestor.isContainerNode())
        throw new IllegalArgumentException(ancestorPtr + " does not identify a container node");

    while (!stack.isEmpty()) {
        String ancestorProperty = stack.pop();
        String childProperty = stack.isEmpty() // split
                ? forceArray && !isInteger(lastProperty) // split
                ? "0" // split
                : lastProperty // split
                : stack.peek();
        // Parent can be array or object; child can be array or object - that's four possible combinations.
        // Infer the child container type from the child property name: an integer pattern implies an array node.
        if (isInteger(childProperty)) {
            switch (ancestor.getNodeType()) {
                case ARRAY:
                    // ARRAY/ARRAY
                    ancestor = ((ArrayNode) ancestor).insertArray(Integer.parseInt(ancestorProperty));
                    break;
                case OBJECT:
                    // OBJECT/ARRAY
                    ancestor = ((ObjectNode) ancestor).putArray(ancestorProperty);
                    break;
                default:
                    throw new IllegalArgumentException(ancestorProperty + " does not identify an array node");
            }
        } else {
            switch (ancestor.getNodeType()) {
                case ARRAY:
                    // ARRAY/OBJECT
                    ancestor = ((ArrayNode) ancestor).insertObject(Integer.parseInt(ancestorProperty));
                    break;
                case OBJECT:
                    // OBJECT/OBJECT
                    ancestor = ((ObjectNode) ancestor).putObject(ancestorProperty);
                    break;
                default:
                    throw new IllegalArgumentException(ancestorProperty + " does not identify an array node");
            }
        }
        setDirty();
    }

    return (ContainerNode<?>) ancestor;
}