Browse Source

Merge pull request #369 from jochenberger/fix-jacksonnode-empty

fix evaluation of `empty` with wrapping providers (fixes #366)
pull/337/merge
kallestenflo 8 years ago committed by GitHub
parent
commit
259beb94bd
  1. 9
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java
  2. 29
      json-path/src/test/java/com/jayway/jsonpath/JacksonJsonNodeJsonProviderTest.java

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

@ -359,20 +359,19 @@ public abstract class ValueNode {
}
public boolean isArray(Predicate.PredicateContext ctx) {
return parse(ctx) instanceof List;
return ctx.configuration().jsonProvider().isArray(parse(ctx));
}
public boolean isMap(Predicate.PredicateContext ctx) {
return parse(ctx) instanceof Map;
return ctx.configuration().jsonProvider().isMap(parse(ctx));
}
public int length(Predicate.PredicateContext ctx) {
return isArray(ctx) ? ((List) parse(ctx)).size() : -1;
return isArray(ctx) ? ctx.configuration().jsonProvider().length(parse(ctx)) : -1;
}
public boolean isEmpty(Predicate.PredicateContext ctx) {
if (isArray(ctx)) return ((List) parse(ctx)).size() == 0;
else if (isMap(ctx)) return ((Map) parse(ctx)).size() == 0;
if (isArray(ctx) || isMap(ctx)) return ctx.configuration().jsonProvider().length(parse(ctx)) == 0;
else if((parse(ctx) instanceof String)) return ((String)parse(ctx)).length() == 0;
return true;
}

29
json-path/src/test/java/com/jayway/jsonpath/JacksonJsonNodeJsonProviderTest.java

@ -110,6 +110,35 @@ public class JacksonJsonNodeJsonProviderTest extends BaseTest {
using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON).read("$", typeRef);
}
@Test
// https://github.com/json-path/JsonPath/issues/366
public void empty_array_check_works() throws IOException {
String json = "[" +
" {" +
" \"name\": \"a\"," +
" \"groups\": [{" +
" \"type\": \"phase\"," +
" \"name\": \"alpha\"" +
" }, {" +
" \"type\": \"not_phase\"," +
" \"name\": \"beta\"" +
" }]" +
" }, {" +
" \"name\": \"b\"," +
" \"groups\": [{" +
" \"type\": \"phase\"," +
" \"name\": \"beta\"" +
" }, {" +
" \"type\": \"not_phase\"," +
" \"name\": \"alpha\"" +
" }]" +
" }" +
"]";
ArrayNode node = using(JACKSON_JSON_NODE_CONFIGURATION).parse(json).read("$[?(@.groups[?(@.type == 'phase' && @.name == 'alpha')] empty false)]");
assertThat(node.size()).isEqualTo(1);
assertThat(node.get(0).get("name").asText()).isEqualTo("a");
}
public static class FooBarBaz<T> {
public T gen;
public String foo;

Loading…
Cancel
Save