-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Open
Description
Hello!
I want to concatanate several json paths separated by a string constant with the concat function:
String json =
"""
{
"one": "one",
"two": "two"
}
""";
ReadContext readContext =
JsonPath.using(
Configuration.builder()
.jsonProvider(new JacksonJsonNodeJsonProvider())
.mappingProvider(new JacksonMappingProvider())
.build())
.parse(json);
Object result = readContext.read("concat($.one, \"-\", $.two)");
assertThat(result).isEqualTo("one-two");
but instead of "one-two" there is the "one"-"two" in result.
How to fix it to get correct result?
It seems to me it is because of com.fasterxml.jackson.databind.node.TextNode.toString:
public String toString() {
return InternalNodeMapper.nodeToString(this);
}
which used in the Parameter.consume where the value argument is com.fasterxml.jackson.databind.node.TextNode:
public static void consume(Class expectedType, EvaluationContext ctx, Collection collection, Object value) {
if (ctx.configuration().jsonProvider().isArray(value)) {
for(Object o : ctx.configuration().jsonProvider().toIterable(value)) {
if (o != null && expectedType.isAssignableFrom(o.getClass())) {
collection.add(o);
} else if (o != null && expectedType == String.class) {
collection.add(o.toString()); // here is o.toString() returns ""-"", instead of "-"
}
}
} else if (value != null && expectedType.isAssignableFrom(value.getClass())) {
collection.add(value);
} else if (value != null && expectedType == String.class) {
collection.add(value.toString());
}
}
JsonLateBindingValue.getValue calls JacksonJsonNodeJsonProvider.parse:
public Object get() {
return this.jsonProvider.parse(this.jsonParameter.getJson());
}
JacksonJsonNodeJsonProvider.parse returns TextNode:
public Object parse(String json) throws InvalidJsonException {
try {
return this.objectMapper.readTree(json);
} catch (IOException e) {
throw new InvalidJsonException(e, json);
}
}
I use json-path 2.10.0 based on Jackson:
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.10.0</version>
</dependency>
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels