Skip to content

Add method for using filter predicates w/ typeRef #1018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
}