/** * Variant of {@link #evaluateJsonPath(String)} with a target type. * This can be useful for matching numbers reliably for example coercing an * integer into a double. * @param content the content to evaluate against * @return the result of the evaluation * @throws AssertionError if the evaluation fails */ public Object evaluateJsonPath(String content, Class<?> targetType) { try { return JsonPath.parse(content).read(this.expression, targetType); } catch (Throwable ex) { String message = "No value at JSON path \"" + this.expression + "\""; throw new AssertionError(message, ex); } }
@Test public void head_tail_grabs_correct() { List<Integer> result = JsonPath.parse(JSON).read("$[0:3]"); assertThat(result, Matchers.contains(1, 3, 5)); }
@Test public void patterns_can_be_evaluated_with_ignore_case() { List<String> resLeft = JsonPath.parse(JSON_DOCUMENT).read("$.store.book[?(@.category =~ /REFERENCE/)].author"); assertThat(resLeft).isEmpty(); resLeft = JsonPath.parse(JSON_DOCUMENT).read("$.store.book[?(@.category =~ /REFERENCE/i)].author"); assertThat(resLeft).containsExactly("Nigel Rees"); }
@Test public void add_to_object() { Object res = parse(JSON_DOCUMENT).put("$.store.book[0]", "new-key", "new-value").read("$.store.book[0].new-key"); assertThat(res).isEqualTo("new-value"); }
@Test public void item_can_be_added_to_root_array() { List<Integer> model = new LinkedList<Integer>(); model.add(1); model.add(2); List<Integer> ints = parse(model).add("$", 3).read("$"); assertThat(ints).containsExactly(1,2,3); }
@Test public void patterns_can_be_evaluated() { List<String> resLeft = JsonPath.parse(JSON_DOCUMENT).read("$.store.book[?(@.category =~ /reference/)].author"); assertThat(resLeft).containsExactly("Nigel Rees"); resLeft = JsonPath.parse(JSON_DOCUMENT).read("$.store.book[?(/reference/ =~ @.category)].author"); assertThat(resLeft).containsExactly("Nigel Rees"); }
@Test public void single_match_value_can_be_mapped(){ MapFunction mapFunction = new ToStringMapFunction(); String stringResult = parse(JSON_DOCUMENT).map("$.string-property", mapFunction).read("$.string-property"); assertThat(stringResult.endsWith("converted")).isTrue(); }
@Test public void operations_can_chained() { Object o = parse(JSON_DOCUMENT) .delete("$.store.book[*].display-price") .set("$.store.book[*].category", "A") .json(); List<Integer> prices = parse(o).read("$.store.book[*].display-price"); List<String> categories = parse(o).read("$.store.book[*].category"); assertThat(prices).isEmpty(); assertThat(categories).containsExactly("A", "A", "A", "A"); }
@Test public void when_deep_scanning_array_index_oob_is_ignored() { Object result = JsonPath.parse("{\"x\": [0,1,[0,1,2,3,10],null]}").read("$..[4]"); assertThat(result).asList().containsOnly(10); result = JsonPath.parse("{\"x\": [null,null,[0,1,2,3]], \"y\": [null,null,[0,1]]}").read("$..[2][3]"); assertThat(result).asList().containsOnly(3); }
@Test public void an_array_slice_can_be_updated() { List<String> res = parse(JSON_DOCUMENT).set("$.store.book[0:2]", "a").read("$.store.book[0:2]"); assertThat(res).containsExactly("a", "a"); }
@Test public void multi_prop_delete() { List<Map<String, Object>> res = parse(JSON_DOCUMENT).delete("$.store.book[*]['author', 'category']").read("$.store.book[*]['author', 'category']"); assertThat(res).containsExactly(EMPTY_MAP, EMPTY_MAP, EMPTY_MAP, EMPTY_MAP); }
@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 multi_props_can_be_non_leafs() { Object result = JsonPath.parse("{\"a\": {\"v\": 5}, \"b\": {\"v\": 4}, \"c\": {\"v\": 1}}").read( "$['a', 'c'].v"); assertThat(result).asList().containsOnly(5, 1); }
@Test public void an_array_criteria_can_be_updated() { List<String> res = parse(JSON_DOCUMENT) .set("$.store.book[?(@.category == 'fiction')]", "a") .read("$.store.book[?(@ == 'a')]"); assertThat(res).containsExactly("a", "a", "a"); }
@Test public void an_array_criteria_can_be_deleted() { List<String> res = parse(JSON_DOCUMENT) .delete("$.store.book[?(@.category == 'fiction')]") .read("$.store.book[*].category"); assertThat(res).containsExactly("reference"); }
@Test public void multi_prop_update() { Map<String, Object> expected = new HashMap<String, Object>(){{ put("author", "a"); put("category", "a"); }}; List<Map<String, Object>> res = parse(JSON_DOCUMENT).set("$.store.book[*]['author', 'category']", "a").read("$.store.book[*]['author', 'category']"); assertThat(res).containsExactly(expected, expected, expected, expected); }