Browse Source

Preparations for patch support.

pull/64/head
Kalle Stenflo 10 years ago
parent
commit
6a88323b25
  1. 24
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  2. 17
      json-path/src/main/java/com/jayway/jsonpath/WriteContext.java
  3. 43
      json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java

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

@ -207,7 +207,7 @@ public class JsonPath {
* @param jsonObject a json object * @param jsonObject a json object
* @param configuration configuration to use * @param configuration configuration to use
* @param <T> expected return type * @param <T> expected return type
* @return the updated jsonObject * @return the updated jsonObject or the path list to updated objects if option AS_PATH_LIST is set.
*/ */
public <T> T set(Object jsonObject, Object newVal, Configuration configuration) { public <T> T set(Object jsonObject, Object newVal, Configuration configuration) {
notNull(jsonObject, "json can not be null"); notNull(jsonObject, "json can not be null");
@ -216,7 +216,7 @@ public class JsonPath {
for (PathRef updateOperation : evaluationContext.updateOperations()) { for (PathRef updateOperation : evaluationContext.updateOperations()) {
updateOperation.set(newVal, configuration); updateOperation.set(newVal, configuration);
} }
return (T)jsonObject; return resultByConfiguration(jsonObject, configuration, evaluationContext);
} }
/** /**
@ -225,7 +225,7 @@ public class JsonPath {
* @param jsonObject a json object * @param jsonObject a json object
* @param configuration configuration to use * @param configuration configuration to use
* @param <T> expected return type * @param <T> expected return type
* @return the updated jsonObject * @return the updated jsonObject or the path list to deleted objects if option AS_PATH_LIST is set.
*/ */
public <T> T delete(Object jsonObject, Configuration configuration) { public <T> T delete(Object jsonObject, Configuration configuration) {
notNull(jsonObject, "json can not be null"); notNull(jsonObject, "json can not be null");
@ -234,7 +234,7 @@ public class JsonPath {
for (PathRef updateOperation : evaluationContext.updateOperations()) { for (PathRef updateOperation : evaluationContext.updateOperations()) {
updateOperation.delete(configuration); updateOperation.delete(configuration);
} }
return (T)jsonObject; return resultByConfiguration(jsonObject, configuration, evaluationContext);
} }
/** /**
@ -244,7 +244,7 @@ public class JsonPath {
* @param value the value to add * @param value the value to add
* @param configuration configuration to use * @param configuration configuration to use
* @param <T> expected return type * @param <T> expected return type
* @return the updated jsonObject * @return the updated jsonObject or the path list to updated object if option AS_PATH_LIST is set.
*/ */
public <T> T add(Object jsonObject, Object value, Configuration configuration) { public <T> T add(Object jsonObject, Object value, Configuration configuration) {
notNull(jsonObject, "json can not be null"); notNull(jsonObject, "json can not be null");
@ -253,7 +253,7 @@ public class JsonPath {
for (PathRef updateOperation : evaluationContext.updateOperations()) { for (PathRef updateOperation : evaluationContext.updateOperations()) {
updateOperation.add(value, configuration); updateOperation.add(value, configuration);
} }
return (T)jsonObject; return resultByConfiguration(jsonObject, configuration, evaluationContext);
} }
/** /**
@ -264,7 +264,7 @@ public class JsonPath {
* @param value the new value * @param value the new value
* @param configuration configuration to use * @param configuration configuration to use
* @param <T> expected return type * @param <T> expected return type
* @return the updated jsonObject * @return the updated jsonObject or the path list to updated objects if option AS_PATH_LIST is set.
*/ */
public <T> T put(Object jsonObject, String key, Object value, Configuration configuration) { public <T> T put(Object jsonObject, String key, Object value, Configuration configuration) {
notNull(jsonObject, "json can not be null"); notNull(jsonObject, "json can not be null");
@ -274,7 +274,7 @@ public class JsonPath {
for (PathRef updateOperation : evaluationContext.updateOperations()) { for (PathRef updateOperation : evaluationContext.updateOperations()) {
updateOperation.put(key, value, configuration); updateOperation.put(key, value, configuration);
} }
return (T)jsonObject; return resultByConfiguration(jsonObject, configuration, evaluationContext);
} }
/** /**
@ -665,4 +665,12 @@ public class JsonPath {
public static DocumentContext parse(URL json, Configuration configuration) throws IOException { public static DocumentContext parse(URL json, Configuration configuration) throws IOException {
return new JsonReader(configuration).parse(json); return new JsonReader(configuration).parse(json);
} }
private <T> T resultByConfiguration(Object jsonObject, Configuration configuration, EvaluationContext evaluationContext) {
if(configuration.containsOption(Option.AS_PATH_LIST)){
return (T)evaluationContext.getPathList();
} else {
return (T) jsonObject;
}
}
} }

17
json-path/src/main/java/com/jayway/jsonpath/WriteContext.java

@ -67,7 +67,20 @@ public interface WriteContext {
DocumentContext delete(JsonPath path); DocumentContext delete(JsonPath path);
/** /**
* Add value to array at the given path * Add value to array
*
* <pre>
* <code>
* List<Integer> array = new ArrayList<Integer>(){{
* add(0);
* add(1);
* }};
*
* JsonPath.parse(array).add("$", 2);
*
* assertThat(array).containsExactly(0,1,2);
* </code>
* </pre>
* *
* @param path path to array * @param path path to array
* @param value value to add * @param value value to add
@ -88,7 +101,7 @@ public interface WriteContext {
/** /**
* Add or update the key with a the given value at the given path * Add or update the key with a the given value at the given path
* *
* @param path path to array * @param path path to object
* @param key key to add * @param key key to add
* @param value value of key * @param value value of key
* @param filters filters * @param filters filters

43
json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java

@ -18,17 +18,21 @@ import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext; import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.EvaluationListener; import com.jayway.jsonpath.EvaluationListener;
import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.ParseContext; import com.jayway.jsonpath.ParseContext;
import com.jayway.jsonpath.Predicate; import com.jayway.jsonpath.Predicate;
import com.jayway.jsonpath.ReadContext; import com.jayway.jsonpath.ReadContext;
import com.jayway.jsonpath.TypeRef; import com.jayway.jsonpath.TypeRef;
import com.jayway.jsonpath.spi.http.HttpProviderFactory; import com.jayway.jsonpath.spi.http.HttpProviderFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.util.List;
import static com.jayway.jsonpath.JsonPath.compile; import static com.jayway.jsonpath.JsonPath.compile;
import static com.jayway.jsonpath.internal.Utils.notEmpty; import static com.jayway.jsonpath.internal.Utils.notEmpty;
@ -36,8 +40,11 @@ import static com.jayway.jsonpath.internal.Utils.notNull;
public class JsonReader implements ParseContext, DocumentContext { public class JsonReader implements ParseContext, DocumentContext {
private static final Logger logger = LoggerFactory.getLogger(JsonReader.class);
private final Configuration configuration; private final Configuration configuration;
private Object json; private Object json;
private Object patch;
public JsonReader() { public JsonReader() {
this(Configuration.defaultConfiguration()); this(Configuration.defaultConfiguration());
@ -182,8 +189,13 @@ public class JsonReader implements ParseContext, DocumentContext {
@Override @Override
public DocumentContext set(JsonPath path, Object newValue){ public DocumentContext set(JsonPath path, Object newValue){
Object modifiedJson = path.set(json, newValue, configuration); List<String> modified = path.set(json, newValue, configuration.addOptions(Option.AS_PATH_LIST));
return new JsonReader(modifiedJson, configuration); if(logger.isDebugEnabled()){
for (String p : modified) {
logger.debug("Set path {} new value {}", p, configuration.jsonProvider().toJson(newValue));
}
}
return this;
} }
@Override @Override
@ -193,8 +205,13 @@ public class JsonReader implements ParseContext, DocumentContext {
@Override @Override
public DocumentContext delete(JsonPath path) { public DocumentContext delete(JsonPath path) {
Object modifiedJson = path.delete(json, configuration); List<String> modified = path.delete(json, configuration.addOptions(Option.AS_PATH_LIST));
return new JsonReader(modifiedJson, configuration); if(logger.isDebugEnabled()){
for (String p : modified) {
logger.debug("Delete path {}");
}
}
return this;
} }
@Override @Override
@ -204,8 +221,13 @@ public class JsonReader implements ParseContext, DocumentContext {
@Override @Override
public DocumentContext add(JsonPath path, Object value){ public DocumentContext add(JsonPath path, Object value){
Object modifiedJson = path.add(json, value, configuration); List<String> modified = path.add(json, value, configuration.addOptions(Option.AS_PATH_LIST));
return new JsonReader(modifiedJson, configuration); if(logger.isDebugEnabled()){
for (String p : modified) {
logger.debug("Add path {} new value {}", p, configuration.jsonProvider().toJson(value));
}
}
return this;
} }
@Override @Override
@ -215,8 +237,13 @@ public class JsonReader implements ParseContext, DocumentContext {
@Override @Override
public DocumentContext put(JsonPath path, String key, Object value){ public DocumentContext put(JsonPath path, String key, Object value){
Object modifiedJson = path.put(json, key, value, configuration); List<String> modified = path.put(json, key, value, configuration.addOptions(Option.AS_PATH_LIST));
return new JsonReader(modifiedJson, configuration); if(logger.isDebugEnabled()){
for (String p : modified) {
logger.debug("Put path {} key {} value {}", p, key, configuration.jsonProvider().toJson(value));
}
}
return this;
} }
private final class LimitingEvaluationListener implements EvaluationListener { private final class LimitingEvaluationListener implements EvaluationListener {

Loading…
Cancel
Save