Search in sources :

Example 6 with JsonPointer

use of com.fasterxml.jackson.core.JsonPointer in project KaiZen-OpenAPI-Editor by RepreZen.

the class NodeDeserializer method deserializeValueNode.

protected ValueNode deserializeValueNode(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);
    Object v = context.readValue(p, Object.class);
    ValueNode node = model.valueNode(parent, ptr, v);
    node.setStartLocation(createLocation(startLocation));
    node.setEndLocation(createLocation(p.getCurrentLocation()));
    return node;
}
Also used : JsonPointer(com.fasterxml.jackson.core.JsonPointer)

Example 7 with JsonPointer

use of com.fasterxml.jackson.core.JsonPointer in project KaiZen-OpenAPI-Editor by RepreZen.

the class CompositeSchema method getType.

/**
     * Returns the type that is reachable by the given pointer inside the JSON schema. <br/>
     * 
     * Examples of pointers: <br/>
     * - /properties/swagger <br/>
     * - /definitions/paths
     * 
     * @param pointer
     * @return type
     */
public TypeDefinition getType(String reference) {
    if (Strings.emptyToNull(reference) == null) {
        return swaggerType.getType();
    }
    JsonPointer pointer = pointer(reference);
    JsonSchema schema = getSchema(baseURI(reference));
    return schema.get(pointer);
}
Also used : JsonPointer(com.fasterxml.jackson.core.JsonPointer)

Example 8 with JsonPointer

use of com.fasterxml.jackson.core.JsonPointer in project KaiZen-OpenAPI-Editor by RepreZen.

the class DefinitionHyperlinkDetector method doDetect.

@Override
protected IHyperlink[] doDetect(JsonDocument doc, ITextViewer viewer, HyperlinkInfo info, JsonPointer pointer) {
    JsonPointer targetPath;
    if (pointer.toString().matches(REQUIRED_PATTERN)) {
        targetPath = getRequiredPropertyPath(doc, info, pointer);
    } else {
        targetPath = getTagDefinitionPath(doc, info, pointer);
    }
    if (targetPath == null) {
        return null;
    }
    IRegion target = doc.getRegion(targetPath);
    if (target == null) {
        return null;
    }
    return new IHyperlink[] { new SwaggerHyperlink(info.text, viewer, info.region, target) };
}
Also used : SwaggerHyperlink(com.reprezen.swagedit.core.hyperlinks.SwaggerHyperlink) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) JsonPointer(com.fasterxml.jackson.core.JsonPointer) IRegion(org.eclipse.jface.text.IRegion)

Example 9 with JsonPointer

use of com.fasterxml.jackson.core.JsonPointer in project torodb by torodb.

the class ConfigUtils method mergeParam.

public static ObjectNode mergeParam(ObjectMapper objectMapper, ObjectNode configRootNode, String pathAndProp, String value) throws Exception {
    if (JsonPointer.compile(pathAndProp).equals(JsonPointer.compile("/"))) {
        return (ObjectNode) objectMapper.readTree(value);
    }
    String path = pathAndProp.substring(0, pathAndProp.lastIndexOf("/"));
    String prop = pathAndProp.substring(pathAndProp.lastIndexOf("/") + 1);
    JsonPointer pathPointer = JsonPointer.compile(path);
    JsonNode pathNode = configRootNode.at(pathPointer);
    if (pathNode.isMissingNode() || pathNode.isNull()) {
        JsonPointer currentPointer = pathPointer;
        JsonPointer childOfCurrentPointer = null;
        List<JsonPointer> missingPointers = new ArrayList<>();
        List<JsonPointer> childOfMissingPointers = new ArrayList<>();
        do {
            if (pathNode.isMissingNode() || pathNode.isNull()) {
                missingPointers.add(0, currentPointer);
                childOfMissingPointers.add(0, childOfCurrentPointer);
            }
            childOfCurrentPointer = currentPointer;
            currentPointer = currentPointer.head();
            pathNode = configRootNode.at(currentPointer);
        } while (pathNode.isMissingNode() || pathNode.isNull());
        for (int missingPointerIndex = 0; missingPointerIndex < missingPointers.size(); missingPointerIndex++) {
            final JsonPointer missingPointer = missingPointers.get(missingPointerIndex);
            final JsonPointer childOfMissingPointer = childOfMissingPointers.get(missingPointerIndex);
            final List<JsonNode> newNodes = new ArrayList<>();
            if (pathNode.isObject()) {
                ((ObjectNode) pathNode).set(missingPointer.last().getMatchingProperty(), createNode(childOfMissingPointer, newNodes));
            } else if (pathNode.isArray() && missingPointer.last().mayMatchElement()) {
                for (int index = ((ArrayNode) pathNode).size(); index < missingPointer.last().getMatchingIndex() + 1; index++) {
                    ((ArrayNode) pathNode).add(createNode(childOfMissingPointer, newNodes));
                }
            } else {
                throw new RuntimeException("Cannot set param " + pathAndProp + "=" + value);
            }
            pathNode = newNodes.get(newNodes.size() - 1);
        }
    }
    Object valueAsObject;
    try {
        valueAsObject = objectMapper.readValue(value, Object.class);
    } catch (JsonMappingException jsonMappingException) {
        throw JsonMappingException.wrapWithPath(jsonMappingException, configRootNode, path.substring(1) + "/" + prop);
    }
    if (pathNode instanceof ObjectNode) {
        ObjectNode objectNode = (ObjectNode) pathNode;
        if (valueAsObject != null) {
            JsonNode valueNode = objectMapper.valueToTree(valueAsObject);
            objectNode.set(prop, valueNode);
        } else {
            objectNode.remove(prop);
        }
    } else if (pathNode instanceof ArrayNode) {
        ArrayNode arrayNode = (ArrayNode) pathNode;
        Integer index = Integer.valueOf(prop);
        if (valueAsObject != null) {
            JsonNode valueNode = objectMapper.valueToTree(valueAsObject);
            arrayNode.set(index, valueNode);
        } else {
            arrayNode.remove(index);
        }
    }
    return configRootNode;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonPointer(com.fasterxml.jackson.core.JsonPointer) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 10 with JsonPointer

use of com.fasterxml.jackson.core.JsonPointer in project torodb by torodb.

the class ConfigUtils method getParam.

public static <T> JsonNode getParam(T config, String pathAndProp) throws Exception {
    XmlMapper xmlMapper = xmlMapper();
    JsonNode configNode = xmlMapper.valueToTree(config);
    if (JsonPointer.compile(pathAndProp).equals(JsonPointer.compile("/"))) {
        return configNode;
    }
    JsonPointer pathPointer = JsonPointer.compile(pathAndProp);
    JsonNode pathNode = configNode.at(pathPointer);
    if (pathNode.isMissingNode() || pathNode.isNull()) {
        return null;
    }
    return pathNode;
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonPointer(com.fasterxml.jackson.core.JsonPointer) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper)

Aggregations

JsonPointer (com.fasterxml.jackson.core.JsonPointer)18 ArrayList (java.util.ArrayList)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)2 SwaggerHyperlink (com.reprezen.swagedit.core.hyperlinks.SwaggerHyperlink)2 AbstractNode (com.reprezen.swagedit.core.model.AbstractNode)2 IRegion (org.eclipse.jface.text.IRegion)2 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)2 BeanProperty (com.fasterxml.jackson.databind.BeanProperty)1 JavaType (com.fasterxml.jackson.databind.JavaType)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 JsonFormatVisitable (com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitable)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)1 JsonDocument (com.reprezen.swagedit.core.editor.JsonDocument)1 Model (com.reprezen.swagedit.core.model.Model)1 ObjectNode (com.reprezen.swagedit.core.model.ObjectNode)1 HttpMethod (io.swagger.models.HttpMethod)1 URI (java.net.URI)1