Skip to content

Commit

Permalink
Add method for using filter predicates w/ typeRef
Browse files Browse the repository at this point in the history
The read methods that take a `Predicate...` were missing a typeRef variant.
  • Loading branch information
drucci committed Sep 6, 2024
1 parent 45333e0 commit ab8a400
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
11 changes: 11 additions & 0 deletions json-path/src/main/java/com/jayway/jsonpath/ReadContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ public interface ReadContext {
*/
<T> T read(String path, Class<T> type, Predicate... filters);

/**
* Reads the given path from this context
*
* @param path path to read
* @param typeRef expected return type (will try to map)
* @param filters filters
* @param <T>
* @return result
*/
<T> T read(String path, TypeRef<T> typeRef, Predicate... filters);

/**
* Reads the given path from this context
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public <T> T read(String path, Class<T> type, Predicate... filters) {
return convert(read(path, filters), type, configuration);
}

@Override
public <T> T read(String path, TypeRef<T> type, Predicate... filters) {
return convert(read(path, filters), type, configuration);
}

@Override
public <T> T read(JsonPath path) {
notNull(path, "path can not be null");
Expand Down
14 changes: 14 additions & 0 deletions json-path/src/test/java/com/jayway/jsonpath/PredicateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,18 @@ public boolean apply(PredicateContext ctx) {

assertThat(reader.read("$.store.book[?].isbn", List.class, booksWithISBN)).containsOnly("0-395-19395-8", "0-553-21311-3");
}

@Test
public void predicates_filters_can_be_applied_with_type_ref() {
TypeRef<List<String>> typeRef = new TypeRef<List<String>>() {
};
Predicate booksWithISBN = new Predicate() {
@Override
public boolean apply(PredicateContext ctx) {
return ctx.item(Map.class).containsKey("isbn");
}
};

assertThat(reader.read("$.store.book[?].isbn", typeRef, booksWithISBN)).containsOnly("0-395-19395-8", "0-553-21311-3");
}
}

0 comments on commit ab8a400

Please sign in to comment.