Browse Source

Added an append function such that JSON can be appended to the current document and then a function executed over the result.

pull/167/head
Matthew J Greenwood 9 years ago
parent
commit
5d8e209141
  1. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/function/PathFunctionFactory.java
  2. 31
      json-path/src/main/java/com/jayway/jsonpath/internal/function/json/Append.java
  3. 5
      json-path/src/test/java/com/jayway/jsonpath/internal/function/NestedFunctionTest.java

2
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.InvalidPathException;
import com.jayway.jsonpath.internal.function.http.HttpLoader; 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.Average;
import com.jayway.jsonpath.internal.function.numeric.Max; import com.jayway.jsonpath.internal.function.numeric.Max;
import com.jayway.jsonpath.internal.function.numeric.Min; import com.jayway.jsonpath.internal.function.numeric.Min;
@ -47,6 +48,7 @@ public class PathFunctionFactory {
// JSON Entity Functions // JSON Entity Functions
map.put("length", Length.class); map.put("length", Length.class);
map.put("size", Length.class); map.put("size", Length.class);
map.put("append", Append.class);
FUNCTIONS = Collections.unmodifiableMap(map); FUNCTIONS = Collections.unmodifiableMap(map);

31
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<Parameter> 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;
}
}

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

@ -69,4 +69,9 @@ public class NestedFunctionTest extends BaseFunctionTest {
public void testLoadFunction() { public void testLoadFunction() {
verifyTextFunction(conf, "$.getjson($.urls[0])[0].total", 264); 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);
}
} }

Loading…
Cancel
Save