Browse Source

improved java doc

pull/1/merge
kalle 14 years ago
parent
commit
bd8f5e8ee8
  1. 2
      README
  2. 18
      examples/pom.xml
  3. 15
      json-path-assert/pom.xml
  4. 29
      json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/CollectionMatcher.java
  5. 29
      json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsCollectionWithSize.java
  6. 29
      json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsEmptyCollection.java
  7. 29
      json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingKey.java
  8. 29
      json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingValue.java
  9. 29
      json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/MapTypeSafeMatcher.java
  10. 8
      json-path/pom.xml
  11. 141
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  12. 61
      json-path/src/main/java/com/jayway/jsonpath/JsonUtil.java
  13. 55
      json-path/src/main/java/com/jayway/jsonpath/PathUtil.java
  14. 6
      json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterBase.java
  15. 26
      json-path/src/main/java/com/jayway/jsonpath/filter/ListFilter.java
  16. 8
      json-path/src/main/java/com/jayway/jsonpath/filter/PropertyFilter.java
  17. 14
      json-path/src/main/java/com/jayway/jsonpath/filter/TraverseFilter.java

2
README

@ -1 +1 @@
Java DSL for testing and reading JSON documents
Java DSL for reading and testing JSON documents.

18
examples/pom.xml

@ -1,18 +0,0 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>json-path-parent</artifactId>
<groupId>com.jayway.jsonpath</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.jayway.jsonassert</groupId>
<artifactId>examples</artifactId>
<version>1.0-SNAPSHOT</version>
<name>examples</name>
<url>http://maven.apache.org</url>
<dependencies>
</dependencies>
</project>

15
json-path-assert/pom.xml

@ -1,4 +1,19 @@
<?xml version="1.0"?>
<!--
~ Copyright 2010 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

29
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;

29
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;

29
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;

29
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;

29
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;

29
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;

8
json-path/pom.xml

@ -1,3 +1,4 @@
<?xml version="1.0"?>
<!--
~ Copyright 2010 the original author or authors.
~
@ -13,7 +14,6 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
@ -29,19 +29,15 @@
<name>json-path</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!-- ~~~ß~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!-- test dependencies -->
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<dependency>

141
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
* <p/>
* 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.
* <p/>
* 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.
* <p/>
* Given the Json document:
* <p/>
* <code>
* 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
* }
* }
* }";
* </code>
* <p/>
* A JsonPath can be compiled and used as shown:
* <p/>
* <code>
* JsonPath path = JsonPath.compile("$.store.book[1]");
* <br/>
* List&lt;Object&gt; books = path.read(json);
* </code>
* </p>
* Or:
* <p/>
* <code>
* List&lt;Object&gt; authors = JsonPath.read(json, "$.store.book[*].author")
* </code>
* <p/>
* If the json path returns a single value (is definite):
* </p>
* <code>
* String author = JsonPath.read(json, "$.store.book[1].author")
* </code>
*/
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 <T>
* @return list of objects matched by the given path
*/
public <T> List<T> read(Object json) {
return (List<T>) filters.filter(json);
}
/**
* Applies this json path to the provided object
*
* @param json a json string
* @param <T>
* @return list of objects matched by the given path
*/
public <T> List<T> 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 <T>
* @return list of objects matched by the given path
*/
public static <T> List<T> 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 <T>
* @return list of objects matched by the given path
*/
public static <T> List<T> 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 <T>
* @return the object matched by the given path
*/
public static <T> T readOne(Object json, String jsonPath) {
List<Object> 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 <T>
* @return the object matched by the given path
*/
public static <T> 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 {

61
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 <code>instanceof</code> <code>java.util.List</code> or <code>java.util.Map</code>
*
* @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 <code>instanceof</code> <code>java.util.List</code>
*
* @param obj object to check
* @return true if List
*/
public static boolean isList(Object obj) {
return (obj instanceof List);
}
/**
* checks if object is <code>instanceof</code> <code>java.util.Map</code>
*
* @param obj object to check
* @return true if Map
*/
public static boolean isMap(Object obj) {
return (obj instanceof Map);
}
/**
* converts casts to <code>java.util.List</code>
*
* @param obj
* @return the list
*/
public static List<Object> toList(Object obj) {
return (List) obj;
}
/**
* converts casts to <code>java.util.Map</code>
*
* @param obj
* @return the Map
*/
public static Map<Object, Object> toMap(Object obj) {
return (Map) obj;
}
}

55
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 <strong>not</strong> 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 <code>$.store.book[1].category</code> returns ["$", "store", "book", "[1]", "value"]
*
* @param jsonPath path to split
* @return fragments
*/
public static List<String> splitPath(String jsonPath) {
LinkedList<String> fragments = new LinkedList<String>();

6
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<Object> apply(List<Object> filter);
}

26
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<Object> 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<Object> 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;
}

8
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));
}
}
}

14
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<Object> 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);
}
}

Loading…
Cancel
Save