Search in sources :

Example 1 with Configuration

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

the class Bench method runJayway.

public Result runJayway() {
    String result = null;
    String error = null;
    long time;
    Object res = null;
    Configuration configuration = Configuration.defaultConfiguration();
    if (flagWrap) {
        configuration = configuration.addOptions(Option.ALWAYS_RETURN_LIST);
    }
    if (flagSuppress) {
        configuration = configuration.addOptions(Option.SUPPRESS_EXCEPTIONS);
    }
    if (!optionAsValues) {
        configuration = configuration.addOptions(Option.AS_PATH_LIST);
    }
    if (flagNullLeaf) {
        configuration = configuration.addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);
    }
    if (flagRequireProps) {
        configuration = configuration.addOptions(Option.REQUIRE_PROPERTIES);
    }
    long now = System.currentTimeMillis();
    try {
        res = JsonPath.using(configuration).parse(json).read(path);
    } catch (Exception e) {
        error = getError(e);
    } finally {
        time = System.currentTimeMillis() - now;
        if (res instanceof String) {
            result = "\"" + res + "\"";
        } else if (res instanceof Number) {
            result = res.toString();
        } else if (res instanceof Boolean) {
            result = res.toString();
        } else {
            result = res != null ? Configuration.defaultConfiguration().jsonProvider().toJson(res) : "null";
        }
        return new Result("jayway", time, result, error);
    }
}
Also used : Configuration(com.jayway.jsonpath.Configuration)

Example 2 with Configuration

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

the class IssuesTest method issue_22c.

@Test
public void issue_22c() throws Exception {
    //Configuration configuration = Configuration.builder().build();
    Configuration configuration = Configuration.builder().options(Option.SUPPRESS_EXCEPTIONS).build();
    String json = "{\"a\":{\"b\":1,\"c\":2}}";
    assertNull(JsonPath.parse(json, configuration).read("a.d"));
}
Also used : Configuration(com.jayway.jsonpath.Configuration) BaseTest(com.jayway.jsonpath.BaseTest) Test(org.junit.Test)

Example 3 with Configuration

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

the class IssuesTest method issue_22.

@Test(expected = PathNotFoundException.class)
public void issue_22() throws Exception {
    Configuration configuration = Configuration.defaultConfiguration();
    String json = "{\"a\":{\"b\":1,\"c\":2}}";
    JsonPath.parse(json, configuration).read("a.d");
}
Also used : Configuration(com.jayway.jsonpath.Configuration) BaseTest(com.jayway.jsonpath.BaseTest) Test(org.junit.Test)

Example 4 with Configuration

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

the class IssuesTest method issue_143.

@Test
public void issue_143() {
    String json = "{ \"foo\": { \"bar\" : \"val\" }, \"moo\": { \"cow\" : \"val\" } }";
    Configuration configuration = Configuration.builder().options(Option.AS_PATH_LIST).build();
    List<String> pathList = JsonPath.using(configuration).parse(json).read(JsonPath.compile("$.*.bar"));
    assertThat(pathList).containsExactly("$['foo']['bar']");
}
Also used : Configuration(com.jayway.jsonpath.Configuration) BaseTest(com.jayway.jsonpath.BaseTest) Test(org.junit.Test)

Example 5 with Configuration

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

the class NullHandlingTest method last_token_defaults_to_null.

@Test
public void last_token_defaults_to_null() {
    Configuration configuration = Configuration.builder().options(Option.DEFAULT_PATH_LEAF_TO_NULL).build();
    assertNull(JsonPath.parse(DOCUMENT, configuration).read("$.children[2].age"));
}
Also used : Configuration(com.jayway.jsonpath.Configuration) Test(org.junit.Test)

Aggregations

Configuration (com.jayway.jsonpath.Configuration)12 Test (org.junit.Test)8 BaseTest (com.jayway.jsonpath.BaseTest)6 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)2 DocumentContext (com.jayway.jsonpath.DocumentContext)1 GsonJsonProvider (com.jayway.jsonpath.spi.json.GsonJsonProvider)1 JsonOrgJsonProvider (com.jayway.jsonpath.spi.json.JsonOrgJsonProvider)1 GsonMappingProvider (com.jayway.jsonpath.spi.mapper.GsonMappingProvider)1 JsonOrgMappingProvider (com.jayway.jsonpath.spi.mapper.JsonOrgMappingProvider)1 SQLException (java.sql.SQLException)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1