public JsonPathSelector(String jsonPathStr) { this.jsonPathStr = jsonPathStr; this.jsonPath = JsonPath.compile(this.jsonPathStr); }
/** * Construct a new {@code JsonPathExpectationsHelper}. * @param expression the {@link JsonPath} expression; never {@code null} or empty * @param args arguments to parameterize the {@code JsonPath} expression with, * using formatting specifiers defined in {@link String#format(String, Object...)} */ public JsonPathExpectationsHelper(String expression, Object... args) { Assert.hasText(expression, "expression must not be null or empty"); this.expression = String.format(expression, args); this.jsonPath = JsonPath.compile(this.expression); }
@GET @Path("/validate") @Produces(MediaType.APPLICATION_JSON) public Response validate(@QueryParam("path") String path) { int result = -1; try { JsonPath compiled = JsonPath.compile(path); result = compiled.isDefinite() ? 0 : 1; } catch (Exception e) { } return Response.ok(Collections.singletonMap("result", result)).build(); }
/** * @see JsonPath#isDefinite() */ public static boolean isPathDefinite(String path) { return compile(path).isDefinite(); }
@AssistedInject public JsonPathCodec(@Assisted Configuration configuration) { super(configuration); final String pathString = configuration.getString(CK_PATH); jsonPath = pathString == null ? null : JsonPath.compile(pathString); }
@Override public Function<Group, Object> makeJsonPathExtractor(String expr) { final JsonPath jsonPath = JsonPath.compile(expr); return record -> { Object val = jsonPath.read(record, jsonPathConfiguration); return finalizeConversion(val); }; }
@Override public Function<JsonNode, Object> makeJsonPathExtractor(final String expr) { final JsonPath jsonPath = JsonPath.compile(expr); return node -> valueConversionFunction(jsonPath.read(node, JSONPATH_CONFIGURATION)); }
@Override public Function<GenericRecord, Object> makeJsonPathExtractor(final String expr) { final JsonPath jsonPath = JsonPath.compile(expr); return record -> transformValue(jsonPath.read(record, JSONPATH_CONFIGURATION)); }
private JsonPath pathFromCache(String path, Predicate[] filters) { Cache cache = CacheProvider.getCache(); String cacheKey = Utils.concat(path, new LinkedList<Predicate>(asList(filters)).toString()); JsonPath jsonPath = cache.get(cacheKey); if (jsonPath == null) { jsonPath = compile(path, filters); cache.put(cacheKey, jsonPath); } return jsonPath; }
@Test(expected = InvalidPathException.class) //see https://github.com/json-path/JsonPath/issues/428 public void prevent_stack_overflow_error_when_unclosed_property() { JsonPath.compile("$['boo','foo][?(@ =~ /bar/)]"); }
@Test public void read_store_book_wildcard() throws Exception { JsonPath path = JsonPath.compile("$.store.book[*]"); List<Object> list = path.read(DOCUMENT); Assertions.assertThat(list.size()).isEqualTo(4); }
@Test public void shouldMatchExistingCompiledJsonPath() { assertThat(BOOKS_JSON, withJsonPath(compile("$.expensive"))); assertThat(BOOKS_JSON, withJsonPath(compile("$.store.bicycle"))); assertThat(BOOKS_JSON, withJsonPath(compile("$.store.book[2].title"))); assertThat(BOOKS_JSON, withJsonPath(compile("$.store.book[*].author"))); }
@Test public void read_store_book_1() throws Exception { JsonPath path = JsonPath.compile("$.store.book[1]"); Map map = path.read(DOCUMENT); assertEquals("Evelyn Waugh", map.get("author")); }
@Test public void shouldMatchJsonPathEvaluatedToCollectionValue() { assertThat(BOOKS_JSON, withJsonPath(compile("$.store.book[*].author"), instanceOf(List.class))); assertThat(BOOKS_JSON, withJsonPath(compile("$.store.book[*].author"), hasSize(4))); assertThat(BOOKS_JSON, withJsonPath(compile("$.store.book[*].author"), hasItem("Evelyn Waugh"))); assertThat(BOOKS_JSON, withJsonPath(compile("$..book[2].title"), hasItem("Moby Dick"))); assertThat(BOOKS_JSON, withJsonPath("$.store.book[*].author", instanceOf(Collection.class))); assertThat(BOOKS_JSON, withJsonPath("$.store.book[*].author", hasSize(4))); assertThat(BOOKS_JSON, withJsonPath("$.store.book[*].author", hasItem("Evelyn Waugh"))); assertThat(BOOKS_JSON, withJsonPath("$..book[2].title", hasItem("Moby Dick"))); }
@Test public void issue_94_1() throws Exception { LRUCache cache = new LRUCache(200); JsonPath dummy = JsonPath.compile("$"); for (int i = 0; i < 1000; ++i) { String key = String.valueOf(i); cache.get(key); cache.put(key, dummy); } assertThat(cache.size()).isEqualTo(200); }
@Test public void shouldMatchJsonPathEvaluatedToStringValue() { assertThat(BOOKS_JSON, withJsonPath(compile("$.store.bicycle.color"), equalTo("red"))); assertThat(BOOKS_JSON, withJsonPath(compile("$.store.book[2].title"), equalTo("Moby Dick"))); assertThat(BOOKS_JSON, withJsonPath("$.store.name", equalTo("Little Shop"))); assertThat(BOOKS_JSON, withJsonPath("$.store.book[2].title", equalTo("Moby Dick"))); }
@Test public void shouldMatchJsonPathEvaluatedToIntegerValue() { assertThat(BOOKS_JSON, withJsonPath(compile("$.expensive"), equalTo(10))); assertThat(BOOKS_JSON, withJsonPath("$.expensive", equalTo(10))); }
@Test public void shouldMatchJsonPathEvaluatedToDoubleValue() { assertThat(BOOKS_JSON, withJsonPath(compile("$.store.bicycle.price"), equalTo(19.95))); assertThat(BOOKS_JSON, withJsonPath("$.store.bicycle.price", equalTo(19.95))); }
@Test public void shouldNotMatchExistingJsonPath() { assertThat(JSON, not(withoutJsonPath(compile("$.name")))); assertThat(JSON, not(withoutJsonPath("$.name"))); assertThat(JSON, not(withoutJsonPath("$.flag"))); assertThat(JSON, not(withoutJsonPath("$.empty_array"))); assertThat(JSON, not(withoutJsonPath("$.empty_object"))); assertThat(JSON, not(withoutJsonPath("$.none"))); }
@Test public void issue_143() { String json = "{ \"foo\": { \"bar\" : \"val\" }, \"moo\": { \"cow\" : \"val\" } }"; Configuration configuration = Configuration.builder().options( Option.AS_PATH_LIST ).build(); List<String> pathList = JsonPath.using(configuration).parse(json).read(JsonPath.compile("$.*.bar")); assertThat(pathList).containsExactly("$['foo']['bar']"); }