Browse Source

refactored filter compiler

pull/1/merge
kalle 14 years ago
parent
commit
d87fab1cca
  1. 49
      json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterFactory.java
  2. 73
      json-path/src/main/java/com/jayway/jsonpath/filter/ListEvalFilter.java
  3. 258
      json-path/src/main/java/com/jayway/jsonpath/filter/ListFilter.java
  4. 67
      json-path/src/main/java/com/jayway/jsonpath/filter/ListFrontFilter.java
  5. 72
      json-path/src/main/java/com/jayway/jsonpath/filter/ListIndexFilter.java
  6. 52
      json-path/src/main/java/com/jayway/jsonpath/filter/ListPropertyFilter.java
  7. 56
      json-path/src/main/java/com/jayway/jsonpath/filter/ListTailFilter.java
  8. 19
      json-path/src/main/java/com/jayway/jsonpath/filter/ListWildcardFilter.java
  9. 48
      json-path/src/main/java/com/jayway/jsonpath/filter/PropertyFilter.java
  10. 3
      json-path/src/main/java/com/jayway/jsonpath/filter/RootFilter.java
  11. 3
      json-path/src/main/java/com/jayway/jsonpath/filter/TraverseFilter.java
  12. 39
      json-path/src/main/java/com/jayway/jsonpath/filter/WildcardPropertyFilter.java

49
json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterFactory.java

@ -1,7 +1,5 @@
package com.jayway.jsonpath.filter;
import java.util.regex.Pattern;
/**
* User: kallestenflo
* Date: 2/2/11
@ -9,34 +7,31 @@ import java.util.regex.Pattern;
*/
public class JsonPathFilterFactory {
private final static Pattern ROOT_FILTER_PATTERN = Pattern.compile("\\$");
//private final static Pattern PROPERTY_FILTER_PATTERN = Pattern.compile("(\\w+)|\\['(\\w+)'\\]");
private final static Pattern PROPERTY_FILTER_PATTERN = Pattern.compile("(.*)|\\['(.*?)'\\]");
private final static Pattern WILDCARD_PROPERTY_FILTER_PATTERN = Pattern.compile("\\*");
private final static Pattern LIST_FILTER_PATTERN = Pattern.compile("\\[.*?\\]");
private final static Pattern TRAVERSE_FILTER_PATTERN = Pattern.compile("\\.\\.");
private final static RootFilter ROOT_FILTER = new RootFilter();
private final static TraverseFilter TRAVERSE_FILTER = new TraverseFilter();
public static JsonPathFilterBase createFilter(String pathFragment){
if(ROOT_FILTER_PATTERN.matcher(pathFragment).matches()){
return ROOT_FILTER;
}
else if(LIST_FILTER_PATTERN.matcher(pathFragment).matches()){
return new ListFilter(pathFragment);
}
else if(TRAVERSE_FILTER_PATTERN.matcher(pathFragment).matches()){
return TRAVERSE_FILTER;
}
else if(PROPERTY_FILTER_PATTERN.matcher(pathFragment).matches() || WILDCARD_PROPERTY_FILTER_PATTERN.matcher(pathFragment).matches() ){
public static JsonPathFilterBase createFilter(String pathFragment) {
if (RootFilter.PATTERN.matcher(pathFragment).matches()) {
return new RootFilter();
} else if (ListIndexFilter.PATTERN.matcher(pathFragment).matches()) {
return new ListIndexFilter(pathFragment);
} else if (ListFrontFilter.PATTERN.matcher(pathFragment).matches()) {
return new ListFrontFilter(pathFragment);
} else if (ListWildcardFilter.PATTERN.matcher(pathFragment).matches()) {
return new ListWildcardFilter();
} else if (ListTailFilter.PATTERN.matcher(pathFragment).matches()) {
return new ListTailFilter(pathFragment);
} else if (ListPropertyFilter.PATTERN.matcher(pathFragment).matches()) {
return new ListPropertyFilter(pathFragment);
} else if (ListEvalFilter.PATTERN.matcher(pathFragment).matches()) {
return new ListEvalFilter(pathFragment);
} else if (TraverseFilter.PATTERN.matcher(pathFragment).matches()) {
return new TraverseFilter();
} else if (WildcardPropertyFilter.PATTERN.matcher(pathFragment).matches()) {
return new WildcardPropertyFilter();
} else if (PropertyFilter.PATTERN.matcher(pathFragment).matches()) {
return new PropertyFilter(pathFragment);
}
return null;
}
}

73
json-path/src/main/java/com/jayway/jsonpath/filter/ListEvalFilter.java

@ -0,0 +1,73 @@
package com.jayway.jsonpath.filter;
import com.jayway.jsonpath.JsonUtil;
import com.jayway.jsonpath.eval.ExpressionEvaluator;
import org.json.simple.JSONArray;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 2/15/11
* Time: 8:27 PM
*/
public class ListEvalFilter extends JsonPathFilterBase {
public static final Pattern PATTERN = Pattern.compile("\\[\\s?\\?\\s?\\(\\s?@.(\\w+)\\s?([=<>]+)\\s?(.*)\\s?\\)\\s?\\]"); //[?( @.title< 'ko')]
private final String pathFragment;
public ListEvalFilter(String pathFragment) {
this.pathFragment = pathFragment;
}
@Override
public FilterOutput apply(FilterOutput filterItems) {
List<Object> result = new JSONArray();
for (Object item : filterItems.getResultAsList()) {
if (isMatch(item)) {
result.add(item);
}
}
return new FilterOutput(result);
}
private boolean isMatch(Object check) {
Matcher matcher = PATTERN.matcher(pathFragment);
if (matcher.matches()) {
String property = matcher.group(1);
String operator = matcher.group(2);
String expected = matcher.group(3);
if (!JsonUtil.isMap(check)) {
return false;
}
Map obj = JsonUtil.toMap(check);
if (!obj.containsKey(property)) {
return false;
}
Object propertyValue = obj.get(property);
if (JsonUtil.isContainer(propertyValue)) {
return false;
}
String expression = propertyValue + " " + operator + " " + expected;
return ExpressionEvaluator.eval(propertyValue, operator, expected);
}
return false;
}
}

258
json-path/src/main/java/com/jayway/jsonpath/filter/ListFilter.java

@ -1,258 +0,0 @@
package com.jayway.jsonpath.filter;
import com.jayway.jsonpath.JsonUtil;
import com.jayway.jsonpath.eval.ExpressionEvaluator;
import org.json.simple.JSONArray;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//import javax.script.ScriptEngine;
//import javax.script.ScriptEngineManager;
/**
* User: kalle stenflo
* Date: 2/2/11
* Time: 2:32 PM
*/
public class ListFilter extends JsonPathFilterBase {
//private static ScriptEngine SCRIPT_ENGINE = new ScriptEngineManager().getEngineByName("js");
private static final Pattern LIST_INDEX_PATTERN = Pattern.compile("\\[(\\s?\\d+\\s?,?)+\\]"); //[1] OR [1,2,3]
private static final Pattern LIST_PULL_PATTERN = Pattern.compile("\\[\\s?:(\\d+)\\s?\\]"); //[ :2 ]
private static final Pattern LIST_WILDCARD_PATTERN = Pattern.compile("\\[\\*\\]"); //[*]
private static final Pattern LIST_TAIL_PATTERN_SHORT = Pattern.compile("\\[\\s*-\\s*(\\d+):\\s*\\]"); // [(@.length - 12)] OR [-13:]
private static final Pattern LIST_TAIL_PATTERN_LONG = Pattern.compile("\\[\\s*\\(\\s*@\\.length\\s*-\\s*(\\d+)\\s*\\)\\s*\\]"); //[(@.length-1)]
private static final Pattern LIST_TAIL_PATTERN = Pattern.compile("(" + LIST_TAIL_PATTERN_SHORT.pattern() + "|" + LIST_TAIL_PATTERN_LONG.pattern() + ")");
private static final Pattern LIST_ITEM_HAS_PROPERTY_PATTERN = Pattern.compile("\\[\\s?\\?\\s?\\(\\s?@\\.(\\w+)\\s?\\)\\s?\\]"); //[?(@.title)]
private static final Pattern LIST_ITEM_MATCHES_EVAL = Pattern.compile("\\[\\s?\\?\\s?\\(\\s?@.(\\w+)\\s?([=<>]+)\\s?(.*)\\s?\\)\\s?\\]"); //[?( @.title< 'ko')]
private final String pathFragment;
public ListFilter(String pathFragment) {
this.pathFragment = pathFragment;
}
@Override
public FilterOutput apply(FilterOutput items) {
Object result = new JSONArray();
if (LIST_INDEX_PATTERN.matcher(pathFragment).matches()) {
result = filterByListIndex(items.getResultAsList());
} else if (LIST_WILDCARD_PATTERN.matcher(pathFragment).matches()) {
result = filterByWildcard(items.getResultAsList());
} else if (LIST_TAIL_PATTERN.matcher(pathFragment).matches()) {
result = filterByListTailIndex(items.getResultAsList());
} else if (LIST_PULL_PATTERN.matcher(pathFragment).matches()) {
result = filterByPullIndex(items.getResultAsList());
} else if (LIST_ITEM_HAS_PROPERTY_PATTERN.matcher(pathFragment).matches()) {
result = filterByItemProperty(items.getResultAsList());
} else if (LIST_ITEM_MATCHES_EVAL.matcher(pathFragment).matches()) {
result = filterByItemEvalMatch(items.getResultAsList());
}
return new FilterOutput(result);
}
private List<Object> filterByItemEvalMatch(List<Object> items) {
List<Object> result = new JSONArray();
for (Object current : items) {
for (Object item : items) {
if (isEvalMatch(item)) {
result.add(item);
}
}
}
return result;
}
private List<Object> filterByItemProperty(List<Object> items) {
List<Object> result = new JSONArray();
String prop = getFilterProperty();
//for (Object current : items) {
for (Object item : JsonUtil.toList(items)) {
if (JsonUtil.isMap(item)) {
if (JsonUtil.toMap(item).containsKey(prop)) {
result.add(item);
}
}
}
//}
return result;
}
private List<Object> filterByWildcard(List<Object> items) {
List<Object> result = new JSONArray();
for (Object current : items) {
if(current instanceof List){
result.addAll(JsonUtil.toList(current));
}
else {
result.add(current);
}
}
return result;
}
private Object filterByListTailIndex(List<Object> items) {
//for (Object current : items) {
// Map array = JsonUtil.toMap(current);
int index = getTailIndex(items.size());
return items.get(index);
}
private Object filterByListIndex(List<Object> items) {
Object result = null;
//for (Object current : items) {
//List target = JsonUtil.toList(current);
Integer[] index = getArrayIndex();
if (index.length > 1) {
List<Object> tmp = new JSONArray();
for (int i : index) {
if (indexIsInRange(items, i)) {
tmp.add(items.get(i));
}
}
return result = tmp;
} else {
if (indexIsInRange(items, index[0])) {
result = items.get(index[0]);
}
}
//}
return result;
}
private List<Object> filterByPullIndex(List<Object> items) {
List<Object> result = new JSONArray();
//for (Object current : items) {
//List target = JsonUtil.toList(current);
Integer[] index = getListPullIndex();
for (int i : index) {
if (indexIsInRange(items, i)) {
result.add(items.get(i));
}
}
//}
return result;
}
private boolean isEvalMatch(Object check) {
Matcher matcher = LIST_ITEM_MATCHES_EVAL.matcher(pathFragment);
if (matcher.matches()) {
String property = matcher.group(1);
String operator = matcher.group(2);
String expected = matcher.group(3);
if (!JsonUtil.isMap(check)) {
return false;
}
Map obj = JsonUtil.toMap(check);
if (!obj.containsKey(property)) {
return false;
}
Object propertyValue = obj.get(property);
if (JsonUtil.isContainer(propertyValue)) {
return false;
}
String expression = propertyValue + " " + operator + " " + expected;
return ExpressionEvaluator.eval(propertyValue, operator, expected);
}
return false;
}
private String getFilterProperty() {
Matcher matcher = LIST_ITEM_HAS_PROPERTY_PATTERN.matcher(pathFragment);
if (matcher.matches()) {
return matcher.group(1);
}
throw new IllegalArgumentException("invalid list filter property");
}
private int getTailIndex(int arraySize) {
Matcher matcher = LIST_TAIL_PATTERN_SHORT.matcher(pathFragment);
if (matcher.matches()) {
int index = Integer.parseInt(matcher.group(1));
return arraySize - index;
}
matcher = LIST_TAIL_PATTERN_LONG.matcher(pathFragment);
if (matcher.matches()) {
int index = Integer.parseInt(matcher.group(1));
return arraySize - index;
}
throw new IllegalArgumentException("invalid list index");
}
private Integer[] getListPullIndex() {
Matcher matcher = LIST_PULL_PATTERN.matcher(pathFragment);
if (matcher.matches()) {
int pullCount = Integer.parseInt(matcher.group(1));
List<Integer> result = new LinkedList<Integer>();
for (int y = 0; y < pullCount; y++) {
result.add(y);
}
return result.toArray(new Integer[0]);
}
throw new IllegalArgumentException("invalid list index");
}
private Integer[] getArrayIndex() {
String prepared = pathFragment.replaceAll(" ", "");
prepared = prepared.substring(1, prepared.length() - 1);
List<Integer> index = new LinkedList<Integer>();
String[] split = prepared.split(",");
for (String s : split) {
index.add(Integer.parseInt(s));
}
return index.toArray(new Integer[0]);
}
private boolean indexIsInRange(List list, int index) {
if (index < 0) {
return false;
} else if (index > list.size() - 1) {
return false;
} else {
return true;
}
}
}

67
json-path/src/main/java/com/jayway/jsonpath/filter/ListFrontFilter.java

@ -0,0 +1,67 @@
package com.jayway.jsonpath.filter;
import org.json.simple.JSONArray;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 2/15/11
* Time: 8:20 PM
*/
public class ListFrontFilter extends JsonPathFilterBase {
public static final Pattern PATTERN = Pattern.compile("\\[\\s?:(\\d+)\\s?\\]"); //[ :2 ]
private final String pathFragment;
public ListFrontFilter(String pathFragment) {
this.pathFragment = pathFragment;
}
@Override
public FilterOutput apply(FilterOutput filterItems) {
List<Object> result = new JSONArray();
Integer[] index = getListPullIndex();
for (int i : index) {
if (indexIsInRange(filterItems.getResultAsList(), i)) {
result.add(filterItems.getResultAsList().get(i));
}
}
return new FilterOutput(result);
}
private Integer[] getListPullIndex() {
Matcher matcher = PATTERN.matcher(pathFragment);
if (matcher.matches()) {
int pullCount = Integer.parseInt(matcher.group(1));
List<Integer> result = new LinkedList<Integer>();
for (int y = 0; y < pullCount; y++) {
result.add(y);
}
return result.toArray(new Integer[0]);
}
throw new IllegalArgumentException("invalid list index");
}
private boolean indexIsInRange(List list, int index) {
if (index < 0) {
return false;
} else if (index > list.size() - 1) {
return false;
} else {
return true;
}
}
}

72
json-path/src/main/java/com/jayway/jsonpath/filter/ListIndexFilter.java

@ -0,0 +1,72 @@
package com.jayway.jsonpath.filter;
import org.json.simple.JSONArray;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 2/15/11
* Time: 8:02 PM
*/
public class ListIndexFilter extends JsonPathFilterBase {
public static final Pattern PATTERN = Pattern.compile("\\[(\\s?\\d+\\s?,?)+\\]"); //[1] OR [1,2,3]
private final String pathFragment;
public ListIndexFilter(String pathFragment) {
this.pathFragment = pathFragment;
}
@Override
public FilterOutput apply(FilterOutput filterItems) {
Object result = null;
Integer[] index = getArrayIndex();
if (index.length > 1) {
List<Object> tmp = new JSONArray();
for (int i : index) {
if (indexIsInRange(filterItems.getResultAsList(), i)) {
tmp.add(filterItems.getResultAsList().get(i));
}
}
result = tmp;
} else {
if (indexIsInRange(filterItems.getResultAsList(), index[0])) {
result = filterItems.getResultAsList().get(index[0]);
}
}
return new FilterOutput(result);
}
private boolean indexIsInRange(List list, int index) {
if (index < 0) {
return false;
} else if (index > list.size() - 1) {
return false;
} else {
return true;
}
}
private Integer[] getArrayIndex() {
String prepared = pathFragment.replaceAll(" ", "");
prepared = prepared.substring(1, prepared.length() - 1);
List<Integer> index = new LinkedList<Integer>();
String[] split = prepared.split(",");
for (String s : split) {
index.add(Integer.parseInt(s));
}
return index.toArray(new Integer[0]);
}
}

52
json-path/src/main/java/com/jayway/jsonpath/filter/ListPropertyFilter.java

@ -0,0 +1,52 @@
package com.jayway.jsonpath.filter;
import com.jayway.jsonpath.JsonUtil;
import org.json.simple.JSONArray;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 2/15/11
* Time: 8:23 PM
*/
public class ListPropertyFilter extends JsonPathFilterBase {
public static final Pattern PATTERN = Pattern.compile("\\[\\s?\\?\\s?\\(\\s?@\\.(\\w+)\\s?\\)\\s?\\]"); //[?(@.title)]
private final String pathFragment;
public ListPropertyFilter(String pathFragment) {
this.pathFragment = pathFragment;
}
@Override
public FilterOutput apply(FilterOutput filterItems) {
List<Object> result = new JSONArray();
String prop = getFilterProperty();
for (Object item : filterItems.getResultAsList()) {
if (JsonUtil.isMap(item)) {
if (JsonUtil.toMap(item).containsKey(prop)) {
result.add(item);
}
}
}
return new FilterOutput(result);
}
private String getFilterProperty() {
Matcher matcher = PATTERN.matcher(pathFragment);
if (matcher.matches()) {
return matcher.group(1);
}
throw new IllegalArgumentException("invalid list filter property");
}
}

56
json-path/src/main/java/com/jayway/jsonpath/filter/ListTailFilter.java

@ -0,0 +1,56 @@
package com.jayway.jsonpath.filter;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 2/15/11
* Time: 8:16 PM
*/
public class ListTailFilter extends JsonPathFilterBase {
private static final Pattern LIST_TAIL_PATTERN_SHORT = Pattern.compile("\\[\\s*-\\s*(\\d+):\\s*\\]"); // [(@.length - 12)] OR [-13:]
private static final Pattern LIST_TAIL_PATTERN_LONG = Pattern.compile("\\[\\s*\\(\\s*@\\.length\\s*-\\s*(\\d+)\\s*\\)\\s*\\]"); //[(@.length-1)]
public static final Pattern PATTERN = Pattern.compile("(" + LIST_TAIL_PATTERN_SHORT.pattern() + "|" + LIST_TAIL_PATTERN_LONG.pattern() + ")");
private final String pathFragment;
public ListTailFilter(String pathFragment) {
this.pathFragment = pathFragment;
}
@Override
public FilterOutput apply(FilterOutput filterItems) {
int index = getTailIndex(filterItems.getResultAsList().size());
return new FilterOutput(filterItems.getResultAsList().get(index));
}
private int getTailIndex(int arraySize) {
Matcher matcher = LIST_TAIL_PATTERN_SHORT.matcher(pathFragment);
if (matcher.matches()) {
int index = Integer.parseInt(matcher.group(1));
return arraySize - index;
}
matcher = LIST_TAIL_PATTERN_LONG.matcher(pathFragment);
if (matcher.matches()) {
int index = Integer.parseInt(matcher.group(1));
return arraySize - index;
}
throw new IllegalArgumentException("invalid list index");
}
}

19
json-path/src/main/java/com/jayway/jsonpath/filter/ListWildcardFilter.java

@ -0,0 +1,19 @@
package com.jayway.jsonpath.filter;
import java.util.regex.Pattern;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 2/15/11
* Time: 8:09 PM
*/
public class ListWildcardFilter extends JsonPathFilterBase{
public static final Pattern PATTERN = Pattern.compile("\\[\\*\\]");
@Override
public FilterOutput apply(FilterOutput filterItems) {
return new FilterOutput(filterItems.getResultAsList());
}
}

48
json-path/src/main/java/com/jayway/jsonpath/filter/PropertyFilter.java

@ -4,6 +4,7 @@ import com.jayway.jsonpath.JsonUtil;
import org.json.simple.JSONArray;
import java.util.List;
import java.util.regex.Pattern;
/**
* Created by IntelliJ IDEA.
@ -13,7 +14,8 @@ import java.util.List;
*/
public class PropertyFilter extends JsonPathFilterBase {
private final static String WILDCARD = "*";
public final static Pattern PATTERN = Pattern.compile("(.*)|\\['(.*?)'\\]");
private final String pathFragment;
@ -26,41 +28,27 @@ public class PropertyFilter extends JsonPathFilterBase {
List<Object> result = new JSONArray();
if (WILDCARD.equals(pathFragment)) {
if (filter.isList()) {
for (Object current : filter.getResultAsList()) {
for (Object value : JsonUtil.toMap(current).values()) {
result.add(value);
if (filter.isList()) {
for (Object current : filter.getResultAsList()) {
if (JsonUtil.toMap(current).containsKey(pathFragment)) {
Object o = JsonUtil.toMap(current).get(pathFragment);
if (JsonUtil.isList(o)) {
result.addAll(JsonUtil.toList(o));
} else {
result.add(JsonUtil.toMap(current).get(pathFragment));
}
}
} else {
for (Object value : JsonUtil.toMap(filter.getResult()).values()) {
result.add(value);
}
}
return new FilterOutput(result);
} else {
if (filter.isList()) {
for (Object current : filter.getResultAsList()) {
if (JsonUtil.toMap(current).containsKey(pathFragment)) {
Object o = JsonUtil.toMap(current).get(pathFragment);
if (JsonUtil.isList(o)) {
result.addAll(JsonUtil.toList(o));
} else {
result.add(JsonUtil.toMap(current).get(pathFragment));
}
}
}
return new FilterOutput(result);
} else {
Object mapValue = null;
if (JsonUtil.toMap(filter.getResult()).containsKey(pathFragment)) {
mapValue = JsonUtil.toMap(filter.getResult()).get(pathFragment);
}
return new FilterOutput(mapValue);
Object mapValue = null;
if (JsonUtil.toMap(filter.getResult()).containsKey(pathFragment)) {
mapValue = JsonUtil.toMap(filter.getResult()).get(pathFragment);
}
return new FilterOutput(mapValue);
}
}
}

3
json-path/src/main/java/com/jayway/jsonpath/filter/RootFilter.java

@ -1,6 +1,7 @@
package com.jayway.jsonpath.filter;
import java.util.List;
import java.util.regex.Pattern;
/**
* Created by IntelliJ IDEA.
@ -10,6 +11,8 @@ import java.util.List;
*/
public class RootFilter extends JsonPathFilterBase{
public final static Pattern PATTERN = Pattern.compile("\\$");
@Override
public FilterOutput apply(FilterOutput root) {
return root;

3
json-path/src/main/java/com/jayway/jsonpath/filter/TraverseFilter.java

@ -4,6 +4,7 @@ import com.jayway.jsonpath.JsonUtil;
import org.json.simple.JSONArray;
import java.util.List;
import java.util.regex.Pattern;
/**
* Created by IntelliJ IDEA.
@ -13,6 +14,8 @@ import java.util.List;
*/
public class TraverseFilter extends JsonPathFilterBase {
public final static Pattern PATTERN = Pattern.compile("\\.\\.");
@Override
public FilterOutput apply(FilterOutput filter) {
List<Object> result = new JSONArray();

39
json-path/src/main/java/com/jayway/jsonpath/filter/WildcardPropertyFilter.java

@ -0,0 +1,39 @@
package com.jayway.jsonpath.filter;
import com.jayway.jsonpath.JsonUtil;
import org.json.simple.JSONArray;
import java.util.List;
import java.util.regex.Pattern;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 2/15/11
* Time: 8:42 PM
*/
public class WildcardPropertyFilter extends JsonPathFilterBase {
public final static Pattern PATTERN = Pattern.compile("\\*");
@Override
public FilterOutput apply(FilterOutput filter) {
List<Object> result = new JSONArray();
if (filter.isList()) {
for (Object current : filter.getResultAsList()) {
for (Object value : JsonUtil.toMap(current).values()) {
result.add(value);
}
}
} else {
for (Object value : JsonUtil.toMap(filter.getResult()).values()) {
result.add(value);
}
}
return new FilterOutput(result);
}
}
Loading…
Cancel
Save