Browse Source

allow to filter for existence of nested properties

pull/33/head
Jochen Berger 11 years ago
parent
commit
6efa648d17
  1. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterFactory.java
  2. 19
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/HasPathFilter.java
  3. 7
      json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java

2
json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterFactory.java

@ -64,7 +64,7 @@ public class FilterFactory {
return new ArrayEvalFilter(pathFragment); return new ArrayEvalFilter(pathFragment);
} else if (!pathFragment.contains("=") && !pathFragment.contains("<") && !pathFragment.contains(">")) { } else if (!pathFragment.contains("=") && !pathFragment.contains("<") && !pathFragment.contains(">")) {
//[?(@.isbn)] //[?(@.isbn)]
return new HasFieldFilter(pathFragment); return new HasPathFilter(pathFragment);
} else { } else {
throw new InvalidPathException("Failed to create PathTokenFilter for path fragment: " + pathFragment); throw new InvalidPathException("Failed to create PathTokenFilter for path fragment: " + pathFragment);
} }

19
json-path/src/main/java/com/jayway/jsonpath/internal/filter/HasFieldFilter.java → json-path/src/main/java/com/jayway/jsonpath/internal/filter/HasPathFilter.java

@ -15,18 +15,19 @@
package com.jayway.jsonpath.internal.filter; package com.jayway.jsonpath.internal.filter;
import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.spi.JsonProvider; import com.jayway.jsonpath.spi.JsonProvider;
import java.util.Collection;
/** /**
* @author Kalle Stenflo * @author Kalle Stenflo
*/ */
public class HasFieldFilter extends PathTokenFilter { public class HasPathFilter extends PathTokenFilter {
private final String trimmedCondition; private final JsonPath path;
public HasFieldFilter(String condition) { public HasPathFilter(String condition) {
super(condition); super(condition);
String trimmedCondition = condition; String trimmedCondition = condition;
@ -35,7 +36,7 @@ public class HasFieldFilter extends PathTokenFilter {
trimmedCondition = trimmedCondition.replace("']", ""); trimmedCondition = trimmedCondition.replace("']", "");
} }
this.trimmedCondition = trim(trimmedCondition, 5, 2); this.path = JsonPath.compile(trim(trimmedCondition, 5, 2));
} }
@Override @Override
@ -48,9 +49,11 @@ public class HasFieldFilter extends PathTokenFilter {
for (Object item : src) { for (Object item : src) {
if(jsonProvider.isMap(item)){ if(jsonProvider.isMap(item)){
Collection<String> keys = jsonProvider.getPropertyKeys(item); try{
if(keys.contains(trimmedCondition)){ path.read(item, Configuration.builder().options(Option.THROW_ON_MISSING_PROPERTY).jsonProvider(jsonProvider).build());
jsonProvider.setProperty(result, jsonProvider.length(result), item); jsonProvider.setProperty(result, jsonProvider.length(result), item);
} catch (PathNotFoundException e){
// the path was not found in the item
} }
} }
} }

7
json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java

@ -8,6 +8,7 @@ import java.util.Map;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.*; import static org.junit.Assert.*;
/** /**
@ -316,6 +317,12 @@ public class JsonPathTest {
} }
@Test
public void exists_filter_with_nested_path() throws Exception {
assertThat(JsonPath.<List<String>>read(DOCUMENT, "$..[?(@.bicycle.color)]"), hasSize(1));
assertThat(JsonPath.<List<String>>read(DOCUMENT, "$..[?(@.bicycle.numberOfGears)]"), hasSize(0));
}
} }

Loading…
Cancel
Save