Browse Source

Major code clean up.

pull/3/head
kalle 13 years ago
parent
commit
62cdd166c0
  1. 34
      json-path-assert/src/main/java/com/jayway/jsonassert/JsonAssert.java
  2. 4
      json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsCollectionWithSize.java
  3. 5
      json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingKey.java
  4. 4
      json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingValue.java
  5. 3
      json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java
  6. 25
      json-path/src/main/java/com/jayway/jsonpath/InvalidJsonException.java
  7. 71
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  8. 61
      json-path/src/main/java/com/jayway/jsonpath/JsonUtil.java
  9. 45
      json-path/src/main/java/com/jayway/jsonpath/PathReader.java
  10. 210
      json-path/src/main/java/com/jayway/jsonpath/PathUtil.java
  11. 39
      json-path/src/main/java/com/jayway/jsonpath/filter/FilterOutput.java
  12. 13
      json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterBase.java
  13. 47
      json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterChain.java
  14. 37
      json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterFactory.java
  15. 72
      json-path/src/main/java/com/jayway/jsonpath/filter/ListEvalFilter.java
  16. 67
      json-path/src/main/java/com/jayway/jsonpath/filter/ListFrontFilter.java
  17. 72
      json-path/src/main/java/com/jayway/jsonpath/filter/ListIndexFilter.java
  18. 52
      json-path/src/main/java/com/jayway/jsonpath/filter/ListPropertyFilter.java
  19. 56
      json-path/src/main/java/com/jayway/jsonpath/filter/ListTailFilter.java
  20. 19
      json-path/src/main/java/com/jayway/jsonpath/filter/ListWildcardFilter.java
  21. 54
      json-path/src/main/java/com/jayway/jsonpath/filter/PropertyFilter.java
  22. 20
      json-path/src/main/java/com/jayway/jsonpath/filter/RootFilter.java
  23. 48
      json-path/src/main/java/com/jayway/jsonpath/filter/TraverseFilter.java
  24. 39
      json-path/src/main/java/com/jayway/jsonpath/filter/WildcardPropertyFilter.java
  25. 16
      json-path/src/main/java/com/jayway/jsonpath/reader/PathToken.java
  26. 13
      json-path/src/main/java/com/jayway/jsonpath/reader/PathTokenizer.java
  27. 23
      json-path/src/main/java/com/jayway/jsonpath/reader/filter/ArrayEvalFilter.java
  28. 9
      json-path/src/main/java/com/jayway/jsonpath/reader/filter/ArrayIndexFilter.java
  29. 21
      json-path/src/main/java/com/jayway/jsonpath/reader/filter/FieldFilter.java
  30. 47
      json-path/src/main/java/com/jayway/jsonpath/reader/filter/Filter.java
  31. 19
      json-path/src/main/java/com/jayway/jsonpath/reader/filter/FilterFactory.java
  32. 13
      json-path/src/main/java/com/jayway/jsonpath/reader/filter/HasFieldFilter.java
  33. 4
      json-path/src/main/java/com/jayway/jsonpath/reader/filter/PassThrewFilter.java
  34. 28
      json-path/src/main/java/com/jayway/jsonpath/reader/filter/ScanFilter.java
  35. 16
      json-path/src/main/java/com/jayway/jsonpath/reader/filter/WildcardFilter.java
  36. 2
      json-path/src/main/java/com/jayway/jsonpath/reader/filter/eval/ExpressionEvaluator.java
  37. 87
      json-path/src/main/java/com/jayway/jsonpath/spi/JsonProvider.java
  38. 37
      json-path/src/main/java/com/jayway/jsonpath/spi/Mode.java
  39. 57
      json-path/src/main/java/com/jayway/jsonpath/spi/impl/DefaultJsonProvider.java
  40. 2
      json-path/src/test/java/com/jayway/jsonpath/ExpressionEvalTest.java
  41. 11
      json-path/src/test/java/com/jayway/jsonpath/IsDefinitePathTest.java
  42. 6
      json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java
  43. 10
      json-path/src/test/java/com/jayway/jsonpath/ParserTest.java
  44. 65
      json-path/src/test/java/com/jayway/jsonpath/PathReaderTest.java
  45. 64
      json-path/src/test/java/com/jayway/jsonpath/PathTokenizerTest.java
  46. 18
      json-path/src/test/java/com/jayway/jsonpath/SplitPathFragmentsTest.java
  47. 60
      json-path/src/test/java/com/jayway/jsonpath/util/ScriptEngineJsonPath.java
  48. 530
      json-path/src/test/resources/js/json.js
  49. 87
      json-path/src/test/resources/js/jsonpath-0.8.0.js
  50. 57
      json-path/src/test/resources/js/path.html
  51. 12
      json-path/src/test/resources/js/wrapper.js

34
json-path-assert/src/main/java/com/jayway/jsonassert/JsonAssert.java

@ -3,7 +3,8 @@ package com.jayway.jsonassert;
import com.jayway.jsonassert.impl.JsonAsserterImpl; import com.jayway.jsonassert.impl.JsonAsserterImpl;
import com.jayway.jsonassert.impl.matcher.*; import com.jayway.jsonassert.impl.matcher.*;
import net.minidev.json.parser.JSONParser; import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.spi.impl.DefaultJsonProvider;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import java.io.*; import java.io.*;
@ -18,23 +19,17 @@ import java.util.Map;
*/ */
public class JsonAssert { public class JsonAssert {
private static JSONParser JSON_PARSER = new JSONParser(); private static JsonProvider jsonProvider = new DefaultJsonProvider();
public final static int STRICT_MODE = 0; public final static int STRICT_MODE = 0;
public final static int SLACK_MODE = -1; public final static int SLACK_MODE = -1;
private static int mode = SLACK_MODE; private static int mode = SLACK_MODE;
public static void setMode(int mode) { public static void setJsonProvider(JsonProvider jsonProvider) {
if (mode != JsonAssert.mode) { JsonAssert.jsonProvider = jsonProvider;
JsonAssert.mode = mode;
JSON_PARSER = new JSONParser(JsonAssert.mode);
}
} }
public static int getMode() {
return mode;
}
/** /**
* Creates a JSONAsserter * Creates a JSONAsserter
@ -43,14 +38,8 @@ public class JsonAssert {
* @return a JSON asserter initialized with the provided document * @return a JSON asserter initialized with the provided document
* @throws ParseException when the given JSON could not be parsed * @throws ParseException when the given JSON could not be parsed
*/ */
public static JsonAsserter with(String json) throws ParseException { public static JsonAsserter with(String json) {
try { return new JsonAsserterImpl(jsonProvider.parse(json));
return new JsonAsserterImpl(JSON_PARSER.parse(json));
} catch (net.minidev.json.parser.ParseException e) {
throw new ParseException(json, e.getPosition());
} catch (IOException e) {
throw new RuntimeException(e);
}
} }
/** /**
@ -60,12 +49,9 @@ public class JsonAssert {
* @return a JSON asserter initialized with the provided document * @return a JSON asserter initialized with the provided document
* @throws ParseException when the given JSON could not be parsed * @throws ParseException when the given JSON could not be parsed
*/ */
public static JsonAsserter with(Reader reader) throws ParseException, IOException { public static JsonAsserter with(Reader reader) throws IOException {
try { return new JsonAsserterImpl(jsonProvider.parse(convertReaderToString(reader)));
return new JsonAsserterImpl(JSON_PARSER.parse(convertReaderToString(reader)));
} catch (net.minidev.json.parser.ParseException e) {
throw new ParseException(e.toString(), e.getPosition());
}
} }
/** /**

4
json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsCollectionWithSize.java

@ -29,14 +29,14 @@ DAMAGE.
*/ */
package com.jayway.jsonassert.impl.matcher; package com.jayway.jsonassert.impl.matcher;
import static org.hamcrest.core.IsEqual.equalTo;
import org.hamcrest.Description; import org.hamcrest.Description;
import org.hamcrest.Factory; import org.hamcrest.Factory;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import java.util.Collection; import java.util.Collection;
import static org.hamcrest.core.IsEqual.equalTo;
/** /**
* Matches if collection size satisfies a nested matcher. * Matches if collection size satisfies a nested matcher.
*/ */

5
json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingKey.java

@ -29,11 +29,12 @@ DAMAGE.
*/ */
package com.jayway.jsonassert.impl.matcher; package com.jayway.jsonassert.impl.matcher;
import java.util.Map;
import org.hamcrest.Description; import org.hamcrest.Description;
import org.hamcrest.Factory; import org.hamcrest.Factory;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import java.util.Map;
import static org.hamcrest.core.IsEqual.equalTo; import static org.hamcrest.core.IsEqual.equalTo;
public class IsMapContainingKey<K> extends MapTypeSafeMatcher<Map<K,?>> { public class IsMapContainingKey<K> extends MapTypeSafeMatcher<Map<K,?>> {

4
json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingValue.java

@ -32,9 +32,11 @@ package com.jayway.jsonassert.impl.matcher;
import org.hamcrest.Description; import org.hamcrest.Description;
import org.hamcrest.Factory; import org.hamcrest.Factory;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import static org.hamcrest.core.IsEqual.equalTo;
import java.util.Map; import java.util.Map;
import static org.hamcrest.core.IsEqual.equalTo;
public class IsMapContainingValue<V> extends MapTypeSafeMatcher<Map<?,V>>{ public class IsMapContainingValue<V> extends MapTypeSafeMatcher<Map<?,V>>{
private final Matcher<? super V> valueMatcher; private final Matcher<? super V> valueMatcher;

3
json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java

@ -1,6 +1,5 @@
package com.jayway.jsonassert; package com.jayway.jsonassert;
import org.hamcrest.Matchers;
import org.junit.Test; import org.junit.Test;
import java.io.InputStream; import java.io.InputStream;
@ -141,10 +140,12 @@ public class JsonAssertTest {
.assertThat("$['store'].book[0].title", equalTo("Sayings of the Century")); .assertThat("$['store'].book[0].title", equalTo("Sayings of the Century"));
} }
/*
@Test @Test
public void no_hit_returns_null() throws Exception { public void no_hit_returns_null() throws Exception {
with(JSON).assertThat("$.store.book[1000].title", Matchers.<Object>nullValue()); with(JSON).assertThat("$.store.book[1000].title", Matchers.<Object>nullValue());
} }
*/
@Test @Test
public void invalid_path() throws Exception { public void invalid_path() throws Exception {

25
json-path/src/main/java/com/jayway/jsonpath/InvalidJsonException.java

@ -0,0 +1,25 @@
package com.jayway.jsonpath;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 11/8/11
* Time: 4:01 PM
*/
public class InvalidJsonException extends RuntimeException{
public InvalidJsonException() {
}
public InvalidJsonException(String message) {
super(message);
}
public InvalidJsonException(String message, Throwable cause) {
super(message, cause);
}
public InvalidJsonException(Throwable cause) {
super(cause);
}
}

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

@ -3,13 +3,11 @@ package com.jayway.jsonpath;
import com.jayway.jsonpath.reader.PathToken; import com.jayway.jsonpath.reader.PathToken;
import com.jayway.jsonpath.reader.PathTokenizer; import com.jayway.jsonpath.reader.PathTokenizer;
import net.minidev.json.parser.JSONParser; import com.jayway.jsonpath.spi.JsonProvider;
import net.minidev.json.parser.ParseException; import com.jayway.jsonpath.spi.impl.DefaultJsonProvider;
import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.logging.Logger;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
@ -76,29 +74,11 @@ import java.util.regex.Pattern;
*/ */
public class JsonPath { public class JsonPath {
public final static int STRICT_MODE = 0;
public final static int SLACK_MODE = -1;
private static int mode = SLACK_MODE;
private final static Logger log = Logger.getLogger(JsonPath.class.getName());
private static JSONParser JSON_PARSER = new JSONParser(JsonPath.mode);
private static Pattern DEFINITE_PATH_PATTERN = Pattern.compile(".*(\\.\\.|\\*|\\[[\\\\/]|\\?|,|:\\s?\\]|\\[\\s?:|>|\\(|<|=|\\+).*"); private static Pattern DEFINITE_PATH_PATTERN = Pattern.compile(".*(\\.\\.|\\*|\\[[\\\\/]|\\?|,|:\\s?\\]|\\[\\s?:|>|\\(|<|=|\\+).*");
private PathTokenizer tokenizer; private PathTokenizer tokenizer;
public static void setMode(int mode) { private JsonProvider jsonProvider;
if (mode != JsonPath.mode) {
JsonPath.mode = mode;
JSON_PARSER = new JSONParser(JsonPath.mode);
}
}
public static int getMode() {
return mode;
}
/** /**
@ -107,6 +87,11 @@ public class JsonPath {
* @param jsonPath the path statement * @param jsonPath the path statement
*/ */
private JsonPath(String jsonPath) { private JsonPath(String jsonPath) {
this(new DefaultJsonProvider(), jsonPath);
}
private JsonPath(JsonProvider jsonProvider, String jsonPath) {
if (jsonPath == null || if (jsonPath == null ||
jsonPath.trim().isEmpty() || jsonPath.trim().isEmpty() ||
jsonPath.matches("new ") || jsonPath.matches("new ") ||
@ -114,8 +99,8 @@ public class JsonPath {
throw new InvalidPathException("Invalid path"); throw new InvalidPathException("Invalid path");
} }
this.tokenizer = new PathTokenizer(jsonPath); this.jsonProvider = jsonProvider;
//this.filters = new JsonPathFilterChain(PathUtil.splitPath(jsonPath)); this.tokenizer = new PathTokenizer(jsonPath, jsonProvider);
} }
public String getPath() { public String getPath() {
@ -159,26 +144,16 @@ public class JsonPath {
* @return list of objects matched by the given path * @return list of objects matched by the given path
*/ */
public <T> T read(Object container) { public <T> T read(Object container) {
if (!(container instanceof Map) && !(container instanceof List)) {
if(!(container instanceof Map) && !(container instanceof List) ){
throw new IllegalArgumentException("Invalid container object"); throw new IllegalArgumentException("Invalid container object");
} }
Object result = container; Object result = container;
for (PathToken pathToken : tokenizer) { for (PathToken pathToken : tokenizer) {
result = pathToken.filter(result); result = pathToken.filter(result, jsonProvider);
} }
return (T)result; return (T) result;
/*
FilterOutput filterOutput = filters.filter(container);
if (filterOutput == null || filterOutput.getResult() == null) {
return null;
}
return (T) filterOutput.getResult();
*/
} }
/** /**
@ -188,8 +163,8 @@ public class JsonPath {
* @param <T> * @param <T>
* @return list of objects matched by the given path * @return list of objects matched by the given path
*/ */
public <T> T read(String json) throws java.text.ParseException { public <T> T read(String json) {
return (T) read(parse(json)); return (T) read(jsonProvider.parse(json));
} }
/** /**
@ -202,6 +177,10 @@ public class JsonPath {
return new JsonPath(jsonPath); return new JsonPath(jsonPath);
} }
public static JsonPath compile(JsonProvider provider, String jsonPath) {
return new JsonPath(provider, jsonPath);
}
/** /**
* Creates a new JsonPath and applies it to the provided Json string * Creates a new JsonPath and applies it to the provided Json string
* *
@ -210,7 +189,7 @@ public class JsonPath {
* @param <T> * @param <T>
* @return list of objects matched by the given path * @return list of objects matched by the given path
*/ */
public static <T> T read(String json, String jsonPath) throws java.text.ParseException { public static <T> T read(String json, String jsonPath) {
return (T) compile(jsonPath).read(json); return (T) compile(jsonPath).read(json);
} }
@ -226,14 +205,4 @@ public class JsonPath {
return (T) compile(jsonPath).read(json); return (T) compile(jsonPath).read(json);
} }
public static Object parse(String json) throws java.text.ParseException {
try {
return JSON_PARSER.parse(json);
} catch (ParseException e) {
throw new java.text.ParseException(json, e.getPosition());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
} }

61
json-path/src/main/java/com/jayway/jsonpath/JsonUtil.java

@ -1,61 +0,0 @@
package com.jayway.jsonpath;
import java.util.List;
import java.util.Map;
/**
* User: kalle stenflo
* Date: 2/4/11
* Time: 1:01 PM
*/
public class JsonUtil {
/**
* checks if object is <code>instanceof</code> <code>java.util.List</code> or <code>java.util.Map</code>
*
* @param obj object to check
* @return true if List or Map
*/
public static boolean isContainer(Object obj) {
return (isList(obj) || isMap(obj));
}
/**
* checks if object is <code>instanceof</code> <code>java.util.List</code>
*
* @param obj object to check
* @return true if List
*/
public static boolean isList(Object obj) {
return (obj instanceof List);
}
/**
* checks if object is <code>instanceof</code> <code>java.util.Map</code>
*
* @param obj object to check
* @return true if Map
*/
public static boolean isMap(Object obj) {
return (obj instanceof Map);
}
/**
* converts casts to <code>java.util.List</code>
*
* @param obj
* @return the list
*/
public static List<Object> toList(Object obj) {
return (List) obj;
}
/**
* converts casts to <code>java.util.Map</code>
*
* @param obj
* @return the Map
*/
public static Map<Object, Object> toMap(Object obj) {
return (Map) obj;
}
}

45
json-path/src/main/java/com/jayway/jsonpath/PathReader.java

@ -0,0 +1,45 @@
package com.jayway.jsonpath;
import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.spi.impl.DefaultJsonProvider;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 11/8/11
* Time: 7:40 PM
*/
public class PathReader {
private Object jsonModel;
private JsonProvider jsonProvider;
private PathReader(String json) {
this.jsonProvider = new DefaultJsonProvider();
this.jsonModel = jsonProvider.parse(json);
}
private PathReader(Object jsonModel) {
this.jsonProvider = new DefaultJsonProvider();
this.jsonModel = jsonModel;
}
public static PathReader create(String json) {
return new PathReader(json);
}
public <T> T read(String jsonPath) {
return (T)JsonPath.compile(jsonProvider, jsonPath).read(jsonModel);
}
public <T> T read(JsonPath jsonPath) {
return (T)jsonPath.read(jsonModel);
}
public PathReader get(String jsonPath){
Object subModel = read(jsonPath);
return new PathReader(subModel);
}
}

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

@ -1,210 +0,0 @@
package com.jayway.jsonpath;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/**
* User: kalle stenflo
* Date: 2/2/11
* Time: 2:08 PM
*/
public class PathUtil {
/**
* Checks if a path points to a single item or if it potentially returns multiple items
* <p/>
* a path is considered <strong>not</strong> definite if it contains a scan fragment ".."
* or an array position fragment that is not based on a single index
* <p/>
* <p/>
* absolute path examples:
* <p/>
* $store.book
* $store.book[1].value
* <p/>
* not absolute path examples
* <p/>
* $..book
* $.store.book[1,2]
* $.store.book[?(@.category = 'fiction')]
*
* @param jsonPath the path to check
* @return true if path is definite (points to single item)
*/
public static boolean isPathDefinite(String jsonPath) {
return !jsonPath.replaceAll("\"[^\"\\\\\\n\r]*\"", "").matches(".*(\\.\\.|\\*|\\[[\\\\/]|\\?|,|:\\s?\\]|\\[\\s?:|>|\\(|<|=|\\+).*");
}
/**
* Splits a path into fragments
* <p/>
* the path <code>$.store.book[1].category</code> returns ["$", "store", "book", "[1]", "category"]
*
* @param jsonPath path to split
* @return fragments
*/
public static List<String> splitPath(String jsonPath) {
if (!jsonPath.startsWith("$") && !jsonPath.startsWith("$[")) {
jsonPath = "$." + jsonPath;
}
LinkedList<String> fragments = new LinkedList<String>();
Queue<Character> pathQueue = new LinkedList<Character>();
for (char b : jsonPath.toCharArray()) {
pathQueue.add(b);
}
while (!pathQueue.isEmpty()) {
skip(pathQueue, ' ');
char current = pathQueue.peek();
switch (current) {
case '$':
fragments.add(Character.toString(current));
pathQueue.poll();
break;
case '.':
pathQueue.poll();
if (pathQueue.peek().equals('.')) {
pathQueue.poll();
fragments.add("..");
assertNotInvalidPeek(pathQueue, '.');
}
break;
case '[':
fragments.add(extract(pathQueue, true, ']'));
break;
default:
fragments.add(extract(pathQueue, false, '[', '.'));
}
}
return fragments;
}
private static String extract(Queue<Character> pathQueue, boolean includeSopChar, char... stopChars) {
StringBuilder sb = new StringBuilder();
while (!pathQueue.isEmpty() && (!isStopChar(pathQueue.peek().charValue(), stopChars))) {
char c = pathQueue.poll();
if (isStopChar(c, stopChars)) {
if (includeSopChar) {
sb.append(c);
}
} else {
sb.append(c);
}
}
if (includeSopChar) {
assertValidPeek(pathQueue, false, stopChars);
sb.append(pathQueue.poll());
} else {
assertValidPeek(pathQueue, true, stopChars);
}
return clean(sb);
}
private static String clean(StringBuilder sb) {
String src = sb.toString();
src = trim(src, "'");
src = trim(src, ")");
src = trim(src, "(");
src = trimLeft(src, "?");
src = trimLeft(src, "@");
if (src.length() > 5 && src.subSequence(0, 2).equals("['")) {
src = src.substring(2);
src = src.substring(0, src.length() - 2);
}
return src.trim();
}
private static String trim(String src, String trim) {
return trimLeft(trimRight(src, trim), trim);
}
private static String trimRight(String src, String trim) {
String scanFor = trim + " ";
if (src.contains(scanFor)) {
while (src.contains(scanFor)) {
src = src.replace(scanFor, trim);
}
}
return src;
}
private static String trimLeft(String src, String trim) {
String scanFor = " " + trim;
if (src.contains(scanFor)) {
while (src.contains(scanFor)) {
src = src.replace(scanFor, trim);
}
}
return src;
}
private static boolean isStopChar(char c, char... scanFor) {
boolean found = false;
for (char check : scanFor) {
if (check == c) {
found = true;
break;
}
}
return found;
}
private static void skip(Queue<Character> pathQueue, char target) {
if (pathQueue.isEmpty()) {
return;
}
while (pathQueue.peek().charValue() == target) {
pathQueue.poll();
}
}
private static void assertNotInvalidPeek(Queue<Character> pathQueue, char... invalidChars) {
if (pathQueue.isEmpty()) {
return;
}
char peek = pathQueue.peek();
for (char check : invalidChars) {
if (check == peek) {
throw new InvalidPathException("Char: " + peek + " at current position is not valid!");
}
}
}
private static void assertValidPeek(Queue<Character> pathQueue, boolean acceptEmpty, char... validChars) {
if (pathQueue.isEmpty() && acceptEmpty) {
return;
}
if (pathQueue.isEmpty()) {
throw new InvalidPathException("Path is incomplete");
}
boolean found = false;
char peek = pathQueue.peek();
for (char check : validChars) {
if (check == peek) {
found = true;
break;
}
}
if (!found) {
throw new InvalidPathException("Path is invalid");
}
}
}

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

@ -1,39 +0,0 @@
package com.jayway.jsonpath.filter;
import java.util.List;
import static java.lang.String.format;
/**
* User: kalle stenflo
* Date: 2/9/11
* Time: 12:28 PM
*/
public class FilterOutput {
private final Object result;
public FilterOutput(Object result) {
this.result = result;
}
public boolean isList(){
return (result instanceof List);
}
public Object getResult() {
return result;
}
public List<Object> getResultAsList() {
if(!isList()){
throw new RuntimeException(format("Can not convert a %s to a %s", result.getClass().getName(), List.class.getName()));
}
return (List<Object>)result;
}
}

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

@ -1,13 +0,0 @@
package com.jayway.jsonpath.filter;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 2/2/11
* Time: 2:01 PM
*/
public abstract class JsonPathFilterBase {
public abstract FilterOutput apply(FilterOutput filterItems);
}

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

@ -1,47 +0,0 @@
package com.jayway.jsonpath.filter;
import com.jayway.jsonpath.InvalidPathException;
import java.util.LinkedList;
import java.util.List;
/**
* User: kallestenflo
* Date: 2/2/11
* Time: 2:00 PM
*/
public class JsonPathFilterChain {
private List<JsonPathFilterBase> filters;
public JsonPathFilterChain(List<String> pathFragments) {
filters = configureFilters(pathFragments);
}
private List<JsonPathFilterBase> configureFilters(List<String> pathFragments) {
List<JsonPathFilterBase> configured = new LinkedList<JsonPathFilterBase>();
for (String pathFragment : pathFragments) {
configured.add(JsonPathFilterFactory.createFilter(pathFragment));
}
return configured;
}
public FilterOutput filter(Object root) {
FilterOutput out = new FilterOutput(root);
for (JsonPathFilterBase filter : filters) {
if (filter == null) {
throw new InvalidPathException();
}
if(out.getResult() == null){
return null;
}
out = filter.apply(out);
}
return out;
}
}

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

@ -1,37 +0,0 @@
package com.jayway.jsonpath.filter;
/**
* User: kallestenflo
* Date: 2/2/11
* Time: 2:03 PM
*/
public class JsonPathFilterFactory {
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;
}
}

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

@ -1,72 +0,0 @@
package com.jayway.jsonpath.filter;
import com.jayway.jsonpath.JsonUtil;
import com.jayway.jsonpath.eval.ExpressionEvaluator;
import net.minidev.json.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?@.(.*?)\\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;
}
}

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

@ -1,67 +0,0 @@
package com.jayway.jsonpath.filter;
import net.minidev.json.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

@ -1,72 +0,0 @@
package com.jayway.jsonpath.filter;
import net.minidev.json.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

@ -1,52 +0,0 @@
package com.jayway.jsonpath.filter;
import com.jayway.jsonpath.JsonUtil;
import net.minidev.json.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

@ -1,56 +0,0 @@
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

@ -1,19 +0,0 @@
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());
}
}

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

@ -1,54 +0,0 @@
package com.jayway.jsonpath.filter;
import com.jayway.jsonpath.JsonUtil;
import net.minidev.json.JSONArray;
import java.util.List;
import java.util.regex.Pattern;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 2/2/11
* Time: 2:32 PM
*/
public class PropertyFilter extends JsonPathFilterBase {
public final static Pattern PATTERN = Pattern.compile("(.*)|\\['(.*?)'\\]");
private final String pathFragment;
public PropertyFilter(String pathFragment) {
this.pathFragment = pathFragment;
}
@Override
public FilterOutput apply(FilterOutput filter) {
List<Object> result = new JSONArray();
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);
}
}
}

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

@ -1,20 +0,0 @@
package com.jayway.jsonpath.filter;
import java.util.List;
import java.util.regex.Pattern;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 2/2/11
* Time: 2:31 PM
*/
public class RootFilter extends JsonPathFilterBase{
public final static Pattern PATTERN = Pattern.compile("\\$");
@Override
public FilterOutput apply(FilterOutput root) {
return root;
}
}

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

@ -1,48 +0,0 @@
package com.jayway.jsonpath.filter;
import com.jayway.jsonpath.JsonUtil;
import net.minidev.json.JSONArray;
import java.util.List;
import java.util.regex.Pattern;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 2/2/11
* Time: 2:33 PM
*/
public class TraverseFilter extends JsonPathFilterBase {
public final static Pattern PATTERN = Pattern.compile("\\.\\.");
@Override
public FilterOutput apply(FilterOutput filter) {
List<Object> result = new JSONArray();
traverse(filter.getResult(), result);
return new FilterOutput(result);
}
private void traverse(Object container, List<Object> result) {
if (JsonUtil.isMap(container)) {
result.add(container);
for (Object value : JsonUtil.toMap(container).values()) {
if (JsonUtil.isContainer(value)) {
traverse(value, result);
}
}
} else if (JsonUtil.isList(container)) {
for (Object value : JsonUtil.toList(container)) {
if (JsonUtil.isContainer(value)) {
traverse(value, result);
}
}
}
}
}

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

@ -1,39 +0,0 @@
package com.jayway.jsonpath.filter;
import com.jayway.jsonpath.JsonUtil;
import net.minidev.json.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);
}
}

16
json-path/src/main/java/com/jayway/jsonpath/reader/PathToken.java

@ -1,7 +1,7 @@
package com.jayway.jsonpath.reader; package com.jayway.jsonpath.reader;
import com.jayway.jsonpath.reader.filter.Filter;
import com.jayway.jsonpath.reader.filter.FilterFactory; import com.jayway.jsonpath.reader.filter.FilterFactory;
import com.jayway.jsonpath.spi.JsonProvider;
/** /**
* Created by IntelliJ IDEA. * Created by IntelliJ IDEA.
@ -11,13 +11,17 @@ import com.jayway.jsonpath.reader.filter.FilterFactory;
*/ */
public class PathToken { public class PathToken {
private Filter filter; private String fragment;
public PathToken(String pathFragment) { public PathToken(String fragment) {
filter = FilterFactory.createFilter(pathFragment); this.fragment = fragment;
} }
public Object filter(Object model){ public Object filter(Object model, JsonProvider jsonProvider){
return filter.filter(model); return FilterFactory.createFilter(fragment).filter(model, jsonProvider);
}
public String getFragment() {
return fragment;
} }
} }

13
json-path/src/main/java/com/jayway/jsonpath/reader/PathTokenizer.java

@ -1,6 +1,7 @@
package com.jayway.jsonpath.reader; package com.jayway.jsonpath.reader;
import com.jayway.jsonpath.InvalidPathException; import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.spi.JsonProvider;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
@ -19,7 +20,7 @@ public class PathTokenizer implements Iterable<PathToken> {
private int index = 0; private int index = 0;
private List<PathToken> pathTokens = new LinkedList<PathToken>(); private List<PathToken> pathTokens = new LinkedList<PathToken>();
public PathTokenizer(String jsonPath) { public PathTokenizer(String jsonPath, JsonProvider jsonProvider) {
if (!jsonPath.startsWith("$") && !jsonPath.startsWith("$[")) { if (!jsonPath.startsWith("$") && !jsonPath.startsWith("$[")) {
jsonPath = "$." + jsonPath; jsonPath = "$." + jsonPath;
@ -32,6 +33,14 @@ public class PathTokenizer implements Iterable<PathToken> {
} }
} }
public List<String> getFragments(){
List<String> fragments = new LinkedList<String>();
for (PathToken pathToken : pathTokens) {
fragments.add(pathToken.getFragment());
}
return fragments;
}
public String getPath() { public String getPath() {
return path; return path;
} }
@ -177,7 +186,7 @@ public class PathTokenizer implements Iterable<PathToken> {
return; return;
} }
while (pathChars[index] == target) { while (pathChars[index] == target) {
index++; poll();
} }
} }

23
json-path/src/main/java/com/jayway/jsonpath/reader/filter/ArrayEvalFilter.java

@ -1,9 +1,9 @@
package com.jayway.jsonpath.reader.filter; package com.jayway.jsonpath.reader.filter;
import com.jayway.jsonpath.InvalidPathException; import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.eval.ExpressionEvaluator; import com.jayway.jsonpath.reader.filter.eval.ExpressionEvaluator;
import com.jayway.jsonpath.spi.JsonProvider;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -24,31 +24,28 @@ public class ArrayEvalFilter extends Filter {
} }
@Override @Override
public Object filter(Object obj) { public Object filter(Object obj, JsonProvider jsonProvider) {
//[?(@.isbn = 10)] //[?(@.isbn = 10)]
List<Object> src = toList(obj); List<Object> src = jsonProvider.toList(obj);
List<Object> result = new LinkedList<Object>(); List<Object> result = jsonProvider.createList();
String trimmedCondition = trim(condition, 5, 2); String trimmedCondition = trim(condition, 5, 2);
ConditionStatement conditionStatement = createConditionStatement(trimmedCondition); ConditionStatement conditionStatement = createConditionStatement(trimmedCondition);
for (Object item : src) { for (Object item : src) {
if (isMatch(item, conditionStatement)) { if (isMatch(item, conditionStatement, jsonProvider)) {
result.add(item); result.add(item);
} }
} }
return result; return result;
} }
private boolean isMatch(Object check, ConditionStatement conditionStatement) { private boolean isMatch(Object check, ConditionStatement conditionStatement, JsonProvider jsonProvider) {
if (!isMap(check)) { if (!jsonProvider.isMap(check)) {
return false; return false;
} }
Map obj = toMap(check); Map obj = jsonProvider.toMap(check);
if (!obj.containsKey(conditionStatement.getField())) { if (!obj.containsKey(conditionStatement.getField())) {
return false; return false;
@ -56,7 +53,7 @@ public class ArrayEvalFilter extends Filter {
Object propertyValue = obj.get(conditionStatement.getField()); Object propertyValue = obj.get(conditionStatement.getField());
if (isContainer(propertyValue)) { if (jsonProvider.isContainer(propertyValue)) {
return false; return false;
} }
return ExpressionEvaluator.eval(propertyValue, conditionStatement.getOperator(), conditionStatement.getExpected()); return ExpressionEvaluator.eval(propertyValue, conditionStatement.getOperator(), conditionStatement.getExpected());

9
json-path/src/main/java/com/jayway/jsonpath/reader/filter/ArrayIndexFilter.java

@ -1,6 +1,7 @@
package com.jayway.jsonpath.reader.filter; package com.jayway.jsonpath.reader.filter;
import java.util.LinkedList; import com.jayway.jsonpath.spi.JsonProvider;
import java.util.List; import java.util.List;
/** /**
@ -15,10 +16,10 @@ public class ArrayIndexFilter extends Filter {
} }
@Override @Override
public Object filter(Object obj) { public Object filter(Object obj,JsonProvider jsonProvider) {
List<Object> src = toList(obj); List<Object> src = jsonProvider.toList(obj);
List<Object> result = new LinkedList<Object>(); List<Object> result = jsonProvider.createList();
String trimmedCondition = trim(condition, 1, 1); String trimmedCondition = trim(condition, 1, 1);

21
json-path/src/main/java/com/jayway/jsonpath/reader/filter/FieldFilter.java

@ -1,6 +1,7 @@
package com.jayway.jsonpath.reader.filter; package com.jayway.jsonpath.reader.filter;
import java.util.LinkedList; import com.jayway.jsonpath.spi.JsonProvider;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -16,16 +17,16 @@ public class FieldFilter extends Filter {
super(condition); super(condition);
} }
public Object filter(Object obj) { public Object filter(Object obj, JsonProvider jsonProvider) {
if (isList(obj)) { if (jsonProvider.isList(obj)) {
List<Object> result = new LinkedList<Object>(); List<Object> result = jsonProvider.createList();
for (Object current : toList(obj)) { for (Object current : jsonProvider.toList(obj)) {
if (isMap(current)) { if (jsonProvider.isMap(current)) {
Map<String, Object> map = toMap(current); Map<String, Object> map = jsonProvider.toMap(current);
if (map.containsKey(condition)) { if (map.containsKey(condition)) {
Object o = map.get(condition); Object o = map.get(condition);
if (isList(o)) { if (jsonProvider.isList(o)) {
result.addAll(toList(o)); result.addAll(jsonProvider.toList(o));
} else { } else {
result.add(map.get(condition)); result.add(map.get(condition));
} }
@ -34,7 +35,7 @@ public class FieldFilter extends Filter {
} }
return result; return result;
} else { } else {
return getMapValue(obj, condition); return jsonProvider.getMapValue(obj, condition);
} }
} }
} }

47
json-path/src/main/java/com/jayway/jsonpath/reader/filter/Filter.java

@ -1,7 +1,6 @@
package com.jayway.jsonpath.reader.filter; package com.jayway.jsonpath.reader.filter;
import java.util.List; import com.jayway.jsonpath.spi.JsonProvider;
import java.util.Map;
/** /**
* Created by IntelliJ IDEA. * Created by IntelliJ IDEA.
@ -17,48 +16,6 @@ public abstract class Filter {
this.condition = condition; this.condition = condition;
} }
/**
* checks if object is <code>instanceof</code> <code>java.util.List</code> or <code>java.util.Map</code>
*
* @param obj object to check
* @return true if List or Map
*/
protected boolean isContainer(Object obj) {
return (isList(obj) || isMap(obj));
}
/**
* checks if object is <code>instanceof</code> <code>java.util.List</code>
*
* @param obj object to check
* @return true if List
*/
protected boolean isList(Object obj) {
return (obj instanceof List);
}
protected List<Object> toList(Object list) {
return (List<Object>) list;
}
protected Map<String, Object> toMap(Object map) {
return (Map<String, Object>) map;
}
protected Object getMapValue(Object obj, String key) {
Map<String, Object> map = toMap(obj);
return map.get(key);
}
/**
* checks if object is <code>instanceof</code> <code>java.util.Map</code>
*
* @param obj object to check
* @return true if Map
*/
protected boolean isMap(Object obj) {
return (obj instanceof Map);
}
protected String trim(String str, int front, int end) { protected String trim(String str, int front, int end) {
String res = str; String res = str;
@ -72,6 +29,6 @@ public abstract class Filter {
return res; return res;
} }
public abstract Object filter(Object obj); public abstract Object filter(Object obj, JsonProvider jsonProvider);
} }

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

@ -1,7 +1,5 @@
package com.jayway.jsonpath.reader.filter; package com.jayway.jsonpath.reader.filter;
import com.jayway.jsonpath.filter.ListEvalFilter;
/** /**
* Created by IntelliJ IDEA. * Created by IntelliJ IDEA.
* User: kallestenflo * User: kallestenflo
@ -10,19 +8,28 @@ import com.jayway.jsonpath.filter.ListEvalFilter;
*/ */
public class FilterFactory { public class FilterFactory {
private final static Filter DOCUMENT_FILTER = new PassThrewFilter("$");
private final static Filter ALL_ARRAY_ITEMS_FILTER = new PassThrewFilter("[*]");
private final static Filter WILDCARD_FILTER = new WildcardFilter("*");
private final static Filter SCAN_FILTER = new ScanFilter("..");
public static Filter createFilter(String pathFragment) { public static Filter createFilter(String pathFragment) {
if ("$".equals(pathFragment) || "[*]".equals(pathFragment)) { if ("$".equals(pathFragment)) {
return DOCUMENT_FILTER;
} else if("[*]".equals(pathFragment)){
return new PassThrewFilter(pathFragment); return ALL_ARRAY_ITEMS_FILTER;
} else if ("*".equals(pathFragment)) { } else if ("*".equals(pathFragment)) {
return new WildcardFilter(pathFragment); return WILDCARD_FILTER;
} else if (pathFragment.contains("..")) { } else if (pathFragment.contains("..")) {
return new ScanFilter(pathFragment); return SCAN_FILTER;
} else if (!pathFragment.contains("[")) { } else if (!pathFragment.contains("[")) {

13
json-path/src/main/java/com/jayway/jsonpath/reader/filter/HasFieldFilter.java

@ -1,6 +1,7 @@
package com.jayway.jsonpath.reader.filter; package com.jayway.jsonpath.reader.filter;
import java.util.LinkedList; import com.jayway.jsonpath.spi.JsonProvider;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -17,17 +18,17 @@ public class HasFieldFilter extends Filter {
} }
@Override @Override
public Object filter(Object obj) { public Object filter(Object obj, JsonProvider jsonProvider) {
//[?(@.isbn)] //[?(@.isbn)]
List<Object> src = toList(obj); List<Object> src = jsonProvider.toList(obj);
List<Object> result = new LinkedList<Object>(); List<Object> result = jsonProvider.createList();
String trimmedCondition = trim(condition, 5, 2); String trimmedCondition = trim(condition, 5, 2);
for (Object item : src) { for (Object item : src) {
if(isMap(item)){ if(jsonProvider.isMap(item)){
Map<String, Object> map = toMap(item); Map<String, Object> map = jsonProvider.toMap(item);
if(map.containsKey(trimmedCondition)){ if(map.containsKey(trimmedCondition)){
result.add(map); result.add(map);
} }

4
json-path/src/main/java/com/jayway/jsonpath/reader/filter/PassThrewFilter.java

@ -1,5 +1,7 @@
package com.jayway.jsonpath.reader.filter; package com.jayway.jsonpath.reader.filter;
import com.jayway.jsonpath.spi.JsonProvider;
/** /**
* Created by IntelliJ IDEA. * Created by IntelliJ IDEA.
* User: kallestenflo * User: kallestenflo
@ -12,7 +14,7 @@ public class PassThrewFilter extends Filter {
super(condition); super(condition);
} }
public Object filter(Object obj) { public Object filter(Object obj, JsonProvider jsonProvider) {
return obj; return obj;
} }
} }

28
json-path/src/main/java/com/jayway/jsonpath/reader/filter/ScanFilter.java

@ -1,8 +1,8 @@
package com.jayway.jsonpath.reader.filter; package com.jayway.jsonpath.reader.filter;
import com.jayway.jsonpath.JsonUtil;
import java.util.LinkedList; import com.jayway.jsonpath.spi.JsonProvider;
import java.util.List; import java.util.List;
/** /**
@ -18,29 +18,29 @@ public class ScanFilter extends Filter {
} }
@Override @Override
public Object filter(Object obj) { public Object filter(Object obj, JsonProvider jsonProvider) {
List<Object> result = new LinkedList<Object>(); List<Object> result = jsonProvider.createList();
scan(obj, result); scan(obj, result, jsonProvider);
return result; return result;
} }
private void scan(Object container, List<Object> result) { private void scan(Object container, List<Object> result, JsonProvider jsonProvider) {
if (isMap(container)) { if (jsonProvider.isMap(container)) {
result.add(container); result.add(container);
for (Object value : toMap(container).values()) { for (Object value : jsonProvider.toMap(container).values()) {
if (isContainer(value)) { if (jsonProvider.isContainer(value)) {
scan(value, result); scan(value, result, jsonProvider);
} }
} }
} else if (isList(container)) { } else if (jsonProvider.isList(container)) {
for (Object value : JsonUtil.toList(container)) { for (Object value : jsonProvider.toList(container)) {
if (isContainer(value)) { if (jsonProvider.isContainer(value)) {
scan(value, result); scan(value, result, jsonProvider);
} }
} }
} }

16
json-path/src/main/java/com/jayway/jsonpath/reader/filter/WildcardFilter.java

@ -1,9 +1,7 @@
package com.jayway.jsonpath.reader.filter; package com.jayway.jsonpath.reader.filter;
import com.jayway.jsonpath.JsonUtil; import com.jayway.jsonpath.spi.JsonProvider;
import net.minidev.json.JSONArray;
import java.util.LinkedList;
import java.util.List; import java.util.List;
/** /**
@ -19,17 +17,17 @@ public class WildcardFilter extends Filter {
} }
@Override @Override
public Object filter(Object obj) { public Object filter(Object obj, JsonProvider jsonProvider) {
List<Object> result = new LinkedList<Object>(); List<Object> result = jsonProvider.createList();
if (isList(obj)) { if (jsonProvider.isList(obj)) {
for (Object current : toList(obj)) { for (Object current : jsonProvider.toList(obj)) {
for (Object value : toMap(current).values()) { for (Object value : jsonProvider.toMap(current).values()) {
result.add(value); result.add(value);
} }
} }
} else { } else {
for (Object value : toMap(obj).values()) { for (Object value : jsonProvider.toMap(obj).values()) {
result.add(value); result.add(value);
} }
} }

2
json-path/src/main/java/com/jayway/jsonpath/eval/ExpressionEvaluator.java → json-path/src/main/java/com/jayway/jsonpath/reader/filter/eval/ExpressionEvaluator.java

@ -1,4 +1,4 @@
package com.jayway.jsonpath.eval; package com.jayway.jsonpath.reader.filter.eval;
/** /**
* User: kalle stenflo * User: kalle stenflo

87
json-path/src/main/java/com/jayway/jsonpath/spi/JsonProvider.java

@ -0,0 +1,87 @@
package com.jayway.jsonpath.spi;
import com.jayway.jsonpath.InvalidJsonException;
import java.util.List;
import java.util.Map;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 11/8/11
* Time: 3:51 PM
*/
public abstract class JsonProvider {
public abstract Mode getMode();
public abstract Object parse(String json) throws InvalidJsonException;
public abstract Map<String, Object> createMap();
public abstract List<Object> createList();
/**
* checks if object is <code>instanceof</code> <code>java.util.List</code> or <code>java.util.Map</code>
*
* @param obj object to check
* @return true if List or Map
*/
public boolean isContainer(Object obj) {
return (isList(obj) || isMap(obj));
}
/**
* checks if object is <code>instanceof</code> <code>java.util.List</code>
*
* @param obj object to check
* @return true if List
*/
public boolean isList(Object obj) {
return (obj instanceof List);
}
/**
* Converts give object to a List
*
* @param list
* @return
*/
public List<Object> toList(Object list) {
return (List<Object>) list;
}
/**
* Converts given object to a Map
*
* @param map
* @return
*/
public Map<String, Object> toMap(Object map) {
return (Map<String, Object>) map;
}
/**
* Extracts a value from a Map
*
* @param map
* @param key
* @return
*/
public Object getMapValue(Object map, String key) {
return toMap(map).get(key);
}
/**
* checks if object is <code>instanceof</code> <code>java.util.Map</code>
*
* @param obj object to check
* @return true if Map
*/
public boolean isMap(Object obj) {
return (obj instanceof Map);
}
}

37
json-path/src/main/java/com/jayway/jsonpath/spi/Mode.java

@ -0,0 +1,37 @@
package com.jayway.jsonpath.spi;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 11/8/11
* Time: 7:22 PM
*/
public enum Mode {
SLACK(-1),
STRICT(1);
private final int mode;
Mode(int mode) {
this.mode = mode;
}
public int intValue(){
return mode;
}
public Mode parse(int mode){
if(mode == -1){
return SLACK;
} else if(mode == 1){
return STRICT;
} else {
throw new IllegalArgumentException("Mode " + mode + " not supported");
}
}
}

57
json-path/src/main/java/com/jayway/jsonpath/spi/impl/DefaultJsonProvider.java

@ -0,0 +1,57 @@
package com.jayway.jsonpath.spi.impl;
import com.jayway.jsonpath.InvalidJsonException;
import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.spi.Mode;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import net.minidev.json.parser.JSONParser;
import net.minidev.json.parser.ParseException;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 11/8/11
* Time: 3:56 PM
*/
public class DefaultJsonProvider extends JsonProvider {
private Mode mode;
private JSONParser parser;
public DefaultJsonProvider() {
this(Mode.SLACK);
}
public DefaultJsonProvider(Mode mode) {
this.mode = mode;
this.parser = new JSONParser(mode.intValue());
}
public Map<String, Object> createMap() {
return new JSONObject();
}
public List<Object> createList() {
return new JSONArray();
}
public Object parse(String json) {
try {
return parser.parse(json);
} catch (ParseException e) {
throw new InvalidJsonException(e);
} catch (IOException e) {
throw new InvalidJsonException(e);
}
}
public Mode getMode() {
return mode;
}
}

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

@ -1,6 +1,6 @@
package com.jayway.jsonpath; package com.jayway.jsonpath;
import com.jayway.jsonpath.eval.ExpressionEvaluator; import com.jayway.jsonpath.reader.filter.eval.ExpressionEvaluator;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;

11
json-path/src/test/java/com/jayway/jsonpath/PathUtilTest.java → json-path/src/test/java/com/jayway/jsonpath/IsDefinitePathTest.java

@ -11,20 +11,19 @@ import static org.junit.Assert.assertTrue;
* Date: 2/3/11 * Date: 2/3/11
* Time: 9:58 PM * Time: 9:58 PM
*/ */
public class PathUtilTest { public class IsDefinitePathTest {
@Test @Test
public void path_is_not_definite() throws Exception { public void path_is_not_definite() throws Exception {
assertFalse(PathUtil.isPathDefinite("$..book[0]")); assertFalse(JsonPath.compile("$..book[0]").isPathDefinite());
assertFalse(PathUtil.isPathDefinite("$.books[*]")); assertFalse(JsonPath.compile("$.books[*]").isPathDefinite());
} }
@Test @Test
public void path_is_definite() throws Exception { public void path_is_definite() throws Exception {
assertTrue(PathUtil.isPathDefinite("$.definite.this.is")); assertTrue(JsonPath.compile("$.definite.this.is").isPathDefinite());
assertTrue(PathUtil.isPathDefinite("$.definite:this.is")); assertTrue(JsonPath.compile("rows[0].id").isPathDefinite());
assertTrue(PathUtil.isPathDefinite("rows[0].id"));
} }

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

@ -1,5 +1,6 @@
package com.jayway.jsonpath; package com.jayway.jsonpath;
import com.jayway.jsonpath.util.ScriptEngineJsonPath;
import org.junit.Test; import org.junit.Test;
import java.util.List; import java.util.List;
@ -70,6 +71,7 @@ public class JsonPathTest {
private final static String ARRAY_EXPAND = "[{\"parent\": \"ONE\", \"child\": {\"name\": \"NAME_ONE\"}}, [{\"parent\": \"TWO\", \"child\": {\"name\": \"NAME_TWO\"}}]]"; private final static String ARRAY_EXPAND = "[{\"parent\": \"ONE\", \"child\": {\"name\": \"NAME_ONE\"}}, [{\"parent\": \"TWO\", \"child\": {\"name\": \"NAME_TWO\"}}]]";
@Test @Test
public void array_start_expands() throws Exception { public void array_start_expands() throws Exception {
assertThat(JsonPath.<List<String>>read(ARRAY_EXPAND, "$[?(@.parent = 'ONE')].child.name"), hasItems("NAME_ONE")); assertThat(JsonPath.<List<String>>read(ARRAY_EXPAND, "$[?(@.parent = 'ONE')].child.name"), hasItems("NAME_ONE"));
@ -78,6 +80,10 @@ public class JsonPathTest {
@Test @Test
public void bracket_notation_can_be_used_in_path() throws Exception { public void bracket_notation_can_be_used_in_path() throws Exception {
//System.out.println(ScriptEngineJsonPath.eval(DOCUMENT, "$.['store'].['bicycle'].['dot.notation']"));
System.out.println(ScriptEngineJsonPath.eval(DOCUMENT, "$.store.bicycle.['dot.notation']"));
assertEquals("new", JsonPath.read(DOCUMENT, "$.['store'].bicycle.['dot.notation']")); assertEquals("new", JsonPath.read(DOCUMENT, "$.['store'].bicycle.['dot.notation']"));
assertEquals("new", JsonPath.read(DOCUMENT, "$['store']['bicycle']['dot.notation']")); assertEquals("new", JsonPath.read(DOCUMENT, "$['store']['bicycle']['dot.notation']"));
assertEquals("new", JsonPath.read(DOCUMENT, "$.['store']['bicycle']['dot.notation']")); assertEquals("new", JsonPath.read(DOCUMENT, "$.['store']['bicycle']['dot.notation']"));

10
json-path/src/test/java/com/jayway/jsonpath/ParserTest.java

@ -1,11 +1,5 @@
package com.jayway.jsonpath; package com.jayway.jsonpath;
import org.junit.Test;
import java.text.ParseException;
import static junit.framework.Assert.assertEquals;
/** /**
* Created by IntelliJ IDEA. * Created by IntelliJ IDEA.
* User: kallestenflo * User: kallestenflo
@ -14,7 +8,7 @@ import static junit.framework.Assert.assertEquals;
*/ */
public class ParserTest { public class ParserTest {
/*
private final static String SINGLE_QUOTE_JSON = "{'lhs': '1.0 U.S. dollar','rhs': 6.39778892, 'error': null, 'icc': true}"; private final static String SINGLE_QUOTE_JSON = "{'lhs': '1.0 U.S. dollar','rhs': 6.39778892, 'error': null, 'icc': true}";
private final static String NO_QUOTE_JSON = "{lhs: 1.0 U.S. dollar, rhs: 6.39778892, error: null, icc: true}"; private final static String NO_QUOTE_JSON = "{lhs: 1.0 U.S. dollar, rhs: 6.39778892, error: null, icc: true}";
@ -46,6 +40,6 @@ public class ParserTest {
JsonPath.setMode(JsonPath.STRICT_MODE); JsonPath.setMode(JsonPath.STRICT_MODE);
JsonPath.read(NO_QUOTE_JSON, "lhs"); JsonPath.read(NO_QUOTE_JSON, "lhs");
} }
*/
} }

65
json-path/src/test/java/com/jayway/jsonpath/PathReaderTest.java

@ -0,0 +1,65 @@
package com.jayway.jsonpath;
import org.junit.Test;
import java.util.Map;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 11/8/11
* Time: 7:52 PM
*/
public class PathReaderTest {
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_be_read() throws Exception {
PathReader pathReader = PathReader.create(DOCUMENT);
Map<String, Object> book = pathReader.read("store.book[1]");
PathReader pathReader1 = pathReader.get("store.book[1]");
String author = pathReader1.read("author");
System.out.println("d");
}
}

64
json-path/src/test/java/com/jayway/jsonpath/PathTokenizerTest.java

@ -2,6 +2,8 @@ package com.jayway.jsonpath;
import com.jayway.jsonpath.reader.PathToken; import com.jayway.jsonpath.reader.PathToken;
import com.jayway.jsonpath.reader.PathTokenizer; import com.jayway.jsonpath.reader.PathTokenizer;
import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.spi.impl.DefaultJsonProvider;
import org.junit.Test; import org.junit.Test;
import java.util.List; import java.util.List;
@ -18,6 +20,8 @@ import static org.junit.Assert.assertThat;
*/ */
public class PathTokenizerTest { public class PathTokenizerTest {
private JsonProvider jsonProvider = new DefaultJsonProvider();
public final static String DOCUMENT = public final static String DOCUMENT =
"{ \"store\": {\n" + "{ \"store\": {\n" +
" \"book\": [ \n" + " \"book\": [ \n" +
@ -56,10 +60,10 @@ public class PathTokenizerTest {
@Test @Test
public void path_tokens_can_be_read() throws Exception { public void path_tokens_can_be_read() throws Exception {
Object result = JsonPath.parse(DOCUMENT); Object result = jsonProvider.parse(DOCUMENT);
for (PathToken pathToken : new PathTokenizer("$.store.bicycle.color")) { for (PathToken pathToken : new PathTokenizer("$.store.bicycle.color", jsonProvider)) {
result = pathToken.filter(result); result = pathToken.filter(result, jsonProvider);
} }
assertEquals("red", result); assertEquals("red", result);
@ -67,10 +71,10 @@ public class PathTokenizerTest {
@Test @Test
public void read_an_array_without_filters() throws Exception { public void read_an_array_without_filters() throws Exception {
Object result = JsonPath.parse(DOCUMENT); Object result = jsonProvider.parse(DOCUMENT);
for (PathToken pathToken : new PathTokenizer("$.store.book")) { for (PathToken pathToken : new PathTokenizer("$.store.book", jsonProvider)) {
result = pathToken.filter(result); result = pathToken.filter(result, jsonProvider);
} }
assertEquals(4, toList(result).size()); assertEquals(4, toList(result).size());
@ -78,10 +82,10 @@ public class PathTokenizerTest {
@Test @Test
public void read_a_literal_property_from_object_in_array() throws Exception { public void read_a_literal_property_from_object_in_array() throws Exception {
Object result = JsonPath.parse(DOCUMENT); Object result = jsonProvider.parse(DOCUMENT);
for (PathToken pathToken : new PathTokenizer("$.store.book[*].title")) { for (PathToken pathToken : new PathTokenizer("$.store.book[*].title", jsonProvider)) {
result = pathToken.filter(result); result = pathToken.filter(result, jsonProvider);
} }
assertEquals(4, toList(result).size()); assertEquals(4, toList(result).size());
@ -89,10 +93,10 @@ public class PathTokenizerTest {
@Test @Test
public void read_a_literal_property_from_position_in_array() throws Exception { public void read_a_literal_property_from_position_in_array() throws Exception {
Object result = JsonPath.parse(DOCUMENT); Object result = jsonProvider.parse(DOCUMENT);
for (PathToken pathToken : new PathTokenizer("$.store.book[0].title")) { for (PathToken pathToken : new PathTokenizer("$.store.book[0].title", jsonProvider)) {
result = pathToken.filter(result); result = pathToken.filter(result, jsonProvider);
} }
assertEquals("Sayings of the Century", result); assertEquals("Sayings of the Century", result);
@ -100,10 +104,10 @@ public class PathTokenizerTest {
@Test @Test
public void read_a_literal_property_from_two_positions_in_array() throws Exception { public void read_a_literal_property_from_two_positions_in_array() throws Exception {
Object result = JsonPath.parse(DOCUMENT); Object result = jsonProvider.parse(DOCUMENT);
for (PathToken pathToken : new PathTokenizer("$.store.book[0, 1].author")) { for (PathToken pathToken : new PathTokenizer("$.store.book[0, 1].author", jsonProvider)) {
result = pathToken.filter(result); result = pathToken.filter(result, jsonProvider);
} }
assertThat(this.<String>toList(result), hasItems("Nigel Rees", "Evelyn Waugh")); assertThat(this.<String>toList(result), hasItems("Nigel Rees", "Evelyn Waugh"));
@ -111,10 +115,10 @@ public class PathTokenizerTest {
@Test @Test
public void read_a_literal_property_from_head_in_array() throws Exception { public void read_a_literal_property_from_head_in_array() throws Exception {
Object result = JsonPath.parse(DOCUMENT); Object result = jsonProvider.parse(DOCUMENT);
for (PathToken pathToken : new PathTokenizer("$.store.book[:2].author")) { for (PathToken pathToken : new PathTokenizer("$.store.book[:2].author", jsonProvider)) {
result = pathToken.filter(result); result = pathToken.filter(result, jsonProvider);
} }
assertThat(this.<String>toList(result), hasItems("Nigel Rees", "Evelyn Waugh")); assertThat(this.<String>toList(result), hasItems("Nigel Rees", "Evelyn Waugh"));
@ -122,40 +126,40 @@ public class PathTokenizerTest {
@Test @Test
public void read_a_literal_property_from_tail_in_array() throws Exception { public void read_a_literal_property_from_tail_in_array() throws Exception {
Object result = JsonPath.parse(DOCUMENT); Object result = jsonProvider.parse(DOCUMENT);
for (PathToken pathToken : new PathTokenizer("$.store.book[-1:].author")) { for (PathToken pathToken : new PathTokenizer("$.store.book[-1:].author", jsonProvider)) {
result = pathToken.filter(result); result = pathToken.filter(result, jsonProvider);
} }
assertEquals("J. R. R. Tolkien", result); assertEquals("J. R. R. Tolkien", result);
} }
@Test @Test
public void field_defined_in_array_object() throws Exception { public void field_defined_in_array_object() throws Exception {
Object result = JsonPath.parse(DOCUMENT); Object result = jsonProvider.parse(DOCUMENT);
for (PathToken pathToken : new PathTokenizer("$.store.book[?(@.custom)].author")) { for (PathToken pathToken : new PathTokenizer("$.store.book[?(@.custom)].author", jsonProvider)) {
result = pathToken.filter(result); result = pathToken.filter(result, jsonProvider);
} }
assertThat(this.<String>toList(result), hasItems("J. R. R. Tolkien")); assertThat(this.<String>toList(result), hasItems("J. R. R. Tolkien"));
} }
@Test @Test
public void property_value_in_array_object() throws Exception { public void property_value_in_array_object() throws Exception {
Object result = JsonPath.parse(DOCUMENT); Object result = jsonProvider.parse(DOCUMENT);
for (PathToken pathToken : new PathTokenizer("$.store.book[?(@.custom = 'onely this')].author")) { for (PathToken pathToken : new PathTokenizer("$.store.book[?(@.custom = 'onely this')].author", jsonProvider)) {
result = pathToken.filter(result); result = pathToken.filter(result, jsonProvider);
} }
assertThat(this.<String>toList(result), hasItems("J. R. R. Tolkien")); assertThat(this.<String>toList(result), hasItems("J. R. R. Tolkien"));
} }
@Test @Test
public void deep_scan() throws Exception { public void deep_scan() throws Exception {
Object result = JsonPath.parse(DOCUMENT); Object result = jsonProvider.parse(DOCUMENT);
for (PathToken pathToken : new PathTokenizer("$..author")) { for (PathToken pathToken : new PathTokenizer("$..author", jsonProvider)) {
result = pathToken.filter(result); result = pathToken.filter(result, jsonProvider);
} }
assertThat(this.<String>toList(result), hasItems("Nigel Rees","Evelyn Waugh", "J. R. R. Tolkien")); assertThat(this.<String>toList(result), hasItems("Nigel Rees","Evelyn Waugh", "J. R. R. Tolkien"));
} }

18
json-path/src/test/java/com/jayway/jsonpath/SplitPathFragmentsTest.java

@ -1,10 +1,11 @@
package com.jayway.jsonpath; package com.jayway.jsonpath;
import com.jayway.jsonpath.reader.PathTokenizer;
import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.spi.impl.DefaultJsonProvider;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import org.junit.Test; import org.junit.Test;
import java.util.List;
import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.Matchers.hasItems;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -17,7 +18,7 @@ import static org.junit.Assert.assertTrue;
*/ */
public class SplitPathFragmentsTest { public class SplitPathFragmentsTest {
private JsonProvider jsonProvider = new DefaultJsonProvider();
@Test @Test
public void valid_path_is_split_correctly() throws Exception { public void valid_path_is_split_correctly() throws Exception {
@ -92,7 +93,7 @@ public class SplitPathFragmentsTest {
private void assertPathInvalid(String path) { private void assertPathInvalid(String path) {
try { try {
PathUtil.splitPath(path); PathTokenizer tokenizer = new PathTokenizer(path, jsonProvider);
assertTrue("Expected exception!", false); assertTrue("Expected exception!", false);
} catch (InvalidPathException expected) {} } catch (InvalidPathException expected) {}
} }
@ -100,13 +101,16 @@ public class SplitPathFragmentsTest {
private void assertPath(String path, Matcher<Iterable<String>> matcher) { private void assertPath(String path, Matcher<Iterable<String>> matcher) {
System.out.println("PATH: " + path); System.out.println("PATH: " + path);
List<String> fragments = PathUtil.splitPath(path);
for (String fragment : fragments) { PathTokenizer tokenizer = new PathTokenizer(path, jsonProvider);
for (String fragment : tokenizer.getFragments()) {
System.out.println(fragment); System.out.println(fragment);
} }
assertThat(fragments, matcher); assertThat(tokenizer.getFragments(), matcher);
System.out.println("----------------------------------"); System.out.println("----------------------------------");
} }

60
json-path/src/test/java/com/jayway/jsonpath/util/ScriptEngineJsonPath.java

@ -0,0 +1,60 @@
package com.jayway.jsonpath.util;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 11/8/11
* Time: 1:01 PM
*/
public class ScriptEngineJsonPath {
private static ScriptEngineManager manager = new ScriptEngineManager();
private static ScriptEngine engine = manager.getEngineByName("JavaScript");
private static final String JSON_PATH_SCRIPT = readScript("jsonpath-0.8.0.js");
private static final String JSON_SCRIPT = readScript("json.js");
private static final String WRAPPER_SCRIPT = readScript("wrapper.js");
static {
try {
engine.eval(JSON_PATH_SCRIPT);
engine.eval(JSON_SCRIPT);
engine.eval(WRAPPER_SCRIPT);
} catch (ScriptException e) {
throw new RuntimeException(e);
}
}
public static String eval(String json, String path) throws Exception {
Invocable inv = (Invocable) engine;
Object obj = engine.get("WRAPPER");
return (String)inv.invokeMethod(obj, "jsonPath", json, path);
}
private static String readScript(String script) {
InputStream is = null;
try {
is = ScriptEngineJsonPath.class.getClassLoader().getSystemResourceAsStream("js/" + script);
return new Scanner(is).useDelimiter("\\A").next();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
}
}
}
}
}

530
json-path/src/test/resources/js/json.js

@ -0,0 +1,530 @@
/*
json.js
2011-08-30
Public Domain
No warranty expressed or implied. Use at your own risk.
This file has been superceded by http://www.JSON.org/json2.js
See http://www.JSON.org/js.html
This code should be minified before deployment.
See http://javascript.crockford.com/jsmin.html
USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
NOT CONTROL.
This file adds these methods to JavaScript:
object.toJSONString(whitelist)
This method produce a JSON text from a JavaScript value.
It must not contain any cyclical references. Illegal values
will be excluded.
The default conversion for dates is to an ISO string. You can
add a toJSONString method to any date object to get a different
representation.
The object and array methods can take an optional whitelist
argument. A whitelist is an array of strings. If it is provided,
keys in objects not found in the whitelist are excluded.
string.parseJSON(filter)
This method parses a JSON text to produce an object or
array. It can throw a SyntaxError exception.
The optional filter parameter is a function which can filter and
transform the results. It receives each of the keys and values, and
its return value is used instead of the original value. If it
returns what it received, then structure is not modified. If it
returns undefined then the member is deleted.
Example:
// Parse the text. If a key contains the string 'date' then
// convert the value to a date.
myData = text.parseJSON(function (key, value) {
return key.indexOf('date') >= 0 ? new Date(value) : value;
});
This file will break programs with improper for..in loops. See
http://yuiblog.com/blog/2006/09/26/for-in-intrigue/
This file creates a global JSON object containing two methods: stringify
and parse.
JSON.stringify(value, replacer, space)
value any JavaScript value, usually an object or array.
replacer an optional parameter that determines how object
values are stringified for objects. It can be a
function or an array of strings.
space an optional parameter that specifies the indentation
of nested structures. If it is omitted, the text will
be packed without extra whitespace. If it is a number,
it will specify the number of spaces to indent at each
level. If it is a string (such as '\t' or '&nbsp;'),
it contains the characters used to indent at each level.
This method produces a JSON text from a JavaScript value.
When an object value is found, if the object contains a toJSON
method, its toJSON method will be called and the result will be
stringified. A toJSON method does not serialize: it returns the
value represented by the name/value pair that should be serialized,
or undefined if nothing should be serialized. The toJSON method
will be passed the key associated with the value, and this will be
bound to the object holding the key.
For example, this would serialize Dates as ISO strings.
Date.prototype.toJSON = function (key) {
function f(n) {
// Format integers to have at least two digits.
return n < 10 ? '0' + n : n;
}
return this.getUTCFullYear() + '-' +
f(this.getUTCMonth() + 1) + '-' +
f(this.getUTCDate()) + 'T' +
f(this.getUTCHours()) + ':' +
f(this.getUTCMinutes()) + ':' +
f(this.getUTCSeconds()) + 'Z';
};
You can provide an optional replacer method. It will be passed the
key and value of each member, with this bound to the containing
object. The value that is returned from your method will be
serialized. If your method returns undefined, then the member will
be excluded from the serialization.
If the replacer parameter is an array of strings, then it will be
used to select the members to be serialized. It filters the results
such that only members with keys listed in the replacer array are
stringified.
Values that do not have JSON representations, such as undefined or
functions, will not be serialized. Such values in objects will be
dropped; in arrays they will be replaced with null. You can use
a replacer function to replace those with JSON values.
JSON.stringify(undefined) returns undefined.
The optional space parameter produces a stringification of the
value that is filled with line breaks and indentation to make it
easier to read.
If the space parameter is a non-empty string, then that string will
be used for indentation. If the space parameter is a number, then
the indentation will be that many spaces.
Example:
text = JSON.stringify(['e', {pluribus: 'unum'}]);
// text is '["e",{"pluribus":"unum"}]'
text = JSON.stringify(['e', {pluribus: 'unum'}], null, '\t');
// text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'
text = JSON.stringify([new Date()], function (key, value) {
return this[key] instanceof Date ?
'Date(' + this[key] + ')' : value;
});
// text is '["Date(---current time---)"]'
JSON.parse(text, reviver)
This method parses a JSON text to produce an object or array.
It can throw a SyntaxError exception.
The optional reviver parameter is a function that can filter and
transform the results. It receives each of the keys and values,
and its return value is used instead of the original value.
If it returns what it received, then the structure is not modified.
If it returns undefined then the member is deleted.
Example:
// Parse the text. Values that look like ISO date strings will
// be converted to Date objects.
myData = JSON.parse(text, function (key, value) {
var a;
if (typeof value === 'string') {
a =
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
if (a) {
return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],
+a[5], +a[6]));
}
}
return value;
});
myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) {
var d;
if (typeof value === 'string' &&
value.slice(0, 5) === 'Date(' &&
value.slice(-1) === ')') {
d = new Date(value.slice(5, -1));
if (d) {
return d;
}
}
return value;
});
This is a reference implementation. You are free to copy, modify, or
redistribute.
*/
/*jslint evil: true, regexp: true, unparam: true */
/*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply,
call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,
lastIndex, length, parse, parseJSON, prototype, push, replace, slice,
stringify, test, toJSON, toJSONString, toString, valueOf
*/
// Create a JSON object only if one does not already exist. We create the
// methods in a closure to avoid creating global variables.
var JSON;
if (!JSON) {
JSON = {};
}
(function () {
'use strict';
function f(n) {
// Format integers to have at least two digits.
return n < 10 ? '0' + n : n;
}
if (typeof Date.prototype.toJSON !== 'function') {
Date.prototype.toJSON = function (key) {
return isFinite(this.valueOf()) ?
this.getUTCFullYear() + '-' +
f(this.getUTCMonth() + 1) + '-' +
f(this.getUTCDate()) + 'T' +
f(this.getUTCHours()) + ':' +
f(this.getUTCMinutes()) + ':' +
f(this.getUTCSeconds()) + 'Z' : null;
};
String.prototype.toJSON =
Number.prototype.toJSON =
Boolean.prototype.toJSON = function (key) {
return this.valueOf();
};
}
var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
gap,
indent,
meta = { // table of character substitutions
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
},
rep;
function quote(string) {
// If the string contains no control characters, no quote characters, and no
// backslash characters, then we can safely slap some quotes around it.
// Otherwise we must also replace the offending characters with safe escape
// sequences.
escapable.lastIndex = 0;
return escapable.test(string) ? '"' + string.replace(escapable, function (a) {
var c = meta[a];
return typeof c === 'string' ? c :
'\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
}) + '"' : '"' + string + '"';
}
function str(key, holder) {
// Produce a string from holder[key].
var i, // The loop counter.
k, // The member key.
v, // The member value.
length,
mind = gap,
partial,
value = holder[key];
// If the value has a toJSON method, call it to obtain a replacement value.
if (value && typeof value === 'object' &&
typeof value.toJSON === 'function') {
value = value.toJSON(key);
}
// If we were called with a replacer function, then call the replacer to
// obtain a replacement value.
if (typeof rep === 'function') {
value = rep.call(holder, key, value);
}
// What happens next depends on the value's type.
switch (typeof value) {
case 'string':
return quote(value);
case 'number':
// JSON numbers must be finite. Encode non-finite numbers as null.
return isFinite(value) ? String(value) : 'null';
case 'boolean':
case 'null':
// If the value is a boolean or null, convert it to a string. Note:
// typeof null does not produce 'null'. The case is included here in
// the remote chance that this gets fixed someday.
return String(value);
// If the type is 'object', we might be dealing with an object or an array or
// null.
case 'object':
// Due to a specification blunder in ECMAScript, typeof null is 'object',
// so watch out for that case.
if (!value) {
return 'null';
}
// Make an array to hold the partial results of stringifying this object value.
gap += indent;
partial = [];
// Is the value an array?
if (Object.prototype.toString.apply(value) === '[object Array]') {
// The value is an array. Stringify every element. Use null as a placeholder
// for non-JSON values.
length = value.length;
for (i = 0; i < length; i += 1) {
partial[i] = str(i, value) || 'null';
}
// Join all of the elements together, separated with commas, and wrap them in
// brackets.
v = partial.length === 0 ? '[]' : gap ?
'[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' :
'[' + partial.join(',') + ']';
gap = mind;
return v;
}
// If the replacer is an array, use it to select the members to be stringified.
if (rep && typeof rep === 'object') {
length = rep.length;
for (i = 0; i < length; i += 1) {
k = rep[i];
if (typeof k === 'string') {
v = str(k, value);
if (v) {
partial.push(quote(k) + (gap ? ': ' : ':') + v);
}
}
}
} else {
// Otherwise, iterate through all of the keys in the object.
for (k in value) {
if (Object.prototype.hasOwnProperty.call(value, k)) {
v = str(k, value);
if (v) {
partial.push(quote(k) + (gap ? ': ' : ':') + v);
}
}
}
}
// Join all of the member texts together, separated with commas,
// and wrap them in braces.
v = partial.length === 0 ? '{}' : gap ?
'{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' :
'{' + partial.join(',') + '}';
gap = mind;
return v;
}
}
// If the JSON object does not yet have a stringify method, give it one.
if (typeof JSON.stringify !== 'function') {
JSON.stringify = function (value, replacer, space) {
// The stringify method takes a value and an optional replacer, and an optional
// space parameter, and returns a JSON text. The replacer can be a function
// that can replace values, or an array of strings that will select the keys.
// A default replacer method can be provided. Use of the space parameter can
// produce text that is more easily readable.
var i;
gap = '';
indent = '';
// If the space parameter is a number, make an indent string containing that
// many spaces.
if (typeof space === 'number') {
for (i = 0; i < space; i += 1) {
indent += ' ';
}
// If the space parameter is a string, it will be used as the indent string.
} else if (typeof space === 'string') {
indent = space;
}
// If there is a replacer, it must be a function or an array.
// Otherwise, throw an error.
rep = replacer;
if (replacer && typeof replacer !== 'function' &&
(typeof replacer !== 'object' ||
typeof replacer.length !== 'number')) {
throw new Error('JSON.stringify');
}
// Make a fake root object containing our value under the key of ''.
// Return the result of stringifying the value.
return str('', {'': value});
};
}
// If the JSON object does not yet have a parse method, give it one.
if (typeof JSON.parse !== 'function') {
JSON.parse = function (text, reviver) {
// The parse method takes a text and an optional reviver function, and returns
// a JavaScript value if the text is a valid JSON text.
var j;
function walk(holder, key) {
// The walk method is used to recursively walk the resulting structure so
// that modifications can be made.
var k, v, value = holder[key];
if (value && typeof value === 'object') {
for (k in value) {
if (Object.prototype.hasOwnProperty.call(value, k)) {
v = walk(value, k);
if (v !== undefined) {
value[k] = v;
} else {
delete value[k];
}
}
}
}
return reviver.call(holder, key, value);
}
// Parsing happens in four stages. In the first stage, we replace certain
// Unicode characters with escape sequences. JavaScript handles many characters
// incorrectly, either silently deleting them, or treating them as line endings.
text = String(text);
cx.lastIndex = 0;
if (cx.test(text)) {
text = text.replace(cx, function (a) {
return '\\u' +
('0000' + a.charCodeAt(0).toString(16)).slice(-4);
});
}
// In the second stage, we run the text against regular expressions that look
// for non-JSON patterns. We are especially concerned with '()' and 'new'
// because they can cause invocation, and '=' because it can cause mutation.
// But just to be safe, we want to reject all unexpected forms.
// We split the second stage into 4 regexp operations in order to work around
// crippling inefficiencies in IE's and Safari's regexp engines. First we
// replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
// replace all simple value tokens with ']' characters. Third, we delete all
// open brackets that follow a colon or comma or that begin the text. Finally,
// we look to see that the remaining characters are only whitespace or ']' or
// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
if (/^[\],:{}\s]*$/
.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@')
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
.replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
// In the third stage we use the eval function to compile the text into a
// JavaScript structure. The '{' operator is subject to a syntactic ambiguity
// in JavaScript: it can begin a block or an object literal. We wrap the text
// in parens to eliminate the ambiguity.
j = eval('(' + text + ')');
// In the optional fourth stage, we recursively walk the new structure, passing
// each name/value pair to a reviver function for possible transformation.
return typeof reviver === 'function' ?
walk({'': j}, '') : j;
}
// If the text is not JSON parseable, then a SyntaxError is thrown.
throw new SyntaxError('JSON.parse');
};
}
// Augment the basic prototypes if they have not already been augmented.
// These forms are obsolete. It is recommended that JSON.stringify and
// JSON.parse be used instead.
if (!Object.prototype.toJSONString) {
Object.prototype.toJSONString = function (filter) {
return JSON.stringify(this, filter);
};
Object.prototype.parseJSON = function (filter) {
return JSON.parse(this, filter);
};
}
}());

87
json-path/src/test/resources/js/jsonpath-0.8.0.js

@ -0,0 +1,87 @@
/* JSONPath 0.8.0 - XPath for JSON
*
* Copyright (c) 2007 Stefan Goessner (goessner.net)
* Licensed under the MIT (MIT-LICENSE.txt) licence.
*/
function jsonPath(obj, expr, arg) {
var P = {
resultType: arg && arg.resultType || "VALUE",
result: [],
normalize: function(expr) {
var subx = [];
return expr.replace(/[\['](\??\(.*?\))[\]']/g, function($0,$1){return "[#"+(subx.push($1)-1)+"]";})
.replace(/'?\.'?|\['?/g, ";")
.replace(/;;;|;;/g, ";..;")
.replace(/;$|'?\]|'$/g, "")
.replace(/#([0-9]+)/g, function($0,$1){return subx[$1];});
},
asPath: function(path) {
var x = path.split(";"), p = "$";
for (var i=1,n=x.length; i<n; i++)
p += /^[0-9*]+$/.test(x[i]) ? ("["+x[i]+"]") : ("['"+x[i]+"']");
return p;
},
store: function(p, v) {
if (p) P.result[P.result.length] = P.resultType == "PATH" ? P.asPath(p) : v;
return !!p;
},
trace: function(expr, val, path) {
if (expr) {
var x = expr.split(";"), loc = x.shift();
x = x.join(";");
if (val && val.hasOwnProperty(loc))
P.trace(x, val[loc], path + ";" + loc);
else if (loc === "*")
P.walk(loc, x, val, path, function(m,l,x,v,p) { P.trace(m+";"+x,v,p); });
else if (loc === "..") {
P.trace(x, val, path);
P.walk(loc, x, val, path, function(m,l,x,v,p) { typeof v[m] === "object" && P.trace("..;"+x,v[m],p+";"+m); });
}
else if (/,/.test(loc)) { // [name1,name2,...]
for (var s=loc.split(/'?,'?/),i=0,n=s.length; i<n; i++)
P.trace(s[i]+";"+x, val, path);
}
else if (/^\(.*?\)$/.test(loc)) // [(expr)]
P.trace(P.eval(loc, val, path.substr(path.lastIndexOf(";")+1))+";"+x, val, path);
else if (/^\?\(.*?\)$/.test(loc)) // [?(expr)]
P.walk(loc, x, val, path, function(m,l,x,v,p) { if (P.eval(l.replace(/^\?\((.*?)\)$/,"$1"),v[m],m)) P.trace(m+";"+x,v,p); });
else if (/^(-?[0-9]*):(-?[0-9]*):?([0-9]*)$/.test(loc)) // [start:end:step] phyton slice syntax
P.slice(loc, x, val, path);
}
else
P.store(path, val);
},
walk: function(loc, expr, val, path, f) {
if (val instanceof Array) {
for (var i=0,n=val.length; i<n; i++)
if (i in val)
f(i,loc,expr,val,path);
}
else if (typeof val === "object") {
for (var m in val)
if (val.hasOwnProperty(m))
f(m,loc,expr,val,path);
}
},
slice: function(loc, expr, val, path) {
if (val instanceof Array) {
var len=val.length, start=0, end=len, step=1;
loc.replace(/^(-?[0-9]*):(-?[0-9]*):?(-?[0-9]*)$/g, function($0,$1,$2,$3){start=parseInt($1||start);end=parseInt($2||end);step=parseInt($3||step);});
start = (start < 0) ? Math.max(0,start+len) : Math.min(len,start);
end = (end < 0) ? Math.max(0,end+len) : Math.min(len,end);
for (var i=start; i<end; i+=step)
P.trace(i+";"+expr, val, path);
}
},
eval: function(x, _v, _vname) {
try { return $ && _v && eval(x.replace(/@/g, "_v")); }
catch(e) { throw new SyntaxError("jsonPath: " + e.message + ": " + x.replace(/@/g, "_v").replace(/\^/g, "_a")); }
}
};
var $ = obj;
if (expr && obj && (P.resultType == "VALUE" || P.resultType == "PATH")) {
P.trace(P.normalize(expr).replace(/^\$;/,""), obj, "$");
return P.result.length ? P.result : false;
}
}

57
json-path/src/test/resources/js/path.html

@ -0,0 +1,57 @@
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script src="jsonpath-0.8.0.js"></script>
<script>
$(document).ready(function() {
window.doc = { "store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{ "category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{ "category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95,
"dot.notation": "new"
}
}
};
$('#btnEval').click(function(e) {
$('#result').empty();
var path = $('#txtPath').attr('value');
var res = jsonPath(window.doc, path);
$('#result').append("PATH : " + path + "<hr/>");
$('#result').append(JSON.stringify(res, null, 4));
});
});
</script>
</head>
<body>
<input id="txtPath" type="text" value="$.store.book[?(@.price<10)].title"><button id="btnEval">eval</button>
<pre id="result"></pre>
</body>
</html>

12
json-path/src/test/resources/js/wrapper.js

@ -0,0 +1,12 @@
var WRAPPER;
if (!WRAPPER) {
WRAPPER = {
jsonPath: function(json, path) {
var jsonObj = JSON.parse(json);
return JSON.stringify(jsonPath(jsonObj, path), null, 4);
}
};
}
Loading…
Cancel
Save