Search in sources :

Example 6 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 7 with Configuration

use of com.jayway.jsonpath.Configuration 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 8 with Configuration

use of com.jayway.jsonpath.Configuration in project phoenix by apache.

the class JsonEventSerializer method getPatternData.

private String getPatternData(JSONObject json, String pattern) {
    Configuration JSON_ORG_CONFIGURATION = Configuration.builder().mappingProvider(new JsonOrgMappingProvider()).jsonProvider(new JsonOrgJsonProvider()).build();
    String value;
    try {
        Object object = JsonPath.using(JSON_ORG_CONFIGURATION).parse(json).read(pattern);
        value = object.toString();
    } catch (Exception e) {
        value = null;
    }
    return value;
}
Also used : JsonOrgMappingProvider(com.jayway.jsonpath.spi.mapper.JsonOrgMappingProvider) Configuration(com.jayway.jsonpath.Configuration) JsonOrgJsonProvider(com.jayway.jsonpath.spi.json.JsonOrgJsonProvider) JSONObject(org.json.JSONObject) SQLException(java.sql.SQLException) JSONException(org.json.JSONException)

Example 9 with Configuration

use of com.jayway.jsonpath.Configuration in project geode by apache.

the class UseJacksonForJsonPathRule method saveDefaults.

private void saveDefaults() {
    try {
        Configuration defaultConfiguration = Configuration.defaultConfiguration();
        this.jsonProvider = defaultConfiguration.jsonProvider();
        this.mappingProvider = defaultConfiguration.mappingProvider();
        this.options = defaultConfiguration.getOptions();
        this.hadDefaults = true;
    } catch (NoClassDefFoundError ignore) {
        this.hadDefaults = false;
    }
}
Also used : Configuration(com.jayway.jsonpath.Configuration)

Example 10 with Configuration

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

the class IssuesTest method issue_99.

@Test
public void issue_99() throws Exception {
    String json = "{\n" + "    \"array1\": [\n" + "        {\n" + "            \"array2\": []\n" + "        },\n" + "        {\n" + "            \"array2\": [\n" + "                {\n" + "                    \"key\": \"test_key\"\n" + "                }\n" + "            ]\n" + "        }\n" + "    ]\n" + "}";
    Configuration configuration = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);
    List<String> keys = JsonPath.using(configuration).parse(json).read("$.array1[*].array2[0].key");
}
Also used : Configuration(com.jayway.jsonpath.Configuration) BaseTest(com.jayway.jsonpath.BaseTest) 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