Browse Source

Filter support extended

pull/7/merge
Kalle Stenflo 13 years ago
parent
commit
8c10101593
  1. 72
      json-path/src/main/java/com/jayway/jsonpath/Filter.java
  2. 10
      json-path/src/main/java/com/jayway/jsonpath/JsonModel.java
  3. 58
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  4. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/PathToken.java
  5. 1
      json-path/src/main/java/com/jayway/jsonpath/internal/PathTokenizer.java
  6. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayEvalFilter.java
  7. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayIndexFilter.java
  8. 44
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayQueryFilter.java
  9. 6
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java
  10. 14
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterFactory.java
  11. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/HasFieldFilter.java
  12. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/PassthroughFilter.java
  13. 9
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/PathTokenFilter.java
  14. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ScanFilter.java
  15. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/WildcardFilter.java
  16. 1
      json-path/src/main/java/com/jayway/jsonpath/spi/JsonProvider.java
  17. 6
      json-path/src/main/java/com/jayway/jsonpath/spi/MappingProvider.java
  18. 1
      json-path/src/main/java/com/jayway/jsonpath/spi/impl/JacksonProvider.java
  19. 1
      json-path/src/test/java/com/jayway/jsonpath/ComplianceTest.java
  20. 2
      json-path/src/test/java/com/jayway/jsonpath/JsonModelOpsTest.java
  21. 8
      json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java
  22. 117
      json-path/src/test/java/com/jayway/jsonpath/JsonPathFilterTest.java
  23. 1
      json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java
  24. 5
      json-path/src/test/java/com/jayway/jsonpath/PathTest.java

72
json-path/src/main/java/com/jayway/jsonpath/Filter.java

@ -6,46 +6,70 @@ import java.util.*;
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 3/5/12
* Time: 12:05 PM
* Time: 5:31 PM
*/
public class Filter {
public abstract class Filter {
private HashMap<String, Criteria> criteria = new LinkedHashMap<String, Criteria>();
public abstract boolean apply(Map<String, Object> map);
public Filter(Criteria criteria) {
addCriteria(criteria);
public List<?> doFilter(List<Map<String, Object>> filterItems) {
List<Object> result = new ArrayList<Object>();
for (Map<String, Object> filterItem : filterItems) {
if(apply(filterItem)){
result.add(filterItem);
}
}
return result;
}
public static Filter filter(Criteria criteria) {
return new Filter(criteria);
return new MapFilter(criteria);
}
public Filter addCriteria(Criteria criteria) {
Criteria existing = this.criteria.get(criteria.getKey());
String key = criteria.getKey();
if (existing == null) {
this.criteria.put(key, criteria);
} else {
existing.andOperator(criteria);
}
return this;
}
// --------------------------------------------------------
//
// Default filter implementation
//
// --------------------------------------------------------
private static class MapFilter extends Filter {
protected List<Criteria> getCriteria() {
return new ArrayList<Criteria>(this.criteria.values());
}
private HashMap<String, Criteria> criteria = new LinkedHashMap<String, Criteria>();
public boolean apply(Map<String, Object> map) {
public MapFilter(Criteria criteria) {
addCriteria(criteria);
}
for (Criteria criterion : getCriteria()) {
if(!criterion.apply(map)){
return false;
public MapFilter addCriteria(Criteria criteria) {
Criteria existing = this.criteria.get(criteria.getKey());
String key = criteria.getKey();
if (existing == null) {
this.criteria.put(key, criteria);
} else {
existing.andOperator(criteria);
}
return this;
}
return true;
protected List<Criteria> getCriteria() {
return new ArrayList<Criteria>(this.criteria.values());
}
@Override
public boolean apply(Map<String, Object> map) {
for (Criteria criterion : getCriteria()) {
if (!criterion.apply(map)) {
return false;
}
}
return true;
}
}

10
json-path/src/main/java/com/jayway/jsonpath/JsonModel.java

@ -115,8 +115,8 @@ public class JsonModel {
// --------------------------------------------------------
@SuppressWarnings({"unchecked"})
public <T> T get(String jsonPath) {
return (T) get(JsonPath.compile(jsonPath));
public <T> T get(String jsonPath, Filter... filters) {
return (T) get(JsonPath.compile(jsonPath, filters));
}
@SuppressWarnings({"unchecked"})
@ -180,11 +180,11 @@ public class JsonModel {
//
// --------------------------------------------------------
public JsonModel getSubModel(String jsonPath) {
return getSubModel(JsonPath.compile(jsonPath));
public JsonModel getModel(String jsonPath) {
return getModel(JsonPath.compile(jsonPath));
}
public JsonModel getSubModel(JsonPath jsonPath) {
public JsonModel getModel(JsonPath jsonPath) {
notNull(jsonPath, "jsonPath can not be null");
Object subModel = jsonPath.read(jsonObject);

58
json-path/src/main/java/com/jayway/jsonpath/JsonPath.java

@ -17,20 +17,20 @@ package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.PathToken;
import com.jayway.jsonpath.internal.PathTokenizer;
import com.jayway.jsonpath.internal.filter.Filter;
import com.jayway.jsonpath.internal.filter.PathTokenFilter;
import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.spi.JsonProviderFactory;
import org.apache.commons.io.IOUtils;
import java.io.*;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import static org.apache.commons.lang.Validate.isTrue;
import static org.apache.commons.lang.Validate.notEmpty;
import static org.apache.commons.lang.Validate.notNull;
import static java.util.Arrays.asList;
import static org.apache.commons.lang.Validate.*;
/**
* <p/>
@ -97,9 +97,17 @@ public class JsonPath {
private static Pattern DEFINITE_PATH_PATTERN = Pattern.compile(".*(\\.\\.|\\*|\\[[\\\\/]|\\?|,|:\\s?\\]|\\[\\s?:|>|\\(|<|=|\\+).*");
private static final Filter[] NO_FILTERS = new Filter[0];
private PathTokenizer tokenizer;
private LinkedList<Filter> filters;
private JsonPath(String jsonPath) {
this(jsonPath, new Filter[0]);
}
public JsonPath(String jsonPath, Filter[] filters) {
if (jsonPath == null ||
jsonPath.trim().isEmpty() ||
jsonPath.matches("[^\\?\\+\\=\\-\\*\\/\\!]\\(")) {
@ -107,12 +115,18 @@ public class JsonPath {
throw new InvalidPathException("Invalid path");
}
this.tokenizer = new PathTokenizer(jsonPath);
this.filters = new LinkedList<Filter>();
this.filters.addAll(asList(filters));
}
PathTokenizer getTokenizer(){
return this.tokenizer;
}
/**
* Returns the string representation of this JsonPath
*
@ -163,7 +177,7 @@ public class JsonPath {
if (!(jsonObject instanceof Map) && !(jsonObject instanceof List)) {
throw new IllegalArgumentException("Invalid container object");
}
LinkedList<Filter> contextFilters = new LinkedList<Filter>(filters);
JsonProvider jsonProvider = JsonProviderFactory.getInstance();
Object result = jsonObject;
@ -171,8 +185,8 @@ public class JsonPath {
boolean inArrayContext = false;
for (PathToken pathToken : tokenizer) {
Filter filter = pathToken.getFilter();
result = filter.filter(result, jsonProvider, inArrayContext);
PathTokenFilter filter = pathToken.getFilter();
result = filter.filter(result, jsonProvider, contextFilters, inArrayContext);
if (!inArrayContext) {
inArrayContext = filter.isArrayFilter();
@ -267,12 +281,13 @@ public class JsonPath {
* Compiles a JsonPath
*
* @param jsonPath to compile
* @param filters filters to be applied to the filter place holders [?] in the path
* @return compiled JsonPath
*/
public static JsonPath compile(String jsonPath) {
public static JsonPath compile(String jsonPath, Filter... filters) {
notEmpty(jsonPath, "json can not be null or empty");
return new JsonPath(jsonPath);
return new JsonPath(jsonPath, filters);
}
@ -287,15 +302,16 @@ public class JsonPath {
*
* @param json a json string
* @param jsonPath the json path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param <T> expected return type
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
public static <T> T read(String json, String jsonPath) {
public static <T> T read(String json, String jsonPath, Filter... filters) {
notEmpty(json, "json can not be null or empty");
notEmpty(jsonPath, "jsonPath can not be null or empty");
return (T) compile(jsonPath).read(json);
return (T) compile(jsonPath, filters).read(json);
}
/**
@ -303,15 +319,16 @@ public class JsonPath {
*
* @param json a json object
* @param jsonPath the json path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param <T> expected return type
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
public static <T> T read(Object json, String jsonPath) {
public static <T> T read(Object json, String jsonPath, Filter... filters) {
notNull(json, "json can not be null");
notNull(jsonPath, "jsonPath can not be null");
return (T) compile(jsonPath).read(json);
return (T) compile(jsonPath, filters).read(json);
}
/**
@ -319,15 +336,16 @@ public class JsonPath {
*
* @param jsonURL url pointing to json doc
* @param jsonPath the json path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param <T> expected return type
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
public static <T> T read(URL jsonURL, String jsonPath) throws IOException {
public static <T> T read(URL jsonURL, String jsonPath, Filter... filters) throws IOException {
notNull(jsonURL, "json URL can not be null");
notEmpty(jsonPath, "jsonPath can not be null or empty");
return (T) compile(jsonPath).read(jsonURL);
return (T) compile(jsonPath, filters).read(jsonURL);
}
/**
@ -335,15 +353,16 @@ public class JsonPath {
*
* @param jsonFile json file
* @param jsonPath the json path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param <T> expected return type
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
public static <T> T read(File jsonFile, String jsonPath) throws IOException {
public static <T> T read(File jsonFile, String jsonPath, Filter... filters) throws IOException {
notNull(jsonFile, "json file can not be null");
notEmpty(jsonPath, "jsonPath can not be null or empty");
return (T) compile(jsonPath).read(jsonFile);
return (T) compile(jsonPath, filters).read(jsonFile);
}
/**
@ -351,15 +370,16 @@ public class JsonPath {
*
* @param jsonInputStream json input stream
* @param jsonPath the json path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param <T> expected return type
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
public static <T> T read(InputStream jsonInputStream, String jsonPath) throws IOException {
public static <T> T read(InputStream jsonInputStream, String jsonPath, Filter... filters) throws IOException {
notNull(jsonInputStream, "json input stream can not be null");
notEmpty(jsonPath, "jsonPath can not be null or empty");
return (T) compile(jsonPath).read(jsonInputStream);
return (T) compile(jsonPath, filters).read(jsonInputStream);
}
}

4
json-path/src/main/java/com/jayway/jsonpath/internal/PathToken.java

@ -14,8 +14,8 @@
*/
package com.jayway.jsonpath.internal;
import com.jayway.jsonpath.internal.filter.Filter;
import com.jayway.jsonpath.internal.filter.FilterFactory;
import com.jayway.jsonpath.internal.filter.PathTokenFilter;
import com.jayway.jsonpath.spi.JsonProvider;
/**
@ -29,7 +29,7 @@ public class PathToken {
this.fragment = fragment;
}
public Filter getFilter(){
public PathTokenFilter getFilter(){
return FilterFactory.createFilter(fragment);
}

1
json-path/src/main/java/com/jayway/jsonpath/internal/PathTokenizer.java

@ -16,7 +16,6 @@ package com.jayway.jsonpath.internal;
import com.jayway.jsonpath.InvalidPathException;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

2
json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayEvalFilter.java

@ -26,7 +26,7 @@ import java.util.regex.Pattern;
/**
* @author Kalle Stenflo
*/
public class ArrayEvalFilter extends Filter {
public class ArrayEvalFilter extends PathTokenFilter {
private static final Pattern PATTERN = Pattern.compile("(.*?)\\s?([=<>]+)\\s?(.*)");

2
json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayIndexFilter.java

@ -22,7 +22,7 @@ import java.util.regex.Pattern;
/**
* @author Kalle Stenflo
*/
public class ArrayIndexFilter extends Filter {
public class ArrayIndexFilter extends PathTokenFilter {
private static final Pattern SINGLE_ARRAY_INDEX_PATTERN = Pattern.compile("\\[\\d+\\]");

44
json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayQueryFilter.java

@ -0,0 +1,44 @@
package com.jayway.jsonpath.internal.filter;
import com.jayway.jsonpath.Filter;
import com.jayway.jsonpath.spi.JsonProvider;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 3/5/12
* Time: 4:41 PM
*/
public class ArrayQueryFilter extends PathTokenFilter {
ArrayQueryFilter(String condition) {
super(condition);
}
@Override
public Object filter(Object obj, JsonProvider jsonProvider, LinkedList<Filter> filters, boolean inArrayContext) {
Filter filter = filters.poll();
return filter.doFilter((List<Map<String, Object>>) obj);
}
@Override
public Object filter(Object obj, JsonProvider jsonProvider) {
throw new UnsupportedOperationException();
}
@Override
public Object getRef(Object obj, JsonProvider jsonProvider) {
throw new UnsupportedOperationException("");
}
@Override
public boolean isArrayFilter() {
return true;
}
}

6
json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java

@ -14,23 +14,25 @@
*/
package com.jayway.jsonpath.internal.filter;
import com.jayway.jsonpath.Filter;
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.spi.JsonProvider;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* @author Kalle Stenflo
*/
public class FieldFilter extends Filter {
public class FieldFilter extends PathTokenFilter {
public FieldFilter(String condition) {
super(condition);
}
@Override
public Object filter(Object obj, JsonProvider jsonProvider, boolean inArrayContext) {
public Object filter(Object obj, JsonProvider jsonProvider, LinkedList<Filter> filters, boolean inArrayContext) {
if (jsonProvider.isList(obj)) {
if (!inArrayContext) {
return null;

14
json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterFactory.java

@ -19,12 +19,12 @@ package com.jayway.jsonpath.internal.filter;
*/
public class FilterFactory {
private final static Filter DOCUMENT_FILTER = new PassthroughFilter("$", false);
private final static Filter ALL_ARRAY_ITEMS_FILTER = new PassthroughFilter("[*]", true);
private final static Filter WILDCARD_FILTER = new WildcardFilter("*");
private final static Filter SCAN_FILTER = new ScanFilter("..");
private final static PathTokenFilter DOCUMENT_FILTER = new PassthroughFilter("$", false);
private final static PathTokenFilter ALL_ARRAY_ITEMS_FILTER = new PassthroughFilter("[*]", true);
private final static PathTokenFilter WILDCARD_FILTER = new WildcardFilter("*");
private final static PathTokenFilter SCAN_FILTER = new ScanFilter("..");
public static Filter createFilter(String pathFragment) {
public static PathTokenFilter createFilter(String pathFragment) {
if ("$".equals(pathFragment)) {
@ -46,6 +46,10 @@ public class FilterFactory {
return new FieldFilter(pathFragment);
} else if (pathFragment.equals("[?]")) {
return new ArrayQueryFilter(pathFragment);
} else if (pathFragment.contains("[")) {
if (pathFragment.startsWith("[?")) {

2
json-path/src/main/java/com/jayway/jsonpath/internal/filter/HasFieldFilter.java

@ -22,7 +22,7 @@ import java.util.Map;
/**
* @author Kalle Stenflo
*/
public class HasFieldFilter extends Filter {
public class HasFieldFilter extends PathTokenFilter {
public HasFieldFilter(String condition) {
super(condition);

2
json-path/src/main/java/com/jayway/jsonpath/internal/filter/PassthroughFilter.java

@ -19,7 +19,7 @@ import com.jayway.jsonpath.spi.JsonProvider;
/**
* @author Kalle Stenflo
*/
public class PassthroughFilter extends Filter {
public class PassthroughFilter extends PathTokenFilter {
private boolean isArrayFilter;

9
json-path/src/main/java/com/jayway/jsonpath/internal/filter/Filter.java → json-path/src/main/java/com/jayway/jsonpath/internal/filter/PathTokenFilter.java

@ -14,16 +14,19 @@
*/
package com.jayway.jsonpath.internal.filter;
import com.jayway.jsonpath.Filter;
import com.jayway.jsonpath.spi.JsonProvider;
import java.util.LinkedList;
/**
* @author Kalle Stenflo
*/
public abstract class Filter {
public abstract class PathTokenFilter {
final String condition;
Filter(String condition) {
PathTokenFilter(String condition) {
this.condition = condition;
}
@ -40,7 +43,7 @@ public abstract class Filter {
return res;
}
public Object filter(Object obj, JsonProvider jsonProvider, boolean inArrayContext){
public Object filter(Object obj, JsonProvider jsonProvider, LinkedList<Filter> filters, boolean inArrayContext){
return filter(obj, jsonProvider);
}

2
json-path/src/main/java/com/jayway/jsonpath/internal/filter/ScanFilter.java

@ -22,7 +22,7 @@ import java.util.List;
/**
* @author Kalle Stenflo
*/
public class ScanFilter extends Filter {
public class ScanFilter extends PathTokenFilter {
public ScanFilter(String condition) {
super(condition);

2
json-path/src/main/java/com/jayway/jsonpath/internal/filter/WildcardFilter.java

@ -21,7 +21,7 @@ import java.util.List;
/**
* @author Kalle Stenflo
*/
public class WildcardFilter extends Filter {
public class WildcardFilter extends PathTokenFilter {
public WildcardFilter(String condition) {
super(condition);

1
json-path/src/main/java/com/jayway/jsonpath/spi/JsonProvider.java

@ -18,7 +18,6 @@ import com.jayway.jsonpath.InvalidJsonException;
import java.io.InputStream;
import java.io.Reader;
import java.net.URL;
import java.util.List;
import java.util.Map;

6
json-path/src/main/java/com/jayway/jsonpath/spi/MappingProvider.java

@ -1,11 +1,5 @@
package com.jayway.jsonpath.spi;
import org.codehaus.jackson.map.type.CollectionType;
import org.codehaus.jackson.map.type.TypeFactory;
import org.codehaus.jackson.map.type.TypeModifier;
import org.codehaus.jackson.map.type.TypeParser;
import org.codehaus.jackson.type.JavaType;
import java.util.Collection;
/**

1
json-path/src/main/java/com/jayway/jsonpath/spi/impl/JacksonProvider.java

@ -15,7 +15,6 @@
package com.jayway.jsonpath.spi.impl;
import com.jayway.jsonpath.InvalidJsonException;
import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.spi.MappingProvider;
import com.jayway.jsonpath.spi.Mode;
import org.codehaus.jackson.map.ObjectMapper;

1
json-path/src/test/java/com/jayway/jsonpath/ComplianceTest.java

@ -7,7 +7,6 @@ import java.util.List;
import java.util.Map;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.hasItems;
import static org.junit.Assert.assertThat;
/**

2
json-path/src/test/java/com/jayway/jsonpath/JsonModelOpsTest.java

@ -79,7 +79,7 @@ public class JsonModelOpsTest {
model.opsForArray("store.book").add(newBook);
JsonModel subModel = model.getSubModel("store.book[4]");
JsonModel subModel = model.getModel("store.book[4]");
assertEquals("reference", subModel.get("category"));
assertEquals("Kalle", subModel.get("author"));

8
json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java

@ -4,7 +4,9 @@ import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.net.URL;
import java.util.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import static java.util.Arrays.asList;
import static junit.framework.Assert.assertEquals;
@ -67,8 +69,8 @@ public class JsonModelTest {
@Test
public void test_a_sub_model_can_be_fetched_and_read() throws Exception {
JsonModel model = JsonModel.create(DOCUMENT);
assertEquals("Nigel Rees", model.getSubModel("$store.book[0]").get("author"));
assertEquals("Nigel Rees", model.getSubModel(JsonPath.compile("$store.book[0]")).get("author"));
assertEquals("Nigel Rees", model.getModel("$store.book[0]").get("author"));
assertEquals("Nigel Rees", model.getModel(JsonPath.compile("$store.book[0]")).get("author"));
}
@Test

117
json-path/src/test/java/com/jayway/jsonpath/JsonPathFilterTest.java

@ -0,0 +1,117 @@
package com.jayway.jsonpath;
import org.junit.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import static com.jayway.jsonpath.Criteria.where;
import static com.jayway.jsonpath.Filter.filter;
import static java.util.Arrays.asList;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 3/5/12
* Time: 4:24 PM
*/
public class JsonPathFilterTest {
public final static String DOCUMENT =
"{ \"store\": {\n" +
" \"book\": [ \n" +
" { \"category\": \"reference\",\n" +
" \"author\": \"Nigel Rees\",\n" +
" \"title\": \"Sayings of the Century\",\n" +
" \"price\": 8.95\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"Evelyn Waugh\",\n" +
" \"title\": \"Sword of Honour\",\n" +
" \"price\": 12.99\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"Herman Melville\",\n" +
" \"title\": \"Moby Dick\",\n" +
" \"isbn\": \"0-553-21311-3\",\n" +
" \"price\": 8.99\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"J. R. R. Tolkien\",\n" +
" \"title\": \"The Lord of the Rings\",\n" +
" \"isbn\": \"0-395-19395-8\",\n" +
" \"price\": 22.99\n" +
" }\n" +
" ],\n" +
" \"bicycle\": {\n" +
" \"color\": \"red\",\n" +
" \"price\": 19.95,\n" +
" \"foo:bar\": \"fooBar\",\n" +
" \"dot.notation\": \"new\"\n" +
" }\n" +
" }\n" +
"}";
@Test
public void a_path_can_use_filters() throws Exception {
Filter lowPricedBooksFilter = filter(where("price").lt(10));
List read = JsonPath.read(DOCUMENT, "store.book[?]", lowPricedBooksFilter);
System.out.println(read.size());
}
@Test
public void a_path_can_use_many_filters() throws Exception {
Map<String, Object> rootGrandChild_A = new HashMap<String, Object>();
rootGrandChild_A.put("name", "rootGrandChild_A");
Map<String, Object> rootGrandChild_B = new HashMap<String, Object>();
rootGrandChild_B.put("name", "rootGrandChild_B");
Map<String, Object> rootGrandChild_C = new HashMap<String, Object>();
rootGrandChild_C.put("name", "rootGrandChild_C");
Map<String, Object> rootChild_A = new HashMap<String, Object>();
rootChild_A.put("name", "rootChild_A");
rootChild_A.put("children", asList(rootGrandChild_A, rootGrandChild_B, rootGrandChild_C));
Map<String, Object> rootChild_B = new HashMap<String, Object>();
rootChild_B.put("name", "rootChild_B");
rootChild_B.put("children", asList(rootGrandChild_A, rootGrandChild_B, rootGrandChild_C));
Map<String, Object> rootChild_C = new HashMap<String, Object>();
rootChild_C.put("name", "rootChild_C");
rootChild_C.put("children", asList(rootGrandChild_A, rootGrandChild_B, rootGrandChild_C));
Map<String, Object> root = new HashMap<String, Object>();
root.put("children", asList(rootChild_A, rootChild_B, rootChild_C));
Filter customFilter = new Filter() {
@Override
public boolean apply(Map<String, Object> map) {
if(map.get("name").equals("rootGrandChild_A")){
return true;
}
return false;
}
};
Filter rootChildFilter = filter(where("name").regex(Pattern.compile("rootChild_[A|B]")));
Filter rootGrandChildFilter = filter(where("name").regex(Pattern.compile("rootGrandChild_[A|B]")));
List read = JsonPath.read(root, "children[?].children[?][?]", rootChildFilter, rootGrandChildFilter, customFilter);
System.out.println(read.size());
}
}

1
json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java

@ -9,7 +9,6 @@ import java.util.Map;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItems;
import static org.junit.Assert.*;
import static org.junit.Assert.assertThat;
/**
* Created by IntelliJ IDEA.

5
json-path/src/test/java/com/jayway/jsonpath/PathTest.java

@ -5,9 +5,7 @@ import org.hamcrest.Matcher;
import org.junit.Test;
import static org.hamcrest.Matchers.hasItems;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
/**
* Created by IntelliJ IDEA.
@ -21,6 +19,7 @@ public class PathTest {
@Test
public void path_is_not_definite() throws Exception {
assertFalse(JsonPath.compile("$..book[0]").isPathDefinite());
assertFalse(JsonPath.compile("$book[?]").isPathDefinite());
assertFalse(JsonPath.compile("$.books[*]").isPathDefinite());
}

Loading…
Cancel
Save