Browse Source

Overloaded read methods to take JsonProvider argument.

pull/32/head
Kalle Stenflo 11 years ago
parent
commit
9efa29cfbc
  1. 131
      json-path/src/main/java/com/jayway/jsonpath/JsonModel.java
  2. 183
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  3. 13
      json-path/src/main/java/com/jayway/jsonpath/spi/impl/JsonSmartJsonProvider.java

131
json-path/src/main/java/com/jayway/jsonpath/JsonModel.java

@ -126,7 +126,7 @@ public class JsonModel {
/** /**
* Prints this JsonModel to standard out * Prints this JsonModel to standard out
*/ */
public void print(){ public void print() {
String json = toJson(); String json = toJson();
System.out.println(JsonFormatter.prettyPrint(json)); System.out.println(JsonFormatter.prettyPrint(json));
} }
@ -134,30 +134,28 @@ public class JsonModel {
/** /**
* Check if this JsonModel has the given definite path * Check if this JsonModel has the given definite path
* *
* @see com.jayway.jsonpath.JsonPath#isPathDefinite()
*
* @param jsonPath path to check * @param jsonPath path to check
* @return true if model contains path * @return true if model contains path
* @see com.jayway.jsonpath.JsonPath#isPathDefinite()
*/ */
public boolean hasPath(String jsonPath){ public boolean hasPath(String jsonPath) {
return hasPath(JsonPath.compile(jsonPath)); return hasPath(JsonPath.compile(jsonPath));
} }
/** /**
* Check if this JsonModel has the given definite path * Check if this JsonModel has the given definite path
* *
* @see com.jayway.jsonpath.JsonPath#isPathDefinite()
*
* @param jsonPath path to check * @param jsonPath path to check
* @return true if model contains path * @return true if model contains path
* @see com.jayway.jsonpath.JsonPath#isPathDefinite()
*/ */
public boolean hasPath(JsonPath jsonPath){ public boolean hasPath(JsonPath jsonPath) {
isTrue(jsonPath.isPathDefinite(), "hasPath can only be used for definite paths"); isTrue(jsonPath.isPathDefinite(), "hasPath can only be used for definite paths");
try { try {
get(jsonPath); get(jsonPath);
} catch(InvalidPathException e){ } catch (InvalidPathException e) {
return false; return false;
} }
return true; return true;
@ -219,7 +217,7 @@ public class JsonModel {
*/ */
@SuppressWarnings({"unchecked"}) @SuppressWarnings({"unchecked"})
public <T> T get(String jsonPath, Filter... filters) { public <T> T get(String jsonPath, Filter... filters) {
return (T) get(JsonPath.compile(jsonPath, filters)); return get(JsonPath.compile(jsonPath, filters));
} }
/** /**
@ -233,7 +231,7 @@ public class JsonModel {
public <T> T get(JsonPath jsonPath) { public <T> T get(JsonPath jsonPath) {
notNull(jsonPath, "jsonPath can not be null"); notNull(jsonPath, "jsonPath can not be null");
return (T) jsonPath.read(jsonObject); return jsonPath.read(jsonProvider, jsonObject);
} }
// -------------------------------------------------------- // --------------------------------------------------------
@ -341,7 +339,7 @@ public class JsonModel {
*/ */
public String toJson(boolean prettyPrint) { public String toJson(boolean prettyPrint) {
String json = jsonProvider.toJson(jsonObject); String json = jsonProvider.toJson(jsonObject);
if(prettyPrint) if (prettyPrint)
return JsonFormatter.prettyPrint(json); return JsonFormatter.prettyPrint(json);
else else
return json; return json;
@ -378,10 +376,8 @@ public class JsonModel {
* addressed by a definite path. In contrast to a detached model changes on the sub model * addressed by a definite path. In contrast to a detached model changes on the sub model
* will be applied on the source model (the JsonModel from which the sub model was created) * will be applied on the source model (the JsonModel from which the sub model was created)
* *
*
* @param jsonPath the absolute path to extract a JsonModel for * @param jsonPath the absolute path to extract a JsonModel for
* @return the new JsonModel * @return the new JsonModel
*
* @see com.jayway.jsonpath.JsonPath#isPathDefinite() * @see com.jayway.jsonpath.JsonPath#isPathDefinite()
*/ */
public JsonModel getSubModel(String jsonPath) { public JsonModel getSubModel(String jsonPath) {
@ -393,10 +389,8 @@ public class JsonModel {
* addressed by a definite path. In contrast to a detached model changes on the sub model * addressed by a definite path. In contrast to a detached model changes on the sub model
* will be applied on the source model (the JsonModel from which the sub model was created) * will be applied on the source model (the JsonModel from which the sub model was created)
* *
*
* @param jsonPath the absolute path to extract a JsonModel for * @param jsonPath the absolute path to extract a JsonModel for
* @return the new JsonModel * @return the new JsonModel
*
* @see com.jayway.jsonpath.JsonPath#isPathDefinite() * @see com.jayway.jsonpath.JsonPath#isPathDefinite()
*/ */
public JsonModel getSubModel(JsonPath jsonPath) { public JsonModel getSubModel(JsonPath jsonPath) {
@ -424,7 +418,7 @@ public class JsonModel {
* source model (the JsonModel from which the sub model was created). * source model (the JsonModel from which the sub model was created).
* *
* @param jsonPath the absolute path to extract a JsonModel for * @param jsonPath the absolute path to extract a JsonModel for
* @param filters filters to expand the path * @param filters filters to expand the path
* @return a detached JsonModel * @return a detached JsonModel
*/ */
public JsonModel getSubModelDetached(String jsonPath, Filter... filters) { public JsonModel getSubModelDetached(String jsonPath, Filter... filters) {
@ -442,7 +436,7 @@ public class JsonModel {
public JsonModel getSubModelDetached(JsonPath jsonPath) { public JsonModel getSubModelDetached(JsonPath jsonPath) {
notNull(jsonPath, "jsonPath can not be null"); notNull(jsonPath, "jsonPath can not be null");
Object subModel = jsonPath.read(jsonObject); Object subModel = jsonPath.read(jsonProvider, jsonObject);
if (!jsonProvider.isContainer(subModel)) { if (!jsonProvider.isContainer(subModel)) {
throw new InvalidModelException("The path " + jsonPath.getPath() + " returned an invalid model " + (subModel != null ? subModel.getClass() : "null")); throw new InvalidModelException("The path " + jsonPath.getPath() + " returned an invalid model " + (subModel != null ? subModel.getClass() : "null"));
@ -500,14 +494,27 @@ public class JsonModel {
/** /**
* Creates a JsonModel * Creates a JsonModel
* *
* @param json json string * @param jsonProvider JsonProvider to use
* @param json json string
* @return a new JsonModel * @return a new JsonModel
*/ */
public static JsonModel model(String json) { public static JsonModel model(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 new JsonModel(json, JsonProviderFactory.createProvider()); return new JsonModel(json, jsonProvider);
} }
/**
* Creates a JsonModel
*
* @param json json string
* @return a new JsonModel
*/
public static JsonModel model(String json) {
return model(JsonProviderFactory.createProvider(), json);
}
public static JsonModel create(String json) { public static JsonModel create(String json) {
return model(json); return model(json);
} }
@ -515,14 +522,27 @@ public class JsonModel {
/** /**
* Creates a JsonModel * Creates a JsonModel
* *
* @param jsonObject a json container (a {@link Map} or a {@link List}) * @param jsonProvider JsonProvider to use
* @param jsonObject a json container (a {@link Map} or a {@link List})
* @return a new JsonModel * @return a new JsonModel
*/ */
public static JsonModel model(Object jsonObject) { public static JsonModel model(JsonProvider jsonProvider, Object jsonObject) {
notNull(jsonProvider, "jsonProvider can not be null");
notNull(jsonObject, "jsonObject can not be null"); notNull(jsonObject, "jsonObject can not be null");
return new JsonModel(jsonObject, jsonProvider);
}
return new JsonModel(jsonObject, JsonProviderFactory.createProvider()); /**
* Creates a JsonModel
*
* @param jsonObject a json container (a {@link Map} or a {@link List})
* @return a new JsonModel
*/
public static JsonModel model(Object jsonObject) {
return model(JsonProviderFactory.createProvider(), jsonObject);
} }
public static JsonModel create(Object jsonObject) { public static JsonModel create(Object jsonObject) {
return model(jsonObject); return model(jsonObject);
} }
@ -530,30 +550,56 @@ public class JsonModel {
/** /**
* Creates a JsonModel * Creates a JsonModel
* *
* @param jsonProvider JsonProvider to use
* @param url pointing to a Json document * @param url pointing to a Json document
* @return a new JsonModel * @return a new JsonModel
*/ */
public static JsonModel model(URL url) throws IOException { public static JsonModel model(JsonProvider jsonProvider, URL url) throws IOException {
notNull(jsonProvider, "jsonProvider can not be null");
notNull(url, "url can not be null"); notNull(url, "url can not be null");
return new JsonModel(url, JsonProviderFactory.createProvider()); return new JsonModel(url, jsonProvider);
}
/**
* Creates a JsonModel
*
* @param url pointing to a Json document
* @return a new JsonModel
*/
public static JsonModel model(URL url) throws IOException {
return model(JsonProviderFactory.createProvider(), url);
} }
public static JsonModel create(URL url) throws IOException {
public static JsonModel create(URL url) throws IOException {
return model(url); return model(url);
} }
/** /**
* Creates a JsonModel * Creates a JsonModel
* *
* @param jsonProvider JsonProvider to use
* @param jsonInputStream json document stream * @param jsonInputStream json document stream
* @return a new JsonModel * @return a new JsonModel
*/ */
public static JsonModel model(InputStream jsonInputStream) throws IOException { public static JsonModel model(JsonProvider jsonProvider, InputStream jsonInputStream) throws IOException {
notNull(jsonProvider, "jsonProvider can not be null");
notNull(jsonInputStream, "jsonInputStream can not be null"); notNull(jsonInputStream, "jsonInputStream can not be null");
return new JsonModel(jsonInputStream, JsonProviderFactory.createProvider()); return new JsonModel(jsonInputStream, jsonProvider);
} }
public static JsonModel create(InputStream jsonInputStream) throws IOException {
/**
* Creates a JsonModel
*
* @param jsonInputStream json document stream
* @return a new JsonModel
*/
public static JsonModel model(InputStream jsonInputStream) throws IOException {
return model(JsonProviderFactory.createProvider(), jsonInputStream);
}
public static JsonModel create(InputStream jsonInputStream) throws IOException {
return model(jsonInputStream); return model(jsonInputStream);
} }
@ -570,8 +616,6 @@ public class JsonModel {
throw new IndefinitePathException(jsonPath.getPath()); throw new IndefinitePathException(jsonPath.getPath());
} }
JsonProvider jsonProvider = JsonProviderFactory.createProvider();
Object modelRef = jsonObject; Object modelRef = jsonObject;
if (jsonPath.getTokenizer().size() == 1) { if (jsonPath.getTokenizer().size() == 1) {
@ -633,11 +677,10 @@ public class JsonModel {
/** /**
* Converts this JsonModel to the specified class using the configured {@link com.jayway.jsonpath.spi.MappingProvider} * Converts this JsonModel to the specified class using the configured {@link com.jayway.jsonpath.spi.MappingProvider}
* *
* @see MappingProviderFactory
*
* @param targetClass class to convert the {@link JsonModel} to * @param targetClass class to convert the {@link JsonModel} to
* @param <T> template class * @param <T> template class
* @return the mapped model * @return the mapped model
* @see MappingProviderFactory
*/ */
<T> T to(Class<T> targetClass); <T> T to(Class<T> targetClass);
} }
@ -650,7 +693,7 @@ public class JsonModel {
* Converts this JsonModel to the a list of objects with the provided class using the configured {@link com.jayway.jsonpath.spi.MappingProvider} * Converts this JsonModel to the a list of objects with the provided class using the configured {@link com.jayway.jsonpath.spi.MappingProvider}
* *
* @param targetClass class to convert the {@link JsonModel} array items to * @param targetClass class to convert the {@link JsonModel} array items to
* @param <T> template class * @param <T> template class
* @return the mapped mode * @return the mapped mode
*/ */
<T> List<T> of(Class<T> targetClass); <T> List<T> of(Class<T> targetClass);
@ -664,7 +707,7 @@ public class JsonModel {
* Converts this JsonModel to the a {@link List} of objects with the provided class using the configured {@link com.jayway.jsonpath.spi.MappingProvider} * Converts this JsonModel to the a {@link List} of objects with the provided class using the configured {@link com.jayway.jsonpath.spi.MappingProvider}
* *
* @param targetClass class to convert the {@link JsonModel} array items to * @param targetClass class to convert the {@link JsonModel} array items to
* @param <T> template class * @param <T> template class
* @return the mapped mode * @return the mapped mode
*/ */
<T> List<T> toListOf(Class<T> targetClass); <T> List<T> toListOf(Class<T> targetClass);
@ -673,7 +716,7 @@ public class JsonModel {
* Converts this JsonModel to the a {@link Set} of objects with the provided class using the configured {@link com.jayway.jsonpath.spi.MappingProvider} * Converts this JsonModel to the a {@link Set} of objects with the provided class using the configured {@link com.jayway.jsonpath.spi.MappingProvider}
* *
* @param targetClass class to convert the {@link JsonModel} array items to * @param targetClass class to convert the {@link JsonModel} array items to
* @param <T> template class * @param <T> template class
* @return the mapped model * @return the mapped model
*/ */
<T> Set<T> toSetOf(Class<T> targetClass); <T> Set<T> toSetOf(Class<T> targetClass);
@ -693,6 +736,7 @@ public class JsonModel {
/** /**
* Returns the operation target * Returns the operation target
*
* @return the operation target * @return the operation target
*/ */
Map<String, Object> getTarget(); Map<String, Object> getTarget();
@ -709,7 +753,8 @@ public class JsonModel {
/** /**
* Adds the value to the target map if it is not already present * Adds the value to the target map if it is not already present
* @param key the key *
* @param key the key
* @param value the value * @param value the value
* @return this {@link ObjectOps} * @return this {@link ObjectOps}
*/ */
@ -722,6 +767,7 @@ public class JsonModel {
/** /**
* Tries to convert the value associated with the key to an {@link Integer} * Tries to convert the value associated with the key to an {@link Integer}
*
* @param key the key * @param key the key
* @return converted value * @return converted value
*/ */
@ -729,6 +775,7 @@ public class JsonModel {
/** /**
* Tries to convert the value associated with the key to an {@link Long} * Tries to convert the value associated with the key to an {@link Long}
*
* @param key the key * @param key the key
* @return converted value * @return converted value
*/ */
@ -736,6 +783,7 @@ public class JsonModel {
/** /**
* Tries to convert the value associated with the key to an {@link Double} * Tries to convert the value associated with the key to an {@link Double}
*
* @param key the key * @param key the key
* @return converted value * @return converted value
*/ */
@ -743,6 +791,7 @@ public class JsonModel {
/** /**
* Tries to convert the value associated with the key to an {@link String} * Tries to convert the value associated with the key to an {@link String}
*
* @param key the key * @param key the key
* @return converted value * @return converted value
*/ */
@ -769,8 +818,9 @@ public class JsonModel {
/** /**
* Map the target of this {@link ObjectOps} to the provided class * Map the target of this {@link ObjectOps} to the provided class
*
* @param targetClass class to convert the target object to * @param targetClass class to convert the target object to
* @param <T> template class * @param <T> template class
* @return the mapped model * @return the mapped model
*/ */
<T> T to(Class<T> targetClass); <T> T to(Class<T> targetClass);
@ -783,6 +833,7 @@ public class JsonModel {
/** /**
* Returns the operation target * Returns the operation target
*
* @return the operation target * @return the operation target
*/ */
List<Object> getTarget(); List<Object> getTarget();
@ -984,7 +1035,7 @@ public class JsonModel {
public ArrayOps each(Transformer<Object> transformer) { public ArrayOps each(Transformer<Object> transformer) {
List targetObject = getTargetObject(jsonPath, List.class); List targetObject = getTargetObject(jsonPath, List.class);
for(int i = 0; i < targetObject.size(); i++){ for (int i = 0; i < targetObject.size(); i++) {
targetObject.set(i, transformer.transform(targetObject.get(i))); targetObject.set(i, transformer.transform(targetObject.get(i)));
} }
return this; return this;

183
json-path/src/main/java/com/jayway/jsonpath/JsonPath.java

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

13
json-path/src/main/java/com/jayway/jsonpath/spi/impl/JsonSmartJsonProvider.java

@ -35,8 +35,6 @@ public class JsonSmartJsonProvider extends AbstractJsonProvider {
private Mode mode; private Mode mode;
private JSONParser parser;
private ContainerFactory containerFactory = ContainerFactory.FACTORY_SIMPLE; private ContainerFactory containerFactory = ContainerFactory.FACTORY_SIMPLE;
public JsonSmartJsonProvider() { public JsonSmartJsonProvider() {
@ -45,7 +43,6 @@ public class JsonSmartJsonProvider extends AbstractJsonProvider {
public JsonSmartJsonProvider(Mode mode) { public JsonSmartJsonProvider(Mode mode) {
this.mode = mode; this.mode = mode;
this.parser = new JSONParser(mode.intValue());
} }
public Map<String, Object> createMap() { public Map<String, Object> createMap() {
@ -59,7 +56,7 @@ public class JsonSmartJsonProvider extends AbstractJsonProvider {
public Object parse(String json) { public Object parse(String json) {
try { try {
//return parser.parse(json, ContainerFactory.FACTORY_ORDERED); //return parser.parse(json, ContainerFactory.FACTORY_ORDERED);
return parser.parse(json, containerFactory); return createParser().parse(json, containerFactory);
} catch (ParseException e) { } catch (ParseException e) {
throw new InvalidJsonException(e); throw new InvalidJsonException(e);
} }
@ -68,7 +65,7 @@ public class JsonSmartJsonProvider extends AbstractJsonProvider {
@Override @Override
public Object parse(Reader jsonReader) throws InvalidJsonException { public Object parse(Reader jsonReader) throws InvalidJsonException {
try { try {
return parser.parse(jsonReader, containerFactory); return createParser().parse(jsonReader, containerFactory);
} catch (ParseException e) { } catch (ParseException e) {
throw new InvalidJsonException(e); throw new InvalidJsonException(e);
} }
@ -77,7 +74,7 @@ public class JsonSmartJsonProvider extends AbstractJsonProvider {
@Override @Override
public Object parse(InputStream jsonStream) throws InvalidJsonException { public Object parse(InputStream jsonStream) throws InvalidJsonException {
try { try {
return parser.parse(new InputStreamReader(jsonStream), containerFactory); return createParser().parse(new InputStreamReader(jsonStream), containerFactory);
} catch (ParseException e) { } catch (ParseException e) {
throw new InvalidJsonException(e); throw new InvalidJsonException(e);
} }
@ -98,4 +95,8 @@ public class JsonSmartJsonProvider extends AbstractJsonProvider {
public Mode getMode() { public Mode getMode() {
return mode; return mode;
} }
private JSONParser createParser(){
return new JSONParser(mode.intValue());
}
} }

Loading…
Cancel
Save