|
|
|
@ -16,6 +16,7 @@ package com.jayway.jsonpath;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.jayway.jsonpath.internal.IOUtils; |
|
|
|
|
import com.jayway.jsonpath.internal.Log; |
|
|
|
|
import com.jayway.jsonpath.internal.PathToken; |
|
|
|
|
import com.jayway.jsonpath.internal.PathTokenizer; |
|
|
|
|
import com.jayway.jsonpath.internal.filter.PathTokenFilter; |
|
|
|
@ -98,37 +99,44 @@ import static org.apache.commons.lang3.Validate.*;
|
|
|
|
|
*/ |
|
|
|
|
public class JsonPath { |
|
|
|
|
|
|
|
|
|
private static Pattern DEFINITE_PATH_PATTERN = Pattern.compile(".*(\\.\\.|\\*|\\[[\\\\/]|\\?|,|:\\s?\\]|\\[\\s?:|>|\\(|<|=|\\+).*"); |
|
|
|
|
private static Pattern DEFINITE_PATH_PATTERN = Pattern.compile(".*(\\.\\.|\\*|\\[[\\\\/]|\\?|,|:\\s?]|\\[\\s?:|>|\\(|<|=|\\+).*"); |
|
|
|
|
private static Pattern INVALID_PATH_PATTERN = Pattern.compile("[^\\?\\+=\\-\\*/!]\\("); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private PathTokenizer tokenizer; |
|
|
|
|
private LinkedList<Filter> filters; |
|
|
|
|
|
|
|
|
|
public JsonPath(String jsonPath, Filter[] filters) { |
|
|
|
|
if (jsonPath == null || |
|
|
|
|
jsonPath.trim().length() == 0 || |
|
|
|
|
jsonPath.matches("[^\\?\\+\\=\\-\\*\\/\\!]\\(")) { |
|
|
|
|
private JsonPath(String jsonPath, Filter[] filters) { |
|
|
|
|
|
|
|
|
|
notNull(jsonPath, "path can not be null"); |
|
|
|
|
jsonPath = jsonPath.trim(); |
|
|
|
|
notEmpty(jsonPath, "path can not be empty"); |
|
|
|
|
|
|
|
|
|
if (INVALID_PATH_PATTERN.matcher(jsonPath).matches()) { |
|
|
|
|
throw new InvalidPathException("Invalid path"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int filterCountInPath = StringUtils.countMatches(jsonPath, "[?]"); |
|
|
|
|
isTrue(filterCountInPath == filters.length, "Filters in path ([?]) does not match provided filters."); |
|
|
|
|
|
|
|
|
|
this.tokenizer = new PathTokenizer(jsonPath); |
|
|
|
|
|
|
|
|
|
//System.out.println(tokenizer.toString());
|
|
|
|
|
if(Log.isDebugEnabled()){ |
|
|
|
|
Log.debug("New JsonPath:\n{}", this.tokenizer.toString()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.filters = new LinkedList<Filter>(); |
|
|
|
|
this.filters.addAll(asList(filters)); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
PathTokenizer getTokenizer(){ |
|
|
|
|
PathTokenizer getTokenizer() { |
|
|
|
|
return this.tokenizer; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public JsonPath copy(){ |
|
|
|
|
public JsonPath copy() { |
|
|
|
|
return new JsonPath(tokenizer.getPath(), filters.toArray(new Filter[0])); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -199,7 +207,7 @@ public class JsonPath {
|
|
|
|
|
* the {@link JsonProvider} |
|
|
|
|
* |
|
|
|
|
* @param jsonObject a container Object |
|
|
|
|
* @param <T> expected return type |
|
|
|
|
* @param <T> expected return type |
|
|
|
|
* @return list of objects matched by the given path |
|
|
|
|
*/ |
|
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
@ -213,8 +221,8 @@ public class JsonPath {
|
|
|
|
|
* the {@link JsonProvider} |
|
|
|
|
* |
|
|
|
|
* @param jsonProvider JsonProvider to use |
|
|
|
|
* @param jsonObject a container Object |
|
|
|
|
* @param <T> expected return type |
|
|
|
|
* @param jsonObject a container Object |
|
|
|
|
* @param <T> expected return type |
|
|
|
|
* @return list of objects matched by the given path |
|
|
|
|
*/ |
|
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
@ -222,9 +230,9 @@ public class JsonPath {
|
|
|
|
|
notNull(jsonProvider, "jsonProvider can not be null"); |
|
|
|
|
notNull(jsonObject, "json can not be null"); |
|
|
|
|
|
|
|
|
|
if(this.getPath().equals("$")){ |
|
|
|
|
if (this.getPath().equals("$")) { |
|
|
|
|
//This path only references the whole object. No need to do any work here...
|
|
|
|
|
return (T)jsonObject; |
|
|
|
|
return (T) jsonObject; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!jsonProvider.isContainer(jsonObject)) { |
|
|
|
@ -265,8 +273,8 @@ public class JsonPath {
|
|
|
|
|
* Applies this JsonPath to the provided json string |
|
|
|
|
* |
|
|
|
|
* @param jsonProvider JsonProvider to use |
|
|
|
|
* @param json a json string |
|
|
|
|
* @param <T> expected return type |
|
|
|
|
* @param json a json string |
|
|
|
|
* @param <T> expected return type |
|
|
|
|
* @return list of objects matched by the given path |
|
|
|
|
*/ |
|
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
@ -294,8 +302,8 @@ public class JsonPath {
|
|
|
|
|
* Applies this JsonPath to the provided json URL |
|
|
|
|
* |
|
|
|
|
* @param jsonProvider JsonProvider to use |
|
|
|
|
* @param jsonURL url to read from |
|
|
|
|
* @param <T> expected return type |
|
|
|
|
* @param jsonURL url to read from |
|
|
|
|
* @param <T> expected return type |
|
|
|
|
* @return list of objects matched by the given path |
|
|
|
|
* @throws IOException |
|
|
|
|
*/ |
|
|
|
@ -331,8 +339,8 @@ public class JsonPath {
|
|
|
|
|
* Applies this JsonPath to the provided json file |
|
|
|
|
* |
|
|
|
|
* @param jsonProvider JsonProvider to use |
|
|
|
|
* @param jsonFile file to read from |
|
|
|
|
* @param <T> expected return type |
|
|
|
|
* @param jsonFile file to read from |
|
|
|
|
* @param <T> expected return type |
|
|
|
|
* @return list of objects matched by the given path |
|
|
|
|
* @throws IOException |
|
|
|
|
*/ |
|
|
|
@ -373,7 +381,7 @@ public class JsonPath {
|
|
|
|
|
/** |
|
|
|
|
* Applies this JsonPath to the provided json input stream |
|
|
|
|
* |
|
|
|
|
* @param jsonProvider JsonProvider to use |
|
|
|
|
* @param jsonProvider JsonProvider to use |
|
|
|
|
* @param jsonInputStream input stream to read from |
|
|
|
|
* @param <T> expected return type |
|
|
|
|
* @return list of objects matched by the given path |
|
|
|
@ -401,7 +409,7 @@ public class JsonPath {
|
|
|
|
|
* Compiles a JsonPath |
|
|
|
|
* |
|
|
|
|
* @param jsonPath to compile |
|
|
|
|
* @param filters filters to be applied to the filter place holders [?] in the path |
|
|
|
|
* @param filters filters to be applied to the filter place holders [?] in the path |
|
|
|
|
* @return compiled JsonPath |
|
|
|
|
*/ |
|
|
|
|
public static JsonPath compile(String jsonPath, Filter... filters) { |
|
|
|
@ -422,7 +430,7 @@ 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 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 |
|
|
|
|
*/ |
|
|
|
@ -436,10 +444,10 @@ public class JsonPath {
|
|
|
|
|
* Creates a new JsonPath and applies it to the provided Json string |
|
|
|
|
* |
|
|
|
|
* @param jsonProvider JsonProvider to use |
|
|
|
|
* @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 |
|
|
|
|
* @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"}) |
|
|
|
@ -456,7 +464,7 @@ 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 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 |
|
|
|
|
*/ |
|
|
|
@ -464,14 +472,15 @@ public class JsonPath {
|
|
|
|
|
public static <T> T read(Object json, String jsonPath, Filter... filters) { |
|
|
|
|
return read(JsonProviderFactory.createProvider(), json, jsonPath, filters); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Creates a new JsonPath and applies it to the provided Json object |
|
|
|
|
* |
|
|
|
|
* @param jsonProvider JsonProvider to use |
|
|
|
|
* @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 |
|
|
|
|
* @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"}) |
|
|
|
@ -489,7 +498,7 @@ 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 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 |
|
|
|
|
*/ |
|
|
|
@ -502,10 +511,10 @@ public class JsonPath {
|
|
|
|
|
* Creates a new JsonPath and applies it to the provided Json object |
|
|
|
|
* |
|
|
|
|
* @param jsonProvider JsonProvider to use |
|
|
|
|
* @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 |
|
|
|
|
* @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"}) |
|
|
|
@ -520,9 +529,9 @@ public class JsonPath {
|
|
|
|
|
/** |
|
|
|
|
* Creates a new JsonPath and applies it to the provided Json object |
|
|
|
|
* |
|
|
|
|
* @param jsonFile json file |
|
|
|
|
* @param jsonFile json file |
|
|
|
|
* @param jsonPath the json path |
|
|
|
|
* @param filters filters to be applied to the filter place holders [?] in the 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 |
|
|
|
|
*/ |
|
|
|
@ -535,10 +544,10 @@ public class JsonPath {
|
|
|
|
|
* Creates a new JsonPath and applies it to the provided Json object |
|
|
|
|
* |
|
|
|
|
* @param jsonProvider JsonProvider to use |
|
|
|
|
* @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 |
|
|
|
|
* @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"}) |
|
|
|
@ -553,10 +562,10 @@ public class JsonPath {
|
|
|
|
|
/** |
|
|
|
|
* Creates a new JsonPath and applies it to the provided Json object |
|
|
|
|
* |
|
|
|
|
* @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 |
|
|
|
|
* @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"}) |
|
|
|
@ -567,11 +576,11 @@ public class JsonPath {
|
|
|
|
|
/** |
|
|
|
|
* Creates a new JsonPath and applies it to the provided Json object |
|
|
|
|
* |
|
|
|
|
* @param jsonProvider JsonProvider to use |
|
|
|
|
* @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 |
|
|
|
|
* @param jsonProvider JsonProvider to use |
|
|
|
|
* @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"}) |
|
|
|
|