4 changed files with 109 additions and 0 deletions
@ -0,0 +1,47 @@ |
|||||||
|
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; |
||||||
|
|
||||||
|
public class Join implements PathFunction { |
||||||
|
@Override |
||||||
|
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) { |
||||||
|
StringBuilder result = new StringBuilder(); |
||||||
|
|
||||||
|
List<String> strings = null; |
||||||
|
String separator = " "; |
||||||
|
if (null != parameters){ |
||||||
|
strings = Parameter.toList(String.class, ctx, parameters); |
||||||
|
if (strings.size() == 0) |
||||||
|
separator = " "; |
||||||
|
else |
||||||
|
separator = strings.remove(strings.size() - 1); |
||||||
|
} |
||||||
|
|
||||||
|
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()); |
||||||
|
result.append(separator); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if (null != strings){ |
||||||
|
for (String string : strings){ |
||||||
|
result.append(string); |
||||||
|
result.append(separator); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if (result.length() > 0) |
||||||
|
return result.delete(result.length() - separator.length(), result.length()).toString(); |
||||||
|
else |
||||||
|
return result; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,49 @@ |
|||||||
|
package com.jayway.jsonpath.internal.function; |
||||||
|
|
||||||
|
import com.jayway.jsonpath.Configuration; |
||||||
|
import com.jayway.jsonpath.Configurations; |
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
import org.junit.runners.Parameterized; |
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
|
||||||
|
@RunWith(Parameterized.class) |
||||||
|
public class Issue623 extends BaseFunctionTest{ |
||||||
|
private static final Logger logger = LoggerFactory.getLogger(NumericPathFunctionTest.class); |
||||||
|
private Configuration conf; |
||||||
|
|
||||||
|
@Parameterized.Parameters |
||||||
|
public static Iterable<Configuration> configurations() { |
||||||
|
return Configurations.configurations(); |
||||||
|
} |
||||||
|
|
||||||
|
public Issue623(Configuration conf) { |
||||||
|
logger.debug("Testing with configuration {}", conf.getClass().getName()); |
||||||
|
this.conf = conf; |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void joinFunctionSimpleTest() { |
||||||
|
verifyTextFunction(conf, "$.text.join()", "a b c d e f"); |
||||||
|
verifyTextFunction(conf, "$.text.join(\"\\, \")", "a, b, c, d, e, f"); |
||||||
|
verifyTextFunction(conf, "$.text.join(\"1\", \"2\", \"3\", \"-\")", "a-b-c-d-e-f-1-2-3"); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testJoinNestedTest() { |
||||||
|
String json = "{\"document\" : { " + |
||||||
|
"\"text\" : [ " + |
||||||
|
"{ \"value\": \"a\", \"int\": \"1\" }, " + |
||||||
|
"{ \"value\": \"b\", \"int\": \"2\" }, " + |
||||||
|
"{ \"value\": \"c\", \"int\": \"3\" }, " + |
||||||
|
"{ \"value\": \"d\", \"int\": \"4\" }, " + |
||||||
|
"{ \"value\": \"e\", \"int\": \"5\" }, " + |
||||||
|
"{ \"value\": \"f\", \"int\": \"6\" } ]}}"; |
||||||
|
|
||||||
|
verifyFunction(conf, "join(" + |
||||||
|
"$.join($.document.text[*]['value'], \"\t\"), " + |
||||||
|
"$.join($.document.text[*]['int'], \"\t\"), " + |
||||||
|
"\"\n\")", json, "a\tb\tc\td\te\tf\n1\t2\t3\t4\t5\t6"); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue