Example usage for com.fasterxml.jackson.core JsonPointer getMatchingIndex

List of usage examples for com.fasterxml.jackson.core JsonPointer getMatchingIndex

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonPointer getMatchingIndex.

Prototype

public int getMatchingIndex() 

Source Link

Usage

From source file:com.meltmedia.dropwizard.crypto.JsonPointerEditor.java

public JsonNode setValue(JsonNode value) {
    JsonPointer currentPointer = pointer;
    JsonNode currentNode = root;/*from   www  .  j a va  2  s.  co  m*/
    JsonNode parentNode = null;
    String matchingProperty = null;
    int matchingIndex = -1;

    while (!"".equals(currentPointer.getMatchingProperty())) {
        parentNode = currentNode;
        matchingProperty = currentPointer.getMatchingProperty();
        matchingIndex = currentPointer.getMatchingIndex();
        currentNode = currentNode.get(matchingProperty);
        currentPointer = currentPointer.tail();
    }

    if (parentNode == null) {
        root = value;
    } else if (parentNode instanceof ObjectNode) {
        ((ObjectNode) parentNode).set(matchingProperty, value);
    } else if (parentNode instanceof ArrayNode) {
        ((ArrayNode) parentNode).set(matchingIndex, value);
    }

    return value;
}

From source file:io.progix.dropwizard.patch.explicit.JsonPath.java

/**
 * Creates the path using a {@link JsonPointer} by iterating through the segments and creating {@link
 * JsonPathProperty} and {@link JsonPathElement} for each segment.
 * <p/>//from   w  w  w  .  j  a  va2 s . c  o m
 * If a given segment does not match as a String property, an empty {@link JsonPathProperty} is created.
 * <p/>
 * If a given segment does not match as a Integer index, an empty {@link JsonPathElement} is created.
 *
 * @param pointer
 */
public JsonPath(JsonPointer pointer) {
    this.properties = new ArrayList<>();
    this.elements = new ArrayList<>();
    this.pathString = "";

    while (pointer != null) {
        if (pointer.mayMatchProperty() && !pointer.getMatchingProperty().isEmpty()) {
            properties.add(new JsonPathProperty(pointer.getMatchingProperty()));
            this.pathString += pointer.getMatchingProperty() + "/";
        } else if (pointer.getMatchingProperty().equals("-")) {
            /* This character represents the last element in an array */
            elements.add(new JsonPathElement(true));
            this.pathString += pointer.getMatchingProperty() + "/";
        } else {
            properties.add(new JsonPathProperty());
        }

        if (pointer.mayMatchElement()) {
            elements.add(new JsonPathElement(pointer.getMatchingIndex()));
            this.pathString += pointer.getMatchingIndex() + "/";
        } else {
            elements.add(new JsonPathElement());
        }
        pointer = pointer.tail();
    }

    this.pathString = this.pathString.substring(0, this.pathString.length() - 1);
}