Example usage for javax.json JsonValue.ValueType toString

List of usage examples for javax.json JsonValue.ValueType toString

Introduction

In this page you can find the example usage for javax.json JsonValue.ValueType toString.

Prototype

@Override
String toString();

Source Link

Document

Returns JSON text for this JSON value.

Usage

From source file:de.pksoftware.springstrap.cms.util.CmsUtils.java

public static String getModelProperty(CmsModel model, String path, String defaultValue)
        throws InvalidPathException {
    if (null == model) {
        return defaultValue;
    }/*from  www . j  a  v  a 2 s .  c  om*/

    Pattern pattern = Pattern.compile("^(\\/\\w+)+$");
    Matcher matcher = pattern.matcher(path);

    if (!matcher.matches()) {
        throw new RuntimeException(
                "Path must start with /, must not end with / and must only contain letters and numbers.");
    }

    String value = defaultValue;

    logger.info("Trying to get " + model.getModelName() + ">" + path);

    String data = model.getData();

    String[] pathParts = path.split("\\/");
    int partsLength = pathParts.length;

    JsonReader jsonReader = Json.createReader(new StringReader(data));
    JsonObject jsonObject = jsonReader.readObject();

    boolean pointerIsArray = false;
    Object pointer = jsonObject;

    for (int i = 1; i < partsLength; i++) {
        String pathPart = pathParts[i];
        logger.info("Testing property: " + pathPart);
        JsonValue jsonValue = null;
        Assert.notNull(pointer);
        if (pointerIsArray) {
            if (!NumberUtils.isNumber(pathPart)) {
                throw new InvalidPathException("Path element '" + pathPart + "' should be numeric.");
            }
            jsonValue = ((JsonArray) pointer).get(Integer.parseInt(pathPart));
        } else {
            jsonValue = ((JsonObject) pointer).get(pathPart);
        }

        if (null == jsonValue) {
            logger.info(model.getModelName() + " has no property: " + path);

            break;
        }

        JsonValue.ValueType valueType = jsonValue.getValueType();
        if (i < partsLength - 1) {
            //Must be Object or Array
            if (valueType == JsonValue.ValueType.ARRAY) {
                logger.info(pathPart + " is an array.");
                pointer = (JsonArray) jsonValue;
                pointerIsArray = true;
            } else if (valueType == JsonValue.ValueType.OBJECT) {
                logger.info(pathPart + " is an object.");
                pointer = (JsonObject) jsonValue;
                pointerIsArray = false;
            } else {
                throw new InvalidPathException("Path element '" + pathPart
                        + "' should be a be an object or array, but " + valueType.toString() + " found.");
            }
        } else {
            if (valueType == JsonValue.ValueType.STRING) {
                logger.info(pathPart + " is an string.");
                value = ((JsonString) jsonValue).getString();
                break;
            } else {
                throw new InvalidPathException("Path element '" + pathPart + "' should be a string, but "
                        + valueType.toString() + " found.");
            }
        }
    }

    jsonReader.close();

    return value;
}