Skip to content

Commit 759ecf3

Browse files
author
kalle
committed
implemented naive eval function to remove java ScriptEngine dependency (support for java 5)
1 parent bd8f5e8 commit 759ecf3

File tree

6 files changed

+176
-32
lines changed

6 files changed

+176
-32
lines changed

json-path/src/main/java/com/jayway/jsonpath/PathUtil.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ public static List<String> splitPath(String jsonPath) {
5353

5454
jsonPath = jsonPath.replace("..", ".~.")
5555
.replace("[", ".[")
56-
.replace("@.", "@");
56+
.replace("@.", "@")
57+
.replace("['", "")
58+
.replace("']", "");
5759

5860
String[] split = jsonPath.split("\\.");
5961

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.jayway.jsonpath.eval;
2+
3+
/**
4+
* User: kalle stenflo
5+
* Date: 2/4/11
6+
* Time: 9:21 PM
7+
*/
8+
public class Expression {
9+
10+
11+
public static <T> boolean eval(T actual, String comparator, String expected) {
12+
13+
comparator = comparator.trim();
14+
15+
if (actual instanceof Long) {
16+
17+
Long a = (Long) actual;
18+
Long e = Long.parseLong(expected.trim());
19+
20+
if ("=".equals(comparator)) {
21+
return a.longValue() == e.longValue();
22+
} else if ("!=".equals(comparator) || "<>".equals(comparator)) {
23+
return a.longValue() != e.longValue();
24+
} else if (">".equals(comparator)) {
25+
return a.longValue() > e.longValue();
26+
} else if (">=".equals(comparator)) {
27+
return a.longValue() >= e.longValue();
28+
} else if ("<".equals(comparator)) {
29+
return a.longValue() < e.longValue();
30+
} else if ("<=".equals(comparator)) {
31+
return a.longValue() <= e.longValue();
32+
}
33+
} else if (actual instanceof Integer) {
34+
Integer a = (Integer) actual;
35+
Integer e = Integer.parseInt(expected.trim());
36+
37+
if ("=".equals(comparator)) {
38+
return a.longValue() == e.longValue();
39+
} else if ("!=".equals(comparator) || "<>".equals(comparator)) {
40+
return a.longValue() != e.longValue();
41+
} else if (">".equals(comparator)) {
42+
return a.longValue() > e.longValue();
43+
} else if (">=".equals(comparator)) {
44+
return a.longValue() >= e.longValue();
45+
} else if ("<".equals(comparator)) {
46+
return a.longValue() < e.longValue();
47+
} else if ("<=".equals(comparator)) {
48+
return a.longValue() <= e.longValue();
49+
}
50+
} else if (actual instanceof Double) {
51+
52+
Double a = (Double) actual;
53+
Double e = Double.parseDouble(expected.trim());
54+
55+
if ("=".equals(comparator)) {
56+
return a.longValue() == e.longValue();
57+
} else if ("!=".equals(comparator) || "<>".equals(comparator)) {
58+
return a.longValue() != e.longValue();
59+
} else if (">".equals(comparator)) {
60+
return a.longValue() > e.longValue();
61+
} else if (">=".equals(comparator)) {
62+
return a.longValue() >= e.longValue();
63+
} else if ("<".equals(comparator)) {
64+
return a.longValue() < e.longValue();
65+
} else if ("<=".equals(comparator)) {
66+
return a.longValue() <= e.longValue();
67+
}
68+
} else if (actual instanceof String) {
69+
70+
String a = (String)actual;
71+
expected = expected.trim();
72+
if(expected.startsWith("'")) {
73+
expected = expected.substring(1);
74+
}
75+
if(expected.endsWith("'")){
76+
expected = expected.substring(0, expected.length()-1);
77+
}
78+
79+
if ("=".equals(comparator)) {
80+
return a.equals(expected);
81+
} else if ("!=".equals(comparator) || "<>".equals(comparator)) {
82+
return !a.equals(expected);
83+
}
84+
}
85+
86+
return false;
87+
}
88+
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
public class JsonPathFilterFactory {
1111

1212
private final static Pattern ROOT_FILTER_PATTERN = Pattern.compile("\\$");
13-
private final static Pattern PROPERTY_FILTER_PATTERN = Pattern.compile("\\w+");
13+
private final static Pattern PROPERTY_FILTER_PATTERN = Pattern.compile("(\\w+)|\\['(\\w+)'\\]");
14+
//private final static Pattern PROPERTY_FILTER_PATTERN = Pattern.compile("\\w+");
1415
private final static Pattern WILDCARD_PROPERTY_FILTER_PATTERN = Pattern.compile("\\*");
1516
private final static Pattern LIST_FILTER_PATTERN = Pattern.compile("\\[.*?\\]");
1617
private final static Pattern TRAVERSE_FILTER_PATTERN = Pattern.compile("\\.\\.");

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

+17-30
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.jayway.jsonpath.filter;
22

33
import com.jayway.jsonpath.JsonUtil;
4+
import com.jayway.jsonpath.eval.Expression;
45
import org.json.simple.JSONArray;
56

6-
import javax.script.ScriptEngine;
7-
import javax.script.ScriptEngineManager;
8-
import javax.script.ScriptException;
7+
//import javax.script.ScriptEngine;
8+
//import javax.script.ScriptEngineManager;
99
import java.util.LinkedList;
1010
import java.util.List;
1111
import java.util.Map;
@@ -20,7 +20,7 @@
2020
*/
2121
public class ListFilter extends JsonPathFilterBase {
2222

23-
private static ScriptEngine SCRIPT_ENGINE = new ScriptEngineManager().getEngineByName("js");
23+
//private static ScriptEngine SCRIPT_ENGINE = new ScriptEngineManager().getEngineByName("js");
2424

2525
private static final Pattern LIST_INDEX_PATTERN = Pattern.compile("\\[(\\s?\\d+\\s?,?)+\\]"); //[1] OR [1,2,3]
2626
private static final Pattern LIST_PULL_PATTERN = Pattern.compile("\\[\\s?:(\\d+)\\s?\\]"); //[ :2 ]
@@ -51,8 +51,7 @@ public List<Object> apply(List<Object> items) {
5151
return filterByPullIndex(items);
5252
} else if (LIST_ITEM_HAS_PROPERTY_PATTERN.matcher(pathFragment).matches()) {
5353
return filterByItemProperty(items);
54-
}
55-
else if (LIST_ITEM_MATCHES_EVAL.matcher(pathFragment).matches()) {
54+
} else if (LIST_ITEM_MATCHES_EVAL.matcher(pathFragment).matches()) {
5655
return filterByItemEvalMatch(items);
5756
}
5857

@@ -64,7 +63,7 @@ private List<Object> filterByItemEvalMatch(List<Object> items) {
6463

6564
for (Object current : items) {
6665
for (Object item : JsonUtil.toList(current)) {
67-
if(isEvalMatch(item)){
66+
if (isEvalMatch(item)) {
6867
result.add(item);
6968
}
7069
}
@@ -81,8 +80,8 @@ private List<Object> filterByItemProperty(List<Object> items) {
8180
for (Object current : items) {
8281
for (Object item : JsonUtil.toList(current)) {
8382

84-
if(JsonUtil.isMap(item)){
85-
if(JsonUtil.toMap(item).containsKey(prop)) {
83+
if (JsonUtil.isMap(item)) {
84+
if (JsonUtil.toMap(item).containsKey(prop)) {
8685
result.add(item);
8786
}
8887
}
@@ -142,43 +141,31 @@ private boolean isEvalMatch(Object check) {
142141
Matcher matcher = LIST_ITEM_MATCHES_EVAL.matcher(pathFragment);
143142

144143
if (matcher.matches()) {
145-
String property = matcher.group(1);
146-
String operator = matcher.group(2);
147-
String expected = matcher.group(3);
144+
String property = matcher.group(1);
145+
String operator = matcher.group(2);
146+
String expected = matcher.group(3);
148147

149-
if(!JsonUtil.isMap(check)){
148+
if (!JsonUtil.isMap(check)) {
150149
return false;
151150
}
152151
Map obj = JsonUtil.toMap(check);
153152

154-
if(!obj.containsKey(property)){
153+
if (!obj.containsKey(property)) {
155154
return false;
156155
}
157156

158157
Object propertyValue = obj.get(property);
159158

160-
if(JsonUtil.isContainer(propertyValue)){
159+
if (JsonUtil.isContainer(propertyValue)) {
161160
return false;
162161
}
163162

164-
165-
if(propertyValue instanceof String){
166-
propertyValue = "'" + propertyValue + "'";
167-
}
168-
169-
if(operator.trim().equals("=")){
170-
operator = "===";
171-
}
172-
173-
174163
String expression = propertyValue + " " + operator + " " + expected;
175164
System.out.println("EVAL" + expression);
176165

177-
try {
178-
return (Boolean) SCRIPT_ENGINE.eval(expression);
179-
} catch (ScriptException e) {
180-
return false;
181-
}
166+
167+
return Expression.eval(propertyValue, operator, expected);
168+
182169
}
183170
return false;
184171
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.jayway.jsonpath;
2+
3+
import com.jayway.jsonpath.eval.Expression;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertFalse;
7+
import static org.junit.Assert.assertTrue;
8+
9+
/**
10+
* Created by IntelliJ IDEA.
11+
* User: kallestenflo
12+
* Date: 2/4/11
13+
* Time: 9:32 PM
14+
*/
15+
public class ExpressionEvalTest {
16+
17+
18+
@Test
19+
public void long_eval() throws Exception {
20+
21+
assertTrue(Expression.eval(1L, "=", "1"));
22+
assertTrue(Expression.eval(2L, "!=", "1"));
23+
assertTrue(Expression.eval(2L, ">", "1"));
24+
assertTrue(Expression.eval(2L, ">=", "1"));
25+
assertTrue(Expression.eval(2L, ">=", "2"));
26+
assertTrue(Expression.eval(1L, "<", "2"));
27+
assertTrue(Expression.eval(2L, "<=", "2"));
28+
29+
assertFalse(Expression.eval(1, ">", "2"));
30+
assertFalse(Expression.eval(1, ">=", "2"));
31+
assertFalse(Expression.eval(2, "<", "1"));
32+
assertFalse(Expression.eval(2, "<=", "1"));
33+
assertFalse(Expression.eval(1, "=", "2"));
34+
assertFalse(Expression.eval(1, "!=", "1"));
35+
}
36+
37+
@Test
38+
public void double_eval() throws Exception {
39+
40+
assertTrue(Expression.eval(1D, "=", "1"));
41+
assertTrue(Expression.eval(2D, "!=", "1"));
42+
assertTrue(Expression.eval(2D, ">", "1"));
43+
assertTrue(Expression.eval(2D, ">=", "1"));
44+
assertTrue(Expression.eval(2D, ">=", "2"));
45+
assertTrue(Expression.eval(1D, "<", "2"));
46+
assertTrue(Expression.eval(2D, "<=", "2"));
47+
48+
assertFalse(Expression.eval(1D, ">", "2"));
49+
assertFalse(Expression.eval(1D, ">=", "2"));
50+
assertFalse(Expression.eval(2D, "<", "1"));
51+
assertFalse(Expression.eval(2D, "<=", "1"));
52+
assertFalse(Expression.eval(1D, "=", "2"));
53+
assertFalse(Expression.eval(1D, "!=", "1"));
54+
}
55+
56+
@Test
57+
public void string_eval() throws Exception {
58+
59+
assertTrue(Expression.eval("A", "=", "A"));
60+
assertTrue(Expression.eval("B", "!=", "A"));
61+
62+
}
63+
64+
65+
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public void read_store_book_wildcard() throws Exception {
8686
@Test
8787
public void read_store_book_author() throws Exception {
8888
assertThat(JsonPath.<String>read(DOCUMENT, "$.store.book[*].author"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien"));
89+
assertThat(JsonPath.<String>read(DOCUMENT, "$.['store'].['book'][*].['author']"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien"));
8990
}
9091

9192

0 commit comments

Comments
 (0)