Browse Source

Test for string, added concat function

pull/167/head
Matthew J Greenwood 9 years ago
parent
commit
f127edabe1
  1. 6
      json-path/src/main/java/com/jayway/jsonpath/internal/function/PathFunctionFactory.java
  2. 37
      json-path/src/main/java/com/jayway/jsonpath/internal/function/text/Concatenate.java
  3. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/function/text/Length.java
  4. 4
      json-path/src/test/java/com/jayway/jsonpath/internal/function/BaseFunctionTest.java
  5. 10
      json-path/src/test/java/com/jayway/jsonpath/internal/function/NestedFunctionTest.java

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

@ -6,6 +6,8 @@ import com.jayway.jsonpath.internal.function.numeric.Max;
import com.jayway.jsonpath.internal.function.numeric.Min;
import com.jayway.jsonpath.internal.function.numeric.StandardDeviation;
import com.jayway.jsonpath.internal.function.numeric.Sum;
import com.jayway.jsonpath.internal.function.text.Concatenate;
import com.jayway.jsonpath.internal.function.text.Length;
import java.util.Collections;
import java.util.HashMap;
@ -35,10 +37,14 @@ public class PathFunctionFactory {
map.put("min", Min.class);
map.put("max", Max.class);
// Text Functions
map.put("concat", Concatenate.class);
// JSON Entity Functions
map.put("length", Length.class);
map.put("size", Length.class);
FUNCTIONS = Collections.unmodifiableMap(map);
}

37
json-path/src/main/java/com/jayway/jsonpath/internal/function/text/Concatenate.java

@ -0,0 +1,37 @@
package com.jayway.jsonpath.internal.function.text;
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 java.util.List;
/**
* String function concat - simple takes a list of arguments and/or an array and concatenates them together to form a
* single string
*
* Created by mgreenwood on 12/11/15.
*/
public class Concatenate implements PathFunction {
@Override
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
StringBuffer result = new StringBuffer();
if(ctx.configuration().jsonProvider().isArray(model)){
Iterable<?> objects = ctx.configuration().jsonProvider().toIterable(model);
for (Object obj : objects) {
if (obj instanceof String) {
result.append(obj.toString());
}
}
}
if (parameters != null) {
for (Parameter param : parameters) {
if (param.getCachedValue() != null) {
result.append(param.getCachedValue().toString());
}
}
}
return result.toString();
}
}

4
json-path/src/main/java/com/jayway/jsonpath/internal/function/Length.java → json-path/src/main/java/com/jayway/jsonpath/internal/function/text/Length.java

@ -1,7 +1,9 @@
package com.jayway.jsonpath.internal.function;
package com.jayway.jsonpath.internal.function.text;
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 java.util.List;

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

@ -39,6 +39,10 @@ public class BaseFunctionTest {
verifyFunction(conf, pathExpr, NUMBER_SERIES, expectedValue);
}
protected void verifyTextFunction(Configuration conf, String pathExpr, Object expectedValue) {
verifyFunction(conf, pathExpr, TEXT_SERIES, expectedValue);
}
protected String getResourceAsText(String resourceName) throws IOException {
return new Scanner(BaseFunctionTest.class.getResourceAsStream(resourceName), "UTF-8").useDelimiter("\\A").next();
}

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

@ -54,4 +54,14 @@ public class NestedFunctionTest extends BaseFunctionTest {
public void testJsonInnerArgumentArray() {
verifyMathFunction(conf, "$.sum(5, 3, $.numbers.max(), 2)", 20.0);
}
@Test
public void testStringConcat() {
verifyTextFunction(conf, "$.text.concat()", "abcdef");
}
@Test
public void testStringConcatWithJSONParameter() {
verifyTextFunction(conf, "$.text.concat(\"-\", \"ghijk\")", "abcdef-ghijk");
}
}

Loading…
Cancel
Save