@Test public void path() { String json = "{\"a\":[{\"b\":1,\"c\":2},{\"b\":5,\"c\":2}]}"; List<Object> result = JsonPath.using(Configuration.defaultConfiguration().setOptions(Option.DEFAULT_PATH_LEAF_TO_NULL)).parse(json).read("a[?(@.b==5)].d"); Assertions.assertThat(result).hasSize(1); Assertions.assertThat(result.get(0)).isNull(); }
@Test public void issue_46() { String json = "{\"a\": {}}"; Configuration configuration = Configuration.defaultConfiguration().setOptions(Option.SUPPRESS_EXCEPTIONS); assertThat((String)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']"); } }
@Test public void the_age_of_all_with_age_defined() { //List<Integer> result = JsonPath.read(DOCUMENT, "$.children[*].age"); List<Integer> result = JsonPath.using(Configuration.defaultConfiguration().setOptions(Option.SUPPRESS_EXCEPTIONS)).parse(DOCUMENT).read("$.children[*].age"); Assertions.assertThat(result).containsSequence(0, null); }
@Test public void issue_22b() throws Exception { String json = "{\"a\":[{\"b\":1,\"c\":2},{\"b\":5,\"c\":2}]}"; List<Object> res = JsonPath.using(Configuration.defaultConfiguration().setOptions(Option.DEFAULT_PATH_LEAF_TO_NULL)).parse(json).read("a[?(@.b==5)].d"); assertThat(res).hasSize(1).containsNull(); }
@Test public void issue_43() { String json = "{\"test\":null}"; assertThat((String)read(json, "test")).isNull(); assertThat((String)JsonPath.using(Configuration.defaultConfiguration().setOptions(Option.SUPPRESS_EXCEPTIONS)).parse(json).read("nonExistingProperty")).isNull(); try { read(json, "nonExistingProperty"); failBecauseExceptionWasNotThrown(PathNotFoundException.class); } catch (PathNotFoundException e) { } try { read(json, "nonExisting.property"); failBecauseExceptionWasNotThrown(PathNotFoundException.class); } catch (PathNotFoundException e) { } }