Skip to content

Commit 5d8e209

Browse files
Matthew J GreenwoodMatthew J Greenwood
authored andcommitted
Added an append function such that JSON can be appended to the current document and then a function executed over the result.
1 parent 01bdbe2 commit 5d8e209

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

json-path/src/main/java/com/jayway/jsonpath/internal/function/PathFunctionFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.jayway.jsonpath.InvalidPathException;
44
import com.jayway.jsonpath.internal.function.http.HttpLoader;
5+
import com.jayway.jsonpath.internal.function.json.Append;
56
import com.jayway.jsonpath.internal.function.numeric.Average;
67
import com.jayway.jsonpath.internal.function.numeric.Max;
78
import com.jayway.jsonpath.internal.function.numeric.Min;
@@ -47,6 +48,7 @@ public class PathFunctionFactory {
4748
// JSON Entity Functions
4849
map.put("length", Length.class);
4950
map.put("size", Length.class);
51+
map.put("append", Append.class);
5052

5153

5254
FUNCTIONS = Collections.unmodifiableMap(map);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.jayway.jsonpath.internal.function.json;
2+
3+
import com.jayway.jsonpath.internal.EvaluationContext;
4+
import com.jayway.jsonpath.internal.PathRef;
5+
import com.jayway.jsonpath.internal.function.Parameter;
6+
import com.jayway.jsonpath.internal.function.PathFunction;
7+
import com.jayway.jsonpath.spi.json.JsonProvider;
8+
9+
import java.util.List;
10+
11+
/**
12+
* Appends JSON structure to the current document so that you can utilize the JSON added thru another function call.
13+
* If there are multiple parameters then this function call will add each element that is json to the structure
14+
*
15+
* Created by mgreenwood on 12/14/15.
16+
*/
17+
public class Append implements PathFunction {
18+
@Override
19+
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
20+
JsonProvider jsonProvider = ctx.configuration().jsonProvider();
21+
if (parameters != null && parameters.size() > 0) {
22+
for (Parameter param : parameters) {
23+
if (jsonProvider.isArray(model)) {
24+
int len = jsonProvider.length(model);
25+
jsonProvider.setArrayIndex(model, len, param.getCachedValue());
26+
}
27+
}
28+
}
29+
return model;
30+
}
31+
}

json-path/src/test/java/com/jayway/jsonpath/internal/function/NestedFunctionTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,9 @@ public void testStringConcatWithJSONParameter() {
6969
public void testLoadFunction() {
7070
verifyTextFunction(conf, "$.getjson($.urls[0])[0].total", 264);
7171
}
72+
73+
@Test
74+
public void testAppendNumber() {
75+
verifyMathFunction(conf, "$.numbers.append(11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 0).avg()", 10.0);
76+
}
7277
}

0 commit comments

Comments
 (0)