Browse Source

convert lists and maps for ValueNode (fixes #378)

pull/381/head
Jochen Berger 7 years ago
parent
commit
ed9fd3adf7
  1. 12
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java
  2. 28
      json-path/src/test/java/com/jayway/jsonpath/old/IssuesTest.java

12
json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java

@ -359,19 +359,19 @@ public abstract class ValueNode {
}
public boolean isArray(Predicate.PredicateContext ctx) {
return ctx.configuration().jsonProvider().isArray(parse(ctx));
return parse(ctx) instanceof List;
}
public boolean isMap(Predicate.PredicateContext ctx) {
return ctx.configuration().jsonProvider().isMap(parse(ctx));
return parse(ctx) instanceof Map;
}
public int length(Predicate.PredicateContext ctx) {
return isArray(ctx) ? ctx.configuration().jsonProvider().length(parse(ctx)) : -1;
return isArray(ctx) ? ((List<?>) parse(ctx)).size() : -1;
}
public boolean isEmpty(Predicate.PredicateContext ctx) {
if (isArray(ctx) || isMap(ctx)) return ctx.configuration().jsonProvider().length(parse(ctx)) == 0;
if (isArray(ctx) || isMap(ctx)) return ((Collection<?>) parse(ctx)).size() == 0;
else if((parse(ctx) instanceof String)) return ((String)parse(ctx)).length() == 0;
return true;
}
@ -844,8 +844,8 @@ public abstract class ValueNode {
else if (res instanceof String) return ValueNode.createStringNode(res.toString(), false);
else if (res instanceof Boolean) return ValueNode.createBooleanNode(res.toString());
else if (res == null) return ValueNode.NULL_NODE;
else if (ctx.configuration().jsonProvider().isArray(res)) return ValueNode.createJsonNode(res);
else if (ctx.configuration().jsonProvider().isMap(res)) return ValueNode.createJsonNode(res);
else if (ctx.configuration().jsonProvider().isArray(res)) return ValueNode.createJsonNode(ctx.configuration().mappingProvider().map(res, List.class, ctx.configuration()));
else if (ctx.configuration().jsonProvider().isMap(res)) return ValueNode.createJsonNode(ctx.configuration().mappingProvider().map(res, Map.class, ctx.configuration()));
else throw new JsonPathException("Could not convert " + res.toString() + " to a ValueNode");
} catch (PathNotFoundException e) {
return ValueNode.UNDEFINED;

28
json-path/src/test/java/com/jayway/jsonpath/old/IssuesTest.java

@ -13,8 +13,10 @@ import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.internal.Utils;
import com.jayway.jsonpath.spi.cache.LRUCache;
import com.jayway.jsonpath.spi.json.GsonJsonProvider;
import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider;
import com.jayway.jsonpath.spi.json.JsonProvider;
import com.jayway.jsonpath.spi.mapper.GsonMappingProvider;
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
import com.jayway.jsonpath.spi.mapper.MappingException;
import net.minidev.json.JSONAware;
import net.minidev.json.parser.JSONParser;
@ -1019,4 +1021,30 @@ public class IssuesTest extends BaseTest {
assertThat(doc.read("$.jsonArr[0].name")).isEqualTo("nOne");
assertThat(doc.read("$.jsonArr[1].name")).isEqualTo("Jayway");
}
@Test
public void issue_378(){
String json = "{\n" +
" \"nodes\": {\n" +
" \"unnamed1\": {\n" +
" \"ntpServers\": [\n" +
" \"1.2.3.4\"\n" +
" ]\n" +
" }\n" +
" }\n" +
"}";
Configuration configuration = Configuration.builder()
.jsonProvider(new JacksonJsonNodeJsonProvider())
.mappingProvider(new JacksonMappingProvider())
.build();
DocumentContext ctx = JsonPath.using(configuration).parse(json);
String path = "$.nodes[*][?(!([\"1.2.3.4\"] subsetof @.ntpServers))].ntpServers";
JsonPath jsonPath = JsonPath.compile(path);
ctx.read(jsonPath);
}
}

Loading…
Cancel
Save