Search in sources :

Example 6 with PathRef

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

the class PathToken method handleArrayIndex.

protected void handleArrayIndex(int index, String currentPath, Object model, EvaluationContextImpl ctx) {
    String evalPath = Utils.concat(currentPath, "[", String.valueOf(index), "]");
    PathRef pathRef = ctx.forUpdate() ? PathRef.create(model, index) : PathRef.NO_OP;
    try {
        Object evalHit = ctx.jsonProvider().getArrayIndex(model, index);
        if (isLeaf()) {
            ctx.addResult(evalPath, pathRef, evalHit);
        } else {
            next().evaluate(evalPath, pathRef, evalHit, ctx);
        }
    } catch (IndexOutOfBoundsException e) {
    }
}
Also used : PathRef(com.jayway.jsonpath.internal.PathRef)

Example 7 with PathRef

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

the class PathToken method handleObjectProperty.

void handleObjectProperty(String currentPath, Object model, EvaluationContextImpl ctx, List<String> properties) {
    if (properties.size() == 1) {
        String property = properties.get(0);
        String evalPath = Utils.concat(currentPath, "['", property, "']");
        Object propertyVal = readObjectProperty(property, model, ctx);
        if (propertyVal == JsonProvider.UNDEFINED) {
            // Better safe than sorry.
            assert this instanceof PropertyPathToken : "only PropertyPathToken is supported";
            if (isLeaf()) {
                if (ctx.options().contains(Option.DEFAULT_PATH_LEAF_TO_NULL)) {
                    propertyVal = null;
                } else {
                    if (ctx.options().contains(Option.SUPPRESS_EXCEPTIONS) || !ctx.options().contains(Option.REQUIRE_PROPERTIES)) {
                        return;
                    } else {
                        throw new PathNotFoundException("No results for path: " + evalPath);
                    }
                }
            } else {
                if (!(isUpstreamDefinite() && isTokenDefinite()) && !ctx.options().contains(Option.REQUIRE_PROPERTIES) || ctx.options().contains(Option.SUPPRESS_EXCEPTIONS)) {
                    // branches could be examined.
                    return;
                } else {
                    throw new PathNotFoundException("Missing property in path " + evalPath);
                }
            }
        }
        PathRef pathRef = ctx.forUpdate() ? PathRef.create(model, property) : PathRef.NO_OP;
        if (isLeaf()) {
            ctx.addResult(evalPath, pathRef, propertyVal);
        } else {
            next().evaluate(evalPath, pathRef, propertyVal, ctx);
        }
    } else {
        String evalPath = currentPath + "[" + Utils.join(", ", "'", properties) + "]";
        assert isLeaf() : "non-leaf multi props handled elsewhere";
        Object merged = ctx.jsonProvider().createMap();
        for (String property : properties) {
            Object propertyVal;
            if (hasProperty(property, model, ctx)) {
                propertyVal = readObjectProperty(property, model, ctx);
                if (propertyVal == JsonProvider.UNDEFINED) {
                    if (ctx.options().contains(Option.DEFAULT_PATH_LEAF_TO_NULL)) {
                        propertyVal = null;
                    } else {
                        continue;
                    }
                }
            } else {
                if (ctx.options().contains(Option.DEFAULT_PATH_LEAF_TO_NULL)) {
                    propertyVal = null;
                } else if (ctx.options().contains(Option.REQUIRE_PROPERTIES)) {
                    throw new PathNotFoundException("Missing property in path " + evalPath);
                } else {
                    continue;
                }
            }
            ctx.jsonProvider().setProperty(merged, property, propertyVal);
        }
        PathRef pathRef = ctx.forUpdate() ? PathRef.create(model, properties) : PathRef.NO_OP;
        ctx.addResult(evalPath, pathRef, merged);
    }
}
Also used : PathRef(com.jayway.jsonpath.internal.PathRef) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException)

Example 8 with PathRef

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

the class RootPathToken method evaluate.

@Override
public void evaluate(String currentPath, PathRef pathRef, Object model, EvaluationContextImpl ctx) {
    if (isLeaf()) {
        PathRef op = ctx.forUpdate() ? pathRef : PathRef.NO_OP;
        ctx.addResult(rootToken, op, model);
    } else {
        next().evaluate(rootToken, pathRef, model, ctx);
    }
}
Also used : PathRef(com.jayway.jsonpath.internal.PathRef)

Example 9 with PathRef

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

the class CompiledPath method evaluate.

@Override
public EvaluationContext evaluate(Object document, Object rootDocument, Configuration configuration, boolean forUpdate) {
    if (logger.isDebugEnabled()) {
        logger.debug("Evaluating path: {}", toString());
    }
    EvaluationContextImpl ctx = new EvaluationContextImpl(this, rootDocument, configuration, forUpdate);
    try {
        PathRef op = ctx.forUpdate() ? PathRef.createRoot(rootDocument) : PathRef.NO_OP;
        root.evaluate("", op, document, ctx);
    } catch (EvaluationAbortException abort) {
    }
    ;
    return ctx;
}
Also used : EvaluationAbortException(com.jayway.jsonpath.internal.EvaluationAbortException) PathRef(com.jayway.jsonpath.internal.PathRef)

Example 10 with PathRef

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

the class JsonPath method renameKey.

public <T> T renameKey(Object jsonObject, String oldKeyName, String newKeyName, Configuration configuration) {
    notNull(jsonObject, "json can not be null");
    notEmpty(newKeyName, "newKeyName 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.renameKey(oldKeyName, newKeyName, 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