@Test public void json_can_be_parsed() { JsonObject node = using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$"); assertThat(node.get("string-property").getAsString()).isEqualTo("string-value"); }
@Test public void ints_are_unwrapped() { JsonElement node = using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.int-max-property"); int unwrapped = using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.int-max-property", int.class); assertThat(unwrapped).isEqualTo(Integer.MAX_VALUE); assertThat(unwrapped).isEqualTo(node.getAsInt()); }
@Test public void an_object_can_be_read() { JSONObject book = using(JSON_ORG_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book[0]"); assertThat(book.get("author").toString()).isEqualTo("Nigel Rees"); }
@Test public void ints_are_unwrapped() { JsonNode node = using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.int-max-property"); int unwrapped = using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.int-max-property", int.class); assertThat(unwrapped).isEqualTo(Integer.MAX_VALUE); assertThat(unwrapped).isEqualTo(node.asInt()); }
@Test public void bigintegers_are_unwrapped() { final BigInteger bi = BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.TEN); final String json = "{bi-property = " + bi.toString() + "}"; JsonElement node = using(GSON_CONFIGURATION).parse(json).read("$.bi-property"); BigInteger val = using(GSON_CONFIGURATION).parse(json).read("$.bi-property", BigInteger.class); assertThat(val).isEqualTo(bi); assertThat(val).isEqualTo(node.getAsBigInteger()); }
@Test public void result_can_be_mapped_to_object() { List<Map<String, Object>> books = using(JSON_ORG_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book", List.class); assertThat(books.size()).isEqualTo(4); }
@Test public void test_type_ref() throws IOException { TypeRef<List<FooBarBaz<Gen>>> typeRef = new TypeRef<List<FooBarBaz<Gen>>>() {}; List<FooBarBaz<Gen>> list = using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON).read("$", typeRef); assertThat(list.get(0).gen.eric).isEqualTo("yepp"); }
@Test public void small_bigintegers_are_unwrapped() { final BigInteger bi = BigInteger.valueOf(Long.MAX_VALUE); final String json = "{bi-property = " + bi.toString() + "}"; JsonElement node = using(GSON_CONFIGURATION).parse(json).read("$.bi-property"); BigInteger val = using(GSON_CONFIGURATION).parse(json).read("$.bi-property", BigInteger.class); assertThat(val).isEqualTo(bi); assertThat(val).isEqualTo(node.getAsBigInteger()); }
@Test public void a_property_can_be_read() { String category = using(JSON_ORG_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book[0].category"); assertThat(category).isEqualTo("reference"); }
/** * Shortcut for counting found nodes. * @param json json to be parsed * @param path path to be evaluated * @param expectedResultCount expected number of nodes to be found * @param conf conf to use during evaluation */ public static void assertHasResults(final String json, final String path, final int expectedResultCount, Configuration conf) { Object result = JsonPath.using(conf).parse(json).read(path); assertThat(conf.jsonProvider().length(result)).isEqualTo(expectedResultCount); } }
@Test public void test_type_ref() throws IOException { TypeRef<List<FooBarBaz<Sub>>> typeRef = new TypeRef<List<FooBarBaz<Sub>>>() {}; assertThat(using(conf).parse(JSON).read("$", typeRef)).extracting("foo").containsExactly("foo0", "foo1", "foo2"); }
@Test public void testUnclosedFunctionCallNegative() { try { using(conf).parse(this.NUMBER_SERIES).read("$.numbers.append(0, 1, 2"); assert(false); } catch (Exception e) { assertTrue(e.getMessage().startsWith("Arguments to function: 'append'")); } }
@Test public void list_of_numbers() { ArrayNode objs = using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book[*].display-price"); assertThat(objs.get(0).asDouble()).isEqualTo(8.95D); assertThat(objs.get(1).asDouble()).isEqualTo(12.99D); assertThat(objs.get(2).asDouble()).isEqualTo(8.99D); assertThat(objs.get(3).asDouble()).isEqualTo(22.99D); }
@Test(expected = MappingException.class) public void test_type_ref_fail() throws IOException { TypeRef<List<FooBarBaz<Integer>>> typeRef = new TypeRef<List<FooBarBaz<Integer>>>() {}; using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON).read("$", typeRef); }
@Test public void list_of_numbers() { JsonArray objs = using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book[*].display-price"); assertThat(objs.iterator()).extracting("asDouble").containsExactly(8.95D, 12.99D, 8.99D, 22.99D); }
@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 always_return_same_object() { // Test because of Bug #211 DocumentContext context = using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON_DOCUMENT); ObjectNode node1 = context.read("$"); ObjectNode child1 = new ObjectNode(JsonNodeFactory.instance); child1.put("name", "test"); context.put("$", "child", child1); ObjectNode node2 = context.read("$"); ObjectNode child2 = context.read("$.child"); assertThat(node1).isSameAs(node2); assertThat(child1).isSameAs(child2); }
@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 a_path_evaluation_can_be_returned_as_PATH_LIST() { Configuration conf = Configuration.builder().options(AS_PATH_LIST).build(); List<String> pathList = using(conf).parse("{\"foo\" : \"bar\"}").read("$.foo"); assertThat(pathList).containsOnly("$['foo']"); }
@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); }