Search in sources :

Example 6 with JsonException

use of com.axway.ats.action.exceptions.JsonException in project ats-framework by Axway.

the class JsonText method getParentOf.

private JsonText getParentOf(String keyPath) {
    if (StringUtils.isNullOrEmpty(keyPath)) {
        throw new JsonException("Invalid json path '" + keyPath + "'");
    }
    List<String> paths = new ArrayList<>(Arrays.asList(keyPath.split(PATH_DELIMETER)));
    if (paths.size() == 1) {
        // there is only one token, check it is present in our JSON text as a top level element
        String lastTokenPath = paths.get(0);
        boolean pathIsPresent = false;
        if (jsonObject != null && jsonObject.containsKey(lastTokenPath)) {
            pathIsPresent = true;
        } else if (jsonArray != null) {
            Matcher m = NAME_AND_INDEX_PATTERN.matcher(lastTokenPath);
            if (m.find()) {
                // last token is pointing to array
                if (m.groupCount() < 2) {
                    throw new JsonException("'" + lastTokenPath + "' does not specify an array in the valid way 'key_name[index_number]'");
                }
                // name in the path token
                String name = m.group(1);
                // index in the path token, for example "name[3]"
                int index = getIndex(lastTokenPath, m);
                if (StringUtils.isNullOrEmpty(name) && index < jsonArray.size()) {
                    pathIsPresent = true;
                }
            }
        }
        if (pathIsPresent) {
            return this;
        } else {
            throw new JsonException("'" + keyPath + "' is not a valid path");
        }
    } else {
        paths.remove(paths.size() - 1);
        return getInternalJson(new ArrayList<>(paths));
    }
}
Also used : JsonException(com.axway.ats.action.exceptions.JsonException) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList)

Example 7 with JsonException

use of com.axway.ats.action.exceptions.JsonException in project ats-framework by Axway.

the class JsonText method replace.

/**
     * Replace a JSON key. It will throw an error if the keys does not exist prior to replacing.
     * 
     * @param keyPath key path
     * @param newKeyValue the new key value
     * @return this instance
     */
@PublicAtsApi
@SuppressWarnings("unchecked")
public JsonText replace(String keyPath, Object newKeyValue) throws JsonException {
    JsonText parentJsonText = getParentOf(keyPath);
    List<String> paths = new ArrayList<>(Arrays.asList(keyPath.split(PATH_DELIMETER)));
    String lastTokenPath = paths.get(paths.size() - 1);
    Matcher m = NAME_AND_INDEX_PATTERN.matcher(lastTokenPath);
    if (m.find()) {
        // last token is pointing to array
        if (m.groupCount() < 2) {
            throw new JsonException("'" + lastTokenPath + "' does not specify an array in the valid way 'key_name[index_number]'");
        }
        // name in the path token
        String name = m.group(1);
        // index in the path token, for example "name[3]"
        int index = getIndex(lastTokenPath, m);
        if (parentJsonText.jsonObject != null) {
            List<Object> array = (List<Object>) parentJsonText.jsonObject.get(name);
            if (index >= array.size()) {
                throw new JsonException("Cannot replace JSON item at positin " + (index + 1) + " as there are only " + array.size() + " items present");
            } else {
                array.set(index, newKeyValue);
            }
        } else // if( parentJsonText.jsonObject != null ) 
        {
            parentJsonText.jsonArray.set(index, newKeyValue);
        }
    } else {
        // last token is pointing to object
        if (parentJsonText.jsonObject != null) {
            if (!parentJsonText.jsonObject.containsKey(lastTokenPath)) {
                throw new JsonException("Cannot replace JSON item '" + keyPath + "' as it does not exist");
            } else {
                parentJsonText.jsonObject.put(lastTokenPath, newKeyValue);
            }
        } else {
            throw new RuntimeException("Not implemented");
        }
    }
    return this;
}
Also used : JsonException(com.axway.ats.action.exceptions.JsonException) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) JSONObject(org.json.simple.JSONObject) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 8 with JsonException

use of com.axway.ats.action.exceptions.JsonException in project ats-framework by Axway.

the class JsonText method remove.

/**
     * Remove a JSON key 
     * 
     * @param keyPath key path
     * @return this instance
     * @throws JsonException
     */
@PublicAtsApi
public JsonText remove(String keyPath) throws JsonException {
    JsonText parentJsonText = getParentOf(keyPath);
    List<String> paths = new ArrayList<>(Arrays.asList(keyPath.split(PATH_DELIMETER)));
    String lastTokenPath = paths.get(paths.size() - 1);
    Matcher m = NAME_AND_INDEX_PATTERN.matcher(lastTokenPath);
    if (m.find()) {
        // last token is pointing to array
        if (m.groupCount() < 2) {
            throw new JsonException("'" + lastTokenPath + "' does not specify an array in the valid way 'key_name[index_number]'");
        }
        // name in the path token
        String name = m.group(1);
        // index in the path token, for example "name[3]"
        int index = getIndex(lastTokenPath, m);
        if (parentJsonText.jsonObject != null) {
            List<?> array = (List<?>) parentJsonText.jsonObject.get(name);
            if (index >= array.size()) {
                throw new JsonException("Cannot remove item at position " + (index + 1) + " as there are only " + array.size() + " items present");
            } else {
                array.remove(index);
            }
        } else // if( parentJsonText.jsonObject != null ) 
        {
            parentJsonText.jsonArray.remove(index);
        }
    } else {
        // last token is pointing to object
        if (parentJsonText.jsonObject != null) {
            if (!parentJsonText.jsonObject.containsKey(lastTokenPath)) {
                throw new JsonException("Cannot remove JSON item '" + keyPath + "' as it does not exist");
            } else {
                parentJsonText.jsonObject.remove(lastTokenPath);
            }
        } else if (parentJsonText.jsonArray != null) {
            parentJsonText.jsonArray.remove(lastTokenPath);
        } else {
            throw new RuntimeException("Not implemented");
        }
    }
    return this;
}
Also used : JsonException(com.axway.ats.action.exceptions.JsonException) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 9 with JsonException

use of com.axway.ats.action.exceptions.JsonException in project ats-framework by Axway.

the class JsonText method getInternalJson.

/**
     * Return the internal entity available at the pointed position
     * 
     * @param pathTokens
     * @return
     */
private JsonText getInternalJson(List<String> pathTokens) {
    for (String path : pathTokens) {
        Matcher m = NAME_AND_INDEX_PATTERN.matcher(path);
        if (m.find()) {
            // path is pointing to array
            if (m.groupCount() < 2) {
                throw new JsonException("'" + path + "' does not specify an array in the valid way 'key_name[index_number]'");
            }
            // name in the path token
            String name = m.group(1);
            // index in the path token, for example "name[3]"
            int index = getIndex(path, m);
            if (index == -1) {
            // we have an array but no index is specified -> "[]"
            } else if (!StringUtils.isNullOrEmpty(name)) {
                // pointing to JSON object
                pathTokens.remove(0);
                if (index >= 0) {
                    pathTokens.add(0, "[" + String.valueOf(index) + "]");
                }
                return new JsonText(jsonObject.get(name)).getInternalJson(pathTokens);
            } else {
                // directly pointing to JSON array, for example "[3]"
                pathTokens.remove(0);
                if (pathTokens.size() > 0) {
                    return new JsonText(jsonArray.get(index)).getInternalJson(pathTokens);
                } else {
                    if (index >= jsonArray.size()) {
                        throw new JsonException("Cannot remove item at positin " + (index + 1) + " as there are only " + jsonArray.size() + " items present");
                    } else {
                        return new JsonText(jsonArray.get(index));
                    }
                }
            }
        } else {
            // path is pointing to object
            if (!jsonObject.containsKey(path)) {
                throw new JsonException("'" + path + "' is not a valid path");
            }
            if (jsonObject.get(path) == null) {
                // the value is null
                return null;
            }
            JsonText jsonText = new JsonText(jsonObject.get(path));
            pathTokens.remove(0);
            if (pathTokens.size() > 0) {
                jsonText = jsonText.getInternalJson(pathTokens);
            }
            return jsonText;
        }
    }
    return this;
}
Also used : JsonException(com.axway.ats.action.exceptions.JsonException) Matcher(java.util.regex.Matcher)

Aggregations

JsonException (com.axway.ats.action.exceptions.JsonException)9 Matcher (java.util.regex.Matcher)5 BaseTest (com.axway.ats.action.BaseTest)4 JsonText (com.axway.ats.action.json.JsonText)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 PublicAtsApi (com.axway.ats.common.PublicAtsApi)2 List (java.util.List)2 JSONObject (org.json.simple.JSONObject)2 JSONArray (org.json.simple.JSONArray)1