Java Code Examples for com.fasterxml.jackson.databind.node.ContainerNode#getNodeType()

The following examples show how to use com.fasterxml.jackson.databind.node.ContainerNode#getNodeType() . 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
protected JsonCache add(JsonPointer ptr, JsonNode node) {
    // If ptr ends with an array index, this implies inserting at the specified index.
    // If ptr does not end with an array index, this implies appending to the end of the array.
    // In both cases the array in question and its ancestors must be created if they do not already exist.
    String lastProperty = ptr.last().getMatchingProperty();
    boolean isIndexed = isInteger(lastProperty);
    ContainerNode<?> container = ensureContainerExists(ptr, !isIndexed);
    switch (container.getNodeType()) {
        case ARRAY:
            ArrayNode array = (ArrayNode) container;
            int index = isIndexed ? Integer.parseInt(lastProperty) : array.size();
            if (index < array.size()) {
                array.insert(index, node);
            } else {
                // Fill any gap between current size and index with nulls (Jackson doesn't support sparse arrays).
                for (int i = array.size(); i < index; i++)
                    array.add(array.nullNode());
                array.add(node);
            }
            break;
        default:
            throw new IllegalArgumentException(ptr + " does not identify an array");
    }
    setDirty();
    return this;
}
 
Example 2
Source File: JsonCacheImpl.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@Override
public JsonCache set(JsonPointer ptr, Object value) {
    String property = ptr.last().getMatchingProperty();
    ContainerNode<?> container = ensureContainerExists(ptr);
    JsonNode node = nodeFor(value);
    switch (container.getNodeType()) {
        case ARRAY:
            ArrayNode array = (ArrayNode) container;
            int index = Integer.parseInt(property);
            if (index < array.size()) {
                array.set(index, node);
            } else {
                // Fill any gap between current size and index with nulls (Jackson doesn't support sparse arrays).
                for (int i = array.size(); i < index; i++)
                    array.add(array.nullNode());
                array.add(node);
            }
            break;
        case OBJECT:
            ((ObjectNode) container).set(property, node);
            break;
        default:
            throw new IllegalArgumentException(ptr + " does not identify a settable container");
    }
    setDirty();
    return this;
}
 
Example 3
Source File: JsonCacheImpl.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
protected void merge(ContainerNode<?> dest, ContainerNode<?> src) {
    if (dest.getNodeType() == src.getNodeType()) {
        if (dest.isArray()) {
            ArrayNode destArray = (ArrayNode) dest;
            ArrayNode srcArray = (ArrayNode) src;
            outer:
            for (int i = 0; i < srcArray.size(); i++) {
                // Only add a source element if it is not already present in the destination array.
                JsonNode srcElem = srcArray.get(i);
                for (int j = 0; j < destArray.size(); j++) {
                    if (destArray.get(j).equals(srcElem))
                        continue outer;
                }
                destArray.add(srcElem);
            }
        } else if (dest.isObject()) {
            ObjectNode destObject = (ObjectNode) dest;
            ObjectNode srcObject = (ObjectNode) src;
            Iterator<Entry<String, JsonNode>> fields = srcObject.fields();
            while (fields.hasNext()) {
                Entry<String, JsonNode> field = fields.next();
                String fieldName = field.getKey();
                JsonNode srcChild = field.getValue();
                if (destObject.has(fieldName)) {
                    JsonNode destChild = destObject.get(fieldName);
                    switch (mergePolicy) {
                        case OVERWRITE_EXISTING:
                            destObject.set(fieldName, srcChild);
                            // Mark the cache as dirty as we've added items from another file.
                            isDirty = true;
                            LOGGER.info("Existing root property '" + fieldName
                                    + "' has been overwritten by incoming data");
                            break;
                        case MERGE_RECURSIVE:
                            if (destChild.isContainerNode() && srcChild.isContainerNode())
                                merge((ContainerNode<?>) destChild, (ContainerNode<?>) srcChild);
                            break;
                        case KEEP_EXISTING:
                            LOGGER.info("Existing root property '" + fieldName
                                    + "' will not be overwritten by incoming data");
                        default:
                            // Nothing to do.
                            break;
                    }
                } else {
                    destObject.set(fieldName, srcChild);
                    LOGGER.info("New property '" + fieldName + "' has been added from incoming data");
                    // Mark the cache as dirty as we've added items from another file.
                    isDirty = true;
                }
            }
        }
    } else {
        LOGGER.warn("Cannot merge containers of differing types");
    }
}