Example usage for com.fasterxml.jackson.databind.node JsonNodeType OBJECT

List of usage examples for com.fasterxml.jackson.databind.node JsonNodeType OBJECT

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.node JsonNodeType OBJECT.

Prototype

JsonNodeType OBJECT

To view the source code for com.fasterxml.jackson.databind.node JsonNodeType OBJECT.

Click Source Link

Usage

From source file:io.swagger.v3.parser.util.OpenAPIDeserializer.java

public List<Tag> getTagList(ArrayNode obj, String location, ParseResult result) {
    if (obj == null) {
        return null;
    }/*from  w  ww.  j a va  2  s.  c o m*/
    List<Tag> tags = new ArrayList<>();
    Set<String> tagsTracker = new HashSet<>();
    for (JsonNode item : obj) {
        if (item.getNodeType().equals(JsonNodeType.OBJECT)) {
            Tag tag = getTag((ObjectNode) item, location, result);
            if (tag != null) {
                tags.add(tag);

                if (tagsTracker.contains((String) tag.getName())) {
                    result.uniqueTags(location, tag.getName());
                }

                tagsTracker.add(tag.getName());
            }
        }
    }
    return tags;
}

From source file:io.swagger.parser.util.SwaggerDeserializer.java

public List<Parameter> parameters(ArrayNode obj, String location, ParseResult result) {
    List<Parameter> output = new ArrayList<Parameter>();
    if (obj == null) {
        return output;
    }/*from ww  w  .  jav a2  s  . c om*/
    for (JsonNode item : obj) {
        if (item.getNodeType().equals(JsonNodeType.OBJECT)) {
            Parameter param = parameter((ObjectNode) item, location, result);
            if (param != null) {
                output.add(param);
            }
        }
    }
    return output;
}

From source file:org.apache.hadoop.gateway.util.JsonPathTest.java

@Test
public void testEvaluateObjects() throws IOException {
    String json;/*www  . j a v a  2s .  c o  m*/
    JsonPath.Segment seg;
    List<JsonPath.Match> matches;
    JsonPath.Match match;
    JsonPath.Match parent;
    JsonNode root;
    JsonNode node;
    JsonPath.Expression expression;

    JsonFactory factory = new JsonFactory();
    ObjectMapper mapper = new ObjectMapper(factory);

    json = "{ \"field\" : \"value\" }";
    root = mapper.readTree(json);
    assertThat(root, notNullValue());

    expression = JsonPath.compile("$.field");
    matches = expression.evaluate(root);
    assertThat(matches, notNullValue());
    assertThat(matches.size(), is(1));
    match = matches.get(0);
    assertThat(matches, notNullValue());
    seg = matches.get(0).getSegment();
    assertThat(seg, notNullValue());
    assertThat(seg.getType(), is(JsonPath.Segment.Type.FIELD));
    assertThat(seg.getField(), is("field"));
    node = match.getNode();
    assertThat(node, notNullValue());
    assertThat(node.getNodeType(), is(JsonNodeType.STRING));
    assertThat(node.asText(), is("value"));
    parent = match.getParent();
    assertThat(parent, notNullValue());
    assertThat(parent.getNode(), sameInstance(root));
    assertThat(parent.getParent(), nullValue());
    assertThat(parent.getSegment().getType(), is(JsonPath.Segment.Type.ROOT));

    json = "{ \"outer\" : { \"inner\" : \"value\"}  }";
    root = mapper.readTree(json);
    assertThat(root, notNullValue());

    expression = JsonPath.compile("$.outer.inner");
    matches = expression.evaluate(root);
    match = matches.get(0);
    assertThat(match, notNullValue());
    assertThat(match.getField(), is("inner"));
    seg = match.getSegment();
    assertThat(seg, notNullValue());
    assertThat(seg.getField(), is("inner"));
    assertThat(seg.getType(), is(JsonPath.Segment.Type.FIELD));

    node = match.getNode();
    assertThat(node, notNullValue());
    assertThat(node.asText(), is("value"));

    parent = match.getParent();
    assertThat(parent, notNullValue());
    assertThat(parent.getField(), is("outer"));
    assertThat(parent.getNode().getNodeType(), is(JsonNodeType.OBJECT));

    parent = parent.getParent();
    assertThat(parent.getSegment().getType(), is(JsonPath.Segment.Type.ROOT));
    assertThat(parent.getNode().getNodeType(), is(JsonNodeType.OBJECT));

    json = "{ \"outer\" : { \"inner\" : \"value\"}  }";
    root = mapper.readTree(json);
    assertThat(root, notNullValue());

    expression = JsonPath.compile("$.*.inner");
    matches = expression.evaluate(root);
    assertThat(matches.size(), is(1));
    match = matches.get(0);
    assertThat(match.getField(), is("inner"));
    assertThat(match.getNode().asText(), is("value"));
}

From source file:io.swagger.v3.parser.util.OpenAPIDeserializer.java

public List<Server> getServersList(ArrayNode obj, String location, ParseResult result, String path) {

    List<Server> servers = new ArrayList<>();
    if (obj == null) {
        return null;

    }/* ww w .  j  a v a2 s .c  o  m*/
    for (JsonNode item : obj) {
        if (item.getNodeType().equals(JsonNodeType.OBJECT)) {
            Server server = getServer((ObjectNode) item, location, result, path);
            if (server != null) {
                servers.add(server);
            } else {
                Server defaultServer = new Server();
                defaultServer.setUrl("/");
                servers.add(defaultServer);
            }
        }
    }
    return servers;
}

From source file:io.swagger.v3.parser.util.OpenAPIDeserializer.java

public Paths getPaths(ObjectNode obj, String location, ParseResult result) {
    final Paths paths = new Paths();
    if (obj == null) {
        return null;
    }//  w  ww. j  av a  2 s  . com
    Set<String> pathKeys = getKeys(obj);
    for (String pathName : pathKeys) {
        JsonNode pathValue = obj.get(pathName);
        if (pathName.startsWith("x-")) {
            Map<String, Object> extensions = getExtensions(obj);
            if (extensions != null && extensions.size() > 0) {
                paths.setExtensions(extensions);
            }
        } else {
            if (!pathValue.getNodeType().equals(JsonNodeType.OBJECT)) {
                result.invalidType(location, pathName, "object", pathValue);
            } else {
                if (!pathName.startsWith("/")) {
                    result.warning(location, " Resource " + pathName + " should start with /");
                }
                ObjectNode path = (ObjectNode) pathValue;
                PathItem pathObj = getPathItem(path, String.format("%s.'%s'", location, pathName), result);
                paths.put(pathName, pathObj);
            }
        }
    }
    return paths;
}

From source file:io.swagger.parser.util.SwaggerDeserializer.java

public Map<String, Model> definitions(ObjectNode node, String location, ParseResult result) {
    if (node == null)
        return null;
    Set<String> schemas = getKeys(node);
    Map<String, Model> output = new LinkedHashMap<String, Model>();

    for (String schemaName : schemas) {
        JsonNode schema = node.get(schemaName);
        if (schema.getNodeType().equals(JsonNodeType.OBJECT)) {
            Model model = definition((ObjectNode) schema, location + "." + schemaName, result);
            if (model != null) {
                output.put(schemaName, model);
            }// www. j  av  a  2 s .  c om
        } else {
            result.invalidType(location, schemaName, "object", schema);
        }
    }
    return output;
}

From source file:io.swagger.v3.parser.util.OpenAPIDeserializer.java

public PathItem getPathItem(ObjectNode obj, String location, ParseResult result) {

    PathItem pathItem = new PathItem();

    if (obj.get("$ref") != null) {
        JsonNode ref = obj.get("$ref");

        if (ref.getNodeType().equals(JsonNodeType.STRING)) {
            String mungedRef = mungedRef(ref.textValue());
            if (mungedRef != null) {
                pathItem.set$ref(mungedRef);
            } else {
                pathItem.set$ref(ref.textValue());
            }/*  www  .  j a  v a  2s . c  om*/
            return pathItem;
        } else if (ref.getNodeType().equals(JsonNodeType.OBJECT)) {
            ObjectNode node = (ObjectNode) ref;

            // extra keys
            Set<String> keys = getKeys(node);
            for (String key : keys) {
                result.extra(location, key, node.get(key));
            }
        }
        return null;
    }

    String value = getString("summary", obj, false, location, result);
    if (StringUtils.isNotBlank(value)) {
        pathItem.setSummary(value);
    }

    value = getString("description", obj, false, location, result);
    if (StringUtils.isNotBlank(value)) {
        pathItem.setDescription(value);
    }

    ArrayNode parameters = getArray("parameters", obj, false, location, result);
    if (parameters != null && parameters.size() > 0) {
        pathItem.setParameters(getParameterList(parameters, location, result));
    }

    ArrayNode servers = getArray("servers", obj, false, location, result);
    if (servers != null && servers.size() > 0) {
        pathItem.setServers(getServersList(servers, location, result));
    }

    ObjectNode node = getObject("get", obj, false, location, result);
    if (node != null) {
        Operation operation = getOperation(node, location + "(get)", result);
        if (operation != null) {
            pathItem.setGet(operation);
        }
    }
    node = getObject("put", obj, false, location, result);
    if (node != null) {
        Operation operation = getOperation(node, location + "(put)", result);
        if (operation != null) {
            pathItem.setPut(operation);
        }
    }
    node = getObject("post", obj, false, location, result);
    if (node != null) {
        Operation operation = getOperation(node, location + "(post)", result);
        if (operation != null) {
            pathItem.setPost(operation);
        }
    }
    node = getObject("head", obj, false, location, result);
    if (node != null) {
        Operation operation = getOperation(node, location + "(head)", result);
        if (operation != null) {
            pathItem.setHead(operation);
        }
    }
    node = getObject("delete", obj, false, location, result);
    if (node != null) {
        Operation operation = getOperation(node, location + "(delete)", result);
        if (operation != null) {
            pathItem.setDelete(operation);
        }
    }
    node = getObject("patch", obj, false, location, result);
    if (node != null) {
        Operation operation = getOperation(node, location + "(patch)", result);
        if (operation != null) {
            pathItem.setPatch(operation);
        }
    }
    node = getObject("options", obj, false, location, result);
    if (node != null) {
        Operation operation = getOperation(node, location + "(options)", result);
        if (operation != null) {
            pathItem.setOptions(operation);
        }
    }
    node = getObject("trace", obj, false, location, result);
    if (node != null) {
        Operation operation = getOperation(node, location + "(trace)", result);
        if (operation != null) {
            pathItem.setTrace(operation);
        }
    }

    Map<String, Object> extensions = getExtensions(obj);
    if (extensions != null && extensions.size() > 0) {
        pathItem.setExtensions(extensions);
    }

    Set<String> keys = getKeys(obj);
    for (String key : keys) {
        if (!PATHITEM_KEYS.contains(key) && !key.startsWith("x-")) {
            result.extra(location, key, obj.get(key));
        }
    }

    return pathItem;
}

From source file:io.swagger.parser.util.SwaggerDeserializer.java

public Model definition(ObjectNode node, String location, ParseResult result) {
    if (node == null) {
        result.missing(location, "empty schema");
    }// w  w  w  .  ja v a 2 s .co m
    if (node.get("$ref") != null) {
        return refModel(node, location, result);
    }
    if (node.get("allOf") != null) {
        return allOfModel(node, location, result);
    }
    Model model = null;
    String value = null;

    String type = getString("type", node, false, location, result);
    Model m = new ModelImpl();
    if ("array".equals(type)) {
        ArrayModel am = new ArrayModel();
        ObjectNode propertyNode = getObject("properties", node, false, location, result);
        Map<String, Property> properties = properties(propertyNode, location, result);
        am.setProperties(properties);

        ObjectNode itemsNode = getObject("items", node, false, location, result);
        Property items = property(itemsNode, location, result);
        if (items != null) {
            am.items(items);
        }

        model = am;
    } else {
        ModelImpl impl = new ModelImpl();
        impl.setType(value);

        JsonNode ap = node.get("additionalProperties");
        if (ap != null && ap.getNodeType().equals(JsonNodeType.OBJECT)) {
            impl.setAdditionalProperties(Json.mapper().convertValue(ap, Property.class));
        }

        value = getString("default", node, false, location, result);
        impl.setDefaultValue(value);

        value = getString("format", node, false, location, result);
        impl.setFormat(value);

        value = getString("discriminator", node, false, location, result);
        impl.setDiscriminator(value);

        JsonNode xml = node.get("xml");
        if (xml != null) {
            impl.setXml(Json.mapper().convertValue(xml, Xml.class));
        }

        ObjectNode externalDocs = getObject("externalDocs", node, false, location, result);
        ExternalDocs docs = externalDocs(externalDocs, location, result);
        impl.setExternalDocs(docs);

        ObjectNode properties = getObject("properties", node, true, location, result);
        if (properties != null) {
            Set<String> propertyNames = getKeys(properties);
            for (String propertyName : propertyNames) {
                JsonNode propertyNode = properties.get(propertyName);
                if (propertyNode.getNodeType().equals(JsonNodeType.OBJECT)) {
                    ObjectNode on = (ObjectNode) propertyNode;
                    Property property = property(on, location, result);
                    impl.property(propertyName, property);
                } else {
                    result.invalidType(location, "properties", "object", propertyNode);
                }
            }
        }

        // need to set properties first
        ArrayNode required = getArray("required", node, false, location, result);
        if (required != null) {
            List<String> requiredProperties = new ArrayList<String>();
            for (JsonNode n : required) {
                if (n.getNodeType().equals(JsonNodeType.STRING)) {
                    requiredProperties.add(((TextNode) n).textValue());
                } else {
                    result.invalidType(location, "required", "string", n);
                }
            }
            if (requiredProperties.size() > 0) {
                impl.setRequired(requiredProperties);
            }
        }

        // extra keys
        Set<String> keys = getKeys(node);
        for (String key : keys) {
            if (key.startsWith("x-")) {
                impl.setVendorExtension(key, extension(node.get(key)));
            } else if (!SCHEMA_KEYS.contains(key)) {
                result.extra(location, key, node.get(key));
            }
        }
        if ("{ }".equals(Json.pretty(impl)))
            return null;
        model = impl;
    }
    JsonNode exampleNode = node.get("example");
    if (exampleNode != null) {
        // we support text or object nodes
        if (exampleNode.getNodeType().equals(JsonNodeType.OBJECT)) {
            ObjectNode on = getObject("example", node, false, location, result);
            if (on != null) {
                model.setExample(on);
            }
        } else {
            model.setExample(exampleNode.asText());
        }
    }

    if (model != null) {
        value = getString("description", node, false, location, result);
        model.setDescription(value);

        value = getString("title", node, false, location, result);
        model.setTitle(value);
    }

    return model;
}

From source file:io.swagger.parser.util.SwaggerDeserializer.java

public Model allOfModel(ObjectNode node, String location, ParseResult result) {
    JsonNode sub = node.get("$ref");
    JsonNode allOf = node.get("allOf");

    if (sub != null) {
        if (sub.getNodeType().equals(JsonNodeType.OBJECT)) {
            return refModel((ObjectNode) sub, location, result);
        } else {/*from  w ww  .j ava  2s  .  c  o m*/
            result.invalidType(location, "$ref", "object", sub);
            return null;
        }
    } else if (allOf != null) {
        ComposedModel model = null;
        // we only support one parent, no multiple inheritance or composition
        if (allOf.getNodeType().equals(JsonNodeType.ARRAY)) {
            model = new ComposedModel();
            int pos = 0;
            for (JsonNode part : allOf) {
                if (part.getNodeType().equals(JsonNodeType.OBJECT)) {
                    Model segment = definition((ObjectNode) part, location, result);
                    if (segment != null) {
                        model.getAllOf().add(segment);
                    }
                } else {
                    result.invalidType(location, "allOf[" + pos + "]", "object", part);
                }
                pos++;
            }

            List<Model> allComponents = model.getAllOf();
            if (allComponents.size() >= 1) {
                model.setParent(allComponents.get(0));
                if (allComponents.size() >= 2) {
                    model.setChild(allComponents.get(allComponents.size() - 1));
                    List<RefModel> interfaces = new ArrayList<RefModel>();
                    int size = allComponents.size();
                    for (Model m : allComponents.subList(1, size - 1)) {
                        if (m instanceof RefModel) {
                            RefModel ref = (RefModel) m;
                            interfaces.add(ref);
                        }
                    }
                    model.setInterfaces(interfaces);
                } else {
                    model.setChild(new ModelImpl());
                }
            }
            return model;
        } else {
            result.invalidType(location, "allOf", "array", allOf);
        }

        return model;
    }
    return null;
}

From source file:io.swagger.v3.parser.util.OpenAPIDeserializer.java

public ObjectNode getObject(String key, ObjectNode node, boolean required, String location,
        ParseResult result) {// ww  w  . ja  v  a 2  s .  com
    JsonNode value = node.get(key);
    ObjectNode object = null;
    if (value == null) {
        if (required) {
            result.missing(location, key);
            result.invalid();
        }
    } else if (!value.getNodeType().equals(JsonNodeType.OBJECT)) {
        result.invalidType(location, key, "object", value);
        if (required) {
            result.invalid();
        }
    } else {
        object = (ObjectNode) value;
    }
    return object;
}