Kalle Stenflo
13 years ago
24 changed files with 288 additions and 84 deletions
@ -0,0 +1,44 @@ |
|||||||
|
package com.jayway.jsonpath.internal.filter; |
||||||
|
|
||||||
|
import com.jayway.jsonpath.Filter; |
||||||
|
import com.jayway.jsonpath.spi.JsonProvider; |
||||||
|
|
||||||
|
import java.util.LinkedList; |
||||||
|
import java.util.List; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by IntelliJ IDEA. |
||||||
|
* User: kallestenflo |
||||||
|
* Date: 3/5/12 |
||||||
|
* Time: 4:41 PM |
||||||
|
*/ |
||||||
|
public class ArrayQueryFilter extends PathTokenFilter { |
||||||
|
|
||||||
|
ArrayQueryFilter(String condition) { |
||||||
|
super(condition); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Object filter(Object obj, JsonProvider jsonProvider, LinkedList<Filter> filters, boolean inArrayContext) { |
||||||
|
|
||||||
|
Filter filter = filters.poll(); |
||||||
|
|
||||||
|
return filter.doFilter((List<Map<String, Object>>) obj); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Object filter(Object obj, JsonProvider jsonProvider) { |
||||||
|
throw new UnsupportedOperationException(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Object getRef(Object obj, JsonProvider jsonProvider) { |
||||||
|
throw new UnsupportedOperationException(""); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean isArrayFilter() { |
||||||
|
return true; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,117 @@ |
|||||||
|
package com.jayway.jsonpath; |
||||||
|
|
||||||
|
import org.junit.Test; |
||||||
|
|
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.List; |
||||||
|
import java.util.Map; |
||||||
|
import java.util.regex.Pattern; |
||||||
|
|
||||||
|
import static com.jayway.jsonpath.Criteria.where; |
||||||
|
import static com.jayway.jsonpath.Filter.filter; |
||||||
|
import static java.util.Arrays.asList; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by IntelliJ IDEA. |
||||||
|
* User: kallestenflo |
||||||
|
* Date: 3/5/12 |
||||||
|
* Time: 4:24 PM |
||||||
|
*/ |
||||||
|
public class JsonPathFilterTest { |
||||||
|
|
||||||
|
public final static String DOCUMENT = |
||||||
|
"{ \"store\": {\n" + |
||||||
|
" \"book\": [ \n" + |
||||||
|
" { \"category\": \"reference\",\n" + |
||||||
|
" \"author\": \"Nigel Rees\",\n" + |
||||||
|
" \"title\": \"Sayings of the Century\",\n" + |
||||||
|
" \"price\": 8.95\n" + |
||||||
|
" },\n" + |
||||||
|
" { \"category\": \"fiction\",\n" + |
||||||
|
" \"author\": \"Evelyn Waugh\",\n" + |
||||||
|
" \"title\": \"Sword of Honour\",\n" + |
||||||
|
" \"price\": 12.99\n" + |
||||||
|
" },\n" + |
||||||
|
" { \"category\": \"fiction\",\n" + |
||||||
|
" \"author\": \"Herman Melville\",\n" + |
||||||
|
" \"title\": \"Moby Dick\",\n" + |
||||||
|
" \"isbn\": \"0-553-21311-3\",\n" + |
||||||
|
" \"price\": 8.99\n" + |
||||||
|
" },\n" + |
||||||
|
" { \"category\": \"fiction\",\n" + |
||||||
|
" \"author\": \"J. R. R. Tolkien\",\n" + |
||||||
|
" \"title\": \"The Lord of the Rings\",\n" + |
||||||
|
" \"isbn\": \"0-395-19395-8\",\n" + |
||||||
|
" \"price\": 22.99\n" + |
||||||
|
" }\n" + |
||||||
|
" ],\n" + |
||||||
|
" \"bicycle\": {\n" + |
||||||
|
" \"color\": \"red\",\n" + |
||||||
|
" \"price\": 19.95,\n" + |
||||||
|
" \"foo:bar\": \"fooBar\",\n" + |
||||||
|
" \"dot.notation\": \"new\"\n" + |
||||||
|
" }\n" + |
||||||
|
" }\n" + |
||||||
|
"}"; |
||||||
|
|
||||||
|
@Test |
||||||
|
public void a_path_can_use_filters() throws Exception { |
||||||
|
|
||||||
|
Filter lowPricedBooksFilter = filter(where("price").lt(10)); |
||||||
|
|
||||||
|
List read = JsonPath.read(DOCUMENT, "store.book[?]", lowPricedBooksFilter); |
||||||
|
|
||||||
|
System.out.println(read.size()); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Test |
||||||
|
public void a_path_can_use_many_filters() throws Exception { |
||||||
|
|
||||||
|
|
||||||
|
Map<String, Object> rootGrandChild_A = new HashMap<String, Object>(); |
||||||
|
rootGrandChild_A.put("name", "rootGrandChild_A"); |
||||||
|
|
||||||
|
Map<String, Object> rootGrandChild_B = new HashMap<String, Object>(); |
||||||
|
rootGrandChild_B.put("name", "rootGrandChild_B"); |
||||||
|
|
||||||
|
Map<String, Object> rootGrandChild_C = new HashMap<String, Object>(); |
||||||
|
rootGrandChild_C.put("name", "rootGrandChild_C"); |
||||||
|
|
||||||
|
|
||||||
|
Map<String, Object> rootChild_A = new HashMap<String, Object>(); |
||||||
|
rootChild_A.put("name", "rootChild_A"); |
||||||
|
rootChild_A.put("children", asList(rootGrandChild_A, rootGrandChild_B, rootGrandChild_C)); |
||||||
|
|
||||||
|
Map<String, Object> rootChild_B = new HashMap<String, Object>(); |
||||||
|
rootChild_B.put("name", "rootChild_B"); |
||||||
|
rootChild_B.put("children", asList(rootGrandChild_A, rootGrandChild_B, rootGrandChild_C)); |
||||||
|
|
||||||
|
Map<String, Object> rootChild_C = new HashMap<String, Object>(); |
||||||
|
rootChild_C.put("name", "rootChild_C"); |
||||||
|
rootChild_C.put("children", asList(rootGrandChild_A, rootGrandChild_B, rootGrandChild_C)); |
||||||
|
|
||||||
|
Map<String, Object> root = new HashMap<String, Object>(); |
||||||
|
root.put("children", asList(rootChild_A, rootChild_B, rootChild_C)); |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Filter customFilter = new Filter() { |
||||||
|
@Override |
||||||
|
public boolean apply(Map<String, Object> map) { |
||||||
|
if(map.get("name").equals("rootGrandChild_A")){ |
||||||
|
return true; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
Filter rootChildFilter = filter(where("name").regex(Pattern.compile("rootChild_[A|B]"))); |
||||||
|
Filter rootGrandChildFilter = filter(where("name").regex(Pattern.compile("rootGrandChild_[A|B]"))); |
||||||
|
|
||||||
|
List read = JsonPath.read(root, "children[?].children[?][?]", rootChildFilter, rootGrandChildFilter, customFilter); |
||||||
|
|
||||||
|
System.out.println(read.size()); |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue