From f3aad94dbbe3dbb7037b19eeefc261b191e90842 Mon Sep 17 00:00:00 2001 From: AdultCensusSalaryPrediction Date: Sun, 26 Nov 2023 14:52:11 -0400 Subject: [PATCH] Set2 refactoring --- .../java/com/jayway/jsonpath/JsonPath.java | 82 +++++++------------ .../com/jayway/jsonpath/PathEvaluator.java | 35 ++++++++ .../com/jayway/jsonpath/internal/Path.java | 4 +- .../spi/json/JacksonJsonNodeJsonProvider.java | 32 +++++++- .../spi/json/JsonLengthCalculator.java | 5 ++ 5 files changed, 101 insertions(+), 57 deletions(-) create mode 100644 json-path/src/main/java/com/jayway/jsonpath/PathEvaluator.java create mode 100644 json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonLengthCalculator.java 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 01c7db03..6dca7db2 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java +++ b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java @@ -16,6 +16,7 @@ package com.jayway.jsonpath; import com.jayway.jsonpath.internal.*; +import com.jayway.jsonpath.internal.PathEvaluator; import com.jayway.jsonpath.internal.path.PathCompiler; import com.jayway.jsonpath.spi.json.JsonProvider; @@ -139,6 +140,23 @@ public class JsonPath { return path.isDefinite(); } + +// public boolean isFunctionPath() { +// return path.isFunctionPath(); +// } +// +// public boolean isRootPath() { +// return path.isRootPath(); +// } + + public EvaluationContext evaluate(Object document, Object rootDocument, Configuration configuration) { + return evaluate(document, rootDocument, configuration, false); + } + + public EvaluationContext evaluate(Object document, Object rootDocument, Configuration configuration, boolean forUpdate) { + EvaluationContext evaluationContext = null; + return resultByConfiguration(document, configuration, evaluationContext); + } /** * Applies this JsonPath to the provided json document. * Note that the document must be identified as either a List or Map by @@ -165,46 +183,8 @@ public class JsonPath { */ @SuppressWarnings("unchecked") public T read(Object jsonObject, Configuration configuration) { - boolean optAsPathList = configuration.containsOption(AS_PATH_LIST); - boolean optAlwaysReturnList = configuration.containsOption(Option.ALWAYS_RETURN_LIST); - boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS); - - if (path.isFunctionPath()) { - if (optAsPathList || optAlwaysReturnList) { - if (optSuppressExceptions) { - return (T) (path.isDefinite() ? null : configuration.jsonProvider().createArray()); - } - throw new JsonPathException("Options " + AS_PATH_LIST + " and " + ALWAYS_RETURN_LIST + " are not allowed when using path functions!"); - } - EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration); - if (optSuppressExceptions && evaluationContext.getPathList().isEmpty()) { - return (T) (path.isDefinite() ? null : configuration.jsonProvider().createArray()); - } - return evaluationContext.getValue(true); - } else if (optAsPathList) { - EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration); - if (optSuppressExceptions && evaluationContext.getPathList().isEmpty()) { - return (T) configuration.jsonProvider().createArray(); - } - return (T) evaluationContext.getPath(); - } else { - EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration); - if (optSuppressExceptions && evaluationContext.getPathList().isEmpty()) { - if (optAlwaysReturnList) { - return (T) configuration.jsonProvider().createArray(); - } else { - return (T) (path.isDefinite() ? null : configuration.jsonProvider().createArray()); - } - } - Object res = evaluationContext.getValue(false); - if (optAlwaysReturnList && path.isDefinite()) { - Object array = configuration.jsonProvider().createArray(); - configuration.jsonProvider().setArrayIndex(array, 0, res); - return (T) array; - } else { - return (T) res; - } - } + PathEvaluator evaluator = new PathEvaluator(path, configuration); + return evaluator.evaluate(jsonObject); } /** @@ -218,7 +198,7 @@ public class JsonPath { public T set(Object jsonObject, Object newVal, Configuration configuration) { notNull(jsonObject, "json can not be null"); notNull(configuration, "configuration can not be null"); - EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true); + EvaluationContext evaluationContext = evaluate(jsonObject, jsonObject, configuration, true); if (evaluationContext.getPathList().isEmpty()) { boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS); if (optSuppressExceptions) { @@ -246,7 +226,7 @@ public class JsonPath { notNull(jsonObject, "json can not be null"); notNull(configuration, "configuration can not be null"); notNull(mapFunction, "mapFunction can not be null"); - EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true); + EvaluationContext evaluationContext = evaluate(jsonObject, jsonObject, configuration, true); if (evaluationContext.getPathList().isEmpty()) { boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS); if (optSuppressExceptions) { @@ -273,7 +253,7 @@ public class JsonPath { public T delete(Object jsonObject, Configuration configuration) { notNull(jsonObject, "json can not be null"); notNull(configuration, "configuration can not be null"); - EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true); + EvaluationContext evaluationContext = evaluate(jsonObject, jsonObject, configuration, true); if (evaluationContext.getPathList().isEmpty()) { boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS); if (optSuppressExceptions) { @@ -300,7 +280,7 @@ public class JsonPath { public T add(Object jsonObject, Object value, Configuration configuration) { notNull(jsonObject, "json can not be null"); notNull(configuration, "configuration can not be null"); - EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true); + EvaluationContext evaluationContext = evaluate(jsonObject, jsonObject, configuration, true); if (evaluationContext.getPathList().isEmpty()) { boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS); if (optSuppressExceptions) { @@ -329,7 +309,7 @@ public class JsonPath { notNull(jsonObject, "json can not be null"); notEmpty(key, "key can not be null or empty"); notNull(configuration, "configuration can not be null"); - EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true); + EvaluationContext evaluationContext = evaluate(jsonObject, jsonObject, configuration, true); if (evaluationContext.getPathList().isEmpty()) { boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS); if (optSuppressExceptions) { @@ -348,7 +328,7 @@ public class JsonPath { notNull(jsonObject, "json can not be null"); notEmpty(newKeyName, "newKeyName can not be null or empty"); notNull(configuration, "configuration can not be null"); - EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true); + EvaluationContext evaluationContext = evaluate(jsonObject, jsonObject, configuration, true); for (PathRef updateOperation : evaluationContext.updateOperations()) { boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS); try { @@ -554,17 +534,17 @@ public class JsonPath { /** * Creates a new JsonPath and applies it to the provided Json object * - * @param jsonURL url pointing to json doc + // * @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 expected return type * @return list of objects matched by the given path */ - @SuppressWarnings({"unchecked"}) +// @SuppressWarnings({"unchecked"}) @Deprecated - public static T read(URL jsonURL, String jsonPath, Predicate... filters) throws IOException { - return new ParseContextImpl().parse(jsonURL).read(jsonPath, filters); - } +// public static T read(URL jsonURL, String jsonPath, Predicate... filters) throws IOException { +// return new ParseContextImpl().parse(jsonURL).read(jsonPath, filters); +// } /** * Creates a new JsonPath and applies it to the provided Json object diff --git a/json-path/src/main/java/com/jayway/jsonpath/PathEvaluator.java b/json-path/src/main/java/com/jayway/jsonpath/PathEvaluator.java new file mode 100644 index 00000000..33fa3366 --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/PathEvaluator.java @@ -0,0 +1,35 @@ +package com.jayway.jsonpath.internal; + +import com.jayway.jsonpath.Configuration; +import static com.jayway.jsonpath.internal.Utils.*; +import static com.jayway.jsonpath.Option.AS_PATH_LIST; + +public class PathEvaluator { + + private final Path path; + private final Configuration configuration; + + public PathEvaluator(Path path, Configuration configuration) { + notNull(path, "path can not be null"); + notNull(configuration, "configuration can not be null"); + this.path = path; + this.configuration = configuration; + } + + public T evaluate(Object jsonObject) { + boolean optAsPathList = configuration.containsOption(AS_PATH_LIST); + EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration); + + + return resultByConfiguration(jsonObject, evaluationContext); + } + + private T resultByConfiguration(Object jsonObject, EvaluationContext evaluationContext) { + if(configuration.containsOption(AS_PATH_LIST)){ + return (T)evaluationContext.getPathList(); + } else { + return (T) jsonObject; + } + } + +} \ No newline at end of file diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/Path.java b/json-path/src/main/java/com/jayway/jsonpath/internal/Path.java index 7decbd64..e66943da 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/Path.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/Path.java @@ -30,7 +30,7 @@ public interface Path { * @param configuration configuration to use * @return EvaluationContext containing results of evaluation */ - EvaluationContext evaluate(Object document, Object rootDocument, Configuration configuration); +// EvaluationContext evaluate(Object document, Object rootDocument, Configuration configuration); /** * Evaluates this path @@ -41,7 +41,7 @@ public interface Path { * @param forUpdate is this a read or a write operation * @return EvaluationContext containing results of evaluation */ - EvaluationContext evaluate(Object document, Object rootDocument, Configuration configuration, boolean forUpdate); +// EvaluationContext evaluate(Object document, Object rootDocument, Configuration configuration, boolean forUpdate); /** * diff --git a/json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonNodeJsonProvider.java b/json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonNodeJsonProvider.java index ae165927..9a54dcbb 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonNodeJsonProvider.java +++ b/json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonNodeJsonProvider.java @@ -212,18 +212,42 @@ public class JacksonJsonNodeJsonProvider extends AbstractJsonProvider { @Override public int length(Object obj) { + JsonLengthCalculator lengthCalculator = getLengthCalculator(obj); + return lengthCalculator.calculateLength(obj); + } + + private JsonLengthCalculator getLengthCalculator(Object obj) { if (isArray(obj)) { - return toJsonArray(obj).size(); + return new JsonArrayLengthCalculator(); } else if (isMap(obj)) { - return toJsonObject(obj).size(); + return new JsonObjectLengthCalculator(); } else { + return new TextNodeLengthCalculator(); + } + } + + public class JsonArrayLengthCalculator implements JsonLengthCalculator { + @Override + public int calculateLength(Object obj) { + return toJsonArray(obj).size(); + } + } + public class JsonObjectLengthCalculator implements JsonLengthCalculator { + @Override + public int calculateLength(Object obj) { + return toJsonObject(obj).size(); + } + } + + public class TextNodeLengthCalculator implements JsonLengthCalculator { + @Override + public int calculateLength(Object obj) { if (obj instanceof TextNode) { TextNode element = (TextNode) obj; return element.size(); } + throw new JsonPathException("length operation cannot be applied to " + (obj != null ? obj.getClass().getName() : "null")); } - throw new JsonPathException("length operation can not applied to " + (obj != null ? obj.getClass().getName() - : "null")); } @Override diff --git a/json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonLengthCalculator.java b/json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonLengthCalculator.java new file mode 100644 index 00000000..1056cdb0 --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonLengthCalculator.java @@ -0,0 +1,5 @@ +package com.jayway.jsonpath.spi.json; + +public interface JsonLengthCalculator { + int calculateLength(Object obj); +}