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

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

Introduction

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

Prototype

public static JsonPointer compile(String input) throws IllegalArgumentException 

Source Link

Document

Factory method that parses given input and construct matching pointer instance, if it represents a valid JSON Pointer: if not, a IllegalArgumentException is thrown.

Usage

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

@Test
public void shouldAllowRootNode() throws Exception {
    JsonNode node = mapper.readValue("{\"key\": {\"sub\": \"value\"}}", JsonNode.class);
    JsonPointer pointer = JsonPointer.compile("/").tail();
    JsonNode root = node.at(pointer);//from  w  w  w.  j ava 2  s  .  c o  m

    JsonPointerEditor editor = new JsonPointerEditor(node, pointer);
    editor.setValue(factory.objectNode().set("value", factory.textNode("text")));
    assertThat(editor.getRoot().at("/value").asText(), equalTo("text"));

    assertThat(editor.getRoot(), sameInstance(editor.getValue()));
}

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

/**
 * Using available patch operation handlers, this method will iterate through all {@link PatchInstruction} in this
 * patch request and run the handler logic.
 * <p/>// w  w  w.  j a  v  a  2 s.co  m
 * Calling this method is required for the patch to be applied and is left to the user to decide where and when it
 * will applied in a resource method.
 *
 * @throws PatchTestFailedException when a TEST patch operation fails
 * @see PatchOperation#TEST
 */
public void apply() throws PatchTestFailedException {
    for (PatchInstruction instruction : instructions) {
        JsonPath path = new JsonPath(JsonPointer.compile(instruction.getPath()));

        switch (instruction.getOperation()) {
        case ADD:
            if (addHandler == null) {
                throw new PatchOperationNotSupportedException(PatchOperation.ADD);
            }

            addHandler.add(path, new JsonPatchValue(instruction.getValue()));

            break;
        case COPY:
            if (copyHandler == null) {
                throw new PatchOperationNotSupportedException(PatchOperation.COPY);
            }

            copyHandler.copy(new JsonPath(JsonPointer.compile(instruction.getFrom())), path);
            break;
        case MOVE:
            if (moveHandler == null) {
                throw new PatchOperationNotSupportedException(PatchOperation.MOVE);
            }

            moveHandler.move(new JsonPath(JsonPointer.compile(instruction.getFrom())), path);
            break;
        case REMOVE:
            if (removeHandler == null) {
                throw new PatchOperationNotSupportedException(PatchOperation.REMOVE);
            }

            removeHandler.remove(path);
            break;
        case REPLACE:
            if (replaceHandler == null) {
                throw new PatchOperationNotSupportedException(PatchOperation.REPLACE);
            }

            replaceHandler.replace(path, new JsonPatchValue(instruction.getValue()));
            break;
        case TEST:
            if (testHandler == null) {
                throw new PatchOperationNotSupportedException(PatchOperation.TEST);
            }

            boolean success = testHandler.test(path, new JsonPatchValue(instruction.getValue()));

            if (!success) {
                throw new PatchTestFailedException(path, instruction.getValue());
            }
            break;
        default:
            break;
        }
    }
}

From source file:com.reprezen.swagedit.model.NodeDeserializer.java

protected ArrayNode deserializeArrayNode(JsonParser p, DeserializationContext context,
        JsonLocation startLocation) throws IOException {
    final Model model = (Model) context.getAttribute(ATTRIBUTE_MODEL);
    final AbstractNode parent = (AbstractNode) context.getAttribute(ATTRIBUTE_PARENT);
    final JsonPointer ptr = (JsonPointer) context.getAttribute(ATTRIBUTE_POINTER);

    ArrayNode node = model.arrayNode(parent, ptr);

    int i = 0;/*from ww  w . j  a v  a 2  s  .  c  o  m*/
    while (p.nextToken() != JsonToken.END_ARRAY) {
        JsonPointer pp = JsonPointer.compile(ptr.toString() + "/" + i);

        context.setAttribute(ATTRIBUTE_PARENT, node);
        context.setAttribute(ATTRIBUTE_POINTER, pp);

        AbstractNode v = deserialize(p, context);

        node.add(v);
        i++;
    }

    node.setStartLocation(createLocation(startLocation));
    node.setEndLocation(createLocation(p.getCurrentLocation()));
    return node;
}

From source file:com.reprezen.swagedit.model.Model.java

protected static ObjectReader reader(Model model) {
    return createMapper().reader() //
            .withAttribute(ATTRIBUTE_MODEL, model) //
            .withAttribute(ATTRIBUTE_PARENT, null) //
            .withAttribute(ATTRIBUTE_POINTER, JsonPointer.compile("")) //
            .withType(AbstractNode.class);
}

From source file:com.reprezen.swagedit.json.references.JsonReferenceFactory.java

protected JsonReference doCreate(String value, Object source) {
    String notNull = Strings.nullToEmpty(value);

    URI uri;// www .  j a  v a2  s .  c o m
    try {
        uri = URI.create(notNull);
    } catch (NullPointerException | IllegalArgumentException e) {
        // try to encode illegal characters, e.g. curly braces
        try {
            uri = URI.create(URLUtils.encodeURL(notNull));
        } catch (NullPointerException | IllegalArgumentException e2) {
            return new JsonReference(null, null, false, false, false, source);
        }
    }

    String fragment = uri.getFragment();
    JsonPointer pointer = null;
    try {
        pointer = JsonPointer.compile(Strings.emptyToNull(fragment));
    } catch (IllegalArgumentException e) {
        // let the pointer be null
    }

    uri = uri.normalize();
    boolean absolute = uri.isAbsolute();
    boolean local = !absolute && uri.getPath().isEmpty();
    // should warn when using curly braces
    boolean warnings = notNull.contains("{") || uri.toString().contains("}");

    return new JsonReference(uri, pointer, absolute, local, warnings, source);
}

From source file:com.reprezen.swagedit.editor.hyperlinks.PathParamHyperlinkDetector.java

private Iterable<JsonPointer> findParameterPath(SwaggerDocument doc, JsonPointer basePath, String parameter) {
    AbstractNode parent = doc.getModel().find(basePath);

    if (parent == null || !parent.isObject()) {
        return Lists.newArrayList();
    }//  w w  w.  j  a  va  2 s .c  om

    List<JsonPointer> paths = new ArrayList<>();
    for (HttpMethod method : HttpMethod.values()) {
        String mName = method.name().toLowerCase();

        if (parent.get(mName) == null) {
            continue;
        }

        AbstractNode parameters = parent.get(mName).get("parameters");

        if (parameters != null && parameters.isArray()) {
            for (int i = 0; i < parameters.size(); i++) {
                AbstractNode current = parameters.get(i);

                if (JsonReference.isReference(current)) {
                    JsonPointer ptr = JsonReference.getPointer(current.asObject());
                    AbstractNode resolved = doc.getModel().find(ptr);

                    if (resolved != null && resolved.isObject() && resolved.get("name") != null) {
                        if (parameter.equals(resolved.get("name").asValue().getValue())) {
                            paths.add(ptr);
                        }
                    }

                } else if (current.isObject() && current.get("name") != null) {

                    if (parameter.equals(current.get("name").asValue().getValue())) {
                        paths.add(JsonPointer.compile(basePath + "/" + mName + "/parameters/" + i));
                    }
                }
            }
        }
    }

    return paths;
}

From source file:org.forgerock.openig.migrate.action.InlineDeclarationsAction.java

private JsonPointer parentOf(final JsonPointer pointer) {
    String ser = pointer.toString();
    int i = ser.lastIndexOf('/');
    return JsonPointer.compile(ser.substring(0, i));
}

From source file:com.reprezen.swagedit.schema.SwaggerSchema.java

protected void init() {
    JsonNode core;//w  ww .  j  a va 2 s.c o  m
    try {
        core = mapper.readTree(getClass().getResourceAsStream("core.json"));
    } catch (IOException e) {
        return;
    }

    JsonNode content;
    try {
        content = mapper.readTree(getClass().getResourceAsStream("schema.json"));
    } catch (IOException e) {
        return;
    }

    coreType = new JsonSchema(core, this);
    coreType.setType(new ObjectTypeDefinition(coreType, JsonPointer.compile(""), core));

    swaggerType = new JsonSchema(content, this);
    swaggerType.setType(new ObjectTypeDefinition(swaggerType, JsonPointer.compile(""), content));
}

From source file:com.reprezen.swagedit.json.references.JsonReference.java

private static JsonPointer createPointer(String text) {
    if (Strings.emptyToNull(text) == null) {
        return JsonPointer.compile("");
    }/*from  ww w  .  j  a  v a 2 s .com*/

    if (text.startsWith("#")) {
        text = text.substring(1);
    }
    return JsonPointer.compile(text);
}

From source file:com.reprezen.swagedit.schema.SwaggerSchema.java

/**
 * Returns the type of a node.// w  ww .j  a  v a 2 s  . c  o  m
 * 
 * <br/>
 * 
 * Note: this method should be used only during initialization of a model.
 * 
 * @param node
 * @return node's type
 */
public TypeDefinition getType(AbstractNode node) {
    JsonPointer pointer = node.getPointer();

    if (JsonPointer.compile("").equals(pointer)) {
        return swaggerType.getType();
    }

    String[] paths = pointer.toString().substring(1).split("/");
    TypeDefinition current = swaggerType.getType();

    if (current != null) {
        for (String property : paths) {
            TypeDefinition next = current.getPropertyType(property);
            // not found, we stop here
            if (next == null) {
                break;
            }
            current = next;
        }
    }

    return current;
}