Search in sources :

Example 1 with PathRef

use of com.jayway.jsonpath.internal.PathRef in project JsonPath by jayway.

the class JsonPath method delete.

/**
     * Deletes the object this path points to in the provided jsonObject
     *
     * @param jsonObject    a json object
     * @param configuration configuration to use
     * @param <T>           expected return type
     * @return the updated jsonObject or the path list to deleted objects if option AS_PATH_LIST is set.
     */
public <T> T delete(Object jsonObject, Configuration configuration) {
    notNull(jsonObject, "json can not be null");
    notNull(configuration, "configuration can not be null");
    EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true);
    for (PathRef updateOperation : evaluationContext.updateOperations()) {
        updateOperation.delete(configuration);
    }
    return resultByConfiguration(jsonObject, configuration, evaluationContext);
}
Also used : PathRef(com.jayway.jsonpath.internal.PathRef) EvaluationContext(com.jayway.jsonpath.internal.EvaluationContext)

Example 2 with PathRef

use of com.jayway.jsonpath.internal.PathRef in project JsonPath by jayway.

the class JsonPath method set.

/**
     * Set the value this path points to in the provided jsonObject
     *
     * @param jsonObject    a json object
     * @param configuration configuration to use
     * @param <T>           expected return type
     * @return the updated jsonObject or the path list to updated objects if option AS_PATH_LIST is set.
     */
public <T> T set(Object jsonObject, Object newVal, Configuration configuration) {
    notNull(jsonObject, "json can not be null");
    notNull(configuration, "configuration can not be null");
    EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true);
    for (PathRef updateOperation : evaluationContext.updateOperations()) {
        updateOperation.set(newVal, configuration);
    }
    return resultByConfiguration(jsonObject, configuration, evaluationContext);
}
Also used : PathRef(com.jayway.jsonpath.internal.PathRef) EvaluationContext(com.jayway.jsonpath.internal.EvaluationContext)

Example 3 with PathRef

use of com.jayway.jsonpath.internal.PathRef in project JsonPath by jayway.

the class JsonPath method add.

/**
     * Adds a new value to the Array this path points to in the provided jsonObject
     *
     * @param jsonObject    a json object
     * @param value         the value to add
     * @param configuration configuration to use
     * @param <T>           expected return type
     * @return the updated jsonObject or the path list to updated object if option AS_PATH_LIST is set.
     */
public <T> T add(Object jsonObject, Object value, Configuration configuration) {
    notNull(jsonObject, "json can not be null");
    notNull(configuration, "configuration can not be null");
    EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true);
    for (PathRef updateOperation : evaluationContext.updateOperations()) {
        updateOperation.add(value, configuration);
    }
    return resultByConfiguration(jsonObject, configuration, evaluationContext);
}
Also used : PathRef(com.jayway.jsonpath.internal.PathRef) EvaluationContext(com.jayway.jsonpath.internal.EvaluationContext)

Example 4 with PathRef

use of com.jayway.jsonpath.internal.PathRef in project JsonPath by jayway.

the class JsonPath method map.

/**
     * Replaces the value on the given path with the result of the {@link MapFunction}.
     *
     * @param jsonObject        a json object
     * @param mapFunction       Converter object to be invoked
     * @param configuration     configuration to use
     * @return the updated jsonObject or the path list to updated objects if option AS_PATH_LIST is set.
     */
public <T> T map(Object jsonObject, MapFunction mapFunction, Configuration configuration) {
    notNull(jsonObject, "json can not be null");
    notNull(configuration, "configuration can not be null");
    notNull(mapFunction, "mapFunction can not be null");
    EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true);
    for (PathRef updateOperation : evaluationContext.updateOperations()) {
        updateOperation.convert(mapFunction, configuration);
    }
    return resultByConfiguration(jsonObject, configuration, evaluationContext);
}
Also used : PathRef(com.jayway.jsonpath.internal.PathRef) EvaluationContext(com.jayway.jsonpath.internal.EvaluationContext)

Example 5 with PathRef

use of com.jayway.jsonpath.internal.PathRef in project JsonPath by jayway.

the class JsonPath method put.

/**
     * Adds or updates the Object this path points to in the provided jsonObject with a key with a value
     *
     * @param jsonObject    a json object
     * @param value         the key to add or update
     * @param value         the new value
     * @param configuration configuration to use
     * @param <T>           expected return type
     * @return the updated jsonObject or the path list to updated objects if option AS_PATH_LIST is set.
     */
public <T> T put(Object jsonObject, String key, Object value, Configuration configuration) {
    notNull(jsonObject, "json can not be null");
    notEmpty(key, "key can not be null or empty");
    notNull(configuration, "configuration can not be null");
    EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true);
    for (PathRef updateOperation : evaluationContext.updateOperations()) {
        updateOperation.put(key, value, configuration);
    }
    return resultByConfiguration(jsonObject, configuration, evaluationContext);
}
Also used : PathRef(com.jayway.jsonpath.internal.PathRef) EvaluationContext(com.jayway.jsonpath.internal.EvaluationContext)

Aggregations

PathRef (com.jayway.jsonpath.internal.PathRef)10 EvaluationContext (com.jayway.jsonpath.internal.EvaluationContext)6 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)1 EvaluationAbortException (com.jayway.jsonpath.internal.EvaluationAbortException)1