Search in sources :

Example 11 with Configuration

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

the class IssuesTest method issue_97.

@Test
public void issue_97() throws Exception {
    String json = "{ \"books\": [ " + "{ \"category\": \"fiction\" }, " + "{ \"category\": \"reference\" }, " + "{ \"category\": \"fiction\" }, " + "{ \"category\": \"fiction\" }, " + "{ \"category\": \"reference\" }, " + "{ \"category\": \"fiction\" }, " + "{ \"category\": \"reference\" }, " + "{ \"category\": \"reference\" }, " + "{ \"category\": \"reference\" }, " + "{ \"category\": \"reference\" }, " + "{ \"category\": \"reference\" } ]  }";
    Configuration conf = Configuration.builder().jsonProvider(new GsonJsonProvider()).mappingProvider(new GsonMappingProvider()).build();
    DocumentContext context = JsonPath.using(conf).parse(json);
    context.delete("$.books[?(@.category == 'reference')]");
    List<String> categories = context.read("$..category", List.class);
    assertThat(categories).containsOnly("fiction");
}
Also used : GsonMappingProvider(com.jayway.jsonpath.spi.mapper.GsonMappingProvider) Configuration(com.jayway.jsonpath.Configuration) GsonJsonProvider(com.jayway.jsonpath.spi.json.GsonJsonProvider) DocumentContext(com.jayway.jsonpath.DocumentContext) BaseTest(com.jayway.jsonpath.BaseTest) Test(org.junit.Test)

Example 12 with Configuration

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

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