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

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

Introduction

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

Prototype

JsonNodeType ARRAY

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

Click Source Link

Usage

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

public Object extension(JsonNode jsonNode) {
    if (jsonNode.getNodeType().equals(JsonNodeType.BOOLEAN)) {
        return jsonNode.asBoolean();
    }//from w  w w .j a v a 2  s  . c  o m
    if (jsonNode.getNodeType().equals(JsonNodeType.STRING)) {
        return jsonNode.asText();
    }
    if (jsonNode.getNodeType().equals(JsonNodeType.NUMBER)) {
        NumericNode n = (NumericNode) jsonNode;
        if (n.isLong()) {
            return jsonNode.asLong();
        }
        if (n.isInt()) {
            return jsonNode.asInt();
        }
        if (n.isBigDecimal()) {
            return jsonNode.textValue();
        }
        if (n.isBoolean()) {
            return jsonNode.asBoolean();
        }
        if (n.isFloat()) {
            return jsonNode.floatValue();
        }
        if (n.isDouble()) {
            return jsonNode.doubleValue();
        }
        if (n.isShort()) {
            return jsonNode.intValue();
        }
        return jsonNode.asText();
    }
    if (jsonNode.getNodeType().equals(JsonNodeType.ARRAY)) {
        ArrayNode an = (ArrayNode) jsonNode;
        List<Object> o = new ArrayList<Object>();
        for (JsonNode i : an) {
            Object obj = extension(i);
            if (obj != null) {
                o.add(obj);
            }
        }
        return o;
    }
    return jsonNode;
}

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 {// ww  w .j av a 2 s .  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.parser.util.SwaggerDeserializer.java

public ArrayNode getArray(String key, ObjectNode node, boolean required, String location, ParseResult result) {
    JsonNode value = node.get(key);//from   w  ww . j  av a2 s  .  c  o m
    ArrayNode an = null;
    if (value == null) {
        if (required) {
            result.missing(location, key);
            result.invalid();
        }
    } else if (!value.getNodeType().equals(JsonNodeType.ARRAY)) {
        result.invalidType(location, key, "array", value);
    } else {
        an = (ArrayNode) value;
    }
    return an;
}

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

public ArrayNode getArray(String key, ObjectNode node, boolean required, String location, ParseResult result) {
    JsonNode value = node.get(key);//from   w ww  .  j a va 2 s .  co  m
    ArrayNode arrayNode = null;
    if (value == null) {
        if (required) {
            result.missing(location, key);
            result.invalid();
        }
    } else if (!value.getNodeType().equals(JsonNodeType.ARRAY)) {
        result.invalidType(location, key, "array", value);
    } else {
        arrayNode = (ArrayNode) value;
    }
    return arrayNode;
}

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

public Object getAnyExample(String nodeKey, ObjectNode node, String location, ParseResult result) {
    JsonNode example = node.get(nodeKey);
    if (example != null) {
        if (example.getNodeType().equals(JsonNodeType.STRING)) {
            String value = getString(nodeKey, node, false, location, result);
            if (StringUtils.isNotBlank(value)) {
                return value;
            }//from  ww w  .j  a v a  2 s .  c  o m
        }
        if (example.getNodeType().equals(JsonNodeType.NUMBER)) {
            Integer integerExample = getInteger(nodeKey, node, false, location, result);
            if (integerExample != null) {
                return integerExample;
            } else {
                BigDecimal bigDecimalExample = getBigDecimal(nodeKey, node, false, location, result);
                if (bigDecimalExample != null) {
                    return bigDecimalExample;

                }
            }
        } else if (example.getNodeType().equals(JsonNodeType.OBJECT)) {
            ObjectNode objectValue = getObject(nodeKey, node, false, location, result);
            if (objectValue != null) {
                return objectValue;
            }
        } else if (example.getNodeType().equals(JsonNodeType.ARRAY)) {
            ArrayNode arrayValue = getArray(nodeKey, node, false, location, result);
            if (arrayValue != null) {
                return arrayValue;
            }
        }
    }
    return null;
}

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

public Schema getSchema(ObjectNode node, String location, ParseResult result) {
    if (node == null) {
        return null;
    }/*from  ww  w  .  j  a  v  a  2s  . c o  m*/

    Schema schema = null;
    ArrayNode oneOfArray = getArray("oneOf", node, false, location, result);
    ArrayNode allOfArray = getArray("allOf", node, false, location, result);
    ArrayNode anyOfArray = getArray("anyOf", node, false, location, result);
    ObjectNode itemsNode = getObject("items", node, false, location, result);

    if ((allOfArray != null) || (anyOfArray != null) || (oneOfArray != null)) {
        ComposedSchema composedSchema = new ComposedSchema();

        if (allOfArray != null) {

            for (JsonNode n : allOfArray) {
                if (n.isObject()) {
                    schema = getSchema((ObjectNode) n, location, result);
                    composedSchema.addAllOfItem(schema);
                }
            }
            schema = composedSchema;
        }
        if (anyOfArray != null) {

            for (JsonNode n : anyOfArray) {
                if (n.isObject()) {
                    schema = getSchema((ObjectNode) n, location, result);
                    composedSchema.addAnyOfItem(schema);
                }
            }
            schema = composedSchema;
        }
        if (oneOfArray != null) {

            for (JsonNode n : oneOfArray) {
                if (n.isObject()) {
                    schema = getSchema((ObjectNode) n, location, result);
                    composedSchema.addOneOfItem(schema);
                }
            }
            schema = composedSchema;
        }
    }

    if (itemsNode != null) {
        ArraySchema items = new ArraySchema();
        if (itemsNode.getNodeType().equals(JsonNodeType.OBJECT)) {
            items.setItems(getSchema(itemsNode, location, result));
        } else if (itemsNode.getNodeType().equals(JsonNodeType.ARRAY)) {
            for (JsonNode n : itemsNode) {
                if (n.isValueNode()) {
                    items.setItems(getSchema(itemsNode, location, result));
                }
            }
        }
        schema = items;
    }

    if (schema == null) {
        schema = SchemaTypeUtil.createSchemaByType(node);
    }

    JsonNode ref = node.get("$ref");
    if (ref != null) {
        if (ref.getNodeType().equals(JsonNodeType.STRING)) {
            String mungedRef = mungedRef(ref.textValue());
            if (mungedRef != null) {
                schema.set$ref(mungedRef);
            } else {
                schema.set$ref(ref.asText());
            }
            return schema;
        } else {
            result.invalidType(location, "$ref", "string", node);
            return null;
        }
    }

    String value = getString("title", node, false, location, result);
    if (StringUtils.isNotBlank(value)) {
        schema.setTitle(value);
    }

    ObjectNode discriminatorNode = getObject("discriminator", node, false, location, result);
    if (discriminatorNode != null) {
        schema.setDiscriminator(getDiscriminator(discriminatorNode, location, result));
    }

    BigDecimal bigDecimal = getBigDecimal("multipleOf", node, false, location, result);
    if (bigDecimal != null) {
        schema.setMultipleOf(bigDecimal);
    }

    bigDecimal = getBigDecimal("maximum", node, false, location, result);
    if (bigDecimal != null) {
        schema.setMaximum(bigDecimal);
    }

    Boolean bool = getBoolean("exclusiveMaximum", node, false, location, result);
    if (bool != null) {
        schema.setExclusiveMaximum(bool);
    }

    bigDecimal = getBigDecimal("minimum", node, false, location, result);
    if (bigDecimal != null) {
        schema.setMinimum(bigDecimal);
    }

    bool = getBoolean("exclusiveMinimum", node, false, location, result);
    if (bool != null) {
        schema.setExclusiveMinimum(bool);
    }

    Integer integer = getInteger("minLength", node, false, location, result);
    if (integer != null) {
        schema.setMinLength(integer);
    }

    integer = getInteger("maxLength", node, false, location, result);
    if (integer != null) {
        schema.setMaxLength(integer);
    }

    String pattern = getString("pattern", node, false, location, result);
    if (StringUtils.isNotBlank(pattern)) {
        schema.setPattern(pattern);
    }

    integer = getInteger("maxItems", node, false, location, result);
    if (integer != null) {
        schema.setMaxItems(integer);
    }
    integer = getInteger("minItems", node, false, location, result);
    if (integer != null) {
        schema.setMinItems(integer);
    }

    bool = getBoolean("uniqueItems", node, false, location, result);
    if (bool != null) {
        schema.setUniqueItems(bool);
    }

    integer = getInteger("maxProperties", node, false, location, result);
    if (integer != null) {
        schema.setMaxProperties(integer);
    }

    integer = getInteger("minProperties", node, false, location, result);
    if (integer != null) {
        schema.setMinProperties(integer);
    }

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

    ArrayNode enumArray = getArray("enum", node, false, location, result);
    if (enumArray != null) {
        for (JsonNode n : enumArray) {
            if (n.isNumber()) {
                schema.addEnumItemObject(n.numberValue());
            } else if (n.isValueNode()) {
                schema.addEnumItemObject(n.asText());
            } else {
                result.invalidType(location, "enum", "value", n);
            }
        }
    }

    value = getString("type", node, false, location, result);
    if (StringUtils.isNotBlank(value)) {
        schema.setType(value);
    } else {
        // may have an enum where type can be inferred
        JsonNode enumNode = node.get("enum");
        if (enumNode != null && enumNode.isArray()) {
            String type = inferTypeFromArray((ArrayNode) enumNode);
            schema.setType(type);
        }
    }

    ObjectNode notObj = getObject("not", node, false, location, result);
    if (notObj != null) {
        Schema not = getSchema(notObj, location, result);
        if (not != null) {
            schema.setNot(not);
        }
    }

    Map<String, Schema> properties = new LinkedHashMap<>();
    ObjectNode propertiesObj = getObject("properties", node, false, location, result);
    Schema property = null;

    Set<String> keys = getKeys(propertiesObj);
    for (String name : keys) {
        JsonNode propertyValue = propertiesObj.get(name);
        if (!propertyValue.getNodeType().equals(JsonNodeType.OBJECT)) {
            result.invalidType(location, "properties", "object", propertyValue);
        } else {
            if (propertiesObj != null) {
                property = getSchema((ObjectNode) propertyValue, location, result);
                if (property != null) {
                    properties.put(name, property);
                }
            }
        }
    }
    if (propertiesObj != null) {
        schema.setProperties(properties);
    }

    if (node.get("additionalProperties") != null) {
        if (node.get("additionalProperties").getNodeType().equals(JsonNodeType.OBJECT)) {
            ObjectNode additionalPropertiesObj = getObject("additionalProperties", node, false, location,
                    result);
            if (additionalPropertiesObj != null) {
                Schema additionalProperties = getSchema(additionalPropertiesObj, location, result);
                if (additionalProperties != null) {
                    schema.setAdditionalProperties(additionalProperties);
                }
            }
        } else if (node.get("additionalProperties").getNodeType().equals(JsonNodeType.BOOLEAN)) {
            Boolean additionalProperties = getBoolean("additionalProperties", node, false, location, result);
            if (additionalProperties != null) {
                schema.setAdditionalProperties(additionalProperties);
            }

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

    value = getString("format", node, false, location, result);
    if (StringUtils.isNotBlank(value)) {
        schema.setFormat(value);
    }

    value = getString("default", node, false, location, result);
    if (StringUtils.isNotBlank(value)) {
        schema.setDefault(value);
    }

    //discriminator

    bool = getBoolean("nullable", node, false, location, result);
    if (bool != null) {
        schema.setNullable(bool);
    }

    bool = getBoolean("readOnly", node, false, location, result);
    if (bool != null) {
        schema.setReadOnly(bool);
    }

    bool = getBoolean("writeOnly", node, false, location, result);
    if (bool != null) {
        schema.setWriteOnly(bool);
    }

    ObjectNode xmlNode = getObject("xml", node, false, location, result);
    if (xmlNode != null) {
        XML xml = getXml(xmlNode, location, result);
        if (xml != null) {
            schema.setXml(xml);
        }
    }

    ObjectNode externalDocs = getObject("externalDocs", node, false, location, result);
    if (externalDocs != null) {
        ExternalDocumentation docs = getExternalDocs(externalDocs, location, result);
        if (docs != null) {
            schema.setExternalDocs(docs);
        }
    }

    Object example = getAnyExample("example", node, location, result);
    if (example != null) {
        schema.setExample(example);
    }

    bool = getBoolean("deprecated", node, false, location, result);
    if (bool != null) {
        schema.setDeprecated(bool);
    }

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

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

    return schema;

}

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

public List<SecurityRequirement> getSecurityRequirementsList(ArrayNode nodes, String location,
        ParseResult result) {/*from  w  ww.ja va2  s  .c  o m*/
    if (nodes == null)
        return null;

    List<SecurityRequirement> securityRequirements = new ArrayList<>();

    for (JsonNode node : nodes) {
        if (node.getNodeType().equals(JsonNodeType.OBJECT)) {
            SecurityRequirement securityRequirement = new SecurityRequirement();
            Set<String> keys = getKeys((ObjectNode) node);
            if (keys.size() == 0) {
                securityRequirements.add(securityRequirement);
            } else {
                for (String key : keys) {
                    if (key != null) {
                        JsonNode value = node.get(key);
                        if (key != null && JsonNodeType.ARRAY.equals(value.getNodeType())) {
                            ArrayNode arrayNode = (ArrayNode) value;
                            List<String> scopes = Stream.generate(arrayNode.elements()::next)
                                    .map((n) -> n.asText()).limit(arrayNode.size())
                                    .collect(Collectors.toList());
                            securityRequirement.addList(key, scopes);
                        }
                    }
                }
                if (securityRequirement.size() > 0) {
                    securityRequirements.add(securityRequirement);
                }
            }
        }
    }

    return securityRequirements;

}

From source file:org.apache.olingo.server.core.serializer.json.ODataErrorSerializerTest.java

@Test
public void verifiedWithJacksonParser() throws Exception {
    List<ODataErrorDetail> details = new ArrayList<ODataErrorDetail>();
    details.add(//from   w ww.ja  v  a 2s. c om
            new ODataErrorDetail().setCode("detailCode").setMessage("detailMessage").setTarget("detailTarget"));
    ODataServerError error = new ODataServerError().setCode("Code").setMessage("Message").setTarget("Target")
            .setDetails(details);
    InputStream stream = ser.error(error);
    JsonNode tree = new ObjectMapper().readTree(stream);
    assertNotNull(tree);
    tree = tree.get("error");
    assertNotNull(tree);
    assertEquals("Code", tree.get("code").textValue());
    assertEquals("Message", tree.get("message").textValue());
    assertEquals("Target", tree.get("target").textValue());

    tree = tree.get("details");
    assertNotNull(tree);
    assertEquals(JsonNodeType.ARRAY, tree.getNodeType());

    tree = tree.get(0);
    assertNotNull(tree);
    assertEquals("detailCode", tree.get("code").textValue());
    assertEquals("detailMessage", tree.get("message").textValue());
    assertEquals("detailTarget", tree.get("target").textValue());
}

From source file:org.apache.olingo.server.core.serializer.json.ServerErrorSerializerTest.java

@Test
public void verifiedWithJacksonParser() throws Exception {
    ODataServerError error = new ODataServerError().setCode("Code").setMessage("Message").setTarget("Target")
            .setDetails(Collections.singletonList(new ODataErrorDetail().setCode("detailCode")
                    .setMessage("detailMessage").setTarget("detailTarget")));
    InputStream stream = ser.error(error).getContent();
    JsonNode tree = new ObjectMapper().readTree(stream);
    assertNotNull(tree);// ww  w  .j  av a  2  s  . co m
    tree = tree.get("error");
    assertNotNull(tree);
    assertEquals("Code", tree.get("code").textValue());
    assertEquals("Message", tree.get("message").textValue());
    assertEquals("Target", tree.get("target").textValue());

    tree = tree.get("details");
    assertNotNull(tree);
    assertEquals(JsonNodeType.ARRAY, tree.getNodeType());

    tree = tree.get(0);
    assertNotNull(tree);
    assertEquals("detailCode", tree.get("code").textValue());
    assertEquals("detailMessage", tree.get("message").textValue());
    assertEquals("detailTarget", tree.get("target").textValue());
}