|
|
@ -204,9 +204,23 @@ public class JsonPath { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
public <T> T read(Object jsonObject) { |
|
|
|
public <T> T read(Object jsonObject) { |
|
|
|
notNull(jsonObject, "json can not be null"); |
|
|
|
return read(JsonProviderFactory.createProvider(), jsonObject); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
JsonProvider jsonProvider = JsonProviderFactory.createProvider(); |
|
|
|
/** |
|
|
|
|
|
|
|
* Applies this JsonPath to the provided json document. |
|
|
|
|
|
|
|
* Note that the document must be identified as either a List or Map by |
|
|
|
|
|
|
|
* the {@link JsonProvider} |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param jsonProvider JsonProvider to use |
|
|
|
|
|
|
|
* @param jsonObject a container Object |
|
|
|
|
|
|
|
* @param <T> expected return type |
|
|
|
|
|
|
|
* @return list of objects matched by the given path |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
|
|
|
|
public <T> T read(JsonProvider jsonProvider, Object jsonObject) { |
|
|
|
|
|
|
|
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...
|
|
|
|
//This path only references the whole object. No need to do any work here...
|
|
|
@ -244,9 +258,23 @@ public class JsonPath { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
public <T> T read(String json) { |
|
|
|
public <T> T read(String json) { |
|
|
|
|
|
|
|
return read(JsonProviderFactory.createProvider(), json); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Applies this JsonPath to the provided json string |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param jsonProvider JsonProvider to use |
|
|
|
|
|
|
|
* @param json a json string |
|
|
|
|
|
|
|
* @param <T> expected return type |
|
|
|
|
|
|
|
* @return list of objects matched by the given path |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
|
|
|
|
public <T> T read(JsonProvider jsonProvider, String json) { |
|
|
|
|
|
|
|
notNull(jsonProvider, "jsonProvider can not be null"); |
|
|
|
notEmpty(json, "json can not be null or empty"); |
|
|
|
notEmpty(json, "json can not be null or empty"); |
|
|
|
|
|
|
|
|
|
|
|
return (T) read(JsonProviderFactory.createProvider().parse(json)); |
|
|
|
return read(jsonProvider, jsonProvider.parse(json)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -259,12 +287,27 @@ public class JsonPath { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
public <T> T read(URL jsonURL) throws IOException { |
|
|
|
public <T> T read(URL jsonURL) throws IOException { |
|
|
|
|
|
|
|
return read(JsonProviderFactory.createProvider(), jsonURL); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Applies this JsonPath to the provided json URL |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param jsonProvider JsonProvider to use |
|
|
|
|
|
|
|
* @param jsonURL url to read from |
|
|
|
|
|
|
|
* @param <T> expected return type |
|
|
|
|
|
|
|
* @return list of objects matched by the given path |
|
|
|
|
|
|
|
* @throws IOException |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
|
|
|
|
public <T> T read(JsonProvider jsonProvider, URL jsonURL) throws IOException { |
|
|
|
|
|
|
|
notNull(jsonProvider, "jsonProvider can not be null"); |
|
|
|
notNull(jsonURL, "json URL can not be null"); |
|
|
|
notNull(jsonURL, "json URL can not be null"); |
|
|
|
|
|
|
|
|
|
|
|
InputStream in = null; |
|
|
|
InputStream in = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
in = HttpProviderFactory.getProvider().get(jsonURL); |
|
|
|
in = HttpProviderFactory.getProvider().get(jsonURL); |
|
|
|
return (T) read(JsonProviderFactory.createProvider().parse(in)); |
|
|
|
return read(jsonProvider, jsonProvider.parse(in)); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
IOUtils.closeQuietly(in); |
|
|
|
IOUtils.closeQuietly(in); |
|
|
|
} |
|
|
|
} |
|
|
@ -280,13 +323,29 @@ public class JsonPath { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
public <T> T read(File jsonFile) throws IOException { |
|
|
|
public <T> T read(File jsonFile) throws IOException { |
|
|
|
|
|
|
|
return read(JsonProviderFactory.createProvider(), jsonFile); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Applies this JsonPath to the provided json file |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param jsonProvider JsonProvider to use |
|
|
|
|
|
|
|
* @param jsonFile file to read from |
|
|
|
|
|
|
|
* @param <T> expected return type |
|
|
|
|
|
|
|
* @return list of objects matched by the given path |
|
|
|
|
|
|
|
* @throws IOException |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
|
|
|
|
public <T> T read(JsonProvider jsonProvider, File jsonFile) throws IOException { |
|
|
|
|
|
|
|
notNull(jsonProvider, "jsonProvider can not be null"); |
|
|
|
notNull(jsonFile, "json file can not be null"); |
|
|
|
notNull(jsonFile, "json file can not be null"); |
|
|
|
isTrue(jsonFile.exists(), "json file does not exist"); |
|
|
|
isTrue(jsonFile.exists(), "json file does not exist"); |
|
|
|
|
|
|
|
|
|
|
|
FileInputStream fis = null; |
|
|
|
FileInputStream fis = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
fis = new FileInputStream(jsonFile); |
|
|
|
fis = new FileInputStream(jsonFile); |
|
|
|
return (T) read(JsonProviderFactory.createProvider().parse(fis)); |
|
|
|
return read(jsonProvider, jsonProvider.parse(fis)); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
IOUtils.closeQuietly(fis); |
|
|
|
IOUtils.closeQuietly(fis); |
|
|
|
} |
|
|
|
} |
|
|
@ -305,7 +364,28 @@ public class JsonPath { |
|
|
|
notNull(jsonInputStream, "json input stream can not be null"); |
|
|
|
notNull(jsonInputStream, "json input stream can not be null"); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
return (T) read(JsonProviderFactory.createProvider().parse(jsonInputStream)); |
|
|
|
return read(JsonProviderFactory.createProvider().parse(jsonInputStream)); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
IOUtils.closeQuietly(jsonInputStream); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Applies this JsonPath to the provided json input stream |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @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 |
|
|
|
|
|
|
|
* @throws IOException |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
|
|
|
|
public <T> T read(JsonProvider jsonProvider, InputStream jsonInputStream) throws IOException { |
|
|
|
|
|
|
|
notNull(jsonProvider, "jsonProvider can not be null"); |
|
|
|
|
|
|
|
notNull(jsonInputStream, "json input stream can not be null"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
return read(jsonProvider, jsonProvider.parse(jsonInputStream)); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
IOUtils.closeQuietly(jsonInputStream); |
|
|
|
IOUtils.closeQuietly(jsonInputStream); |
|
|
|
} |
|
|
|
} |
|
|
@ -348,10 +428,27 @@ public class JsonPath { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
public static <T> T read(String json, String jsonPath, Filter... filters) { |
|
|
|
public static <T> T read(String json, String jsonPath, Filter... filters) { |
|
|
|
|
|
|
|
return read(JsonProviderFactory.createProvider(), json, jsonPath, filters); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 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 |
|
|
|
|
|
|
|
* @return list of objects matched by the given path |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
|
|
|
|
public static <T> T read(JsonProvider jsonProvider, String json, String jsonPath, Filter... filters) { |
|
|
|
|
|
|
|
notNull(jsonProvider, "jsonProvider can not be null"); |
|
|
|
notEmpty(json, "json can not be null or empty"); |
|
|
|
notEmpty(json, "json can not be null or empty"); |
|
|
|
notEmpty(jsonPath, "jsonPath can not be null or empty"); |
|
|
|
notEmpty(jsonPath, "jsonPath can not be null or empty"); |
|
|
|
|
|
|
|
|
|
|
|
return (T) compile(jsonPath, filters).read(json); |
|
|
|
return compile(jsonPath, filters).read(jsonProvider, json); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -365,12 +462,28 @@ public class JsonPath { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
public static <T> T read(Object json, String jsonPath, Filter... filters) { |
|
|
|
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 |
|
|
|
|
|
|
|
* @return list of objects matched by the given path |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
|
|
|
|
public static <T> T read(JsonProvider jsonProvider, Object json, String jsonPath, Filter... filters) { |
|
|
|
|
|
|
|
notNull(jsonProvider, "jsonProvider can not be null"); |
|
|
|
notNull(json, "json can not be null"); |
|
|
|
notNull(json, "json can not be null"); |
|
|
|
notNull(jsonPath, "jsonPath can not be null"); |
|
|
|
notNull(jsonPath, "jsonPath can not be null"); |
|
|
|
|
|
|
|
|
|
|
|
return (T) compile(jsonPath, filters).read(json); |
|
|
|
return compile(jsonPath, filters).read(jsonProvider, json); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Creates a new JsonPath and applies it to the provided Json object |
|
|
|
* Creates a new JsonPath and applies it to the provided Json object |
|
|
|
* |
|
|
|
* |
|
|
@ -382,10 +495,26 @@ public class JsonPath { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
public static <T> T read(URL jsonURL, String jsonPath, Filter... filters) throws IOException { |
|
|
|
public static <T> T read(URL jsonURL, String jsonPath, Filter... filters) throws IOException { |
|
|
|
|
|
|
|
return read(JsonProviderFactory.createProvider(), jsonURL, jsonPath, filters); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 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 |
|
|
|
|
|
|
|
* @return list of objects matched by the given path |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
|
|
|
|
public static <T> T read(JsonProvider jsonProvider, URL jsonURL, String jsonPath, Filter... filters) throws IOException { |
|
|
|
|
|
|
|
notNull(jsonProvider, "jsonProvider can not be null"); |
|
|
|
notNull(jsonURL, "json URL can not be null"); |
|
|
|
notNull(jsonURL, "json URL can not be null"); |
|
|
|
notEmpty(jsonPath, "jsonPath can not be null or empty"); |
|
|
|
notEmpty(jsonPath, "jsonPath can not be null or empty"); |
|
|
|
|
|
|
|
|
|
|
|
return (T) compile(jsonPath, filters).read(jsonURL); |
|
|
|
return compile(jsonPath, filters).read(jsonProvider, jsonURL); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -399,10 +528,26 @@ public class JsonPath { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
public static <T> T read(File jsonFile, String jsonPath, Filter... filters) throws IOException { |
|
|
|
public static <T> T read(File jsonFile, String jsonPath, Filter... filters) throws IOException { |
|
|
|
|
|
|
|
return read(JsonProviderFactory.createProvider(), jsonFile, jsonPath, filters); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 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 |
|
|
|
|
|
|
|
* @return list of objects matched by the given path |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
|
|
|
|
public static <T> T read(JsonProvider jsonProvider, File jsonFile, String jsonPath, Filter... filters) throws IOException { |
|
|
|
|
|
|
|
notNull(jsonProvider, "jsonProvider can not be null"); |
|
|
|
notNull(jsonFile, "json file can not be null"); |
|
|
|
notNull(jsonFile, "json file can not be null"); |
|
|
|
notEmpty(jsonPath, "jsonPath can not be null or empty"); |
|
|
|
notEmpty(jsonPath, "jsonPath can not be null or empty"); |
|
|
|
|
|
|
|
|
|
|
|
return (T) compile(jsonPath, filters).read(jsonFile); |
|
|
|
return compile(jsonPath, filters).read(jsonProvider, jsonFile); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -416,10 +561,26 @@ public class JsonPath { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
public static <T> T read(InputStream jsonInputStream, String jsonPath, Filter... filters) throws IOException { |
|
|
|
public static <T> T read(InputStream jsonInputStream, String jsonPath, Filter... filters) throws IOException { |
|
|
|
|
|
|
|
return read(JsonProviderFactory.createProvider(), jsonInputStream, jsonPath, filters); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 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 |
|
|
|
|
|
|
|
* @return list of objects matched by the given path |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
|
|
|
|
public static <T> T read(JsonProvider jsonProvider, InputStream jsonInputStream, String jsonPath, Filter... filters) throws IOException { |
|
|
|
|
|
|
|
notNull(jsonProvider, "jsonProvider can not be null"); |
|
|
|
notNull(jsonInputStream, "json input stream can not be null"); |
|
|
|
notNull(jsonInputStream, "json input stream can not be null"); |
|
|
|
notEmpty(jsonPath, "jsonPath can not be null or empty"); |
|
|
|
notEmpty(jsonPath, "jsonPath can not be null or empty"); |
|
|
|
|
|
|
|
|
|
|
|
return (T) compile(jsonPath, filters).read(jsonInputStream); |
|
|
|
return compile(jsonPath, filters).read(jsonProvider, jsonInputStream); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|