diff --git a/json-path-assert/src/main/java/com/jayway/jsonassert/JsonAssert.java b/json-path-assert/src/main/java/com/jayway/jsonassert/JsonAssert.java index d608e2e9..921b0cf0 100644 --- a/json-path-assert/src/main/java/com/jayway/jsonassert/JsonAssert.java +++ b/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.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 java.io.*; @@ -18,23 +19,17 @@ import java.util.Map; */ 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 SLACK_MODE = -1; private static int mode = SLACK_MODE; - public static void setMode(int mode) { - if (mode != JsonAssert.mode) { - JsonAssert.mode = mode; - JSON_PARSER = new JSONParser(JsonAssert.mode); - } + public static void setJsonProvider(JsonProvider jsonProvider) { + JsonAssert.jsonProvider = jsonProvider; } - public static int getMode() { - return mode; - } /** * Creates a JSONAsserter @@ -43,14 +38,8 @@ public class JsonAssert { * @return a JSON asserter initialized with the provided document * @throws ParseException when the given JSON could not be parsed */ - public static JsonAsserter with(String json) throws ParseException { - try { - 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); - } + public static JsonAsserter with(String json) { + return new JsonAsserterImpl(jsonProvider.parse(json)); } /** @@ -60,12 +49,9 @@ public class JsonAssert { * @return a JSON asserter initialized with the provided document * @throws ParseException when the given JSON could not be parsed */ - public static JsonAsserter with(Reader reader) throws ParseException, IOException { - try { - return new JsonAsserterImpl(JSON_PARSER.parse(convertReaderToString(reader))); - } catch (net.minidev.json.parser.ParseException e) { - throw new ParseException(e.toString(), e.getPosition()); - } + public static JsonAsserter with(Reader reader) throws IOException { + return new JsonAsserterImpl(jsonProvider.parse(convertReaderToString(reader))); + } /** diff --git a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsCollectionWithSize.java b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsCollectionWithSize.java index cef7e0b0..c4b7c1df 100644 --- a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsCollectionWithSize.java +++ b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsCollectionWithSize.java @@ -29,14 +29,14 @@ DAMAGE. */ package com.jayway.jsonassert.impl.matcher; -import static org.hamcrest.core.IsEqual.equalTo; - import org.hamcrest.Description; import org.hamcrest.Factory; import org.hamcrest.Matcher; import java.util.Collection; +import static org.hamcrest.core.IsEqual.equalTo; + /** * Matches if collection size satisfies a nested matcher. */ diff --git a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingKey.java b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingKey.java index 58bfc239..97669449 100644 --- a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingKey.java +++ b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingKey.java @@ -29,11 +29,12 @@ DAMAGE. */ package com.jayway.jsonassert.impl.matcher; -import java.util.Map; - import org.hamcrest.Description; import org.hamcrest.Factory; import org.hamcrest.Matcher; + +import java.util.Map; + import static org.hamcrest.core.IsEqual.equalTo; public class IsMapContainingKey extends MapTypeSafeMatcher> { diff --git a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingValue.java b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingValue.java index 9a4b08a4..9fb3a6d0 100644 --- a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingValue.java +++ b/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.Factory; import org.hamcrest.Matcher; -import static org.hamcrest.core.IsEqual.equalTo; + import java.util.Map; +import static org.hamcrest.core.IsEqual.equalTo; + public class IsMapContainingValue extends MapTypeSafeMatcher>{ private final Matcher valueMatcher; diff --git a/json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java b/json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java index 15e2c2b3..4ecff3d2 100644 --- a/json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java +++ b/json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java @@ -1,6 +1,5 @@ package com.jayway.jsonassert; -import org.hamcrest.Matchers; import org.junit.Test; import java.io.InputStream; @@ -141,10 +140,12 @@ public class JsonAssertTest { .assertThat("$['store'].book[0].title", equalTo("Sayings of the Century")); } + /* @Test public void no_hit_returns_null() throws Exception { with(JSON).assertThat("$.store.book[1000].title", Matchers.nullValue()); } + */ @Test public void invalid_path() throws Exception { diff --git a/json-path/src/main/java/com/jayway/jsonpath/InvalidJsonException.java b/json-path/src/main/java/com/jayway/jsonpath/InvalidJsonException.java new file mode 100644 index 00000000..5c2aae5d --- /dev/null +++ b/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); + } +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java index 056b927f..fe252c3a 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java +++ b/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.PathTokenizer; -import net.minidev.json.parser.JSONParser; -import net.minidev.json.parser.ParseException; +import com.jayway.jsonpath.spi.JsonProvider; +import com.jayway.jsonpath.spi.impl.DefaultJsonProvider; -import java.io.IOException; import java.util.List; import java.util.Map; -import java.util.logging.Logger; import java.util.regex.Pattern; /** @@ -76,29 +74,11 @@ import java.util.regex.Pattern; */ 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 PathTokenizer tokenizer; - public static void setMode(int mode) { - if (mode != JsonPath.mode) { - JsonPath.mode = mode; - JSON_PARSER = new JSONParser(JsonPath.mode); - } - } - - public static int getMode() { - return mode; - } + private JsonProvider jsonProvider; /** @@ -107,6 +87,11 @@ public class JsonPath { * @param jsonPath the path statement */ private JsonPath(String jsonPath) { + this(new DefaultJsonProvider(), jsonPath); + } + + + private JsonPath(JsonProvider jsonProvider, String jsonPath) { if (jsonPath == null || jsonPath.trim().isEmpty() || jsonPath.matches("new ") || @@ -114,8 +99,8 @@ public class JsonPath { throw new InvalidPathException("Invalid path"); } - this.tokenizer = new PathTokenizer(jsonPath); - //this.filters = new JsonPathFilterChain(PathUtil.splitPath(jsonPath)); + this.jsonProvider = jsonProvider; + this.tokenizer = new PathTokenizer(jsonPath, jsonProvider); } public String getPath() { @@ -159,26 +144,16 @@ public class JsonPath { * @return list of objects matched by the given path */ public T read(Object container) { - - if(!(container instanceof Map) && !(container instanceof List) ){ + if (!(container instanceof Map) && !(container instanceof List)) { throw new IllegalArgumentException("Invalid container object"); } Object result = container; for (PathToken pathToken : tokenizer) { - result = pathToken.filter(result); + result = pathToken.filter(result, jsonProvider); } - return (T)result; - /* - FilterOutput filterOutput = filters.filter(container); - - if (filterOutput == null || filterOutput.getResult() == null) { - return null; - } - - return (T) filterOutput.getResult(); - */ + return (T) result; } /** @@ -188,8 +163,8 @@ public class JsonPath { * @param * @return list of objects matched by the given path */ - public T read(String json) throws java.text.ParseException { - return (T) read(parse(json)); + public T read(String json) { + return (T) read(jsonProvider.parse(json)); } /** @@ -202,6 +177,10 @@ public class 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 * @@ -210,7 +189,7 @@ public class JsonPath { * @param * @return list of objects matched by the given path */ - public static T read(String json, String jsonPath) throws java.text.ParseException { + public static T read(String json, String jsonPath) { return (T) compile(jsonPath).read(json); } @@ -226,14 +205,4 @@ public class JsonPath { 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); - } - } } diff --git a/json-path/src/main/java/com/jayway/jsonpath/JsonUtil.java b/json-path/src/main/java/com/jayway/jsonpath/JsonUtil.java deleted file mode 100644 index c499b00c..00000000 --- a/json-path/src/main/java/com/jayway/jsonpath/JsonUtil.java +++ /dev/null @@ -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 instanceof java.util.List or java.util.Map - * - * @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 instanceof java.util.List - * - * @param obj object to check - * @return true if List - */ - public static boolean isList(Object obj) { - return (obj instanceof List); - } - - /** - * checks if object is instanceof java.util.Map - * - * @param obj object to check - * @return true if Map - */ - public static boolean isMap(Object obj) { - return (obj instanceof Map); - } - - /** - * converts casts to java.util.List - * - * @param obj - * @return the list - */ - public static List toList(Object obj) { - return (List) obj; - } - - /** - * converts casts to java.util.Map - * - * @param obj - * @return the Map - */ - public static Map toMap(Object obj) { - return (Map) obj; - } -} diff --git a/json-path/src/main/java/com/jayway/jsonpath/PathReader.java b/json-path/src/main/java/com/jayway/jsonpath/PathReader.java new file mode 100644 index 00000000..de69768e --- /dev/null +++ b/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 read(String jsonPath) { + return (T)JsonPath.compile(jsonProvider, jsonPath).read(jsonModel); + } + + public T read(JsonPath jsonPath) { + return (T)jsonPath.read(jsonModel); + } + + public PathReader get(String jsonPath){ + Object subModel = read(jsonPath); + + return new PathReader(subModel); + } +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/PathUtil.java b/json-path/src/main/java/com/jayway/jsonpath/PathUtil.java deleted file mode 100644 index ee8c9727..00000000 --- a/json-path/src/main/java/com/jayway/jsonpath/PathUtil.java +++ /dev/null @@ -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 - *

- * a path is considered not definite if it contains a scan fragment ".." - * or an array position fragment that is not based on a single index - *

- *

- * absolute path examples: - *

- * $store.book - * $store.book[1].value - *

- * not absolute path examples - *

- * $..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 - *

- * the path $.store.book[1].category returns ["$", "store", "book", "[1]", "category"] - * - * @param jsonPath path to split - * @return fragments - */ - public static List splitPath(String jsonPath) { - - if (!jsonPath.startsWith("$") && !jsonPath.startsWith("$[")) { - jsonPath = "$." + jsonPath; - } - - LinkedList fragments = new LinkedList(); - - Queue pathQueue = new LinkedList(); - 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 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 pathQueue, char target) { - if (pathQueue.isEmpty()) { - return; - } - while (pathQueue.peek().charValue() == target) { - pathQueue.poll(); - } - } - - private static void assertNotInvalidPeek(Queue 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 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"); - } - } - -} diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/FilterOutput.java b/json-path/src/main/java/com/jayway/jsonpath/filter/FilterOutput.java deleted file mode 100644 index 4d868dfe..00000000 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/FilterOutput.java +++ /dev/null @@ -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 getResultAsList() { - if(!isList()){ - throw new RuntimeException(format("Can not convert a %s to a %s", result.getClass().getName(), List.class.getName())); - } - return (List)result; - } - - - -} diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterBase.java b/json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterBase.java deleted file mode 100644 index 9b920ab4..00000000 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterBase.java +++ /dev/null @@ -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); -} diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterChain.java b/json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterChain.java deleted file mode 100644 index 4f183641..00000000 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterChain.java +++ /dev/null @@ -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 filters; - - public JsonPathFilterChain(List pathFragments) { - filters = configureFilters(pathFragments); - } - - private List configureFilters(List pathFragments) { - - List configured = new LinkedList(); - - 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; - } -} diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterFactory.java b/json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterFactory.java deleted file mode 100644 index 1df277ac..00000000 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterFactory.java +++ /dev/null @@ -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; - - } - -} diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/ListEvalFilter.java b/json-path/src/main/java/com/jayway/jsonpath/filter/ListEvalFilter.java deleted file mode 100644 index 191e7c86..00000000 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/ListEvalFilter.java +++ /dev/null @@ -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 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; - } -} diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/ListFrontFilter.java b/json-path/src/main/java/com/jayway/jsonpath/filter/ListFrontFilter.java deleted file mode 100644 index 797e0d1f..00000000 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/ListFrontFilter.java +++ /dev/null @@ -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 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 result = new LinkedList(); - - 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; - } - } -} diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/ListIndexFilter.java b/json-path/src/main/java/com/jayway/jsonpath/filter/ListIndexFilter.java deleted file mode 100644 index 18c72cc1..00000000 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/ListIndexFilter.java +++ /dev/null @@ -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 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 index = new LinkedList(); - - String[] split = prepared.split(","); - - for (String s : split) { - index.add(Integer.parseInt(s)); - } - return index.toArray(new Integer[0]); - } -} diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/ListPropertyFilter.java b/json-path/src/main/java/com/jayway/jsonpath/filter/ListPropertyFilter.java deleted file mode 100644 index 33b3d4fd..00000000 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/ListPropertyFilter.java +++ /dev/null @@ -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 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"); - } -} diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/ListTailFilter.java b/json-path/src/main/java/com/jayway/jsonpath/filter/ListTailFilter.java deleted file mode 100644 index f2153ee4..00000000 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/ListTailFilter.java +++ /dev/null @@ -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"); - - } -} diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/ListWildcardFilter.java b/json-path/src/main/java/com/jayway/jsonpath/filter/ListWildcardFilter.java deleted file mode 100644 index fa9a896c..00000000 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/ListWildcardFilter.java +++ /dev/null @@ -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()); - } -} diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/PropertyFilter.java b/json-path/src/main/java/com/jayway/jsonpath/filter/PropertyFilter.java deleted file mode 100644 index 96f7a4fa..00000000 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/PropertyFilter.java +++ /dev/null @@ -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 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); - } - } -} diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/RootFilter.java b/json-path/src/main/java/com/jayway/jsonpath/filter/RootFilter.java deleted file mode 100644 index f796ef13..00000000 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/RootFilter.java +++ /dev/null @@ -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; - } -} diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/TraverseFilter.java b/json-path/src/main/java/com/jayway/jsonpath/filter/TraverseFilter.java deleted file mode 100644 index c8b88a0c..00000000 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/TraverseFilter.java +++ /dev/null @@ -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 result = new JSONArray(); - - traverse(filter.getResult(), result); - - return new FilterOutput(result); - } - - private void traverse(Object container, List 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); - } - } - } - } - -} \ No newline at end of file diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/WildcardPropertyFilter.java b/json-path/src/main/java/com/jayway/jsonpath/filter/WildcardPropertyFilter.java deleted file mode 100644 index 23740456..00000000 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/WildcardPropertyFilter.java +++ /dev/null @@ -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 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); - - } -} diff --git a/json-path/src/main/java/com/jayway/jsonpath/reader/PathToken.java b/json-path/src/main/java/com/jayway/jsonpath/reader/PathToken.java index fa44e7a6..78a44b7b 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/reader/PathToken.java +++ b/json-path/src/main/java/com/jayway/jsonpath/reader/PathToken.java @@ -1,7 +1,7 @@ package com.jayway.jsonpath.reader; -import com.jayway.jsonpath.reader.filter.Filter; import com.jayway.jsonpath.reader.filter.FilterFactory; +import com.jayway.jsonpath.spi.JsonProvider; /** * Created by IntelliJ IDEA. @@ -11,13 +11,17 @@ import com.jayway.jsonpath.reader.filter.FilterFactory; */ public class PathToken { - private Filter filter; + private String fragment; - public PathToken(String pathFragment) { - filter = FilterFactory.createFilter(pathFragment); + public PathToken(String fragment) { + this.fragment = fragment; } - public Object filter(Object model){ - return filter.filter(model); + public Object filter(Object model, JsonProvider jsonProvider){ + return FilterFactory.createFilter(fragment).filter(model, jsonProvider); + } + + public String getFragment() { + return fragment; } } diff --git a/json-path/src/main/java/com/jayway/jsonpath/reader/PathTokenizer.java b/json-path/src/main/java/com/jayway/jsonpath/reader/PathTokenizer.java index f386f96c..e1fa31f8 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/reader/PathTokenizer.java +++ b/json-path/src/main/java/com/jayway/jsonpath/reader/PathTokenizer.java @@ -1,6 +1,7 @@ package com.jayway.jsonpath.reader; import com.jayway.jsonpath.InvalidPathException; +import com.jayway.jsonpath.spi.JsonProvider; import java.util.Iterator; import java.util.LinkedList; @@ -19,7 +20,7 @@ public class PathTokenizer implements Iterable { private int index = 0; private List pathTokens = new LinkedList(); - public PathTokenizer(String jsonPath) { + public PathTokenizer(String jsonPath, JsonProvider jsonProvider) { if (!jsonPath.startsWith("$") && !jsonPath.startsWith("$[")) { jsonPath = "$." + jsonPath; @@ -32,6 +33,14 @@ public class PathTokenizer implements Iterable { } } + public List getFragments(){ + List fragments = new LinkedList(); + for (PathToken pathToken : pathTokens) { + fragments.add(pathToken.getFragment()); + } + return fragments; + } + public String getPath() { return path; } @@ -177,7 +186,7 @@ public class PathTokenizer implements Iterable { return; } while (pathChars[index] == target) { - index++; + poll(); } } diff --git a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/ArrayEvalFilter.java b/json-path/src/main/java/com/jayway/jsonpath/reader/filter/ArrayEvalFilter.java index 781ad048..6be24054 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/ArrayEvalFilter.java +++ b/json-path/src/main/java/com/jayway/jsonpath/reader/filter/ArrayEvalFilter.java @@ -1,9 +1,9 @@ package com.jayway.jsonpath.reader.filter; 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.Map; import java.util.regex.Matcher; @@ -24,31 +24,28 @@ public class ArrayEvalFilter extends Filter { } @Override - public Object filter(Object obj) { + public Object filter(Object obj, JsonProvider jsonProvider) { //[?(@.isbn = 10)] - List src = toList(obj); - List result = new LinkedList(); + List src = jsonProvider.toList(obj); + List result = jsonProvider.createList(); String trimmedCondition = trim(condition, 5, 2); ConditionStatement conditionStatement = createConditionStatement(trimmedCondition); - - - for (Object item : src) { - if (isMatch(item, conditionStatement)) { + if (isMatch(item, conditionStatement, jsonProvider)) { result.add(item); } } return result; } - private boolean isMatch(Object check, ConditionStatement conditionStatement) { - if (!isMap(check)) { + private boolean isMatch(Object check, ConditionStatement conditionStatement, JsonProvider jsonProvider) { + if (!jsonProvider.isMap(check)) { return false; } - Map obj = toMap(check); + Map obj = jsonProvider.toMap(check); if (!obj.containsKey(conditionStatement.getField())) { return false; @@ -56,7 +53,7 @@ public class ArrayEvalFilter extends Filter { Object propertyValue = obj.get(conditionStatement.getField()); - if (isContainer(propertyValue)) { + if (jsonProvider.isContainer(propertyValue)) { return false; } return ExpressionEvaluator.eval(propertyValue, conditionStatement.getOperator(), conditionStatement.getExpected()); diff --git a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/ArrayIndexFilter.java b/json-path/src/main/java/com/jayway/jsonpath/reader/filter/ArrayIndexFilter.java index 3d3bb00d..cb5d46ba 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/ArrayIndexFilter.java +++ b/json-path/src/main/java/com/jayway/jsonpath/reader/filter/ArrayIndexFilter.java @@ -1,6 +1,7 @@ package com.jayway.jsonpath.reader.filter; -import java.util.LinkedList; +import com.jayway.jsonpath.spi.JsonProvider; + import java.util.List; /** @@ -15,10 +16,10 @@ public class ArrayIndexFilter extends Filter { } @Override - public Object filter(Object obj) { + public Object filter(Object obj,JsonProvider jsonProvider) { - List src = toList(obj); - List result = new LinkedList(); + List src = jsonProvider.toList(obj); + List result = jsonProvider.createList(); String trimmedCondition = trim(condition, 1, 1); diff --git a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/FieldFilter.java b/json-path/src/main/java/com/jayway/jsonpath/reader/filter/FieldFilter.java index de8f4d87..82f08c35 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/FieldFilter.java +++ b/json-path/src/main/java/com/jayway/jsonpath/reader/filter/FieldFilter.java @@ -1,6 +1,7 @@ package com.jayway.jsonpath.reader.filter; -import java.util.LinkedList; +import com.jayway.jsonpath.spi.JsonProvider; + import java.util.List; import java.util.Map; @@ -16,16 +17,16 @@ public class FieldFilter extends Filter { super(condition); } - public Object filter(Object obj) { - if (isList(obj)) { - List result = new LinkedList(); - for (Object current : toList(obj)) { - if (isMap(current)) { - Map map = toMap(current); + public Object filter(Object obj, JsonProvider jsonProvider) { + if (jsonProvider.isList(obj)) { + List result = jsonProvider.createList(); + for (Object current : jsonProvider.toList(obj)) { + if (jsonProvider.isMap(current)) { + Map map = jsonProvider.toMap(current); if (map.containsKey(condition)) { Object o = map.get(condition); - if (isList(o)) { - result.addAll(toList(o)); + if (jsonProvider.isList(o)) { + result.addAll(jsonProvider.toList(o)); } else { result.add(map.get(condition)); } @@ -34,7 +35,7 @@ public class FieldFilter extends Filter { } return result; } else { - return getMapValue(obj, condition); + return jsonProvider.getMapValue(obj, condition); } } } diff --git a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/Filter.java b/json-path/src/main/java/com/jayway/jsonpath/reader/filter/Filter.java index 8d9b53bc..c6746a77 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/Filter.java +++ b/json-path/src/main/java/com/jayway/jsonpath/reader/filter/Filter.java @@ -1,7 +1,6 @@ package com.jayway.jsonpath.reader.filter; -import java.util.List; -import java.util.Map; +import com.jayway.jsonpath.spi.JsonProvider; /** * Created by IntelliJ IDEA. @@ -17,48 +16,6 @@ public abstract class Filter { this.condition = condition; } - /** - * checks if object is instanceof java.util.List or java.util.Map - * - * @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 instanceof java.util.List - * - * @param obj object to check - * @return true if List - */ - protected boolean isList(Object obj) { - return (obj instanceof List); - } - - protected List toList(Object list) { - return (List) list; - } - - protected Map toMap(Object map) { - return (Map) map; - } - - protected Object getMapValue(Object obj, String key) { - Map map = toMap(obj); - return map.get(key); - } - - /** - * checks if object is instanceof java.util.Map - * - * @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) { String res = str; @@ -72,6 +29,6 @@ public abstract class Filter { return res; } - public abstract Object filter(Object obj); + public abstract Object filter(Object obj, JsonProvider jsonProvider); } diff --git a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/FilterFactory.java b/json-path/src/main/java/com/jayway/jsonpath/reader/filter/FilterFactory.java index 45f6ebd7..020a5d50 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/FilterFactory.java +++ b/json-path/src/main/java/com/jayway/jsonpath/reader/filter/FilterFactory.java @@ -1,7 +1,5 @@ package com.jayway.jsonpath.reader.filter; -import com.jayway.jsonpath.filter.ListEvalFilter; - /** * Created by IntelliJ IDEA. * User: kallestenflo @@ -10,19 +8,28 @@ import com.jayway.jsonpath.filter.ListEvalFilter; */ 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) { - 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)) { - return new WildcardFilter(pathFragment); + return WILDCARD_FILTER; } else if (pathFragment.contains("..")) { - return new ScanFilter(pathFragment); + return SCAN_FILTER; } else if (!pathFragment.contains("[")) { diff --git a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/HasFieldFilter.java b/json-path/src/main/java/com/jayway/jsonpath/reader/filter/HasFieldFilter.java index 1025692e..cb6c609f 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/HasFieldFilter.java +++ b/json-path/src/main/java/com/jayway/jsonpath/reader/filter/HasFieldFilter.java @@ -1,6 +1,7 @@ package com.jayway.jsonpath.reader.filter; -import java.util.LinkedList; +import com.jayway.jsonpath.spi.JsonProvider; + import java.util.List; import java.util.Map; @@ -17,17 +18,17 @@ public class HasFieldFilter extends Filter { } @Override - public Object filter(Object obj) { + public Object filter(Object obj, JsonProvider jsonProvider) { //[?(@.isbn)] - List src = toList(obj); - List result = new LinkedList(); + List src = jsonProvider.toList(obj); + List result = jsonProvider.createList(); String trimmedCondition = trim(condition, 5, 2); for (Object item : src) { - if(isMap(item)){ - Map map = toMap(item); + if(jsonProvider.isMap(item)){ + Map map = jsonProvider.toMap(item); if(map.containsKey(trimmedCondition)){ result.add(map); } diff --git a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/PassThrewFilter.java b/json-path/src/main/java/com/jayway/jsonpath/reader/filter/PassThrewFilter.java index 3d7b84e4..00c90a7f 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/PassThrewFilter.java +++ b/json-path/src/main/java/com/jayway/jsonpath/reader/filter/PassThrewFilter.java @@ -1,5 +1,7 @@ package com.jayway.jsonpath.reader.filter; +import com.jayway.jsonpath.spi.JsonProvider; + /** * Created by IntelliJ IDEA. * User: kallestenflo @@ -12,7 +14,7 @@ public class PassThrewFilter extends Filter { super(condition); } - public Object filter(Object obj) { + public Object filter(Object obj, JsonProvider jsonProvider) { return obj; } } diff --git a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/ScanFilter.java b/json-path/src/main/java/com/jayway/jsonpath/reader/filter/ScanFilter.java index 637973b3..609e2da2 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/ScanFilter.java +++ b/json-path/src/main/java/com/jayway/jsonpath/reader/filter/ScanFilter.java @@ -1,8 +1,8 @@ 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; /** @@ -18,29 +18,29 @@ public class ScanFilter extends Filter { } @Override - public Object filter(Object obj) { - List result = new LinkedList(); - scan(obj, result); + public Object filter(Object obj, JsonProvider jsonProvider) { + List result = jsonProvider.createList(); + scan(obj, result, jsonProvider); return result; } - private void scan(Object container, List result) { + private void scan(Object container, List result, JsonProvider jsonProvider) { - if (isMap(container)) { + if (jsonProvider.isMap(container)) { result.add(container); - for (Object value : toMap(container).values()) { - if (isContainer(value)) { - scan(value, result); + for (Object value : jsonProvider.toMap(container).values()) { + if (jsonProvider.isContainer(value)) { + scan(value, result, jsonProvider); } } - } else if (isList(container)) { + } else if (jsonProvider.isList(container)) { - for (Object value : JsonUtil.toList(container)) { - if (isContainer(value)) { - scan(value, result); + for (Object value : jsonProvider.toList(container)) { + if (jsonProvider.isContainer(value)) { + scan(value, result, jsonProvider); } } } diff --git a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/WildcardFilter.java b/json-path/src/main/java/com/jayway/jsonpath/reader/filter/WildcardFilter.java index 447fee3d..f3976910 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/reader/filter/WildcardFilter.java +++ b/json-path/src/main/java/com/jayway/jsonpath/reader/filter/WildcardFilter.java @@ -1,9 +1,7 @@ package com.jayway.jsonpath.reader.filter; -import com.jayway.jsonpath.JsonUtil; -import net.minidev.json.JSONArray; +import com.jayway.jsonpath.spi.JsonProvider; -import java.util.LinkedList; import java.util.List; /** @@ -19,17 +17,17 @@ public class WildcardFilter extends Filter { } @Override - public Object filter(Object obj) { - List result = new LinkedList(); + public Object filter(Object obj, JsonProvider jsonProvider) { + List result = jsonProvider.createList(); - if (isList(obj)) { - for (Object current : toList(obj)) { - for (Object value : toMap(current).values()) { + if (jsonProvider.isList(obj)) { + for (Object current : jsonProvider.toList(obj)) { + for (Object value : jsonProvider.toMap(current).values()) { result.add(value); } } } else { - for (Object value : toMap(obj).values()) { + for (Object value : jsonProvider.toMap(obj).values()) { result.add(value); } } diff --git a/json-path/src/main/java/com/jayway/jsonpath/eval/ExpressionEvaluator.java b/json-path/src/main/java/com/jayway/jsonpath/reader/filter/eval/ExpressionEvaluator.java similarity index 98% rename from json-path/src/main/java/com/jayway/jsonpath/eval/ExpressionEvaluator.java rename to json-path/src/main/java/com/jayway/jsonpath/reader/filter/eval/ExpressionEvaluator.java index 8da729f1..fdbac705 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/eval/ExpressionEvaluator.java +++ b/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 diff --git a/json-path/src/main/java/com/jayway/jsonpath/spi/JsonProvider.java b/json-path/src/main/java/com/jayway/jsonpath/spi/JsonProvider.java new file mode 100644 index 00000000..7902379e --- /dev/null +++ b/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 createMap(); + + public abstract List createList(); + + /** + * checks if object is instanceof java.util.List or java.util.Map + * + * @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 instanceof java.util.List + * + * @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 toList(Object list) { + return (List) list; + } + + + /** + * Converts given object to a Map + * + * @param map + * @return + */ + public Map toMap(Object map) { + return (Map) 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 instanceof java.util.Map + * + * @param obj object to check + * @return true if Map + */ + public boolean isMap(Object obj) { + return (obj instanceof Map); + } + + +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/spi/Mode.java b/json-path/src/main/java/com/jayway/jsonpath/spi/Mode.java new file mode 100644 index 00000000..5484675d --- /dev/null +++ b/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"); + } + } + + +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/spi/impl/DefaultJsonProvider.java b/json-path/src/main/java/com/jayway/jsonpath/spi/impl/DefaultJsonProvider.java new file mode 100644 index 00000000..8bd87b87 --- /dev/null +++ b/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 createMap() { + return new JSONObject(); + } + + public List 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; + } +} diff --git a/json-path/src/test/java/com/jayway/jsonpath/ExpressionEvalTest.java b/json-path/src/test/java/com/jayway/jsonpath/ExpressionEvalTest.java index beeeb165..86cfdf8d 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/ExpressionEvalTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/ExpressionEvalTest.java @@ -1,6 +1,6 @@ package com.jayway.jsonpath; -import com.jayway.jsonpath.eval.ExpressionEvaluator; +import com.jayway.jsonpath.reader.filter.eval.ExpressionEvaluator; import org.junit.Test; import static org.junit.Assert.assertFalse; diff --git a/json-path/src/test/java/com/jayway/jsonpath/PathUtilTest.java b/json-path/src/test/java/com/jayway/jsonpath/IsDefinitePathTest.java similarity index 53% rename from json-path/src/test/java/com/jayway/jsonpath/PathUtilTest.java rename to json-path/src/test/java/com/jayway/jsonpath/IsDefinitePathTest.java index 6861f2a2..45ffd350 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/PathUtilTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/IsDefinitePathTest.java @@ -11,20 +11,19 @@ import static org.junit.Assert.assertTrue; * Date: 2/3/11 * Time: 9:58 PM */ -public class PathUtilTest { +public class IsDefinitePathTest { @Test public void path_is_not_definite() throws Exception { - assertFalse(PathUtil.isPathDefinite("$..book[0]")); - assertFalse(PathUtil.isPathDefinite("$.books[*]")); + assertFalse(JsonPath.compile("$..book[0]").isPathDefinite()); + assertFalse(JsonPath.compile("$.books[*]").isPathDefinite()); } @Test public void path_is_definite() throws Exception { - assertTrue(PathUtil.isPathDefinite("$.definite.this.is")); - assertTrue(PathUtil.isPathDefinite("$.definite:this.is")); - assertTrue(PathUtil.isPathDefinite("rows[0].id")); + assertTrue(JsonPath.compile("$.definite.this.is").isPathDefinite()); + assertTrue(JsonPath.compile("rows[0].id").isPathDefinite()); } diff --git a/json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java b/json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java index 3fb92602..f997eed8 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java @@ -1,5 +1,6 @@ package com.jayway.jsonpath; +import com.jayway.jsonpath.util.ScriptEngineJsonPath; import org.junit.Test; 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\"}}]]"; + @Test public void array_start_expands() throws Exception { assertThat(JsonPath.>read(ARRAY_EXPAND, "$[?(@.parent = 'ONE')].child.name"), hasItems("NAME_ONE")); @@ -78,6 +80,10 @@ public class JsonPathTest { @Test 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']")); diff --git a/json-path/src/test/java/com/jayway/jsonpath/ParserTest.java b/json-path/src/test/java/com/jayway/jsonpath/ParserTest.java index 9400be08..3b606612 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/ParserTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/ParserTest.java @@ -1,11 +1,5 @@ package com.jayway.jsonpath; -import org.junit.Test; - -import java.text.ParseException; - -import static junit.framework.Assert.assertEquals; - /** * Created by IntelliJ IDEA. * User: kallestenflo @@ -14,7 +8,7 @@ import static junit.framework.Assert.assertEquals; */ 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 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.read(NO_QUOTE_JSON, "lhs"); } - + */ } diff --git a/json-path/src/test/java/com/jayway/jsonpath/PathReaderTest.java b/json-path/src/test/java/com/jayway/jsonpath/PathReaderTest.java new file mode 100644 index 00000000..8b00d1db --- /dev/null +++ b/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 book = pathReader.read("store.book[1]"); + + PathReader pathReader1 = pathReader.get("store.book[1]"); + + String author = pathReader1.read("author"); + + System.out.println("d"); + + } + +} diff --git a/json-path/src/test/java/com/jayway/jsonpath/PathTokenizerTest.java b/json-path/src/test/java/com/jayway/jsonpath/PathTokenizerTest.java index 5a2483f2..9f5052c5 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/PathTokenizerTest.java +++ b/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.PathTokenizer; +import com.jayway.jsonpath.spi.JsonProvider; +import com.jayway.jsonpath.spi.impl.DefaultJsonProvider; import org.junit.Test; import java.util.List; @@ -18,6 +20,8 @@ import static org.junit.Assert.assertThat; */ public class PathTokenizerTest { + private JsonProvider jsonProvider = new DefaultJsonProvider(); + public final static String DOCUMENT = "{ \"store\": {\n" + " \"book\": [ \n" + @@ -56,10 +60,10 @@ public class PathTokenizerTest { @Test 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")) { - result = pathToken.filter(result); + for (PathToken pathToken : new PathTokenizer("$.store.bicycle.color", jsonProvider)) { + result = pathToken.filter(result, jsonProvider); } assertEquals("red", result); @@ -67,10 +71,10 @@ public class PathTokenizerTest { @Test 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")) { - result = pathToken.filter(result); + for (PathToken pathToken : new PathTokenizer("$.store.book", jsonProvider)) { + result = pathToken.filter(result, jsonProvider); } assertEquals(4, toList(result).size()); @@ -78,10 +82,10 @@ public class PathTokenizerTest { @Test 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")) { - result = pathToken.filter(result); + for (PathToken pathToken : new PathTokenizer("$.store.book[*].title", jsonProvider)) { + result = pathToken.filter(result, jsonProvider); } assertEquals(4, toList(result).size()); @@ -89,10 +93,10 @@ public class PathTokenizerTest { @Test 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")) { - result = pathToken.filter(result); + for (PathToken pathToken : new PathTokenizer("$.store.book[0].title", jsonProvider)) { + result = pathToken.filter(result, jsonProvider); } assertEquals("Sayings of the Century", result); @@ -100,10 +104,10 @@ public class PathTokenizerTest { @Test 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")) { - result = pathToken.filter(result); + for (PathToken pathToken : new PathTokenizer("$.store.book[0, 1].author", jsonProvider)) { + result = pathToken.filter(result, jsonProvider); } assertThat(this.toList(result), hasItems("Nigel Rees", "Evelyn Waugh")); @@ -111,10 +115,10 @@ public class PathTokenizerTest { @Test 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")) { - result = pathToken.filter(result); + for (PathToken pathToken : new PathTokenizer("$.store.book[:2].author", jsonProvider)) { + result = pathToken.filter(result, jsonProvider); } assertThat(this.toList(result), hasItems("Nigel Rees", "Evelyn Waugh")); @@ -122,40 +126,40 @@ public class PathTokenizerTest { @Test 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")) { - result = pathToken.filter(result); + for (PathToken pathToken : new PathTokenizer("$.store.book[-1:].author", jsonProvider)) { + result = pathToken.filter(result, jsonProvider); } assertEquals("J. R. R. Tolkien", result); } @Test 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")) { - result = pathToken.filter(result); + for (PathToken pathToken : new PathTokenizer("$.store.book[?(@.custom)].author", jsonProvider)) { + result = pathToken.filter(result, jsonProvider); } assertThat(this.toList(result), hasItems("J. R. R. Tolkien")); } @Test 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")) { - result = pathToken.filter(result); + for (PathToken pathToken : new PathTokenizer("$.store.book[?(@.custom = 'onely this')].author", jsonProvider)) { + result = pathToken.filter(result, jsonProvider); } assertThat(this.toList(result), hasItems("J. R. R. Tolkien")); } @Test public void deep_scan() throws Exception { - Object result = JsonPath.parse(DOCUMENT); + Object result = jsonProvider.parse(DOCUMENT); - for (PathToken pathToken : new PathTokenizer("$..author")) { - result = pathToken.filter(result); + for (PathToken pathToken : new PathTokenizer("$..author", jsonProvider)) { + result = pathToken.filter(result, jsonProvider); } assertThat(this.toList(result), hasItems("Nigel Rees","Evelyn Waugh", "J. R. R. Tolkien")); } diff --git a/json-path/src/test/java/com/jayway/jsonpath/SplitPathFragmentsTest.java b/json-path/src/test/java/com/jayway/jsonpath/SplitPathFragmentsTest.java index c4389456..4f044988 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/SplitPathFragmentsTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/SplitPathFragmentsTest.java @@ -1,10 +1,11 @@ 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.junit.Test; -import java.util.List; - import static org.hamcrest.Matchers.hasItems; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; @@ -17,7 +18,7 @@ import static org.junit.Assert.assertTrue; */ public class SplitPathFragmentsTest { - + private JsonProvider jsonProvider = new DefaultJsonProvider(); @Test public void valid_path_is_split_correctly() throws Exception { @@ -92,7 +93,7 @@ public class SplitPathFragmentsTest { private void assertPathInvalid(String path) { try { - PathUtil.splitPath(path); + PathTokenizer tokenizer = new PathTokenizer(path, jsonProvider); assertTrue("Expected exception!", false); } catch (InvalidPathException expected) {} } @@ -100,13 +101,16 @@ public class SplitPathFragmentsTest { private void assertPath(String path, Matcher> matcher) { System.out.println("PATH: " + path); - List fragments = PathUtil.splitPath(path); - for (String fragment : fragments) { + PathTokenizer tokenizer = new PathTokenizer(path, jsonProvider); + + + + for (String fragment : tokenizer.getFragments()) { System.out.println(fragment); } - assertThat(fragments, matcher); + assertThat(tokenizer.getFragments(), matcher); System.out.println("----------------------------------"); } diff --git a/json-path/src/test/java/com/jayway/jsonpath/util/ScriptEngineJsonPath.java b/json-path/src/test/java/com/jayway/jsonpath/util/ScriptEngineJsonPath.java new file mode 100644 index 00000000..8f80ba05 --- /dev/null +++ b/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) { + } + } + } + } + + +} diff --git a/json-path/src/test/resources/js/json.js b/json-path/src/test/resources/js/json.js new file mode 100644 index 00000000..713923a4 --- /dev/null +++ b/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 ' '), + 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); + }; + } +}()); \ No newline at end of file diff --git a/json-path/src/test/resources/js/jsonpath-0.8.0.js b/json-path/src/test/resources/js/jsonpath-0.8.0.js new file mode 100644 index 00000000..fc8d2d60 --- /dev/null +++ b/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 + + + + + + + +

+	
+
\ No newline at end of file
diff --git a/json-path/src/test/resources/js/wrapper.js b/json-path/src/test/resources/js/wrapper.js
new file mode 100644
index 00000000..88c21bb1
--- /dev/null
+++ b/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);
+        }
+    };
+}
+
+