/** * 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 an_object_can_be_mapped_to_pojo() { String json = "{\n" + " \"foo\" : \"foo\",\n" + " \"bar\" : 10,\n" + " \"baz\" : true\n" + "}"; FooBarBaz fooBarBaz = JsonPath.using(JACKSON_CONFIGURATION).parse(json).read("$", FooBarBaz.class); assertThat(fooBarBaz.foo).isEqualTo("foo"); assertThat(fooBarBaz.bar).isEqualTo(10L); assertThat(fooBarBaz.baz).isEqualTo(true); }
@Test public void longs_are_unwrapped() { JsonNode node = using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.long-max-property"); long unwrapped = using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.long-max-property", long.class); assertThat(unwrapped).isEqualTo(Long.MAX_VALUE); assertThat(unwrapped).isEqualTo(node.asLong()); }
@Test public void longs_are_unwrapped() { JsonElement node = using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.long-max-property"); long val = using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.long-max-property", Long.class); assertThat(val).isEqualTo(Long.MAX_VALUE); assertThat(val).isEqualTo(node.getAsLong()); }
@Test public void a_filter_can_be_applied() { JSONArray fictionBooks = using(JSON_ORG_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book[?(@.category == 'fiction')]"); assertThat(fictionBooks.length()).isEqualTo(3); }
@Test public void read_books_with_isbn() { JSONArray books = using(JSON_ORG_CONFIGURATION).parse(JSON_DOCUMENT).read("$..book[?(@.isbn)]"); assertThat(books.length()).isEqualTo(2); } }
@Test public void test_type_ref() throws IOException { TypeRef<List<FooBarBaz<Gen>>> typeRef = new TypeRef<List<FooBarBaz<Gen>>>() {}; List<FooBarBaz<Gen>> list = JsonPath.using(GSON_CONFIGURATION).parse(JSON).read("$", typeRef); assertThat(list.get(0).gen.eric).isEqualTo("yepp"); }
@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 small_bigdecimals_are_unwrapped() { final BigDecimal bd = BigDecimal.valueOf(10.5); final String json = "{bd-property = " + bd.toString() + "}"; JsonElement node = using(GSON_CONFIGURATION).parse(json).read("$.bd-property"); BigDecimal val = using(GSON_CONFIGURATION).parse(json).read("$.bd-property", BigDecimal.class); assertThat(val).isEqualTo(bd); assertThat(val).isEqualTo(node.getAsBigDecimal()); }
@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 doubles_are_unwrapped() { final String json = "{double-property = 56.78}"; JsonElement node = using(GSON_CONFIGURATION).parse(json).read("$.double-property"); Double val = using(GSON_CONFIGURATION).parse(json).read("$.double-property", Double.class); assertThat(val).isEqualTo(56.78); assertThat(val).isEqualTo(node.getAsDouble()); }
@Test public void an_indefinite_path_can_be_returned_as_list() { Configuration conf = Configuration.builder().options(ALWAYS_RETURN_LIST).build(); List<Object> result = using(conf).parse("{\"bar\": {\"foo\": null}}").read("$..foo"); assertThat(result).hasSize(1); assertThat(result.get(0)).isNull(); assertThat((List)using(conf).parse("{\"bar\": {\"foo\": [1, 4, 8]}}").read("$..foo")).asList() .containsExactly(Arrays.asList(1, 4, 8)); }
@Test public void an_array_child_property_can_be_updated() { Object o = parse(JSON_DOCUMENT).set("$.store.book[*].display-price", 1).json(); List<Integer> result = parse(o).read("$.store.book[*].display-price"); assertThat(result).containsExactly(1, 1, 1, 1); }
@Test public void keys_in_root_containing_map_can_be_renamed(){ Object o = parse(JSON_DOCUMENT).renameKey("$", "store", "new-store").json(); List<Object> result = parse(o).read("$.new-store[*]"); assertThat(result).isNotEmpty(); }
@Test public void map_array_items_can_be_renamed(){ Object o = parse(JSON_DOCUMENT).renameKey("$.store.book[*]", "category", "renamed-category").json(); List<Object> result = parse(o).read("$.store.book[*].renamed-category"); assertThat(result).isNotEmpty(); }
@Test public void a_path_evaluation_can_be_returned_as_PATH_LIST() { Configuration conf = Configuration.builder().options(AS_PATH_LIST).build(); List<String> pathList = using(conf).parse(JSON_DOCUMENT).read("$..author"); assertThat(pathList).containsExactly( "$['store']['book'][0]['author']", "$['store']['book'][1]['author']", "$['store']['book'][2]['author']", "$['store']['book'][3]['author']"); }
@Test public void non_leaf_multi_props_can_be_required() { final Configuration conf = Configuration.defaultConfiguration().addOptions(Option.REQUIRE_PROPERTIES); final String json = "{\"a\": {\"v\": 5}, \"b\": {\"v\": 4}, \"c\": {\"v\": 1}}"; assertThat((List)using(conf).parse(json).read("$['a', 'c'].v")).asList().containsOnly(5, 1); assertEvaluationThrows(json, "$['d', 'a', 'c', 'm'].v", PathNotFoundException.class, conf); } }