diff --git a/README b/README index 6d13e082..7870c033 100644 --- a/README +++ b/README @@ -1 +1 @@ -Java DSL for testing and reading JSON documents \ No newline at end of file +Java DSL for reading and testing JSON documents. \ No newline at end of file diff --git a/examples/pom.xml b/examples/pom.xml deleted file mode 100644 index a7dfc861..00000000 --- a/examples/pom.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - 4.0.0 - - json-path-parent - com.jayway.jsonpath - 1.0-SNAPSHOT - - com.jayway.jsonassert - examples - 1.0-SNAPSHOT - examples - http://maven.apache.org - - - diff --git a/json-path-assert/pom.xml b/json-path-assert/pom.xml index bae633d3..b4acf59d 100644 --- a/json-path-assert/pom.xml +++ b/json-path-assert/pom.xml @@ -1,4 +1,19 @@ + diff --git a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/CollectionMatcher.java b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/CollectionMatcher.java index cfb30549..364aa885 100644 --- a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/CollectionMatcher.java +++ b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/CollectionMatcher.java @@ -1,3 +1,32 @@ +/* +BSD License + +Copyright (c) 2000-2006, www.hamcrest.org +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list of +conditions and the following disclaimer. Redistributions in binary form must reproduce +the above copyright notice, this list of conditions and the following disclaimer in +the documentation and/or other materials provided with the distribution. + +Neither the name of Hamcrest nor the names of its contributors may be used to endorse +or promote products derived from this software without specific prior written +permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT +SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY +WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH +DAMAGE. +*/ package com.jayway.jsonassert.impl.matcher; import org.hamcrest.BaseMatcher; diff --git a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsCollectionWithSize.java b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsCollectionWithSize.java index 23c10cd5..cef7e0b0 100644 --- a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsCollectionWithSize.java +++ b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsCollectionWithSize.java @@ -1,3 +1,32 @@ +/* +BSD License + +Copyright (c) 2000-2006, www.hamcrest.org +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list of +conditions and the following disclaimer. Redistributions in binary form must reproduce +the above copyright notice, this list of conditions and the following disclaimer in +the documentation and/or other materials provided with the distribution. + +Neither the name of Hamcrest nor the names of its contributors may be used to endorse +or promote products derived from this software without specific prior written +permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT +SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY +WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH +DAMAGE. +*/ package com.jayway.jsonassert.impl.matcher; import static org.hamcrest.core.IsEqual.equalTo; diff --git a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsEmptyCollection.java b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsEmptyCollection.java index 0ee7edc6..23b12d07 100644 --- a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsEmptyCollection.java +++ b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsEmptyCollection.java @@ -1,3 +1,32 @@ +/* +BSD License + +Copyright (c) 2000-2006, www.hamcrest.org +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list of +conditions and the following disclaimer. Redistributions in binary form must reproduce +the above copyright notice, this list of conditions and the following disclaimer in +the documentation and/or other materials provided with the distribution. + +Neither the name of Hamcrest nor the names of its contributors may be used to endorse +or promote products derived from this software without specific prior written +permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT +SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY +WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH +DAMAGE. +*/ package com.jayway.jsonassert.impl.matcher; import org.hamcrest.Description; diff --git a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingKey.java b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingKey.java index abe3bd25..58bfc239 100644 --- a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingKey.java +++ b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingKey.java @@ -1,3 +1,32 @@ +/* +BSD License + +Copyright (c) 2000-2006, www.hamcrest.org +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list of +conditions and the following disclaimer. Redistributions in binary form must reproduce +the above copyright notice, this list of conditions and the following disclaimer in +the documentation and/or other materials provided with the distribution. + +Neither the name of Hamcrest nor the names of its contributors may be used to endorse +or promote products derived from this software without specific prior written +permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT +SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY +WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH +DAMAGE. +*/ package com.jayway.jsonassert.impl.matcher; import java.util.Map; diff --git a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingValue.java b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingValue.java index 4fd99495..9a4b08a4 100644 --- a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingValue.java +++ b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingValue.java @@ -1,3 +1,32 @@ +/* +BSD License + +Copyright (c) 2000-2006, www.hamcrest.org +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list of +conditions and the following disclaimer. Redistributions in binary form must reproduce +the above copyright notice, this list of conditions and the following disclaimer in +the documentation and/or other materials provided with the distribution. + +Neither the name of Hamcrest nor the names of its contributors may be used to endorse +or promote products derived from this software without specific prior written +permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT +SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY +WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH +DAMAGE. +*/ package com.jayway.jsonassert.impl.matcher; import org.hamcrest.Description; diff --git a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/MapTypeSafeMatcher.java b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/MapTypeSafeMatcher.java index 83a6652b..c52a20ef 100644 --- a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/MapTypeSafeMatcher.java +++ b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/MapTypeSafeMatcher.java @@ -1,3 +1,32 @@ +/* +BSD License + +Copyright (c) 2000-2006, www.hamcrest.org +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list of +conditions and the following disclaimer. Redistributions in binary form must reproduce +the above copyright notice, this list of conditions and the following disclaimer in +the documentation and/or other materials provided with the distribution. + +Neither the name of Hamcrest nor the names of its contributors may be used to endorse +or promote products derived from this software without specific prior written +permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT +SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY +WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH +DAMAGE. +*/ package com.jayway.jsonassert.impl.matcher; import org.hamcrest.BaseMatcher; diff --git a/json-path/pom.xml b/json-path/pom.xml index cff22f60..0cdf0300 100644 --- a/json-path/pom.xml +++ b/json-path/pom.xml @@ -1,3 +1,4 @@ + - 4.0.0 @@ -29,19 +29,15 @@ json-path http://maven.apache.org - com.googlecode.json-simple json-simple - - commons-lang commons-lang - - + 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 c5b6e201..002dd787 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java +++ b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java @@ -12,10 +12,66 @@ import java.util.logging.Logger; import static java.lang.String.format; /** - * Created by IntelliJ IDEA. - * User: kallestenflo + * User: kalle stenflo * Date: 2/2/11 * Time: 1:03 PM + *

+ * JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document. JsonPath is + * available in many programming languages such as Javascript, Python and PHP. + *

+ * JsonPath allows you to compile a json path string to use it many times or to compile and apply in one + * single on demand operation. + *

+ * Given the Json document: + *

+ * + * String json = + * "{ + * "store": + * { + * "book": + * [ + * { + * "category": "reference", + * "author": "Nigel Rees", + * "title": "Sayings of the Century", + * "price": 8.95 + * }, + * { + * "category": "fiction", + * "author": "Evelyn Waugh", + * "title": "Sword of Honour", + * "price": 12.99 + * } + * ], + * "bicycle": + * { + * "color": "red", + * "price": 19.95 + * } + * } + * }"; + * + *

+ * A JsonPath can be compiled and used as shown: + *

+ * + * JsonPath path = JsonPath.compile("$.store.book[1]"); + *
+ * List<Object> books = path.read(json); + *
+ *

+ * Or: + *

+ * + * List<Object> authors = JsonPath.read(json, "$.store.book[*].author") + * + *

+ * If the json path returns a single value (is definite): + *

+ * + * String author = JsonPath.read(json, "$.store.book[1].author") + * */ public class JsonPath { @@ -25,47 +81,93 @@ public class JsonPath { private JsonPathFilterChain filters; - public static JsonPath compile(String jsonPath) { - return new JsonPath(jsonPath); - } - /** * Creates a new JsonPath. * * @param jsonPath the path statement */ private JsonPath(String jsonPath) { - if(jsonPath == null || - jsonPath.trim().isEmpty() || - jsonPath.matches("new ") || - jsonPath.matches("[^\\?\\+\\=\\-\\*\\/\\!]\\(")){ + if (jsonPath == null || + jsonPath.trim().isEmpty() || + jsonPath.matches("new ") || + jsonPath.matches("[^\\?\\+\\=\\-\\*\\/\\!]\\(")) { - throw new InvalidPathException("Invalid path"); + throw new InvalidPathException("Invalid path"); } this.filters = new JsonPathFilterChain(PathUtil.splitPath(jsonPath)); } + /** + * Applies this json path to the provided object + * + * @param json a json Object + * @param + * @return list of objects matched by the given path + */ public List read(Object json) { return (List) filters.filter(json); } + /** + * Applies this json path to the provided object + * + * @param json a json string + * @param + * @return list of objects matched by the given path + */ public List read(String json) throws java.text.ParseException { return read(parse(json)); } + /** + * Compiles a JsonPath from the given string + * + * @param jsonPath to compile + * @return compiled JsonPath + */ + public static JsonPath compile(String jsonPath) { + return new JsonPath(jsonPath); + } + + /** + * Creates a new JsonPath and applies it to the provided Json string + * + * @param json a json string + * @param jsonPath the json path + * @param + * @return list of objects matched by the given path + */ public static List read(String json, String jsonPath) throws java.text.ParseException { return compile(jsonPath).read(json); } + /** + * Creates a new JsonPath and applies it to the provided Json object + * + * @param json a json object + * @param jsonPath the json path + * @param + * @return list of objects matched by the given path + */ public static List read(Object json, String jsonPath) { return compile(jsonPath).read(json); } + /** + * Creates a new JsonPath and applies it to the provided Json object. Note this method + * will throw an exception if the provided path returns more than one object. This method + * can be used with paths that are not definite but a warning will be generated. + * + * @param json a json object + * @param jsonPath the json path + * @param + * @return the object matched by the given path + */ public static T readOne(Object json, String jsonPath) { List result = compile(jsonPath).read(json, jsonPath); - if(log.isLoggable(Level.WARNING)){ - if(!PathUtil.isPathDefinite(jsonPath)){ + if (log.isLoggable(Level.WARNING)) { + if (!PathUtil.isPathDefinite(jsonPath)) { log.warning("Using readOne(...) on a not definite json path may give incorrect results."); } } @@ -73,12 +175,21 @@ public class JsonPath { if (result.size() != 1) { throw new RuntimeException(format("Expected one result when reading path: %s but was: ", jsonPath, result.size())); } - return (T) result.get(0); } + /** + * Creates a new JsonPath and applies it to the provided Json object. Note this method + * will throw an exception if the provided path returns more than one object. This method + * can be used with paths that are not definite but a warning will be generated. + * + * @param json a json string + * @param jsonPath the json path + * @param + * @return the object matched by the given path + */ public static T readOne(String json, String jsonPath) throws java.text.ParseException { - return (T)readOne(parse(json), jsonPath); + return (T) readOne(parse(json), jsonPath); } private static Object parse(String json) throws java.text.ParseException { diff --git a/json-path/src/main/java/com/jayway/jsonpath/JsonUtil.java b/json-path/src/main/java/com/jayway/jsonpath/JsonUtil.java new file mode 100644 index 00000000..c499b00c --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/JsonUtil.java @@ -0,0 +1,61 @@ +package com.jayway.jsonpath; + +import java.util.List; +import java.util.Map; + +/** + * User: kalle stenflo + * Date: 2/4/11 + * Time: 1:01 PM + */ +public class JsonUtil { + /** + * checks if object is instanceof java.util.List or java.util.Map + * + * @param obj object to check + * @return true if List or Map + */ + public static boolean isContainer(Object obj) { + return (isList(obj) || isMap(obj)); + } + + /** + * checks if object is instanceof java.util.List + * + * @param obj object to check + * @return true if List + */ + public static boolean isList(Object obj) { + return (obj instanceof List); + } + + /** + * checks if object is instanceof java.util.Map + * + * @param obj object to check + * @return true if Map + */ + public static boolean isMap(Object obj) { + return (obj instanceof Map); + } + + /** + * converts casts to java.util.List + * + * @param obj + * @return the list + */ + public static List toList(Object obj) { + return (List) obj; + } + + /** + * converts casts to java.util.Map + * + * @param obj + * @return the Map + */ + public static Map toMap(Object obj) { + return (Map) obj; + } +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/PathUtil.java b/json-path/src/main/java/com/jayway/jsonpath/PathUtil.java index af17c057..a03e6d82 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/PathUtil.java +++ b/json-path/src/main/java/com/jayway/jsonpath/PathUtil.java @@ -1,43 +1,48 @@ package com.jayway.jsonpath; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; - import java.util.LinkedList; import java.util.List; /** - * User: kallestenflo + * User: kalle stenflo * Date: 2/2/11 * Time: 2:08 PM */ public class PathUtil { + /** + * Checks if a path points to a single item or if it potentially returns multiple items + * + * a path is considered not definite if it contains a scan fragment ".." + * or an array position fragment that is not based on a single index + * + * + * absolute path examples: + * + * $store.book + * $store.book[1].value + * + * not absolute path examples + * + * $..book + * $.store.book[1,2] + * $.store.book[?(@.category = 'fiction')] + * + * @param jsonPath the path to check + * @return true if path is definite (points to single item) + */ public static boolean isPathDefinite(String jsonPath) { return !jsonPath.replaceAll("\"[^\"\\\\\\n\r]*\"", "").matches(".*(\\.\\.|\\*|\\[[\\\\/]|\\?|,|:|>|\\(|<|=|\\+).*"); } - public static boolean isContainer(Object obj) { - return (isArray(obj) || isDocument(obj)); - } - - public static boolean isArray(Object obj) { - return (obj instanceof JSONArray); - } - - public static boolean isDocument(Object obj) { - return (obj instanceof JSONObject); - } - - public static JSONArray toArray(Object array) { - return (JSONArray) array; - } - - public static JSONObject toDocument(Object document) { - return (JSONObject) document; - } - - + /** + * Splits a path into fragments + * + * the path $.store.book[1].category returns ["$", "store", "book", "[1]", "value"] + * + * @param jsonPath path to split + * @return fragments + */ public static List splitPath(String jsonPath) { LinkedList fragments = new LinkedList(); diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterBase.java b/json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterBase.java index 9d25456d..933eec28 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterBase.java +++ b/json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterBase.java @@ -9,11 +9,5 @@ import java.util.List; * Time: 2:01 PM */ public abstract class JsonPathFilterBase { - - - - public abstract List apply(List filter); - - } diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/ListFilter.java b/json-path/src/main/java/com/jayway/jsonpath/filter/ListFilter.java index 5c46078a..0ccbcf6b 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/ListFilter.java +++ b/json-path/src/main/java/com/jayway/jsonpath/filter/ListFilter.java @@ -1,14 +1,14 @@ package com.jayway.jsonpath.filter; -import com.jayway.jsonpath.PathUtil; +import com.jayway.jsonpath.JsonUtil; import org.json.simple.JSONArray; -import org.json.simple.JSONObject; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -63,7 +63,7 @@ public class ListFilter extends JsonPathFilterBase { List result = new JSONArray(); for (Object current : items) { - for (Object item : PathUtil.toArray(current)) { + for (Object item : JsonUtil.toList(current)) { if(isEvalMatch(item)){ result.add(item); } @@ -79,10 +79,10 @@ public class ListFilter extends JsonPathFilterBase { String prop = getFilterProperty(); for (Object current : items) { - for (Object item : PathUtil.toArray(current)) { + for (Object item : JsonUtil.toList(current)) { - if(PathUtil.isDocument(item)){ - if(PathUtil.toDocument(item).containsKey(prop)) { + if(JsonUtil.isMap(item)){ + if(JsonUtil.toMap(item).containsKey(prop)) { result.add(item); } } @@ -96,7 +96,7 @@ public class ListFilter extends JsonPathFilterBase { List result = new JSONArray(); for (Object current : items) { - result.addAll(PathUtil.toArray(current)); + result.addAll(JsonUtil.toList(current)); } return result; } @@ -106,7 +106,7 @@ public class ListFilter extends JsonPathFilterBase { for (Object current : items) { - List array = PathUtil.toArray(current); + List array = JsonUtil.toList(current); result.add(array.get(getTailIndex(array.size()))); } return result; @@ -119,7 +119,7 @@ public class ListFilter extends JsonPathFilterBase { Integer[] index = getArrayIndex(); for (int i : index) { - result.add(PathUtil.toArray(current).get(i)); + result.add(JsonUtil.toList(current).get(i)); } } return result; @@ -132,7 +132,7 @@ public class ListFilter extends JsonPathFilterBase { Integer[] index = getListPullIndex(); for (int i : index) { - result.add(PathUtil.toArray(current).get(i)); + result.add(JsonUtil.toList(current).get(i)); } } return result; @@ -146,10 +146,10 @@ public class ListFilter extends JsonPathFilterBase { String operator = matcher.group(2); String expected = matcher.group(3); - if(!PathUtil.isDocument(check)){ + if(!JsonUtil.isMap(check)){ return false; } - JSONObject obj = PathUtil.toDocument(check); + Map obj = JsonUtil.toMap(check); if(!obj.containsKey(property)){ return false; @@ -157,7 +157,7 @@ public class ListFilter extends JsonPathFilterBase { Object propertyValue = obj.get(property); - if(PathUtil.isContainer(propertyValue)){ + if(JsonUtil.isContainer(propertyValue)){ return false; } diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/PropertyFilter.java b/json-path/src/main/java/com/jayway/jsonpath/filter/PropertyFilter.java index 4bdba091..a91a42ee 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/PropertyFilter.java +++ b/json-path/src/main/java/com/jayway/jsonpath/filter/PropertyFilter.java @@ -1,6 +1,6 @@ package com.jayway.jsonpath.filter; -import com.jayway.jsonpath.PathUtil; +import com.jayway.jsonpath.JsonUtil; import org.json.simple.JSONArray; import java.util.List; @@ -28,15 +28,15 @@ public class PropertyFilter extends JsonPathFilterBase { if (WILDCARD.equals(pathFragment)) { for (Object current : filter) { - for (Object value : PathUtil.toDocument(current).values()) { + for (Object value : JsonUtil.toMap(current).values()) { result.add(value); } } } else { for (Object current : filter) { - if (PathUtil.toDocument(current).containsKey(pathFragment)) { - result.add(PathUtil.toDocument(current).get(pathFragment)); + if (JsonUtil.toMap(current).containsKey(pathFragment)) { + result.add(JsonUtil.toMap(current).get(pathFragment)); } } } diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/TraverseFilter.java b/json-path/src/main/java/com/jayway/jsonpath/filter/TraverseFilter.java index b4ddc0ee..07ca587d 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/TraverseFilter.java +++ b/json-path/src/main/java/com/jayway/jsonpath/filter/TraverseFilter.java @@ -1,6 +1,6 @@ package com.jayway.jsonpath.filter; -import com.jayway.jsonpath.PathUtil; +import com.jayway.jsonpath.JsonUtil; import org.json.simple.JSONArray; import java.util.List; @@ -24,18 +24,18 @@ public class TraverseFilter extends JsonPathFilterBase { private void traverse(Object container, List result) { - if (PathUtil.isDocument(container)) { + if (JsonUtil.isMap(container)) { result.add(container); - for (Object value : PathUtil.toDocument(container).values()) { - if (PathUtil.isContainer(value)) { + for (Object value : JsonUtil.toMap(container).values()) { + if (JsonUtil.isContainer(value)) { traverse(value, result); } } - } else if (PathUtil.isArray(container)) { + } else if (JsonUtil.isList(container)) { - for (Object value : PathUtil.toArray(container)) { - if (PathUtil.isContainer(value)) { + for (Object value : JsonUtil.toList(container)) { + if (JsonUtil.isContainer(value)) { traverse(value, result); } }