From 5d8e2091410661e958493657eb70964d18cd8379 Mon Sep 17 00:00:00 2001 From: Matthew J Greenwood Date: Mon, 14 Dec 2015 07:16:17 -0500 Subject: [PATCH] Added an append function such that JSON can be appended to the current document and then a function executed over the result. --- .../function/PathFunctionFactory.java | 2 ++ .../internal/function/json/Append.java | 31 +++++++++++++++++++ .../internal/function/NestedFunctionTest.java | 5 +++ 3 files changed, 38 insertions(+) create mode 100644 json-path/src/main/java/com/jayway/jsonpath/internal/function/json/Append.java diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/function/PathFunctionFactory.java b/json-path/src/main/java/com/jayway/jsonpath/internal/function/PathFunctionFactory.java index 921f9e98..6414e0d9 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/function/PathFunctionFactory.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/function/PathFunctionFactory.java @@ -2,6 +2,7 @@ package com.jayway.jsonpath.internal.function; import com.jayway.jsonpath.InvalidPathException; import com.jayway.jsonpath.internal.function.http.HttpLoader; +import com.jayway.jsonpath.internal.function.json.Append; import com.jayway.jsonpath.internal.function.numeric.Average; import com.jayway.jsonpath.internal.function.numeric.Max; import com.jayway.jsonpath.internal.function.numeric.Min; @@ -47,6 +48,7 @@ public class PathFunctionFactory { // JSON Entity Functions map.put("length", Length.class); map.put("size", Length.class); + map.put("append", Append.class); FUNCTIONS = Collections.unmodifiableMap(map); diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/function/json/Append.java b/json-path/src/main/java/com/jayway/jsonpath/internal/function/json/Append.java new file mode 100644 index 00000000..805226e4 --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/function/json/Append.java @@ -0,0 +1,31 @@ +package com.jayway.jsonpath.internal.function.json; + +import com.jayway.jsonpath.internal.EvaluationContext; +import com.jayway.jsonpath.internal.PathRef; +import com.jayway.jsonpath.internal.function.Parameter; +import com.jayway.jsonpath.internal.function.PathFunction; +import com.jayway.jsonpath.spi.json.JsonProvider; + +import java.util.List; + +/** + * Appends JSON structure to the current document so that you can utilize the JSON added thru another function call. + * If there are multiple parameters then this function call will add each element that is json to the structure + * + * Created by mgreenwood on 12/14/15. + */ +public class Append implements PathFunction { + @Override + public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List parameters) { + JsonProvider jsonProvider = ctx.configuration().jsonProvider(); + if (parameters != null && parameters.size() > 0) { + for (Parameter param : parameters) { + if (jsonProvider.isArray(model)) { + int len = jsonProvider.length(model); + jsonProvider.setArrayIndex(model, len, param.getCachedValue()); + } + } + } + return model; + } +} diff --git a/json-path/src/test/java/com/jayway/jsonpath/internal/function/NestedFunctionTest.java b/json-path/src/test/java/com/jayway/jsonpath/internal/function/NestedFunctionTest.java index 38f50e68..1b36b362 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/internal/function/NestedFunctionTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/internal/function/NestedFunctionTest.java @@ -69,4 +69,9 @@ public class NestedFunctionTest extends BaseFunctionTest { public void testLoadFunction() { verifyTextFunction(conf, "$.getjson($.urls[0])[0].total", 264); } + + @Test + public void testAppendNumber() { + verifyMathFunction(conf, "$.numbers.append(11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 0).avg()", 10.0); + } }