From 8c10101593b41fa32ff2ca757e0ac92a89626e51 Mon Sep 17 00:00:00 2001 From: Kalle Stenflo Date: Mon, 5 Mar 2012 19:18:04 +0100 Subject: [PATCH] Filter support extended --- .../main/java/com/jayway/jsonpath/Filter.java | 72 +++++++---- .../java/com/jayway/jsonpath/JsonModel.java | 10 +- .../java/com/jayway/jsonpath/JsonPath.java | 58 ++++++--- .../jayway/jsonpath/internal/PathToken.java | 4 +- .../jsonpath/internal/PathTokenizer.java | 1 - .../internal/filter/ArrayEvalFilter.java | 2 +- .../internal/filter/ArrayIndexFilter.java | 2 +- .../internal/filter/ArrayQueryFilter.java | 44 +++++++ .../jsonpath/internal/filter/FieldFilter.java | 6 +- .../internal/filter/FilterFactory.java | 14 ++- .../internal/filter/HasFieldFilter.java | 2 +- .../internal/filter/PassthroughFilter.java | 2 +- .../{Filter.java => PathTokenFilter.java} | 9 +- .../jsonpath/internal/filter/ScanFilter.java | 2 +- .../internal/filter/WildcardFilter.java | 2 +- .../com/jayway/jsonpath/spi/JsonProvider.java | 1 - .../jayway/jsonpath/spi/MappingProvider.java | 6 - .../jsonpath/spi/impl/JacksonProvider.java | 1 - .../com/jayway/jsonpath/ComplianceTest.java | 1 - .../com/jayway/jsonpath/JsonModelOpsTest.java | 2 +- .../com/jayway/jsonpath/JsonModelTest.java | 8 +- .../jayway/jsonpath/JsonPathFilterTest.java | 117 ++++++++++++++++++ .../com/jayway/jsonpath/JsonPathTest.java | 1 - .../java/com/jayway/jsonpath/PathTest.java | 5 +- 24 files changed, 288 insertions(+), 84 deletions(-) create mode 100644 json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayQueryFilter.java rename json-path/src/main/java/com/jayway/jsonpath/internal/filter/{Filter.java => PathTokenFilter.java} (87%) create mode 100644 json-path/src/test/java/com/jayway/jsonpath/JsonPathFilterTest.java diff --git a/json-path/src/main/java/com/jayway/jsonpath/Filter.java b/json-path/src/main/java/com/jayway/jsonpath/Filter.java index 030e288e..bbc883ee 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/Filter.java +++ b/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 criteria = new LinkedHashMap(); + public abstract boolean apply(Map map); - public Filter(Criteria criteria) { - addCriteria(criteria); + public List doFilter(List> filterItems) { + List result = new ArrayList(); + + for (Map 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 getCriteria() { - return new ArrayList(this.criteria.values()); - } + private HashMap criteria = new LinkedHashMap(); - public boolean apply(Map 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 getCriteria() { + return new ArrayList(this.criteria.values()); + } + + @Override + public boolean apply(Map map) { + + for (Criteria criterion : getCriteria()) { + if (!criterion.apply(map)) { + return false; + } + } + + return true; + + } } diff --git a/json-path/src/main/java/com/jayway/jsonpath/JsonModel.java b/json-path/src/main/java/com/jayway/jsonpath/JsonModel.java index c6a711f9..3de3d07c 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/JsonModel.java +++ b/json-path/src/main/java/com/jayway/jsonpath/JsonModel.java @@ -115,8 +115,8 @@ public class JsonModel { // -------------------------------------------------------- @SuppressWarnings({"unchecked"}) - public T get(String jsonPath) { - return (T) get(JsonPath.compile(jsonPath)); + public 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); diff --git a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java index f77eb472..0292b069 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java +++ b/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.*; /** *

@@ -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 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(); + 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 contextFilters = new LinkedList(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 expected return type * @return list of objects matched by the given path */ @SuppressWarnings({"unchecked"}) - public static T read(String json, String jsonPath) { + public static 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 expected return type * @return list of objects matched by the given path */ @SuppressWarnings({"unchecked"}) - public static T read(Object json, String jsonPath) { + public static 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 expected return type * @return list of objects matched by the given path */ @SuppressWarnings({"unchecked"}) - public static T read(URL jsonURL, String jsonPath) throws IOException { + public static 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 expected return type * @return list of objects matched by the given path */ @SuppressWarnings({"unchecked"}) - public static T read(File jsonFile, String jsonPath) throws IOException { + public static 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 expected return type * @return list of objects matched by the given path */ @SuppressWarnings({"unchecked"}) - public static T read(InputStream jsonInputStream, String jsonPath) throws IOException { + public static 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); } } diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/PathToken.java b/json-path/src/main/java/com/jayway/jsonpath/internal/PathToken.java index 2da88ba9..f337b5d7 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/PathToken.java +++ b/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); } diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/PathTokenizer.java b/json-path/src/main/java/com/jayway/jsonpath/internal/PathTokenizer.java index afaa4330..d08cfbff 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/PathTokenizer.java +++ b/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; diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayEvalFilter.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayEvalFilter.java index b4d1cad1..58272224 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayEvalFilter.java +++ b/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?(.*)"); diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayIndexFilter.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayIndexFilter.java index f152eb50..45266f4b 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayIndexFilter.java +++ b/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+\\]"); diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayQueryFilter.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayQueryFilter.java new file mode 100644 index 00000000..af772029 --- /dev/null +++ b/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 filters, boolean inArrayContext) { + + Filter filter = filters.poll(); + + return filter.doFilter((List>) 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; + } +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java index 83906eaa..9c1e0678 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java +++ b/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 filters, boolean inArrayContext) { if (jsonProvider.isList(obj)) { if (!inArrayContext) { return null; diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterFactory.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterFactory.java index 7ca88bc1..f507686c 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterFactory.java +++ b/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("[?")) { diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/HasFieldFilter.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/HasFieldFilter.java index 993e17f8..272f1ea4 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/HasFieldFilter.java +++ b/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); diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/PassthroughFilter.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/PassthroughFilter.java index c40cbdf4..7817145b 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/PassthroughFilter.java +++ b/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; diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/Filter.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/PathTokenFilter.java similarity index 87% rename from json-path/src/main/java/com/jayway/jsonpath/internal/filter/Filter.java rename to json-path/src/main/java/com/jayway/jsonpath/internal/filter/PathTokenFilter.java index 715e27e9..02d1ff48 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/Filter.java +++ b/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 filters, boolean inArrayContext){ return filter(obj, jsonProvider); } diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ScanFilter.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ScanFilter.java index 5b0688bc..7f6128ce 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ScanFilter.java +++ b/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); diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/WildcardFilter.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/WildcardFilter.java index e4cd4e83..f5acbaaf 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/WildcardFilter.java +++ b/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); diff --git a/json-path/src/main/java/com/jayway/jsonpath/spi/JsonProvider.java b/json-path/src/main/java/com/jayway/jsonpath/spi/JsonProvider.java index b936c9fa..2c933dfc 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/spi/JsonProvider.java +++ b/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; diff --git a/json-path/src/main/java/com/jayway/jsonpath/spi/MappingProvider.java b/json-path/src/main/java/com/jayway/jsonpath/spi/MappingProvider.java index 3db0c084..e5ab7f0c 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/spi/MappingProvider.java +++ b/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; /** diff --git a/json-path/src/main/java/com/jayway/jsonpath/spi/impl/JacksonProvider.java b/json-path/src/main/java/com/jayway/jsonpath/spi/impl/JacksonProvider.java index b238a65d..7eb934b6 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/spi/impl/JacksonProvider.java +++ b/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; diff --git a/json-path/src/test/java/com/jayway/jsonpath/ComplianceTest.java b/json-path/src/test/java/com/jayway/jsonpath/ComplianceTest.java index 53ec1ab0..ddea1dbe 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/ComplianceTest.java +++ b/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; /** diff --git a/json-path/src/test/java/com/jayway/jsonpath/JsonModelOpsTest.java b/json-path/src/test/java/com/jayway/jsonpath/JsonModelOpsTest.java index e4638f80..b9d81b16 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/JsonModelOpsTest.java +++ b/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")); diff --git a/json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java b/json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java index 94762412..c88cce7b 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java +++ b/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 diff --git a/json-path/src/test/java/com/jayway/jsonpath/JsonPathFilterTest.java b/json-path/src/test/java/com/jayway/jsonpath/JsonPathFilterTest.java new file mode 100644 index 00000000..aa67788b --- /dev/null +++ b/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 rootGrandChild_A = new HashMap(); + rootGrandChild_A.put("name", "rootGrandChild_A"); + + Map rootGrandChild_B = new HashMap(); + rootGrandChild_B.put("name", "rootGrandChild_B"); + + Map rootGrandChild_C = new HashMap(); + rootGrandChild_C.put("name", "rootGrandChild_C"); + + + Map rootChild_A = new HashMap(); + rootChild_A.put("name", "rootChild_A"); + rootChild_A.put("children", asList(rootGrandChild_A, rootGrandChild_B, rootGrandChild_C)); + + Map rootChild_B = new HashMap(); + rootChild_B.put("name", "rootChild_B"); + rootChild_B.put("children", asList(rootGrandChild_A, rootGrandChild_B, rootGrandChild_C)); + + Map rootChild_C = new HashMap(); + rootChild_C.put("name", "rootChild_C"); + rootChild_C.put("children", asList(rootGrandChild_A, rootGrandChild_B, rootGrandChild_C)); + + Map root = new HashMap(); + root.put("children", asList(rootChild_A, rootChild_B, rootChild_C)); + + + + Filter customFilter = new Filter() { + @Override + public boolean apply(Map 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()); + } + +} diff --git a/json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java b/json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java index cd29b706..5f6c6786 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java +++ b/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. diff --git a/json-path/src/test/java/com/jayway/jsonpath/PathTest.java b/json-path/src/test/java/com/jayway/jsonpath/PathTest.java index 76b9fe4e..9c653fb3 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/PathTest.java +++ b/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()); }