Search in sources :

Example 1 with PathNotFoundException

use of com.jayway.jsonpath.PathNotFoundException in project JsonPath by jayway.

the class JsonAsserterImpl method assertNotDefined.

/**
     * {@inheritDoc}
     */
public JsonAsserter assertNotDefined(String path) {
    try {
        Configuration c = Configuration.defaultConfiguration();
        JsonPath.using(c).parse(jsonObject).read(path);
        throw new AssertionError(format("Document contains the path <%s> but was expected not to.", path));
    } catch (PathNotFoundException e) {
    }
    return this;
}
Also used : Configuration(com.jayway.jsonpath.Configuration) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException)

Example 2 with PathNotFoundException

use of com.jayway.jsonpath.PathNotFoundException 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 3 with PathNotFoundException

use of com.jayway.jsonpath.PathNotFoundException in project JsonPath by jayway.

the class IssuesTest method issue_46.

@Test
public void issue_46() {
    String json = "{\"a\": {}}";
    Configuration configuration = Configuration.defaultConfiguration().setOptions(Option.SUPPRESS_EXCEPTIONS);
    assertThat(JsonPath.using(configuration).parse(json).read("a.x")).isNull();
    try {
        read(json, "a.x");
        failBecauseExceptionWasNotThrown(PathNotFoundException.class);
    } catch (PathNotFoundException e) {
        assertThat(e).hasMessage("No results for path: $['a']['x']");
    }
}
Also used : Configuration(com.jayway.jsonpath.Configuration) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) BaseTest(com.jayway.jsonpath.BaseTest) Test(org.junit.Test)

Example 4 with PathNotFoundException

use of com.jayway.jsonpath.PathNotFoundException in project jmeter by apache.

the class JSONManager method extractWithJsonPath.

/**
     * 
     * @param jsonString JSON String from which data is extracted
     * @param jsonPath JSON-PATH expression
     * @return List of JSON Strings of the extracted data
     * @throws ParseException when parsing fails
     */
public List<Object> extractWithJsonPath(String jsonString, String jsonPath) throws ParseException {
    JsonPath jsonPathParser = getJsonPath(jsonPath);
    List<Object> extractedObjects;
    try {
        extractedObjects = jsonPathParser.read(jsonString, DEFAULT_CONFIGURATION);
    } catch (PathNotFoundException e) {
        if (log.isDebugEnabled()) {
            log.debug("Could not find JSON Path {} in [{}]: {}", jsonPath, jsonString, e.getLocalizedMessage());
        }
        return Collections.emptyList();
    }
    List<Object> results = new ArrayList<>(extractedObjects.size());
    for (Object obj : extractedObjects) {
        results.add(stringifyJSONObject(obj));
    }
    return results;
}
Also used : ArrayList(java.util.ArrayList) JSONObject(net.minidev.json.JSONObject) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) JsonPath(com.jayway.jsonpath.JsonPath)

Aggregations

PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)4 Configuration (com.jayway.jsonpath.Configuration)2 BaseTest (com.jayway.jsonpath.BaseTest)1 JsonPath (com.jayway.jsonpath.JsonPath)1 PathRef (com.jayway.jsonpath.internal.PathRef)1 ArrayList (java.util.ArrayList)1 JSONObject (net.minidev.json.JSONObject)1 Test (org.junit.Test)1