Browse Source

improvements

pull/1/merge
kalle 14 years ago
parent
commit
d77e302cb3
  1. 6
      README
  2. 2
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  3. 26
      json-path/src/main/java/com/jayway/jsonpath/eval/ExpressionEvaluator.java
  4. 5
      json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterChain.java
  5. 42
      json-path/src/main/java/com/jayway/jsonpath/filter/ListFilter.java
  6. 58
      json-path/src/test/java/com/jayway/jsonpath/ExpressionEvalTest.java
  7. 31
      json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java
  8. BIN
      jsonpath.png

6
README

@ -1 +1,5 @@
Java DSL for reading and testing JSON documents.
Java DSL for reading and testing JSON documents.
Santa Fe LET 50

2
json-path/src/main/java/com/jayway/jsonpath/JsonPath.java

@ -168,7 +168,7 @@ public class JsonPath {
if (log.isLoggable(Level.WARNING)) {
if (!PathUtil.isPathDefinite(jsonPath)) {
log.warning("Using readOne(...) on a not definite json path may give incorrect results.");
log.warning("Using readOne() on a not definite json path may give incorrect results. Path : " + jsonPath);
}
}

26
json-path/src/main/java/com/jayway/jsonpath/eval/Expression.java → json-path/src/main/java/com/jayway/jsonpath/eval/ExpressionEvaluator.java

@ -5,7 +5,7 @@ package com.jayway.jsonpath.eval;
* Date: 2/4/11
* Time: 9:21 PM
*/
public class Expression {
public class ExpressionEvaluator {
public static <T> boolean eval(T actual, String comparator, String expected) {
@ -35,17 +35,17 @@ public class Expression {
Integer e = Integer.parseInt(expected.trim());
if ("=".equals(comparator)) {
return a.longValue() == e.longValue();
return a.intValue() == e.intValue();
} else if ("!=".equals(comparator) || "<>".equals(comparator)) {
return a.longValue() != e.longValue();
return a.intValue() != e.intValue();
} else if (">".equals(comparator)) {
return a.longValue() > e.longValue();
return a.intValue() > e.intValue();
} else if (">=".equals(comparator)) {
return a.longValue() >= e.longValue();
return a.intValue() >= e.intValue();
} else if ("<".equals(comparator)) {
return a.longValue() < e.longValue();
return a.intValue() < e.intValue();
} else if ("<=".equals(comparator)) {
return a.longValue() <= e.longValue();
return a.intValue() <= e.intValue();
}
} else if (actual instanceof Double) {
@ -53,17 +53,17 @@ public class Expression {
Double e = Double.parseDouble(expected.trim());
if ("=".equals(comparator)) {
return a.longValue() == e.longValue();
return a.doubleValue() == e.doubleValue();
} else if ("!=".equals(comparator) || "<>".equals(comparator)) {
return a.longValue() != e.longValue();
return a.doubleValue() != e.doubleValue();
} else if (">".equals(comparator)) {
return a.longValue() > e.longValue();
return a.doubleValue() > e.doubleValue();
} else if (">=".equals(comparator)) {
return a.longValue() >= e.longValue();
return a.doubleValue() >= e.doubleValue();
} else if ("<".equals(comparator)) {
return a.longValue() < e.longValue();
return a.doubleValue() < e.doubleValue();
} else if ("<=".equals(comparator)) {
return a.longValue() <= e.longValue();
return a.doubleValue() <= e.doubleValue();
}
} else if (actual instanceof String) {

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

@ -1,8 +1,8 @@
package com.jayway.jsonpath.filter;
import com.jayway.jsonpath.InvalidPathException;
import org.json.simple.JSONArray;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
@ -37,6 +37,9 @@ public class JsonPathFilterChain {
List<Object> result = rootList;
for (JsonPathFilterBase filter : filters) {
if (filter == null) {
throw new InvalidPathException();
}
result = filter.apply(result);
}

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

@ -1,7 +1,7 @@
package com.jayway.jsonpath.filter;
import com.jayway.jsonpath.JsonUtil;
import com.jayway.jsonpath.eval.Expression;
import com.jayway.jsonpath.eval.ExpressionEvaluator;
import org.json.simple.JSONArray;
//import javax.script.ScriptEngine;
@ -13,8 +13,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* User: kalle stenflo
* Date: 2/2/11
* Time: 2:32 PM
*/
@ -22,14 +21,14 @@ 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_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*\\]");
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?\\]");
private static final Pattern LIST_ITEM_MATCHES_EVAL = Pattern.compile("\\[\\s?\\?\\s?\\(\\s?@.(\\w+)\\s?([=<>]+)\\s?(.*)\\s?\\)\\s?\\]"); //[ ?( @.title< 'ko' ) ]
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;
@ -106,6 +105,7 @@ public class ListFilter extends JsonPathFilterBase {
for (Object current : items) {
List array = JsonUtil.toList(current);
result.add(array.get(getTailIndex(array.size())));
}
return result;
@ -115,10 +115,12 @@ public class ListFilter extends JsonPathFilterBase {
List<Object> result = new JSONArray();
for (Object current : items) {
List target = JsonUtil.toList(current);
Integer[] index = getArrayIndex();
for (int i : index) {
result.add(JsonUtil.toList(current).get(i));
if(indexIsInRange(target, i)){
result.add(target.get(i));
}
}
}
return result;
@ -128,10 +130,12 @@ public class ListFilter extends JsonPathFilterBase {
List<Object> result = new JSONArray();
for (Object current : items) {
List target = JsonUtil.toList(current);
Integer[] index = getListPullIndex();
for (int i : index) {
result.add(JsonUtil.toList(current).get(i));
if(indexIsInRange(target, i)){
result.add(target.get(i));
}
}
}
return result;
@ -164,7 +168,7 @@ public class ListFilter extends JsonPathFilterBase {
System.out.println("EVAL" + expression);
return Expression.eval(propertyValue, operator, expected);
return ExpressionEvaluator.eval(propertyValue, operator, expected);
}
return false;
@ -230,4 +234,14 @@ public class ListFilter extends JsonPathFilterBase {
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;
}
}
}

58
json-path/src/test/java/com/jayway/jsonpath/ExpressionEvalTest.java

@ -1,6 +1,6 @@
package com.jayway.jsonpath;
import com.jayway.jsonpath.eval.Expression;
import com.jayway.jsonpath.eval.ExpressionEvaluator;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
@ -18,46 +18,46 @@ public class ExpressionEvalTest {
@Test
public void long_eval() throws Exception {
assertTrue(Expression.eval(1L, "=", "1"));
assertTrue(Expression.eval(2L, "!=", "1"));
assertTrue(Expression.eval(2L, ">", "1"));
assertTrue(Expression.eval(2L, ">=", "1"));
assertTrue(Expression.eval(2L, ">=", "2"));
assertTrue(Expression.eval(1L, "<", "2"));
assertTrue(Expression.eval(2L, "<=", "2"));
assertTrue(ExpressionEvaluator.eval(1L, "=", "1"));
assertTrue(ExpressionEvaluator.eval(2L, "!=", "1"));
assertTrue(ExpressionEvaluator.eval(2L, ">", "1"));
assertTrue(ExpressionEvaluator.eval(2L, ">=", "1"));
assertTrue(ExpressionEvaluator.eval(2L, ">=", "2"));
assertTrue(ExpressionEvaluator.eval(1L, "<", "2"));
assertTrue(ExpressionEvaluator.eval(2L, "<=", "2"));
assertFalse(Expression.eval(1, ">", "2"));
assertFalse(Expression.eval(1, ">=", "2"));
assertFalse(Expression.eval(2, "<", "1"));
assertFalse(Expression.eval(2, "<=", "1"));
assertFalse(Expression.eval(1, "=", "2"));
assertFalse(Expression.eval(1, "!=", "1"));
assertFalse(ExpressionEvaluator.eval(1, ">", "2"));
assertFalse(ExpressionEvaluator.eval(1, ">=", "2"));
assertFalse(ExpressionEvaluator.eval(2, "<", "1"));
assertFalse(ExpressionEvaluator.eval(2, "<=", "1"));
assertFalse(ExpressionEvaluator.eval(1, "=", "2"));
assertFalse(ExpressionEvaluator.eval(1, "!=", "1"));
}
@Test
public void double_eval() throws Exception {
assertTrue(Expression.eval(1D, "=", "1"));
assertTrue(Expression.eval(2D, "!=", "1"));
assertTrue(Expression.eval(2D, ">", "1"));
assertTrue(Expression.eval(2D, ">=", "1"));
assertTrue(Expression.eval(2D, ">=", "2"));
assertTrue(Expression.eval(1D, "<", "2"));
assertTrue(Expression.eval(2D, "<=", "2"));
assertTrue(ExpressionEvaluator.eval(1D, "=", "1"));
assertTrue(ExpressionEvaluator.eval(2D, "!=", "1"));
assertTrue(ExpressionEvaluator.eval(2D, ">", "1"));
assertTrue(ExpressionEvaluator.eval(2D, ">=", "1"));
assertTrue(ExpressionEvaluator.eval(2D, ">=", "2"));
assertTrue(ExpressionEvaluator.eval(1D, "<", "2"));
assertTrue(ExpressionEvaluator.eval(2D, "<=", "2"));
assertFalse(Expression.eval(1D, ">", "2"));
assertFalse(Expression.eval(1D, ">=", "2"));
assertFalse(Expression.eval(2D, "<", "1"));
assertFalse(Expression.eval(2D, "<=", "1"));
assertFalse(Expression.eval(1D, "=", "2"));
assertFalse(Expression.eval(1D, "!=", "1"));
assertFalse(ExpressionEvaluator.eval(1D, ">", "2"));
assertFalse(ExpressionEvaluator.eval(1D, ">=", "2"));
assertFalse(ExpressionEvaluator.eval(2D, "<", "1"));
assertFalse(ExpressionEvaluator.eval(2D, "<=", "1"));
assertFalse(ExpressionEvaluator.eval(1D, "=", "2"));
assertFalse(ExpressionEvaluator.eval(1D, "!=", "1"));
}
@Test
public void string_eval() throws Exception {
assertTrue(Expression.eval("A", "=", "A"));
assertTrue(Expression.eval("B", "!=", "A"));
assertTrue(ExpressionEvaluator.eval("A", "=", "A"));
assertTrue(ExpressionEvaluator.eval("B", "!=", "A"));
}

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

@ -7,7 +7,6 @@ import java.util.List;
import java.util.Map;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasItems;
import static org.junit.Assert.*;
@ -57,7 +56,7 @@ public class JsonPathTest {
List<Object> list = JsonPath.read(DOCUMENT, "$.store");
assertEquals(2, ((Map)list.get(0)).values().size());
assertEquals(2, ((Map) list.get(0)).values().size());
}
@ -68,7 +67,7 @@ public class JsonPathTest {
JsonPath path = JsonPath.compile("$.store.book[1]");
List<Object> list = path.read(DOCUMENT);
List<Map> list = path.read(DOCUMENT);
System.out.println(list.toString());
@ -124,6 +123,7 @@ public class JsonPathTest {
assertThat(JsonPath.<String>read(DOCUMENT, "$.store.book[0,1].author"), hasItems("Nigel Rees", "Evelyn Waugh"));
assertTrue(JsonPath.<String>read(DOCUMENT, "$.store.book[0,1].author").size() == 2);
}
@Test
public void read_store_book_pull_first_2() throws Exception {
@ -146,7 +146,7 @@ public class JsonPathTest {
}
@Test
@Test
public void all_books_with_category_reference() throws Exception {
assertThat(JsonPath.<String>read(DOCUMENT, "$..book[?(@.category = 'reference')].title"), hasItems("Sayings of the Century"));
@ -162,4 +162,27 @@ public class JsonPathTest {
System.out.println(all.toString());
}
@Test
public void access_index_out_of_bounds_does_not_throw_exception() throws Exception {
List<Object> res = JsonPath.read(DOCUMENT, "$.store.book[100].author");
assertTrue(res.isEmpty());
res = JsonPath.read(DOCUMENT, "$.store.book[100, 200].author");
assertTrue(res.isEmpty());
}
@Test(expected = InvalidPathException.class)
public void invalid_space_path_throws_exception() throws Exception {
JsonPath.read(DOCUMENT, "space is not good");
}
@Test(expected = InvalidPathException.class)
public void invalid_new_path_throws_exception() throws Exception {
JsonPath.read(DOCUMENT, "new ");
}
}

BIN
jsonpath.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

Loading…
Cancel
Save