Browse Source

Checking if Node Exists - Bracket Notation Syntax #131

pull/100/merge
Kalle Stenflo 9 years ago
parent
commit
18ddbe90b4
  1. 6
      json-path/src/main/java/com/jayway/jsonpath/Criteria.java
  2. 1
      json-path/src/main/java/com/jayway/jsonpath/internal/token/PathToken.java
  3. 11
      json-path/src/test/java/com/jayway/jsonpath/InlineFilterTest.java
  4. 6
      json-path/src/test/java/com/jayway/jsonpath/old/FilterTest.java
  5. 96
      json-path/src/test/java/com/jayway/jsonpath/old/IssuesTest.java

6
json-path/src/main/java/com/jayway/jsonpath/Criteria.java

@ -445,11 +445,11 @@ public class Criteria implements Predicate {
boolean exists = ((Boolean) right); boolean exists = ((Boolean) right);
try { try {
Configuration c = Configuration.builder().jsonProvider(ctx.configuration().jsonProvider()).options(Option.REQUIRE_PROPERTIES).build(); Configuration c = Configuration.builder().jsonProvider(ctx.configuration().jsonProvider()).options(Option.REQUIRE_PROPERTIES).build();
Object value = ((Path) left).evaluate(ctx.item(), ctx.root(), c).getValue(); Object value = ((Path) left).evaluate(ctx.item(), ctx.root(), c).getValue(false);
if (exists) { if (exists) {
return (value != null); return (value != JsonProvider.UNDEFINED);
} else { } else {
return (value == null); return (value == JsonProvider.UNDEFINED);
} }
} catch (PathNotFoundException e) { } catch (PathNotFoundException e) {
return !exists; return !exists;

1
json-path/src/main/java/com/jayway/jsonpath/internal/token/PathToken.java

@ -53,7 +53,6 @@ public abstract class PathToken {
} else { } else {
throw new PathNotFoundException("No results for path: " + evalPath); throw new PathNotFoundException("No results for path: " + evalPath);
} }
} }
} else { } else {
if(!isUpstreamDefinite() && if(!isUpstreamDefinite() &&

11
json-path/src/test/java/com/jayway/jsonpath/InlineFilterTest.java

@ -152,10 +152,15 @@ public class InlineFilterTest extends BaseTest {
ints.add(3); ints.add(3);
List<Integer> notNull = JsonPath.parse(ints).read("$[?(@)]"); List<Integer> hits = JsonPath.parse(ints).read("$[?(@)]");
assertThat(notNull).containsExactly(0,1,2,3); assertThat(hits).containsExactly(0,1,null,2,3);
hits = JsonPath.parse(ints).read("$[?(@ != null)]");
assertThat(hits).containsExactly(0,1,2,3);
List<Integer> isNull = JsonPath.parse(ints).read("$[?(!@)]"); List<Integer> isNull = JsonPath.parse(ints).read("$[?(!@)]");
assertThat(isNull).containsExactly(new Integer[]{null}); assertThat(isNull).containsExactly(new Integer[]{});
assertThat(isNull).containsExactly(new Integer[]{});
} }
} }

6
json-path/src/test/java/com/jayway/jsonpath/old/FilterTest.java

@ -2,9 +2,7 @@ package com.jayway.jsonpath.old;
import com.jayway.jsonpath.BaseTest; import com.jayway.jsonpath.BaseTest;
import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.Criteria;
import com.jayway.jsonpath.Filter; import com.jayway.jsonpath.Filter;
import com.jayway.jsonpath.InvalidCriteriaException;
import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Predicate; import com.jayway.jsonpath.Predicate;
import com.jayway.jsonpath.spi.json.JsonProvider; import com.jayway.jsonpath.spi.json.JsonProvider;
@ -201,8 +199,8 @@ public class FilterTest extends BaseTest {
assertTrue(filter(where("foo").exists(true)).apply(createPredicateContext(check))); assertTrue(filter(where("foo").exists(true)).apply(createPredicateContext(check)));
assertFalse(filter(where("foo").exists(false)).apply(createPredicateContext(check))); assertFalse(filter(where("foo").exists(false)).apply(createPredicateContext(check)));
assertTrue(filter(where("foo_null").exists(false)).apply(createPredicateContext(check))); assertTrue(filter(where("foo_null").exists(true)).apply(createPredicateContext(check)));
assertFalse(filter(where("foo_null").exists(true)).apply(createPredicateContext(check))); assertFalse(filter(where("foo_null").exists(false)).apply(createPredicateContext(check)));
assertTrue(filter(where("bar").exists(false)).apply(createPredicateContext(check))); assertTrue(filter(where("bar").exists(false)).apply(createPredicateContext(check)));
assertFalse(filter(where("bar").exists(true)).apply(createPredicateContext(check))); assertFalse(filter(where("bar").exists(true)).apply(createPredicateContext(check)));

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

@ -17,7 +17,6 @@ import com.jayway.jsonpath.spi.mapper.GsonMappingProvider;
import com.jayway.jsonpath.spi.mapper.MappingException; import com.jayway.jsonpath.spi.mapper.MappingException;
import net.minidev.json.JSONAware; import net.minidev.json.JSONAware;
import net.minidev.json.parser.JSONParser; import net.minidev.json.parser.JSONParser;
import org.assertj.core.api.Assertions;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
import org.junit.Test; import org.junit.Test;
@ -33,6 +32,7 @@ import static com.jayway.jsonpath.Filter.filter;
import static com.jayway.jsonpath.JsonPath.read; import static com.jayway.jsonpath.JsonPath.read;
import static junit.framework.Assert.assertNull; import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown; import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -175,7 +175,7 @@ public class IssuesTest extends BaseTest {
List<String> result = read(json, "$[?(@.compatible == true)].sku"); List<String> result = read(json, "$[?(@.compatible == true)].sku");
Assertions.assertThat(result).containsExactly("SKU-005", "SKU-003"); assertThat(result).containsExactly("SKU-005", "SKU-003");
} }
@ -259,7 +259,7 @@ public class IssuesTest extends BaseTest {
public void issue_22b() throws Exception { public void issue_22b() throws Exception {
String json = "{\"a\":[{\"b\":1,\"c\":2},{\"b\":5,\"c\":2}]}"; String json = "{\"a\":[{\"b\":1,\"c\":2},{\"b\":5,\"c\":2}]}";
List<Object> res = JsonPath.using(Configuration.defaultConfiguration().setOptions(Option.DEFAULT_PATH_LEAF_TO_NULL)).parse(json).read("a[?(@.b==5)].d"); List<Object> res = JsonPath.using(Configuration.defaultConfiguration().setOptions(Option.DEFAULT_PATH_LEAF_TO_NULL)).parse(json).read("a[?(@.b==5)].d");
Assertions.assertThat(res).hasSize(1).containsNull(); assertThat(res).hasSize(1).containsNull();
} }
@Test(expected = PathNotFoundException.class) @Test(expected = PathNotFoundException.class)
@ -334,8 +334,8 @@ public class IssuesTest extends BaseTest {
List<Map<String, Object>> result = read(json, "$.store.book[?(@.author.age == 36)]"); List<Map<String, Object>> result = read(json, "$.store.book[?(@.author.age == 36)]");
Assertions.assertThat(result).hasSize(1); assertThat(result).hasSize(1);
Assertions.assertThat(result.get(0)).containsEntry("title", "Sayings of the Century"); assertThat(result.get(0)).containsEntry("title", "Sayings of the Century");
} }
@Test @Test
@ -381,7 +381,7 @@ public class IssuesTest extends BaseTest {
List<Map<String, String>> result = read(json, "$.list[?(@.name == 'My (String)')]"); List<Map<String, String>> result = read(json, "$.list[?(@.name == 'My (String)')]");
Assertions.assertThat(result).containsExactly(Collections.singletonMap("name", "My (String)")); assertThat(result).containsExactly(Collections.singletonMap("name", "My (String)"));
} }
@Test @Test
@ -389,9 +389,9 @@ public class IssuesTest extends BaseTest {
String json = "{\"test\":null}"; String json = "{\"test\":null}";
Assertions.assertThat(read(json, "test")).isNull(); assertThat(read(json, "test")).isNull();
Assertions.assertThat(JsonPath.using(Configuration.defaultConfiguration().setOptions(Option.SUPPRESS_EXCEPTIONS)).parse(json).read("nonExistingProperty")).isNull(); assertThat(JsonPath.using(Configuration.defaultConfiguration().setOptions(Option.SUPPRESS_EXCEPTIONS)).parse(json).read("nonExistingProperty")).isNull();
try { try {
read(json, "nonExistingProperty"); read(json, "nonExistingProperty");
@ -416,7 +416,7 @@ public class IssuesTest extends BaseTest {
public void issue_45() { public void issue_45() {
String json = "{\"rootkey\":{\"sub.key\":\"value\"}}"; String json = "{\"rootkey\":{\"sub.key\":\"value\"}}";
Assertions.assertThat(read(json, "rootkey['sub.key']")).isEqualTo("value"); assertThat(read(json, "rootkey['sub.key']")).isEqualTo("value");
} }
@Test @Test
@ -426,14 +426,14 @@ public class IssuesTest extends BaseTest {
String json = "{\"a\": {}}"; String json = "{\"a\": {}}";
Configuration configuration = Configuration.defaultConfiguration().setOptions(Option.SUPPRESS_EXCEPTIONS); Configuration configuration = Configuration.defaultConfiguration().setOptions(Option.SUPPRESS_EXCEPTIONS);
Assertions.assertThat(JsonPath.using(configuration).parse(json).read("a.x")).isNull(); assertThat(JsonPath.using(configuration).parse(json).read("a.x")).isNull();
try { try {
read(json, "a.x"); read(json, "a.x");
failBecauseExceptionWasNotThrown(PathNotFoundException.class); failBecauseExceptionWasNotThrown(PathNotFoundException.class);
} catch (PathNotFoundException e) { } catch (PathNotFoundException e) {
Assertions.assertThat(e).hasMessage("No results for path: $['a']['x']"); assertThat(e).hasMessage("No results for path: $['a']['x']");
} }
} }
@ -449,7 +449,7 @@ public class IssuesTest extends BaseTest {
List<String> result = JsonPath.read(json, "$.a.*.b.*.c"); List<String> result = JsonPath.read(json, "$.a.*.b.*.c");
Assertions.assertThat(result).containsExactly("foo"); assertThat(result).containsExactly("foo");
} }
@ -505,7 +505,7 @@ public class IssuesTest extends BaseTest {
List<String> problems = JsonPath.read(json, "$..narratives[?(@.lastRule==true)].message"); List<String> problems = JsonPath.read(json, "$..narratives[?(@.lastRule==true)].message");
Assertions.assertThat(problems).containsExactly("Chain does not have a discovery event. Possible it was cut by the date that was picked", "No start transcoding events found"); assertThat(problems).containsExactly("Chain does not have a discovery event. Possible it was cut by the date that was picked", "No start transcoding events found");
} }
//http://stackoverflow.com/questions/28596324/jsonpath-filtering-api //http://stackoverflow.com/questions/28596324/jsonpath-filtering-api
@ -567,7 +567,7 @@ public class IssuesTest extends BaseTest {
List<String> result = JsonPath.read(json, "$.logs[?(@.message == 'it\\'s here')].message"); List<String> result = JsonPath.read(json, "$.logs[?(@.message == 'it\\'s here')].message");
Assertions.assertThat(result).containsExactly("it's here"); assertThat(result).containsExactly("it's here");
} }
@Test @Test
@ -600,7 +600,7 @@ public class IssuesTest extends BaseTest {
List<String> res = JsonPath.read(json, "$.c.*.url[2]"); List<String> res = JsonPath.read(json, "$.c.*.url[2]");
Assertions.assertThat(res).containsExactly("url5"); assertThat(res).containsExactly("url5");
} }
@Test @Test
@ -614,7 +614,7 @@ public class IssuesTest extends BaseTest {
} }
Thread.sleep(2000); Thread.sleep(2000);
Assertions.assertThat(cache.size()).isEqualTo(200); assertThat(cache.size()).isEqualTo(200);
} }
@Test @Test
@ -657,12 +657,12 @@ public class IssuesTest extends BaseTest {
cache.get("6"); cache.get("6");
Assertions.assertThat(cache.getSilent("6")).isNotNull(); assertThat(cache.getSilent("6")).isNotNull();
Assertions.assertThat(cache.getSilent("5")).isNotNull(); assertThat(cache.getSilent("5")).isNotNull();
Assertions.assertThat(cache.getSilent("4")).isNotNull(); assertThat(cache.getSilent("4")).isNotNull();
Assertions.assertThat(cache.getSilent("3")).isNotNull(); assertThat(cache.getSilent("3")).isNotNull();
Assertions.assertThat(cache.getSilent("2")).isNotNull(); assertThat(cache.getSilent("2")).isNotNull();
Assertions.assertThat(cache.getSilent("1")).isNull(); assertThat(cache.getSilent("1")).isNull();
} }
@Test @Test
@ -690,7 +690,7 @@ public class IssuesTest extends BaseTest {
List<String> categories = context.read("$..category", List.class); List<String> categories = context.read("$..category", List.class);
Assertions.assertThat(categories).containsOnly("fiction"); assertThat(categories).containsOnly("fiction");
} }
@ -736,10 +736,10 @@ public class IssuesTest extends BaseTest {
Filter parsed = Filter.parse(filterAsString); Filter parsed = Filter.parse(filterAsString);
Assertions.assertThat(orig.apply(createPredicateContext(match))).isTrue(); assertThat(orig.apply(createPredicateContext(match))).isTrue();
Assertions.assertThat(parsed.apply(createPredicateContext(match))).isTrue(); assertThat(parsed.apply(createPredicateContext(match))).isTrue();
Assertions.assertThat(orig.apply(createPredicateContext(noMatch))).isFalse(); assertThat(orig.apply(createPredicateContext(noMatch))).isFalse();
Assertions.assertThat(parsed.apply(createPredicateContext(noMatch))).isFalse(); assertThat(parsed.apply(createPredicateContext(noMatch))).isFalse();
} }
private PredicateContext createPredicateContext(final Map<String, Integer> map){ private PredicateContext createPredicateContext(final Map<String, Integer> map){
@ -769,12 +769,46 @@ public class IssuesTest extends BaseTest {
@Test @Test
public void issue_131() { public void issue_131() {
String json = "[1, 2, {\"d\": { \"random\":null, \"date\": \"1234\"} , \"l\": \"filler\"}]"; String json = "[\n" +
//String json = "[1, 2, {\"d\": { \"random\":1, \"date\": \"1234\"} , \"l\": \"filler\"}]"; " {\n" +
" \"foo\": \"1\"\n" +
" },\n" +
" {\n" +
" \"foo\": null\n" +
" },\n" +
" {\n" +
" \"xxx\": null\n" +
" }\n" +
"]";
Object read = JsonPath.read(json, "$[2]['d'][?(@.random)]['date']"); List<Map<String, String>> result = JsonPath.read(json, "$[?(@.foo)]");
System.out.println(read); assertThat(result).extracting("foo").containsExactly("1", null);
}
@Test
public void issue_131_2() {
String json = "[\n" +
" {\n" +
" \"foo\": { \"bar\" : \"0\"}\n" +
" },\n" +
" {\n" +
" \"foo\": null\n" +
" },\n" +
" {\n" +
" \"xxx\": null\n" +
" }\n" +
"]";
List<String> result = JsonPath.read(json, "$[?(@.foo != null)].foo.bar");
assertThat(result).containsExactly("0");
result = JsonPath.read(json, "$[?(@.foo.bar)].foo.bar");
assertThat(result).containsExactly("0");
} }
} }

Loading…
Cancel
Save