/** * Applies this JsonPath to the provided json string * * @param json a json string * @param <T> expected return type * @return list of objects matched by the given path */ @SuppressWarnings({"unchecked"}) public <T> T read(String json) { return read(json, Configuration.defaultConfiguration()); }
/** * Applies this JsonPath to the provided json URL * * @param jsonURL url to read from * @param <T> expected return type * @return list of objects matched by the given path * @throws IOException */ @SuppressWarnings({"unchecked"}) public <T> T read(URL jsonURL) throws IOException { return read(jsonURL, Configuration.defaultConfiguration()); }
@Override public Configuration configuration() { return Configuration.defaultConfiguration(); } };
/** * Applies this JsonPath to the provided json input stream * * @param jsonInputStream input stream to read from * @param <T> expected return type * @return list of objects matched by the given path * @throws IOException */ @SuppressWarnings({"unchecked"}) public <T> T read(InputStream jsonInputStream) throws IOException { return read(jsonInputStream, Configuration.defaultConfiguration()); }
/** * Applies this JsonPath to the provided json file * * @param jsonFile file to read from * @param <T> expected return type * @return list of objects matched by the given path * @throws IOException */ @SuppressWarnings({"unchecked"}) public <T> T read(File jsonFile) throws IOException { return read(jsonFile, Configuration.defaultConfiguration()); }
/** * {@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; }
@Override public JsonAsserter assertNotDefined(String path, String message) { 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; }
@Test public void a_path_evaluation_is_returned_as_VALUE_by_default() { Configuration conf = Configuration.defaultConfiguration(); assertThat((String)using(conf).parse("{\"foo\" : \"bar\"}").read("$.foo")).isEqualTo("bar"); }
@Test public void when_deep_scanning_require_properties_is_ignored_on_scan_target_but_not_on_children() { final Configuration conf = Configuration.defaultConfiguration().addOptions(Option.REQUIRE_PROPERTIES); assertEvaluationThrows("{\"foo\": {\"baz\": 4}}", "$..foo.bar", PathNotFoundException.class, conf); }
@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"); }
@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 testFilterWithOrShortCircuit2() throws Exception { Object json = Configuration.defaultConfiguration().jsonProvider().parse("{\"firstname\":\"Bob\",\"surname\":\"Smith\",\"age\":30}"); assertThat(Filter.parse("[?((@.firstname == 'Bob' || @.firstname == 'Jane') && @.surname == 'Smith')]").apply(createPredicateContext(json))).isTrue(); }
@Test public void all_store_properties() throws Exception { /* List<Object> itemsInStore = JsonPath.read(DOCUMENT, "$.store.*"); assertEquals(JsonPath.read(itemsInStore, "$.[0].[0].author"), "Nigel Rees"); assertEquals(JsonPath.read(itemsInStore, "$.[0][0].author"), "Nigel Rees"); */ List<String> result = PathCompiler.compile("$.store.*").evaluate(OBJ_DOCUMENT, OBJ_DOCUMENT, Configuration.defaultConfiguration()).getPathList(); Assertions.assertThat(result).containsOnly( "$['store']['bicycle']", "$['store']['book']"); }
@Test(expected = PathNotFoundException.class) public void property_not_found_option_throw() { //String result = JsonPath.using(Configuration.defaultConfiguration().setOptions(Option.THROW_ON_MISSING_PROPERTY)).parse(SIMPLE_MAP).read("$.not-found"); String result = JsonPath.using(Configuration.defaultConfiguration()).parse(SIMPLE_MAP).read("$.not-found"); assertThat(result).isNull(); }
@Test(expected = PathNotFoundException.class) public void a_leafs_is_not_defaulted_to_null() { Configuration conf = Configuration.defaultConfiguration(); assertThat((String)using(conf).parse("{\"foo\" : \"bar\"}").read("$.baz")).isNull(); }
@Test public void a_definite_path_is_not_returned_as_list_by_default() { Configuration conf = Configuration.defaultConfiguration(); assertThat((String)using(conf).parse("{\"foo\" : \"bar\"}").read("$.foo")).isInstanceOf(String.class); }
@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 multi_props_can_be_defaulted_to_null() { Map<String, Object> model = new HashMap<String, Object>(){{ put("a", "a-val"); put("b", "b-val"); put("c", "c-val"); }}; Configuration conf = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL); assertThat(using(conf).parse(model).read("$['a', 'd']", Map.class)) .containsEntry("a", "a-val") .containsEntry("d", null); }
@Test(expected = PathNotFoundException.class) public void multi_props_can_be_required() { Map<String, Object> model = new HashMap<String, Object>(){{ put("a", "a-val"); put("b", "b-val"); put("c", "c-val"); }}; Configuration conf = Configuration.defaultConfiguration().addOptions(Option.REQUIRE_PROPERTIES); using(conf).parse(model).read("$['a', 'x']", Map.class); }