From 6a88323b253e82079e3a55587ea3e6aa207f3af9 Mon Sep 17 00:00:00 2001 From: Kalle Stenflo Date: Thu, 11 Dec 2014 14:36:27 +0100 Subject: [PATCH] Preparations for patch support. --- .../java/com/jayway/jsonpath/JsonPath.java | 24 +++++++---- .../com/jayway/jsonpath/WriteContext.java | 17 +++++++- .../jayway/jsonpath/internal/JsonReader.java | 43 +++++++++++++++---- 3 files changed, 66 insertions(+), 18 deletions(-) diff --git a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java index 617670c6..541e9ade 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java +++ b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java @@ -207,7 +207,7 @@ public class JsonPath { * @param jsonObject a json object * @param configuration configuration to use * @param 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 set(Object jsonObject, Object newVal, Configuration configuration) { notNull(jsonObject, "json can not be null"); @@ -216,7 +216,7 @@ public class JsonPath { for (PathRef updateOperation : evaluationContext.updateOperations()) { 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 configuration configuration to use * @param 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 delete(Object jsonObject, Configuration configuration) { notNull(jsonObject, "json can not be null"); @@ -234,7 +234,7 @@ public class JsonPath { for (PathRef updateOperation : evaluationContext.updateOperations()) { 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 configuration configuration to use * @param 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 add(Object jsonObject, Object value, Configuration configuration) { notNull(jsonObject, "json can not be null"); @@ -253,7 +253,7 @@ public class JsonPath { for (PathRef updateOperation : evaluationContext.updateOperations()) { 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 configuration configuration to use * @param 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 put(Object jsonObject, String key, Object value, Configuration configuration) { notNull(jsonObject, "json can not be null"); @@ -274,7 +274,7 @@ public class JsonPath { for (PathRef updateOperation : evaluationContext.updateOperations()) { 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 { return new JsonReader(configuration).parse(json); } + + private T resultByConfiguration(Object jsonObject, Configuration configuration, EvaluationContext evaluationContext) { + if(configuration.containsOption(Option.AS_PATH_LIST)){ + return (T)evaluationContext.getPathList(); + } else { + return (T) jsonObject; + } + } } diff --git a/json-path/src/main/java/com/jayway/jsonpath/WriteContext.java b/json-path/src/main/java/com/jayway/jsonpath/WriteContext.java index 317863d9..1b254da7 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/WriteContext.java +++ b/json-path/src/main/java/com/jayway/jsonpath/WriteContext.java @@ -67,7 +67,20 @@ public interface WriteContext { DocumentContext delete(JsonPath path); /** - * Add value to array at the given path + * Add value to array + * + *
+     * 
+     * List array = new ArrayList(){{
+     *      add(0);
+     *      add(1);
+     * }};
+     *
+     * JsonPath.parse(array).add("$", 2);
+     *
+     * assertThat(array).containsExactly(0,1,2);
+     * 
+     * 
* * @param path path to array * @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 * - * @param path path to array + * @param path path to object * @param key key to add * @param value value of key * @param filters filters diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java b/json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java index 125eb862..ce201b53 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java +++ b/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.EvaluationListener; import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.Option; import com.jayway.jsonpath.ParseContext; import com.jayway.jsonpath.Predicate; import com.jayway.jsonpath.ReadContext; import com.jayway.jsonpath.TypeRef; import com.jayway.jsonpath.spi.http.HttpProviderFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.util.List; import static com.jayway.jsonpath.JsonPath.compile; 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 { + private static final Logger logger = LoggerFactory.getLogger(JsonReader.class); + private final Configuration configuration; private Object json; + private Object patch; public JsonReader() { this(Configuration.defaultConfiguration()); @@ -182,8 +189,13 @@ public class JsonReader implements ParseContext, DocumentContext { @Override public DocumentContext set(JsonPath path, Object newValue){ - Object modifiedJson = path.set(json, newValue, configuration); - return new JsonReader(modifiedJson, configuration); + List modified = path.set(json, newValue, configuration.addOptions(Option.AS_PATH_LIST)); + if(logger.isDebugEnabled()){ + for (String p : modified) { + logger.debug("Set path {} new value {}", p, configuration.jsonProvider().toJson(newValue)); + } + } + return this; } @Override @@ -193,8 +205,13 @@ public class JsonReader implements ParseContext, DocumentContext { @Override public DocumentContext delete(JsonPath path) { - Object modifiedJson = path.delete(json, configuration); - return new JsonReader(modifiedJson, configuration); + List modified = path.delete(json, configuration.addOptions(Option.AS_PATH_LIST)); + if(logger.isDebugEnabled()){ + for (String p : modified) { + logger.debug("Delete path {}"); + } + } + return this; } @Override @@ -204,8 +221,13 @@ public class JsonReader implements ParseContext, DocumentContext { @Override public DocumentContext add(JsonPath path, Object value){ - Object modifiedJson = path.add(json, value, configuration); - return new JsonReader(modifiedJson, configuration); + List modified = path.add(json, value, configuration.addOptions(Option.AS_PATH_LIST)); + if(logger.isDebugEnabled()){ + for (String p : modified) { + logger.debug("Add path {} new value {}", p, configuration.jsonProvider().toJson(value)); + } + } + return this; } @Override @@ -215,8 +237,13 @@ public class JsonReader implements ParseContext, DocumentContext { @Override public DocumentContext put(JsonPath path, String key, Object value){ - Object modifiedJson = path.put(json, key, value, configuration); - return new JsonReader(modifiedJson, configuration); + List modified = path.put(json, key, value, configuration.addOptions(Option.AS_PATH_LIST)); + 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 {