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

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

Introduction

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

Prototype

JsonNodeType BOOLEAN

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

Click Source Link

Usage

From source file:com.spotify.hamcrest.jackson.IsJsonBoolean.java

private IsJsonBoolean(Matcher<? super Boolean> booleanMatcher) {
    super(JsonNodeType.BOOLEAN);
    this.booleanMatcher = Objects.requireNonNull(booleanMatcher);
}

From source file:de.thingweb.typesystem.jsonschema.JsonSchemaType.java

public static JsonType getJsonType(JsonNode jsonSchemaNode) throws JsonSchemaException {
    JsonType jtype = null;/*  w  w w .j a va2s  .  co  m*/

    if (jsonSchemaNode != null) {
        JsonNode typeNode = jsonSchemaNode.findValue("type");
        if (typeNode != null) {
            switch (typeNode.asText()) {
            case "boolean":
                jtype = new JsonBoolean();
                break;
            case "integer":
            case "number":
                boolean exclusiveMinimum = false;
                JsonNode exclusiveMinimumNode = jsonSchemaNode.findValue("exclusiveMinimum");
                if (exclusiveMinimumNode != null
                        && exclusiveMinimumNode.getNodeType() == JsonNodeType.BOOLEAN) {
                    exclusiveMinimum = exclusiveMinimumNode.asBoolean();
                }
                boolean exclusiveMaximum = false;
                JsonNode exclusiveMaximumNode = jsonSchemaNode.findValue("exclusiveMaximum");
                if (exclusiveMaximumNode != null
                        && exclusiveMaximumNode.getNodeType() == JsonNodeType.BOOLEAN) {
                    exclusiveMaximum = exclusiveMaximumNode.asBoolean();
                }

                if ("integer".equals(typeNode.asText())) {
                    jtype = new JsonInteger();
                    JsonNode minimumNode = jsonSchemaNode.findValue("minimum");
                    if (minimumNode != null && minimumNode.getNodeType() == JsonNodeType.NUMBER) {
                        ((JsonInteger) jtype).setMinimum(minimumNode.asLong());
                    }
                    JsonNode maximumNode = jsonSchemaNode.findValue("maximum");
                    if (maximumNode != null && maximumNode.getNodeType() == JsonNodeType.NUMBER) {
                        ((JsonInteger) jtype).setMaximum(maximumNode.asLong());
                    }
                } else {
                    assert ("number".equals(typeNode.asText()));

                    jtype = new JsonNumber();
                    JsonNode minimumNode = jsonSchemaNode.findValue("minimum");
                    if (minimumNode != null && minimumNode.getNodeType() == JsonNodeType.NUMBER) {
                        ((JsonNumber) jtype).setMinimum(minimumNode.asDouble());
                    }
                    JsonNode maximumNode = jsonSchemaNode.findValue("maximum");
                    if (maximumNode != null && maximumNode.getNodeType() == JsonNodeType.NUMBER) {
                        ((JsonNumber) jtype).setMaximum(maximumNode.asDouble());
                    }
                }

                ((AbstractJsonNumeric) jtype).setExclusiveMinimum(exclusiveMinimum);
                ((AbstractJsonNumeric) jtype).setExclusiveMaximum(exclusiveMaximum);

                break;
            case "null":
                jtype = new JsonNull();
                break;
            case "string":
                jtype = new JsonString();
                break;
            case "array":
                JsonNode itemsNode = jsonSchemaNode.findValue("items");
                if (itemsNode != null && JsonNodeType.OBJECT == itemsNode.getNodeType()) {
                    jtype = new JsonArray(getJsonType(itemsNode));
                } else {
                    throw new JsonSchemaException("items not object");
                }

                break;
            case "object":
                JsonNode propertiesNode = jsonSchemaNode.findValue("properties");
                if (propertiesNode != null && JsonNodeType.OBJECT == propertiesNode.getNodeType()) {
                    Iterator<Entry<String, JsonNode>> iter = propertiesNode.fields();
                    Map<String, JsonType> properties = new HashMap<String, JsonType>();
                    while (iter.hasNext()) {
                        Entry<String, JsonNode> e = iter.next();
                        JsonNode nodeProp = e.getValue();
                        properties.put(e.getKey(), getJsonType(nodeProp));
                    }
                    jtype = new JsonObject(properties);
                } else {
                    throw new JsonSchemaException("Properties not object");
                }
                // required
                JsonNode requiredNode = jsonSchemaNode.findValue("required");
                if (requiredNode != null && JsonNodeType.ARRAY == requiredNode.getNodeType()) {
                    ArrayNode an = (ArrayNode) requiredNode;
                    Iterator<JsonNode> iterReq = an.elements();
                    while (iterReq.hasNext()) {
                        JsonNode nreq = iterReq.next();
                        if (JsonNodeType.STRING == nreq.getNodeType()) {
                            ((JsonObject) jtype).addRequired(nreq.asText());
                        } else {
                            throw new JsonSchemaException("Unexpected required node: " + nreq);
                        }
                    }
                }
                break;
            }
        }
    }

    return jtype;
}

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

public Boolean getBoolean(String key, ObjectNode node, boolean required, String location, ParseResult result) {
    Boolean value = null;/*from  w  w w  .j a  v a 2  s . c  om*/
    JsonNode v = node.get(key);
    if (node == null || v == null) {
        if (required) {
            result.missing(location, key);
            result.invalid();
        }
    } else {
        if (v.getNodeType().equals(JsonNodeType.BOOLEAN)) {
            value = v.asBoolean();
        } else if (v.getNodeType().equals(JsonNodeType.STRING)) {
            String stringValue = v.textValue();
            return Boolean.parseBoolean(stringValue);
        }
    }
    return value;
}

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 a2s.c om
    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.v3.parser.util.OpenAPIDeserializer.java

public Schema getSchema(ObjectNode node, String location, ParseResult result) {
    if (node == null) {
        return null;
    }/*from w w w .jav  a  2  s  .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;

}