Example usage for com.fasterxml.jackson.databind JsonNode textValue

List of usage examples for com.fasterxml.jackson.databind JsonNode textValue

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind JsonNode textValue.

Prototype

public String textValue() 

Source Link

Usage

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

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

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

    String value = getString("description", node, true, location, result);
    if (StringUtils.isNotBlank(value)) {
        apiResponse.description(value);
    }

    ObjectNode headerObject = getObject("headers", node, false, location, result);
    if (headerObject != null) {
        Map<String, Header> headers = getHeaders(headerObject, location, result);
        if (headers != null && headers.size() > 0) {
            apiResponse.setHeaders(headers);
        }
    }

    ObjectNode linksObj = getObject("links", node, false, location, result);
    if (linksObj != null) {
        Map<String, Link> links = getLinks(linksObj, location, result);
        if (links != null && links.size() > 0) {
            apiResponse.setLinks(links);
        }
    }

    ObjectNode contentObject = getObject("content", node, false, location, result);
    if (contentObject != null) {
        apiResponse.setContent(getContent(contentObject, String.format("%s.%s", location, "content"), result));
    }

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

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

    return apiResponse;
}

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

public Link getLink(ObjectNode linkNode, String location, ParseResult result) {
    if (linkNode == null) {
        return null;
    }//  w  w  w  .  jav  a 2s . c om

    Link link = new Link();

    JsonNode ref = linkNode.get("$ref");
    if (ref != null) {
        if (ref.getNodeType().equals(JsonNodeType.STRING)) {
            String mungedRef = mungedRef(ref.textValue());
            if (mungedRef != null) {
                link.set$ref(mungedRef);
            } else {
                link.set$ref(ref.textValue());
            }

            return link;
        } else {
            result.invalidType(location, "$ref", "string", linkNode);
            return null;
        }
    }

    String value = getString("operationRef", linkNode, false, location, result);
    if (StringUtils.isNotBlank(value)) {
        link.setOperationRef(value);
    }

    value = getString("operationId", linkNode, false, location, result);
    if (StringUtils.isNotBlank(value)) {
        link.setOperationId(value);
    }

    ObjectNode parametersObject = getObject("parameters", linkNode, false, location, result);
    if (parametersObject != null) {
        link.setParameters(getLinkParameters(parametersObject, location, result));
    }

    String requestBody = getString("requestBody", linkNode, false, location, result);
    if (requestBody != null) {
        link.setRequestBody(requestBody);
    }

    ObjectNode headerObject = getObject("headers", linkNode, false, location, result);
    if (headerObject != null) {
        link.setHeaders(getHeaders(headerObject, location, result));
    }

    ObjectNode serverObject = getObject("server", linkNode, false, location, result);
    if (serverObject != null) {
        link.setServer(getServer(serverObject, location, result));
    }

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

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

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

    return link;
}

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

public Header getHeader(ObjectNode headerNode, String location, ParseResult result) {
    if (headerNode == null) {
        return null;
    }//from  ww  w .  j a va 2  s .  c  o m

    Header header = new Header();

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

    String value = getString("description", headerNode, false, location, result);
    if (StringUtils.isNotBlank(value)) {
        header.setDescription(value);
    }

    Boolean required = getBoolean("required", headerNode, false, location, result);
    if (required != null) {
        header.setRequired(required);
    }

    Boolean deprecated = getBoolean("deprecated", headerNode, false, location, result);
    if (deprecated != null) {
        header.setDeprecated(deprecated);
    }

    Boolean explode = getBoolean("explode", headerNode, false, location, result);
    if (explode != null) {
        header.setExplode(explode);
    } else {
        header.setExplode(Boolean.FALSE);
    }

    header.setStyle(Header.StyleEnum.SIMPLE);

    ObjectNode headerObject = getObject("schema", headerNode, false, location, result);
    if (headerObject != null) {
        header.setSchema(getSchema(headerObject, location, result));
    }

    ObjectNode examplesObject = getObject("examples", headerNode, false, location, result);
    if (examplesObject != null) {
        header.setExamples(getExamples(examplesObject, location, result));
    }

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

    ObjectNode contentNode = getObject("content", headerNode, false, location, result);
    if (contentNode != null) {
        header.setContent(getContent(contentNode, String.format("%s.%s", location, "content"), result));
    }

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

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

    return header;
}

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());
            }//from   w ww  .  ja va  2  s . co m
            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.v3.parser.util.OpenAPIDeserializer.java

public SecurityScheme getSecurityScheme(ObjectNode node, String location, ParseResult result) {
    if (node == null) {
        return null;
    }//from   ww  w .ja  v a2 s. com

    SecurityScheme securityScheme = new SecurityScheme();

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

    boolean descriptionRequired, bearerFormatRequired, nameRequired, inRequired, schemeRequired, flowsRequired,
            openIdConnectRequired;
    descriptionRequired = bearerFormatRequired = nameRequired = inRequired = schemeRequired = flowsRequired = openIdConnectRequired = false;

    String value = getString("type", node, true, location, result);
    if (StringUtils.isNotBlank(value)) {
        if (SecurityScheme.Type.APIKEY.toString().equals(value)) {
            securityScheme.setType(SecurityScheme.Type.APIKEY);
            nameRequired = inRequired = true;
        } else if (SecurityScheme.Type.HTTP.toString().equals(value)) {
            securityScheme.setType(SecurityScheme.Type.HTTP);
            schemeRequired = true;
        } else if (SecurityScheme.Type.OAUTH2.toString().equals(value)) {
            securityScheme.setType(SecurityScheme.Type.OAUTH2);
            flowsRequired = true;
        } else if (SecurityScheme.Type.OPENIDCONNECT.toString().equals(value)) {
            securityScheme.setType(SecurityScheme.Type.OPENIDCONNECT);
            openIdConnectRequired = true;
        } else {
            result.invalidType(location + ".type", "type", "http|apiKey|oauth2|openIdConnect ", node);
        }
    }
    value = getString("description", node, descriptionRequired, location, result);
    if (StringUtils.isNotBlank(value)) {
        securityScheme.setDescription(value);
    }

    value = getString("name", node, nameRequired, location, result);
    if (StringUtils.isNotBlank(value)) {
        securityScheme.setName(value);
    }

    final String securitySchemeIn = getString("in", node, inRequired, location, result);
    final Optional<SecurityScheme.In> matchingIn = Arrays.stream(SecurityScheme.In.values())
            .filter(in -> in.toString().equals(securitySchemeIn)).findFirst();

    securityScheme.setIn(matchingIn.orElse(null));

    value = getString("scheme", node, schemeRequired, location, result);
    if (StringUtils.isNotBlank(value)) {
        securityScheme.setScheme(value);
    }

    value = getString("bearerFormat", node, bearerFormatRequired, location, result);
    if (StringUtils.isNotBlank(value)) {
        securityScheme.setBearerFormat(value);
    }

    ObjectNode flowsObject = getObject("flows", node, flowsRequired, location, result);
    if (flowsObject != null) {
        securityScheme.setFlows(getOAuthFlows(flowsObject, location, result));
    }

    value = getString("openIdConnectUrl", node, openIdConnectRequired, location, result);
    if (StringUtils.isNotBlank(value)) {
        securityScheme.setOpenIdConnectUrl(value);
    }

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

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

    return securityScheme;
}

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

public Parameter getParameter(ObjectNode obj, String location, ParseResult result) {
    if (obj == null) {
        return null;
    }/* w w w.j a  va 2  s .  com*/

    Parameter parameter = null;

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

    String l = null;
    JsonNode ln = obj.get("name");
    if (ln != null) {
        l = ln.asText();
    } else {
        l = "['unknown']";
    }
    location += ".[" + l + "]";

    String value = getString("in", obj, true, location, result);

    if (StringUtils.isBlank(value)) {
        return null;
    }

    if (QUERY_PARAMETER.equals(value)) {
        parameter = new QueryParameter();
    } else if (HEADER_PARAMETER.equals(value)) {
        parameter = new HeaderParameter();
    } else if (PATH_PARAMETER.equals(value)) {
        parameter = new PathParameter();
    } else if (COOKIE_PARAMETER.equals(value)) {
        parameter = new CookieParameter();
    }

    if (parameter == null) {
        result.invalidType(location, "in", "string", obj);
        return null;
    }

    parameter.setIn(value);

    value = getString("name", obj, true, location, result);
    if (StringUtils.isNotBlank(value)) {
        parameter.setName(value);
    }

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

    Boolean required = getBoolean("required", obj, false, location, result);
    if (required != null) {
        parameter.setRequired(required);
    } else {
        parameter.setRequired(false);
    }

    Boolean deprecated = getBoolean("deprecated", obj, false, location, result);
    if (deprecated != null) {
        parameter.setDeprecated(deprecated);
    }

    if (parameter instanceof QueryParameter) {
        Boolean allowEmptyValue = getBoolean("allowEmptyValue", obj, false, location, result);
        if (allowEmptyValue != null) {
            parameter.setAllowEmptyValue(allowEmptyValue);
        }
    }

    value = getString("style", obj, false, location, result);
    setStyle(value, parameter, location, obj, result);

    Boolean explode = getBoolean("explode", obj, false, location, result);
    if (explode != null) {
        parameter.setExplode(explode);
    } else if (parameter.getStyle().equals(StyleEnum.FORM)) {
        parameter.setExplode(Boolean.TRUE);
    } else {
        parameter.setExplode(Boolean.FALSE);
    }

    ObjectNode parameterObject = getObject("schema", obj, false, location, result);
    if (parameterObject != null) {
        parameter.setSchema(getSchema(parameterObject, String.format("%s.%s", location, "schemas"), result));
    }

    ObjectNode examplesObject = getObject("examples", obj, false, location, result);
    if (examplesObject != null) {
        parameter
                .setExamples(getExamples(examplesObject, String.format("%s.%s", location, "examples"), result));
    }

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

    ObjectNode contentNode = getObject("content", obj, false, location, result);
    if (contentNode != null) {
        parameter.setContent(getContent(contentNode, String.format("%s.%s", location, "content"), result));
    }

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

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

    return parameter;
}

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 ava2s  . 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:com.redhat.lightblue.config.AbstractMetadataConfiguration.java

@Override
public void initializeFromJson(JsonNode node) {
    if (node != null) {
        JsonNode x = node.get("hookConfigurationParsers");
        if (x != null && x.isArray()) {
            // each element in array is a class
            Iterator<JsonNode> elements = ((ArrayNode) x).elements();

            while (elements.hasNext()) {
                JsonNode e = elements.next();
                String clazz = e.asText();

                // instantiate the class
                Object o = null;//from ww  w.  j av a2s  .co m
                try {
                    o = Class.forName(clazz).newInstance();
                } catch (InstantiationException | IllegalAccessException | ClassNotFoundException ex) {
                    throw Error.get(MetadataConstants.ERR_CONFIG_NOT_VALID, ex.getMessage());
                }

                // add to list or fail
                if (o instanceof HookConfigurationParser) {
                    hookConfigurationParsers.add((HookConfigurationParser) o);
                } else {
                    throw Error.get(MetadataConstants.ERR_CONFIG_NOT_VALID,
                            "Class not instance of HookConfigurationParser: " + clazz);
                }
            }
        }

        ArrayNode backendParsersJs = (ArrayNode) node.get("backendParsers");
        if (backendParsersJs != null) {
            Iterator<JsonNode> bpjsItr = backendParsersJs.iterator();
            while (bpjsItr.hasNext()) {
                JsonNode jsonNode = bpjsItr.next();
                String name = jsonNode.get("name").asText();
                String clazz = jsonNode.get("clazz").asText();

                if (name == null || clazz == null) {
                    throw Error.get(MetadataConstants.ERR_CONFIG_NOT_VALID,
                            "Backend/DataStoreParser class was not informed: name='" + name + "' clazz="
                                    + clazz);
                }

                try {
                    DataStoreParser instance = (DataStoreParser) Class.forName(clazz).newInstance();
                    AbstractMap.SimpleEntry<String, DataStoreParser> stringDataStoreParserSimpleEntry = new AbstractMap.SimpleEntry<>(
                            name, instance);
                    backendParsers.add(stringDataStoreParserSimpleEntry);
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
                    throw Error.get(MetadataConstants.ERR_CONFIG_NOT_VALID,
                            "Class not instance of Backend/DataStoreParser: " + clazz);
                }
            }
        }

        ArrayNode propertyParserJs = (ArrayNode) node.get("propertyParsers");
        if (propertyParserJs != null) {
            for (JsonNode jsonNode : propertyParserJs) {
                String name = jsonNode.get("name").asText();
                String clazz = jsonNode.get("clazz").asText();

                if (name == null || clazz == null) {
                    throw Error.get(MetadataConstants.ERR_CONFIG_NOT_VALID,
                            "PropertyParser Name/Class not informed: name=" + name + " clazz=" + clazz);
                }

                try {
                    PropertyParser instance = (PropertyParser) Class.forName(clazz).newInstance();

                    AbstractMap.SimpleEntry<String, PropertyParser> stringPropertyParserSimpleEntry = new AbstractMap.SimpleEntry<>(
                            name, instance);
                    propertyParsers.add(stringPropertyParserSimpleEntry);
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
                    throw Error.get(MetadataConstants.ERR_CONFIG_NOT_VALID,
                            "Class not instance of PropertyParser: " + clazz);
                }
            }
        }

        JsonNode roleMapJs = node.get("roleMap");
        if (roleMapJs != null) {
            // If the roleMap element is defined, it is expected to have all the roles mapped
            MetadataRole[] values = MetadataRole.values();
            for (MetadataRole value : values) {
                String name = value.toString();
                ArrayNode rolesJs = (ArrayNode) roleMapJs.get(name);

                if (rolesJs == null || rolesJs.size() == 0) {
                    throw Error.get(MetadataConstants.ERR_CONFIG_NOT_VALID,
                            "roleMap missing the role \"" + name + "\"");
                }

                roleMap.put(value, new ArrayList<String>());
                for (JsonNode jsonNode : rolesJs) {
                    roleMap.get(value).add(jsonNode.textValue());
                }
            }
        }

        x = node.get("validateRequests");
        if (x != null) {
            validateRequests = x.booleanValue();
        }
    }
}