28 changed files with 1795 additions and 1436 deletions
@ -1,24 +1,24 @@
|
||||
package com.jayway.jsonpath; |
||||
|
||||
/** |
||||
* User: kalle stenflo |
||||
* Date: 1/24/11 |
||||
* Time: 10:09 AM |
||||
*/ |
||||
public class InvalidPathException extends RuntimeException { |
||||
|
||||
public InvalidPathException() { |
||||
} |
||||
|
||||
public InvalidPathException(String message) { |
||||
super(message); |
||||
} |
||||
|
||||
public InvalidPathException(String message, Throwable cause) { |
||||
super(message, cause); |
||||
} |
||||
|
||||
public InvalidPathException(Throwable cause) { |
||||
super(cause); |
||||
} |
||||
} |
||||
package com.jayway.jsonpath; |
||||
|
||||
/** |
||||
* User: kalle stenflo |
||||
* Date: 1/24/11 |
||||
* Time: 10:09 AM |
||||
*/ |
||||
public class InvalidPathException extends RuntimeException { |
||||
|
||||
public InvalidPathException() { |
||||
} |
||||
|
||||
public InvalidPathException(String message) { |
||||
super(message); |
||||
} |
||||
|
||||
public InvalidPathException(String message, Throwable cause) { |
||||
super(message, cause); |
||||
} |
||||
|
||||
public InvalidPathException(Throwable cause) { |
||||
super(cause); |
||||
} |
||||
} |
||||
|
@ -1,189 +1,203 @@
|
||||
package com.jayway.jsonpath; |
||||
|
||||
|
||||
import com.jayway.jsonpath.filter.FilterOutput; |
||||
import com.jayway.jsonpath.filter.JsonPathFilterChain; |
||||
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.logging.Logger; |
||||
|
||||
/** |
||||
* User: kalle stenflo |
||||
* Date: 2/2/11 |
||||
* Time: 1:03 PM |
||||
* <p/> |
||||
* JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document. JsonPath is |
||||
* available in many programming languages such as Javascript, Python and PHP. |
||||
* <p/> |
||||
* JsonPath allows you to compile a json path string to use it many times or to compile and apply in one |
||||
* single on demand operation. |
||||
* <p/> |
||||
* Given the Json document: |
||||
* <p/> |
||||
* <code> |
||||
* String json = |
||||
* "{ |
||||
* "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 |
||||
* } |
||||
* ], |
||||
* "bicycle": |
||||
* { |
||||
* "color": "red", |
||||
* "price": 19.95 |
||||
* } |
||||
* } |
||||
* }"; |
||||
* </code> |
||||
* <p/> |
||||
* A JsonPath can be compiled and used as shown: |
||||
* <p/> |
||||
* <code> |
||||
* JsonPath path = JsonPath.compile("$.store.book[1]"); |
||||
* <br/> |
||||
* List<Object> books = path.read(json); |
||||
* </code> |
||||
* </p> |
||||
* Or: |
||||
* <p/> |
||||
* <code> |
||||
* List<Object> authors = JsonPath.read(json, "$.store.book[*].author") |
||||
* </code> |
||||
* <p/> |
||||
* If the json path returns a single value (is definite): |
||||
* </p> |
||||
* <code> |
||||
* String author = JsonPath.read(json, "$.store.book[1].author") |
||||
* </code> |
||||
*/ |
||||
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 JsonPathFilterChain filters; |
||||
|
||||
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; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Creates a new JsonPath. |
||||
* |
||||
* @param jsonPath the path statement |
||||
*/ |
||||
private JsonPath(String jsonPath) { |
||||
if (jsonPath == null || |
||||
jsonPath.trim().isEmpty() || |
||||
jsonPath.matches("new ") || |
||||
jsonPath.matches("[^\\?\\+\\=\\-\\*\\/\\!]\\(")) { |
||||
|
||||
throw new InvalidPathException("Invalid path"); |
||||
} |
||||
this.filters = new JsonPathFilterChain(PathUtil.splitPath(jsonPath)); |
||||
} |
||||
|
||||
/** |
||||
* Applies this json path to the provided object |
||||
* |
||||
* @param json a json Object |
||||
* @param <T> |
||||
* @return list of objects matched by the given path |
||||
*/ |
||||
public <T> T read(Object json) { |
||||
FilterOutput filterOutput = filters.filter(json); |
||||
|
||||
if (filterOutput == null || filterOutput.getResult() == null) { |
||||
return null; |
||||
} |
||||
|
||||
return (T) filterOutput.getResult(); |
||||
} |
||||
|
||||
/** |
||||
* Applies this json path to the provided object |
||||
* |
||||
* @param json a json string |
||||
* @param <T> |
||||
* @return list of objects matched by the given path |
||||
*/ |
||||
public <T> T read(String json) throws java.text.ParseException { |
||||
return (T) read(parse(json)); |
||||
} |
||||
|
||||
/** |
||||
* Compiles a JsonPath from the given string |
||||
* |
||||
* @param jsonPath to compile |
||||
* @return compiled JsonPath |
||||
*/ |
||||
public static JsonPath compile(String jsonPath) { |
||||
return new JsonPath(jsonPath); |
||||
} |
||||
|
||||
/** |
||||
* Creates a new JsonPath and applies it to the provided Json string |
||||
* |
||||
* @param json a json string |
||||
* @param jsonPath the json path |
||||
* @param <T> |
||||
* @return list of objects matched by the given path |
||||
*/ |
||||
public static <T> T read(String json, String jsonPath) throws java.text.ParseException { |
||||
return (T) compile(jsonPath).read(json); |
||||
} |
||||
|
||||
/** |
||||
* Creates a new JsonPath and applies it to the provided Json object |
||||
* |
||||
* @param json a json object |
||||
* @param jsonPath the json path |
||||
* @param <T> |
||||
* @return list of objects matched by the given path |
||||
*/ |
||||
public static <T> T read(Object json, String jsonPath) { |
||||
return (T) compile(jsonPath).read(json); |
||||
} |
||||
|
||||
|
||||
private 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); |
||||
} |
||||
} |
||||
} |
||||
package com.jayway.jsonpath; |
||||
|
||||
|
||||
import com.jayway.jsonpath.filter.FilterOutput; |
||||
import com.jayway.jsonpath.filter.JsonPathFilterChain; |
||||
import com.jayway.jsonpath.json.JsonElement; |
||||
import com.jayway.jsonpath.json.JsonException; |
||||
import com.jayway.jsonpath.json.JsonFactory; |
||||
import com.jayway.jsonpath.json.JsonParser; |
||||
|
||||
|
||||
|
||||
import java.io.IOException; |
||||
import java.util.logging.Logger; |
||||
|
||||
import net.minidev.json.parser.ParseException; |
||||
|
||||
|
||||
/** |
||||
* User: kalle stenflo |
||||
* Date: 2/2/11 |
||||
* Time: 1:03 PM |
||||
* <p/> |
||||
* JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document. JsonPath is |
||||
* available in many programming languages such as Javascript, Python and PHP. |
||||
* <p/> |
||||
* JsonPath allows you to compile a json path string to use it many times or to compile and apply in one |
||||
* single on demand operation. |
||||
* <p/> |
||||
* Given the Json document: |
||||
* <p/> |
||||
* <code> |
||||
* String json = |
||||
* "{ |
||||
* "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 |
||||
* } |
||||
* ], |
||||
* "bicycle": |
||||
* { |
||||
* "color": "red", |
||||
* "price": 19.95 |
||||
* } |
||||
* } |
||||
* }"; |
||||
* </code> |
||||
* <p/> |
||||
* A JsonPath can be compiled and used as shown: |
||||
* <p/> |
||||
* <code> |
||||
* JsonPath path = JsonPath.compile("$.store.book[1]"); |
||||
* <br/> |
||||
* List<Object> books = path.read(json); |
||||
* </code> |
||||
* </p> |
||||
* Or: |
||||
* <p/> |
||||
* <code> |
||||
* List<Object> authors = JsonPath.read(json, "$.store.book[*].author") |
||||
* </code> |
||||
* <p/> |
||||
* If the json path returns a single value (is definite): |
||||
* </p> |
||||
* <code> |
||||
* String author = JsonPath.read(json, "$.store.book[1].author") |
||||
* </code> |
||||
*/ |
||||
public class JsonPath { |
||||
|
||||
public final static int STRICT_MODE = 0; |
||||
public final static int SLACK_MODE = -1; |
||||
|
||||
// private static int mode = SLACK_MODE;
|
||||
private static JsonFactory factory; |
||||
|
||||
private final static Logger log = Logger.getLogger(JsonPath.class.getName()); |
||||
|
||||
|
||||
|
||||
private JsonPathFilterChain filters; |
||||
|
||||
/* |
||||
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; |
||||
} |
||||
*/ |
||||
|
||||
/** |
||||
* Creates a new JsonPath. |
||||
* |
||||
* @param jsonPath the path statement |
||||
*/ |
||||
private JsonPath(String jsonPath) { |
||||
if (jsonPath == null || |
||||
jsonPath.trim().isEmpty() || |
||||
jsonPath.matches("new ") || |
||||
jsonPath.matches("[^\\?\\+\\=\\-\\*\\/\\!]\\(")) { |
||||
|
||||
throw new InvalidPathException("Invalid path"); |
||||
} |
||||
this.filters = new JsonPathFilterChain(PathUtil.splitPath(jsonPath)); |
||||
} |
||||
|
||||
/** |
||||
* Applies this json path to the provided object |
||||
* |
||||
* @param json a json Object |
||||
* @param <T> |
||||
* @return list of objects matched by the given path |
||||
* @throws JsonException |
||||
*/ |
||||
public JsonElement read(JsonElement json) throws JsonException { |
||||
FilterOutput filterOutput = filters.filter(json); |
||||
|
||||
if (filterOutput == null || filterOutput.getResult() == null) { |
||||
return null; |
||||
} |
||||
|
||||
return filterOutput.getResult(); |
||||
|
||||
} |
||||
|
||||
/** |
||||
* Applies this json path to the provided object |
||||
* |
||||
* @param json a json string |
||||
* @param <T> |
||||
* @return list of objects matched by the given path |
||||
* @throws JsonException |
||||
*/ |
||||
public JsonElement read(String json) throws java.text.ParseException, JsonException { |
||||
return read(parse(json)); |
||||
} |
||||
|
||||
/** |
||||
* Compiles a JsonPath from the given string |
||||
* |
||||
* @param jsonPath to compile |
||||
* @return compiled JsonPath |
||||
*/ |
||||
public static JsonPath compile(String jsonPath) { |
||||
return new JsonPath(jsonPath); |
||||
} |
||||
|
||||
/** |
||||
* Creates a new JsonPath and applies it to the provided Json string |
||||
* |
||||
* @param json a json string |
||||
* @param jsonPath the json path |
||||
* @param <T> |
||||
* @return list of objects matched by the given path |
||||
* @throws JsonException |
||||
*/ |
||||
public static JsonElement read(String json, String jsonPath) throws java.text.ParseException, JsonException { |
||||
return compile(jsonPath).read(json); |
||||
} |
||||
|
||||
/** |
||||
* Creates a new JsonPath and applies it to the provided Json object |
||||
* |
||||
* @param json a json object |
||||
* @param jsonPath the json path |
||||
* @param <T> |
||||
* @return list of objects matched by the given path |
||||
* @throws JsonException |
||||
*/ |
||||
public static JsonElement read(JsonElement json, String jsonPath) throws JsonException { |
||||
return compile(jsonPath).read(json); |
||||
} |
||||
|
||||
|
||||
public static JsonElement parse(String json) { |
||||
try { |
||||
return JsonFactory.getInstance().parse(json); |
||||
} catch (IOException e) { |
||||
throw new RuntimeException(e); |
||||
} catch (com.jayway.jsonpath.json.ParseException e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
@ -1,71 +1,71 @@
|
||||
package com.jayway.jsonpath; |
||||
|
||||
import java.util.LinkedList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 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 <strong>not</strong> 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(".*(\\.\\.|\\*|\\[[\\\\/]|\\?|,|:|>|\\(|<|=|\\+).*");
|
||||
return !jsonPath.replaceAll("\"[^\"\\\\\\n\r]*\"", "").matches(".*(\\.\\.|\\*|\\[[\\\\/]|\\?|,|:\\s?\\]|\\[\\s?:|>|\\(|<|=|\\+).*"); |
||||
} |
||||
|
||||
/** |
||||
* Splits a path into fragments |
||||
* |
||||
* the path <code>$.store.book[1].category</code> returns ["$", "store", "book", "[1]", "value"] |
||||
* |
||||
* @param jsonPath path to split |
||||
* @return fragments |
||||
*/ |
||||
public static List<String> splitPath(String jsonPath) { |
||||
|
||||
LinkedList<String> fragments = new LinkedList<String>(); |
||||
|
||||
if (!jsonPath.startsWith("$.")) { |
||||
jsonPath = "$." + jsonPath; |
||||
} |
||||
|
||||
jsonPath = jsonPath.replace("..", ".~.") |
||||
.replace("[", ".[") |
||||
.replace("@.", "@") |
||||
.replace("['", "") |
||||
.replace("']", ""); |
||||
|
||||
String[] split = jsonPath.split("\\."); |
||||
|
||||
for (int i = 0; i < split.length; i++) { |
||||
if (split[i].trim().isEmpty()) { |
||||
continue; |
||||
} |
||||
fragments.add(split[i].replace("@", "@.").replace("~", "..")); |
||||
} |
||||
return fragments; |
||||
} |
||||
} |
||||
package com.jayway.jsonpath; |
||||
|
||||
import java.util.LinkedList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 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 <strong>not</strong> 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(".*(\\.\\.|\\*|\\[[\\\\/]|\\?|,|:|>|\\(|<|=|\\+).*");
|
||||
return !jsonPath.replaceAll("\"[^\"\\\\\\n\r]*\"", "").matches(".*(\\.\\.|\\*|\\[[\\\\/]|\\?|,|:\\s?\\]|\\[\\s?:|>|\\(|<|=|\\+).*"); |
||||
} |
||||
|
||||
/** |
||||
* Splits a path into fragments |
||||
* |
||||
* the path <code>$.store.book[1].category</code> returns ["$", "store", "book", "[1]", "value"] |
||||
* |
||||
* @param jsonPath path to split |
||||
* @return fragments |
||||
*/ |
||||
public static List<String> splitPath(String jsonPath) { |
||||
|
||||
LinkedList<String> fragments = new LinkedList<String>(); |
||||
|
||||
if (!jsonPath.startsWith("$.")) { |
||||
jsonPath = "$." + jsonPath; |
||||
} |
||||
|
||||
jsonPath = jsonPath.replace("..", ".~.") |
||||
.replace("[", ".[") |
||||
.replace("@.", "@") |
||||
.replace("['", "") |
||||
.replace("']", ""); |
||||
|
||||
String[] split = jsonPath.split("\\."); |
||||
|
||||
for (int i = 0; i < split.length; i++) { |
||||
if (split[i].trim().isEmpty()) { |
||||
continue; |
||||
} |
||||
fragments.add(split[i].replace("@", "@.").replace("~", "..")); |
||||
} |
||||
return fragments; |
||||
} |
||||
} |
||||
|
@ -1,88 +1,88 @@
|
||||
package com.jayway.jsonpath.eval; |
||||
|
||||
/** |
||||
* User: kalle stenflo |
||||
* Date: 2/4/11 |
||||
* Time: 9:21 PM |
||||
*/ |
||||
public class ExpressionEvaluator { |
||||
|
||||
|
||||
public static <T> boolean eval(T actual, String comparator, String expected) { |
||||
|
||||
comparator = comparator.trim(); |
||||
|
||||
if (actual instanceof Long) { |
||||
|
||||
Long a = (Long) actual; |
||||
Long e = Long.parseLong(expected.trim()); |
||||
|
||||
if ("=".equals(comparator)) { |
||||
return a.longValue() == e.longValue(); |
||||
} else if ("!=".equals(comparator) || "<>".equals(comparator)) { |
||||
return a.longValue() != e.longValue(); |
||||
} else if (">".equals(comparator)) { |
||||
return a.longValue() > e.longValue(); |
||||
} else if (">=".equals(comparator)) { |
||||
return a.longValue() >= e.longValue(); |
||||
} else if ("<".equals(comparator)) { |
||||
return a.longValue() < e.longValue(); |
||||
} else if ("<=".equals(comparator)) { |
||||
return a.longValue() <= e.longValue(); |
||||
} |
||||
} else if (actual instanceof Integer) { |
||||
Integer a = (Integer) actual; |
||||
Integer e = Integer.parseInt(expected.trim()); |
||||
|
||||
if ("=".equals(comparator)) { |
||||
return a.intValue() == e.intValue(); |
||||
} else if ("!=".equals(comparator) || "<>".equals(comparator)) { |
||||
return a.intValue() != e.intValue(); |
||||
} else if (">".equals(comparator)) { |
||||
return a.intValue() > e.intValue(); |
||||
} else if (">=".equals(comparator)) { |
||||
return a.intValue() >= e.intValue(); |
||||
} else if ("<".equals(comparator)) { |
||||
return a.intValue() < e.intValue(); |
||||
} else if ("<=".equals(comparator)) { |
||||
return a.intValue() <= e.intValue(); |
||||
} |
||||
} else if (actual instanceof Double) { |
||||
|
||||
Double a = (Double) actual; |
||||
Double e = Double.parseDouble(expected.trim()); |
||||
|
||||
if ("=".equals(comparator)) { |
||||
return a.doubleValue() == e.doubleValue(); |
||||
} else if ("!=".equals(comparator) || "<>".equals(comparator)) { |
||||
return a.doubleValue() != e.doubleValue(); |
||||
} else if (">".equals(comparator)) { |
||||
return a.doubleValue() > e.doubleValue(); |
||||
} else if (">=".equals(comparator)) { |
||||
return a.doubleValue() >= e.doubleValue(); |
||||
} else if ("<".equals(comparator)) { |
||||
return a.doubleValue() < e.doubleValue(); |
||||
} else if ("<=".equals(comparator)) { |
||||
return a.doubleValue() <= e.doubleValue(); |
||||
} |
||||
} else if (actual instanceof String) { |
||||
|
||||
String a = (String)actual; |
||||
expected = expected.trim(); |
||||
if(expected.startsWith("'")) { |
||||
expected = expected.substring(1); |
||||
} |
||||
if(expected.endsWith("'")){ |
||||
expected = expected.substring(0, expected.length()-1); |
||||
} |
||||
|
||||
if ("=".equals(comparator)) { |
||||
return a.equals(expected); |
||||
} else if ("!=".equals(comparator) || "<>".equals(comparator)) { |
||||
return !a.equals(expected); |
||||
} |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
} |
||||
package com.jayway.jsonpath.eval; |
||||
|
||||
/** |
||||
* User: kalle stenflo |
||||
* Date: 2/4/11 |
||||
* Time: 9:21 PM |
||||
*/ |
||||
public class ExpressionEvaluator { |
||||
|
||||
|
||||
public static <T> boolean eval(T actual, String comparator, String expected) { |
||||
|
||||
comparator = comparator.trim(); |
||||
|
||||
if (actual instanceof Long) { |
||||
|
||||
Long a = (Long) actual; |
||||
Long e = Long.parseLong(expected.trim()); |
||||
|
||||
if ("=".equals(comparator)) { |
||||
return a.longValue() == e.longValue(); |
||||
} else if ("!=".equals(comparator) || "<>".equals(comparator)) { |
||||
return a.longValue() != e.longValue(); |
||||
} else if (">".equals(comparator)) { |
||||
return a.longValue() > e.longValue(); |
||||
} else if (">=".equals(comparator)) { |
||||
return a.longValue() >= e.longValue(); |
||||
} else if ("<".equals(comparator)) { |
||||
return a.longValue() < e.longValue(); |
||||
} else if ("<=".equals(comparator)) { |
||||
return a.longValue() <= e.longValue(); |
||||
} |
||||
} else if (actual instanceof Integer) { |
||||
Integer a = (Integer) actual; |
||||
Integer e = Integer.parseInt(expected.trim()); |
||||
|
||||
if ("=".equals(comparator)) { |
||||
return a.intValue() == e.intValue(); |
||||
} else if ("!=".equals(comparator) || "<>".equals(comparator)) { |
||||
return a.intValue() != e.intValue(); |
||||
} else if (">".equals(comparator)) { |
||||
return a.intValue() > e.intValue(); |
||||
} else if (">=".equals(comparator)) { |
||||
return a.intValue() >= e.intValue(); |
||||
} else if ("<".equals(comparator)) { |
||||
return a.intValue() < e.intValue(); |
||||
} else if ("<=".equals(comparator)) { |
||||
return a.intValue() <= e.intValue(); |
||||
} |
||||
} else if (actual instanceof Double) { |
||||
|
||||
Double a = (Double) actual; |
||||
Double e = Double.parseDouble(expected.trim()); |
||||
|
||||
if ("=".equals(comparator)) { |
||||
return a.doubleValue() == e.doubleValue(); |
||||
} else if ("!=".equals(comparator) || "<>".equals(comparator)) { |
||||
return a.doubleValue() != e.doubleValue(); |
||||
} else if (">".equals(comparator)) { |
||||
return a.doubleValue() > e.doubleValue(); |
||||
} else if (">=".equals(comparator)) { |
||||
return a.doubleValue() >= e.doubleValue(); |
||||
} else if ("<".equals(comparator)) { |
||||
return a.doubleValue() < e.doubleValue(); |
||||
} else if ("<=".equals(comparator)) { |
||||
return a.doubleValue() <= e.doubleValue(); |
||||
} |
||||
} else if (actual instanceof String) { |
||||
|
||||
String a = (String)actual; |
||||
expected = expected.trim(); |
||||
if(expected.startsWith("'")) { |
||||
expected = expected.substring(1); |
||||
} |
||||
if(expected.endsWith("'")){ |
||||
expected = expected.substring(0, expected.length()-1); |
||||
} |
||||
|
||||
if ("=".equals(comparator)) { |
||||
return a.equals(expected); |
||||
} else if ("!=".equals(comparator) || "<>".equals(comparator)) { |
||||
return !a.equals(expected); |
||||
} |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
} |
||||
|
@ -1,39 +1,66 @@
|
||||
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; |
||||
} |
||||
|
||||
|
||||
|
||||
} |
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import com.jayway.jsonpath.json.JsonArray; |
||||
import com.jayway.jsonpath.json.JsonElement; |
||||
import com.jayway.jsonpath.json.JsonException; |
||||
import com.jayway.jsonpath.json.JsonFactory; |
||||
|
||||
import static java.lang.String.format; |
||||
|
||||
/** |
||||
* User: kalle stenflo |
||||
* Date: 2/9/11 |
||||
* Time: 12:28 PM |
||||
*/ |
||||
public class FilterOutput { |
||||
|
||||
private final List<JsonElement> result; |
||||
|
||||
public FilterOutput(JsonElement root) { |
||||
this.result = new ArrayList<JsonElement>(); |
||||
result.add(root); |
||||
} |
||||
|
||||
|
||||
public FilterOutput(List<JsonElement> result) { |
||||
this.result = result; |
||||
} |
||||
|
||||
|
||||
public FilterOutput() { |
||||
this.result = new ArrayList<JsonElement>(); |
||||
} |
||||
|
||||
|
||||
public JsonElement getResult() throws JsonException { |
||||
if(result.size()==0){ |
||||
return null; |
||||
} |
||||
else if(result.size()==1){ |
||||
return result.get(0); |
||||
} |
||||
else{ |
||||
JsonFactory fact = JsonFactory.getInstance(); |
||||
JsonArray ja = fact.createJsonArray(); |
||||
for(JsonElement ele:result) |
||||
ja.add(ele); |
||||
return ja; |
||||
} |
||||
} |
||||
|
||||
public JsonArray getResultAsList() throws JsonException { |
||||
return getResult().toJsonArray(); |
||||
} |
||||
|
||||
|
||||
|
||||
public List<JsonElement> getList() throws JsonException { |
||||
return result; |
||||
} |
||||
|
||||
|
||||
|
||||
} |
||||
|
@ -1,13 +1,28 @@
|
||||
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); |
||||
} |
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import com.jayway.jsonpath.json.JsonElement; |
||||
import com.jayway.jsonpath.json.JsonException; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 2/2/11 |
||||
* Time: 2:01 PM |
||||
*/ |
||||
public abstract class JsonPathFilterBase { |
||||
public FilterOutput apply(FilterOutput element) throws JsonException{ |
||||
List<JsonElement> result = new ArrayList<JsonElement>(); |
||||
for(JsonElement el : element.getList()){ |
||||
List<JsonElement> out = apply(el); |
||||
if(out != null) |
||||
result.addAll(out); |
||||
} |
||||
return new FilterOutput(result); |
||||
} |
||||
public abstract List<JsonElement> apply(JsonElement element) throws JsonException; |
||||
public abstract String getPathSegment() throws JsonException;; |
||||
|
||||
} |
||||
|
@ -1,47 +1,62 @@
|
||||
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; |
||||
} |
||||
} |
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
import com.jayway.jsonpath.InvalidPathException; |
||||
import com.jayway.jsonpath.json.JsonElement; |
||||
import com.jayway.jsonpath.json.JsonException; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.LinkedList; |
||||
import java.util.List; |
||||
import java.util.Stack; |
||||
|
||||
import org.apache.log4j.Logger; |
||||
|
||||
/** |
||||
* User: kallestenflo |
||||
* Date: 2/2/11 |
||||
* Time: 2:00 PM |
||||
*/ |
||||
public class JsonPathFilterChain { |
||||
|
||||
private List<JsonPathFilterBase> filters; |
||||
private JsonElement payload = null; |
||||
private final Logger log = Logger.getLogger(JsonPathFilterChain.class); |
||||
|
||||
public JsonPathFilterChain(List<String> pathFragments) { |
||||
filters = configureFilters(pathFragments); |
||||
} |
||||
|
||||
public JsonPathFilterChain(List<String> pathFragments,JsonElement payload) { |
||||
filters = configureFilters(pathFragments); |
||||
this.payload = payload; |
||||
} |
||||
|
||||
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(JsonElement root) throws JsonException { |
||||
FilterOutput out = new FilterOutput(root); |
||||
log.info(out.getResult().toString()); |
||||
for (JsonPathFilterBase filter : filters) { |
||||
if (filter == null) { |
||||
throw new InvalidPathException(); |
||||
} |
||||
if(out.getList() == null){ |
||||
return null; |
||||
} |
||||
out = filter.apply(out); |
||||
if(out.getResult()!=null) |
||||
log.info(out.getResult().toString()); |
||||
} |
||||
|
||||
|
||||
return out; |
||||
} |
||||
} |
||||
|
@ -1,37 +1,43 @@
|
||||
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; |
||||
|
||||
} |
||||
|
||||
} |
||||
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 (M4PropertyFilter.PATTERN.matcher(pathFragment).matches()) { |
||||
return new M4PropertyFilter(pathFragment); |
||||
} |
||||
else if (TypeFilter.PATTERN.matcher(pathFragment).matches()) { |
||||
return new TypeFilter(pathFragment); |
||||
}else if (PropertyFilter.PATTERN.matcher(pathFragment).matches()) { |
||||
return new PropertyFilter(pathFragment); |
||||
} |
||||
return null; |
||||
|
||||
|
||||
} |
||||
|
||||
} |
||||
|
@ -1,73 +1,91 @@
|
||||
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?@.(\\w+)\\s?([=<>]+)\\s?(.*)\\s?\\)\\s?\\]"); //[?( @.title< 'ko')]
|
||||
|
||||
private final String pathFragment; |
||||
|
||||
public ListEvalFilter(String pathFragment) { |
||||
this.pathFragment = pathFragment; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public FilterOutput apply(FilterOutput filterItems) { |
||||
|
||||
List<Object> result = new JSONArray(); |
||||
|
||||
for (Object item : filterItems.getResultAsList()) { |
||||
if (isMatch(item)) { |
||||
result.add(item); |
||||
} |
||||
} |
||||
return new FilterOutput(result); |
||||
} |
||||
|
||||
private boolean isMatch(Object check) { |
||||
Matcher matcher = PATTERN.matcher(pathFragment); |
||||
|
||||
if (matcher.matches()) { |
||||
String property = matcher.group(1); |
||||
String operator = matcher.group(2); |
||||
String expected = matcher.group(3); |
||||
|
||||
if (!JsonUtil.isMap(check)) { |
||||
return false; |
||||
} |
||||
Map obj = JsonUtil.toMap(check); |
||||
|
||||
if (!obj.containsKey(property)) { |
||||
return false; |
||||
} |
||||
|
||||
Object propertyValue = obj.get(property); |
||||
|
||||
if (JsonUtil.isContainer(propertyValue)) { |
||||
return false; |
||||
} |
||||
|
||||
String expression = propertyValue + " " + operator + " " + expected; |
||||
|
||||
return ExpressionEvaluator.eval(propertyValue, operator, expected); |
||||
|
||||
} |
||||
return false; |
||||
} |
||||
} |
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
|
||||
import com.jayway.jsonpath.eval.ExpressionEvaluator; |
||||
import com.jayway.jsonpath.json.JsonArray; |
||||
import com.jayway.jsonpath.json.JsonElement; |
||||
import com.jayway.jsonpath.json.JsonException; |
||||
import com.jayway.jsonpath.json.JsonFactory; |
||||
import com.jayway.jsonpath.json.JsonObject; |
||||
|
||||
|
||||
|
||||
import java.util.ArrayList; |
||||
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:27 PM |
||||
*/ |
||||
public class ListEvalFilter extends JsonPathFilterBase { |
||||
|
||||
//@Autowired
|
||||
public JsonFactory factory = com.jayway.jsonpath.json.minidev.MiniJsonFactory.getInstance(); |
||||
|
||||
public static final Pattern PATTERN = Pattern.compile("\\[\\s?\\?\\s?\\(\\s?@.(\\w+)\\s?([=<>]+)\\s?(.*)\\s?\\)\\s?\\]"); //[?( @.title< 'ko')]
|
||||
|
||||
private final String pathFragment; |
||||
|
||||
public ListEvalFilter(String pathFragment) { |
||||
this.pathFragment = pathFragment; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public List<JsonElement> apply(JsonElement element) throws JsonException { |
||||
List<JsonElement> result = new ArrayList<JsonElement>(); |
||||
|
||||
if(element.isJsonArray()){ |
||||
for(JsonElement subElement: element.toJsonArray()){ |
||||
if (isMatch(subElement)) { |
||||
result.add(subElement); |
||||
} |
||||
} |
||||
} |
||||
|
||||
return result; |
||||
|
||||
} |
||||
|
||||
private boolean isMatch(JsonElement check) throws JsonException { |
||||
Matcher matcher = PATTERN.matcher(pathFragment); |
||||
|
||||
if (matcher.matches()) { |
||||
String property = matcher.group(1); |
||||
String operator = matcher.group(2); |
||||
String expected = matcher.group(3); |
||||
|
||||
if (!check.isJsonObject()) { |
||||
return false; |
||||
} |
||||
|
||||
JsonObject obj = check.toJsonObject(); |
||||
|
||||
if (!obj.hasProperty(property)) { |
||||
return false; |
||||
} |
||||
|
||||
JsonElement propertyValue = obj.getProperty(property); |
||||
|
||||
if (propertyValue.isContainer()) { |
||||
return false; |
||||
} |
||||
|
||||
String expression = propertyValue.toObject() + " " + operator + " " + expected; |
||||
|
||||
return ExpressionEvaluator.eval(propertyValue.toObject(), operator, expected); |
||||
|
||||
} |
||||
return false; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public String getPathSegment() throws JsonException { |
||||
return pathFragment; |
||||
} |
||||
} |
||||
|
@ -1,67 +1,81 @@
|
||||
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; |
||||
} |
||||
} |
||||
} |
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
import com.jayway.jsonpath.json.JsonArray; |
||||
import com.jayway.jsonpath.json.JsonElement; |
||||
import com.jayway.jsonpath.json.JsonException; |
||||
|
||||
|
||||
import java.util.ArrayList; |
||||
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 { |
||||
|
||||
//@Autowired
|
||||
public com.jayway.jsonpath.json.JsonFactory factory = com.jayway.jsonpath.json.minidev.MiniJsonFactory.getInstance(); |
||||
|
||||
public static final Pattern PATTERN = Pattern.compile("\\[\\s?:(\\d+)\\s?\\]"); //[ :2 ]
|
||||
|
||||
private final String pathFragment; |
||||
|
||||
|
||||
@Override |
||||
public String getPathSegment() throws JsonException { |
||||
return pathFragment; |
||||
} |
||||
public ListFrontFilter(String pathFragment) { |
||||
this.pathFragment = pathFragment; |
||||
} |
||||
|
||||
@Override |
||||
public List<JsonElement> apply(JsonElement element) throws JsonException { |
||||
List<JsonElement> result = new ArrayList<JsonElement>(); |
||||
if(element.isJsonArray()){ |
||||
Integer[] index = getListPullIndex(); |
||||
for (int i : index) { |
||||
if (indexIsInRange(element.toJsonArray(), i)) { |
||||
result.add(element.toJsonArray().get(i)); |
||||
} |
||||
} |
||||
} |
||||
return 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; |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
@ -1,72 +1,78 @@
|
||||
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]); |
||||
} |
||||
} |
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
|
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.LinkedList; |
||||
import java.util.List; |
||||
import java.util.regex.Pattern; |
||||
|
||||
import com.jayway.jsonpath.json.JsonArray; |
||||
import com.jayway.jsonpath.json.JsonElement; |
||||
import com.jayway.jsonpath.json.JsonException; |
||||
|
||||
/** |
||||
* 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; |
||||
@Override |
||||
public String getPathSegment() throws JsonException { |
||||
return pathFragment; |
||||
} |
||||
//@Autowired
|
||||
public com.jayway.jsonpath.json.JsonFactory factory = com.jayway.jsonpath.json.minidev.MiniJsonFactory.getInstance(); |
||||
|
||||
public ListIndexFilter(String pathFragment) { |
||||
this.pathFragment = pathFragment; |
||||
} |
||||
|
||||
@Override |
||||
public List<JsonElement> apply(JsonElement element) throws JsonException { |
||||
List<JsonElement> result = new ArrayList<JsonElement>(); |
||||
|
||||
Integer[] index = getArrayIndex(); |
||||
if(element.isJsonArray()){ |
||||
for (int i : index) { |
||||
if (indexIsInRange(element.toJsonArray(), i)) { |
||||
result.add(element.toJsonArray().get(i)); |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
return 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]); |
||||
} |
||||
} |
||||
|
@ -1,52 +1,62 @@
|
||||
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"); |
||||
} |
||||
} |
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
|
||||
import com.jayway.jsonpath.json.JsonArray; |
||||
import com.jayway.jsonpath.json.JsonElement; |
||||
import com.jayway.jsonpath.json.JsonException; |
||||
|
||||
import java.util.ArrayList; |
||||
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 { |
||||
|
||||
//@Autowired
|
||||
public com.jayway.jsonpath.json.JsonFactory factory = com.jayway.jsonpath.json.minidev.MiniJsonFactory.getInstance(); |
||||
|
||||
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 String getPathSegment() throws JsonException { |
||||
return pathFragment; |
||||
} |
||||
@Override |
||||
public List<JsonElement> apply(JsonElement element) throws JsonException { |
||||
List<JsonElement> result = new ArrayList<JsonElement>(); |
||||
|
||||
String prop = getFilterProperty(); |
||||
|
||||
if(element.isJsonArray()){ |
||||
for(JsonElement subElement : element.toJsonArray()){ |
||||
if (subElement.isJsonObject()) { |
||||
if (subElement.toJsonObject().hasProperty(prop)) { |
||||
result.add(subElement); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
return result; |
||||
} |
||||
|
||||
|
||||
private String getFilterProperty() { |
||||
Matcher matcher = PATTERN.matcher(pathFragment); |
||||
if (matcher.matches()) { |
||||
return matcher.group(1); |
||||
} |
||||
throw new IllegalArgumentException("invalid list filter property"); |
||||
} |
||||
} |
||||
|
@ -1,56 +1,69 @@
|
||||
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"); |
||||
|
||||
} |
||||
} |
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.regex.Matcher; |
||||
import java.util.regex.Pattern; |
||||
|
||||
import com.jayway.jsonpath.json.JsonElement; |
||||
import com.jayway.jsonpath.json.JsonException; |
||||
|
||||
/** |
||||
* 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 String getPathSegment() throws JsonException { |
||||
return pathFragment; |
||||
} |
||||
@Override |
||||
public List<JsonElement> apply(JsonElement element) throws JsonException { |
||||
List<JsonElement> result = new ArrayList<JsonElement>(); |
||||
|
||||
if(element.isJsonArray()){ |
||||
int index = getTailIndex(element.toJsonArray().size()); |
||||
result.add(element.toJsonArray().get(index)); |
||||
} |
||||
|
||||
return result; |
||||
|
||||
} |
||||
|
||||
|
||||
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"); |
||||
|
||||
} |
||||
} |
||||
|
@ -1,19 +1,36 @@
|
||||
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()); |
||||
} |
||||
} |
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.regex.Pattern; |
||||
|
||||
import com.jayway.jsonpath.json.JsonElement; |
||||
import com.jayway.jsonpath.json.JsonException; |
||||
|
||||
/** |
||||
* 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 List<JsonElement> apply(JsonElement element) throws JsonException { |
||||
List<JsonElement> result = new ArrayList<JsonElement>(); |
||||
if(element.isJsonArray()){ |
||||
for(JsonElement ele : element.toJsonArray()){ |
||||
result.add(ele); |
||||
} |
||||
} |
||||
|
||||
return result; |
||||
} |
||||
|
||||
@Override |
||||
public String getPathSegment() throws JsonException { |
||||
return null; |
||||
} |
||||
} |
||||
|
@ -1,54 +1,59 @@
|
||||
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); |
||||
} |
||||
} |
||||
} |
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
|
||||
import com.jayway.jsonpath.json.JsonArray; |
||||
import com.jayway.jsonpath.json.JsonElement; |
||||
import com.jayway.jsonpath.json.JsonException; |
||||
import com.jayway.jsonpath.json.JsonFactory; |
||||
|
||||
|
||||
import java.util.ArrayList; |
||||
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; |
||||
|
||||
|
||||
private JsonFactory factory = com.jayway.jsonpath.json.minidev.MiniJsonFactory.getInstance(); |
||||
|
||||
public PropertyFilter(String pathFragment) { |
||||
this.pathFragment = pathFragment; |
||||
} |
||||
|
||||
@Override |
||||
public String getPathSegment() throws JsonException { |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
public List<JsonElement> apply(JsonElement element) throws JsonException { |
||||
|
||||
List<JsonElement> result = new ArrayList<JsonElement>(); |
||||
|
||||
if (element.isJsonObject() && element.toJsonObject().hasProperty(pathFragment)) { |
||||
JsonElement o = element.toJsonObject().getProperty(pathFragment); |
||||
if(o != null){ |
||||
result.add(o); |
||||
} |
||||
else{ |
||||
result.add(factory.createJsonNull(pathFragment,element)); |
||||
} |
||||
} |
||||
else if(element.isJsonObject()){ |
||||
result.add(factory.createJsonNull(pathFragment,element)); |
||||
} |
||||
|
||||
return result; |
||||
} |
||||
} |
||||
|
@ -1,20 +1,32 @@
|
||||
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; |
||||
} |
||||
} |
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.regex.Pattern; |
||||
|
||||
import com.jayway.jsonpath.json.JsonElement; |
||||
import com.jayway.jsonpath.json.JsonException; |
||||
|
||||
/** |
||||
* 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 List<JsonElement> apply(JsonElement element) throws JsonException { |
||||
|
||||
List<JsonElement> result = new ArrayList<JsonElement>(); |
||||
result.add(element); |
||||
return result; |
||||
} |
||||
|
||||
@Override |
||||
public String getPathSegment() throws JsonException { |
||||
return null; |
||||
} |
||||
} |
||||
|
@ -1,48 +1,57 @@
|
||||
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); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
import com.jayway.jsonpath.json.JsonArray; |
||||
import com.jayway.jsonpath.json.JsonElement; |
||||
import com.jayway.jsonpath.json.JsonException; |
||||
import com.jayway.jsonpath.json.JsonFactory; |
||||
import com.jayway.jsonpath.json.JsonObject; |
||||
|
||||
|
||||
import java.util.ArrayList; |
||||
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("\\.\\."); |
||||
|
||||
|
||||
public List<JsonElement> apply(JsonElement element) throws JsonException { |
||||
List<JsonElement> result = new ArrayList<JsonElement>(); |
||||
traverse(element, result); |
||||
return result; |
||||
} |
||||
|
||||
private void traverse(JsonElement container, List<JsonElement> result) throws JsonException { |
||||
|
||||
if (container.isJsonObject()) { |
||||
result.add(container); |
||||
|
||||
for (JsonElement value : container.toJsonObject().getProperties()) { |
||||
if (value.isContainer()) { |
||||
traverse(value, result); |
||||
} |
||||
} |
||||
} else if (container.isJsonArray()) { |
||||
|
||||
for (JsonElement value : container.toJsonArray()) { |
||||
if ( value.isContainer()) { |
||||
traverse(value, result); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public String getPathSegment() throws JsonException { |
||||
|
||||
return null; |
||||
} |
||||
|
||||
} |
@ -1,39 +1,48 @@
|
||||
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); |
||||
|
||||
} |
||||
} |
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
|
||||
import com.jayway.jsonpath.json.JsonArray; |
||||
import com.jayway.jsonpath.json.JsonElement; |
||||
import com.jayway.jsonpath.json.JsonException; |
||||
import com.jayway.jsonpath.json.JsonFactory; |
||||
|
||||
|
||||
import java.util.ArrayList; |
||||
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("\\*"); |
||||
private JsonFactory factory = com.jayway.jsonpath.json.minidev.MiniJsonFactory.getInstance(); |
||||
|
||||
@Override |
||||
public String getPathSegment() throws JsonException { |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
public List<JsonElement> apply(JsonElement element) throws JsonException { |
||||
List<JsonElement> result = new ArrayList<JsonElement>(); |
||||
|
||||
if (element.isJsonArray()) { |
||||
for (JsonElement current : element.toJsonArray()) { |
||||
for (JsonElement value : current.toJsonObject().getProperties()) { |
||||
result.add(value); |
||||
} |
||||
} |
||||
} else { |
||||
for (JsonElement value : element.toJsonObject().getProperties()){ |
||||
result.add(value); |
||||
} |
||||
} |
||||
return result; |
||||
|
||||
} |
||||
} |
||||
|
@ -1,65 +1,65 @@
|
||||
package com.jayway.jsonpath; |
||||
|
||||
import com.jayway.jsonpath.eval.ExpressionEvaluator; |
||||
import org.junit.Test; |
||||
|
||||
import static org.junit.Assert.assertFalse; |
||||
import static org.junit.Assert.assertTrue; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 2/4/11 |
||||
* Time: 9:32 PM |
||||
*/ |
||||
public class ExpressionEvalTest { |
||||
|
||||
|
||||
@Test |
||||
public void long_eval() throws Exception { |
||||
|
||||
assertTrue(ExpressionEvaluator.eval(1L, "=", "1")); |
||||
assertTrue(ExpressionEvaluator.eval(2L, "!=", "1")); |
||||
assertTrue(ExpressionEvaluator.eval(2L, ">", "1")); |
||||
assertTrue(ExpressionEvaluator.eval(2L, ">=", "1")); |
||||
assertTrue(ExpressionEvaluator.eval(2L, ">=", "2")); |
||||
assertTrue(ExpressionEvaluator.eval(1L, "<", "2")); |
||||
assertTrue(ExpressionEvaluator.eval(2L, "<=", "2")); |
||||
|
||||
assertFalse(ExpressionEvaluator.eval(1, ">", "2")); |
||||
assertFalse(ExpressionEvaluator.eval(1, ">=", "2")); |
||||
assertFalse(ExpressionEvaluator.eval(2, "<", "1")); |
||||
assertFalse(ExpressionEvaluator.eval(2, "<=", "1")); |
||||
assertFalse(ExpressionEvaluator.eval(1, "=", "2")); |
||||
assertFalse(ExpressionEvaluator.eval(1, "!=", "1")); |
||||
} |
||||
|
||||
@Test |
||||
public void double_eval() throws Exception { |
||||
|
||||
assertTrue(ExpressionEvaluator.eval(1D, "=", "1")); |
||||
assertTrue(ExpressionEvaluator.eval(2D, "!=", "1")); |
||||
assertTrue(ExpressionEvaluator.eval(2D, ">", "1")); |
||||
assertTrue(ExpressionEvaluator.eval(2D, ">=", "1")); |
||||
assertTrue(ExpressionEvaluator.eval(2D, ">=", "2")); |
||||
assertTrue(ExpressionEvaluator.eval(1D, "<", "2")); |
||||
assertTrue(ExpressionEvaluator.eval(2D, "<=", "2")); |
||||
|
||||
assertFalse(ExpressionEvaluator.eval(1D, ">", "2")); |
||||
assertFalse(ExpressionEvaluator.eval(1D, ">=", "2")); |
||||
assertFalse(ExpressionEvaluator.eval(2D, "<", "1")); |
||||
assertFalse(ExpressionEvaluator.eval(2D, "<=", "1")); |
||||
assertFalse(ExpressionEvaluator.eval(1D, "=", "2")); |
||||
assertFalse(ExpressionEvaluator.eval(1D, "!=", "1")); |
||||
} |
||||
|
||||
@Test |
||||
public void string_eval() throws Exception { |
||||
|
||||
assertTrue(ExpressionEvaluator.eval("A", "=", "A")); |
||||
assertTrue(ExpressionEvaluator.eval("B", "!=", "A")); |
||||
|
||||
} |
||||
|
||||
|
||||
} |
||||
package com.jayway.jsonpath; |
||||
|
||||
import com.jayway.jsonpath.eval.ExpressionEvaluator; |
||||
import org.junit.Test; |
||||
|
||||
import static org.junit.Assert.assertFalse; |
||||
import static org.junit.Assert.assertTrue; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 2/4/11 |
||||
* Time: 9:32 PM |
||||
*/ |
||||
public class ExpressionEvalTest { |
||||
|
||||
|
||||
@Test |
||||
public void long_eval() throws Exception { |
||||
|
||||
assertTrue(ExpressionEvaluator.eval(1L, "=", "1")); |
||||
assertTrue(ExpressionEvaluator.eval(2L, "!=", "1")); |
||||
assertTrue(ExpressionEvaluator.eval(2L, ">", "1")); |
||||
assertTrue(ExpressionEvaluator.eval(2L, ">=", "1")); |
||||
assertTrue(ExpressionEvaluator.eval(2L, ">=", "2")); |
||||
assertTrue(ExpressionEvaluator.eval(1L, "<", "2")); |
||||
assertTrue(ExpressionEvaluator.eval(2L, "<=", "2")); |
||||
|
||||
assertFalse(ExpressionEvaluator.eval(1, ">", "2")); |
||||
assertFalse(ExpressionEvaluator.eval(1, ">=", "2")); |
||||
assertFalse(ExpressionEvaluator.eval(2, "<", "1")); |
||||
assertFalse(ExpressionEvaluator.eval(2, "<=", "1")); |
||||
assertFalse(ExpressionEvaluator.eval(1, "=", "2")); |
||||
assertFalse(ExpressionEvaluator.eval(1, "!=", "1")); |
||||
} |
||||
|
||||
@Test |
||||
public void double_eval() throws Exception { |
||||
|
||||
assertTrue(ExpressionEvaluator.eval(1D, "=", "1")); |
||||
assertTrue(ExpressionEvaluator.eval(2D, "!=", "1")); |
||||
assertTrue(ExpressionEvaluator.eval(2D, ">", "1")); |
||||
assertTrue(ExpressionEvaluator.eval(2D, ">=", "1")); |
||||
assertTrue(ExpressionEvaluator.eval(2D, ">=", "2")); |
||||
assertTrue(ExpressionEvaluator.eval(1D, "<", "2")); |
||||
assertTrue(ExpressionEvaluator.eval(2D, "<=", "2")); |
||||
|
||||
assertFalse(ExpressionEvaluator.eval(1D, ">", "2")); |
||||
assertFalse(ExpressionEvaluator.eval(1D, ">=", "2")); |
||||
assertFalse(ExpressionEvaluator.eval(2D, "<", "1")); |
||||
assertFalse(ExpressionEvaluator.eval(2D, "<=", "1")); |
||||
assertFalse(ExpressionEvaluator.eval(1D, "=", "2")); |
||||
assertFalse(ExpressionEvaluator.eval(1D, "!=", "1")); |
||||
} |
||||
|
||||
@Test |
||||
public void string_eval() throws Exception { |
||||
|
||||
assertTrue(ExpressionEvaluator.eval("A", "=", "A")); |
||||
assertTrue(ExpressionEvaluator.eval("B", "!=", "A")); |
||||
|
||||
} |
||||
|
||||
|
||||
} |
||||
|
@ -1,203 +1,288 @@
|
||||
package com.jayway.jsonpath; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import static org.hamcrest.Matchers.equalTo; |
||||
import static org.hamcrest.Matchers.hasItems; |
||||
import static org.junit.Assert.*; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 2/2/11 |
||||
* Time: 3:07 PM |
||||
*/ |
||||
public class JsonPathTest { |
||||
|
||||
public final static String ARRAY = "[{\"value\": 1},{\"value\": 2}, {\"value\": 3},{\"value\": 4}]"; |
||||
|
||||
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" + |
||||
" }\n" + |
||||
" }\n" + |
||||
"}"; |
||||
|
||||
|
||||
@Test |
||||
public void filter_an_array() throws Exception { |
||||
List<Object> matches = JsonPath.read(ARRAY, "$.[?(@.value = 1)]"); |
||||
|
||||
assertEquals(1, matches.size()); |
||||
System.out.println(matches); |
||||
} |
||||
|
||||
@Test |
||||
public void read_path_with_colon() throws Exception { |
||||
|
||||
assertEquals(JsonPath.read(DOCUMENT, "$.store.bicycle.foo:bar"), "fooBar"); |
||||
assertEquals(JsonPath.read(DOCUMENT, "$.['store'].['bicycle'].['foo:bar']"), "fooBar"); |
||||
} |
||||
|
||||
@Test |
||||
public void read_document_from_root() throws Exception { |
||||
|
||||
Map result = JsonPath.read(DOCUMENT, "$.store"); |
||||
|
||||
assertEquals(2, result.values().size()); |
||||
|
||||
|
||||
} |
||||
|
||||
|
||||
@Test |
||||
public void read_store_book_1() throws Exception { |
||||
|
||||
JsonPath path = JsonPath.compile("$.store.book[1]"); |
||||
|
||||
Map map = path.read(DOCUMENT); |
||||
|
||||
assertEquals("Evelyn Waugh", map.get("author")); |
||||
} |
||||
|
||||
@Test |
||||
public void read_store_book_wildcard() throws Exception { |
||||
JsonPath path = JsonPath.compile("$.store.book[*]"); |
||||
|
||||
List<Object> list = path.read(DOCUMENT); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
public void read_store_book_author() throws Exception { |
||||
assertThat(JsonPath.<List<String>>read(DOCUMENT, "$.store.book[0,1].author"), hasItems("Nigel Rees", "Evelyn Waugh")); |
||||
assertThat(JsonPath.<List<String>>read(DOCUMENT, "$.store.book[*].author"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); |
||||
assertThat(JsonPath.<List<String>>read(DOCUMENT, "$.['store'].['book'][*].['author']"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); |
||||
} |
||||
|
||||
|
||||
@Test |
||||
public void all_authors() throws Exception { |
||||
assertThat(JsonPath.<List<String>>read(DOCUMENT, "$..author"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); |
||||
} |
||||
|
||||
|
||||
@Test |
||||
public void all_store_properties() throws Exception { |
||||
List<Object> itemsInStore = JsonPath.read(DOCUMENT, "$.store.*"); |
||||
|
||||
assertEquals(JsonPath.read(itemsInStore, "$.[0].[0].author"), "Nigel Rees"); |
||||
assertEquals(JsonPath.read(itemsInStore, "$.[0][0].author"), "Nigel Rees"); |
||||
} |
||||
|
||||
@Test |
||||
public void all_prices_in_store() throws Exception { |
||||
|
||||
assertThat(JsonPath.<List<Double>>read(DOCUMENT, "$.store..price"), hasItems(8.95D, 12.99D, 8.99D, 19.95D)); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
public void access_array_by_index_from_tail() throws Exception { |
||||
|
||||
assertThat(JsonPath.<String>read(DOCUMENT, "$..book[(@.length-1)].author"), equalTo("J. R. R. Tolkien")); |
||||
assertThat(JsonPath.<String>read(DOCUMENT, "$..book[-1:].author"), equalTo("J. R. R. Tolkien")); |
||||
} |
||||
|
||||
@Test |
||||
public void read_store_book_index_0_and_1() throws Exception { |
||||
|
||||
assertThat(JsonPath.<List<String>>read(DOCUMENT, "$.store.book[0,1].author"), hasItems("Nigel Rees", "Evelyn Waugh")); |
||||
assertTrue(JsonPath.<List>read(DOCUMENT, "$.store.book[0,1].author").size() == 2); |
||||
} |
||||
|
||||
@Test |
||||
public void read_store_book_pull_first_2() throws Exception { |
||||
|
||||
assertThat(JsonPath.<List<String>>read(DOCUMENT, "$.store.book[:2].author"), hasItems("Nigel Rees", "Evelyn Waugh")); |
||||
assertTrue(JsonPath.<List>read(DOCUMENT, "$.store.book[:2].author").size() == 2); |
||||
} |
||||
|
||||
|
||||
@Test |
||||
public void read_store_book_filter_by_isbn() throws Exception { |
||||
|
||||
assertThat(JsonPath.<List<String>>read(DOCUMENT, "$.store.book[?(@.isbn)].isbn"), hasItems("0-553-21311-3", "0-395-19395-8")); |
||||
assertTrue(JsonPath.<List>read(DOCUMENT, "$.store.book[?(@.isbn)].isbn").size() == 2); |
||||
} |
||||
|
||||
@Test |
||||
public void all_books_cheaper_than_10() throws Exception { |
||||
|
||||
assertThat(JsonPath.<List<String>>read(DOCUMENT, "$.store.book[?(@.price < 10)].title"), hasItems("Sayings of the Century", "Moby Dick")); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
public void all_books_with_category_reference() throws Exception { |
||||
|
||||
assertThat(JsonPath.<List<String>>read(DOCUMENT, "$..book[?(@.category = 'reference')].title"), hasItems("Sayings of the Century")); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
public void all_members_of_all_documents() throws Exception { |
||||
List<String> all = JsonPath.read(DOCUMENT, "$..*"); |
||||
} |
||||
|
||||
@Test |
||||
public void access_index_out_of_bounds_does_not_throw_exception() throws Exception { |
||||
|
||||
Object res = JsonPath.read(DOCUMENT, "$.store.book[100].author"); |
||||
|
||||
assertNull(res); |
||||
|
||||
res = JsonPath.read(DOCUMENT, "$.store.book[1, 200].author"); |
||||
|
||||
|
||||
assertThat((List<String>)res, hasItems("Evelyn Waugh")); |
||||
//assertNull(();
|
||||
} |
||||
|
||||
/* |
||||
@Test(expected = InvalidPathException.class) |
||||
public void invalid_space_path_throws_exception() throws Exception { |
||||
JsonPath.read(DOCUMENT, "space is not good"); |
||||
} |
||||
*/ |
||||
|
||||
@Test(expected = InvalidPathException.class) |
||||
public void invalid_new_path_throws_exception() throws Exception { |
||||
JsonPath.read(DOCUMENT, "new "); |
||||
} |
||||
} |
||||
package com.jayway.jsonpath; |
||||
|
||||
import org.junit.Before; |
||||
import org.junit.Ignore; |
||||
import org.junit.Test; |
||||
|
||||
import com.jayway.jsonpath.json.JsonArray; |
||||
import com.jayway.jsonpath.json.JsonElement; |
||||
import com.jayway.jsonpath.json.JsonException; |
||||
import com.jayway.jsonpath.json.JsonFactory; |
||||
import com.jayway.jsonpath.json.JsonObject; |
||||
|
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import static org.hamcrest.Matchers.equalTo; |
||||
import static org.hamcrest.Matchers.hasItems; |
||||
import static org.junit.Assert.*; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 2/2/11 |
||||
* Time: 3:07 PM |
||||
*/ |
||||
public abstract class JsonPathTest { |
||||
|
||||
public final static String ARRAY = "[{\"value\": 1},{\"value\": 2}, {\"value\": 3},{\"value\": 4}]"; |
||||
|
||||
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" + |
||||
" }\n" + |
||||
" }\n" + |
||||
"}"; |
||||
|
||||
@Before |
||||
public void init_factory(){ |
||||
|
||||
} |
||||
|
||||
@Test |
||||
public void filter_an_array() throws Exception { |
||||
JsonElement matches = JsonPath.read(ARRAY, "$.[?(@.value = 1)]"); |
||||
|
||||
assertEquals(true, matches.isJsonObject()); |
||||
System.out.println(matches); |
||||
} |
||||
|
||||
@Test |
||||
public void read_path_with_colon() throws Exception { |
||||
|
||||
assertEquals(JsonPath.read(DOCUMENT, "$.store.bicycle.foo:bar").toObject(), "fooBar"); |
||||
assertEquals(JsonPath.read(DOCUMENT, "$.['store'].['bicycle'].['foo:bar']").toObject(), "fooBar"); |
||||
} |
||||
|
||||
|
||||
|
||||
|
||||
@Test |
||||
public void parent_ref() throws Exception { |
||||
|
||||
|
||||
String doc = "{foo:{biz:{id:1}}}"; |
||||
|
||||
|
||||
|
||||
assertEquals(JsonPath.read(doc, "$.foo.biz").toString(), "{\"id\":1}"); |
||||
|
||||
JsonElement j = JsonPath.read(doc, "$.foo.biz"); |
||||
assertEquals(j.getParentReference().getParent().toString() , "{\"biz\":{\"id\":1}}");; |
||||
assertEquals(j.getParentReference().getParent().getParentReference().getParent().toString(), "{\"foo\":{\"biz\":{\"id\":1}}}"); |
||||
|
||||
|
||||
doc = "{foo:{biz:[{Id:1},{Id:2},{Id:4,foo:1234}]}}"; |
||||
|
||||
JsonElement root = JsonPath.parse(doc); |
||||
assertEquals(JsonPath.read(doc, "$.foo.biz.[2].foo").toString(), "1234"); |
||||
assertEquals(JsonPath.read(doc, "$.foo.biz.[2].foo").getParentReference().getParent().toJsonObject().get("Id").toString(), "4"); |
||||
|
||||
JsonElement doc_json = JsonFactory.getInstance().parse( "{foo:[[[1],2,3,4],1,2,3]}"); |
||||
JsonElement je = JsonPath.read(doc_json, "$.foo[0][0]"); |
||||
je.getParentReference().setReference( JsonFactory.getInstance().createJsonPrimitive("foo")); |
||||
assertEquals(doc_json.toString(),"{\"foo\":[[\"foo\",2,3,4],1,2,3]}"); |
||||
|
||||
|
||||
|
||||
} |
||||
|
||||
|
||||
@Test |
||||
public void type_test() throws Exception { |
||||
|
||||
|
||||
String doc = "{foo:{biz:{id:1}}}"; |
||||
|
||||
assertEquals(JsonPath.read(doc, "$.foo.biz.(object)").toString(), "{\"id\":1}"); |
||||
assertEquals(JsonPath.read(doc, "$.foo.biz.(collection)"), null); |
||||
|
||||
|
||||
doc = "{foo:{biz:[{Id:1},{Id:2},{Id:4,foo:1234}]}}"; |
||||
|
||||
JsonElement root = JsonPath.parse(doc); |
||||
assertEquals(JsonPath.read(doc, "$.foo.biz.(collection)[2].foo.(value)").toString(), "1234"); |
||||
|
||||
|
||||
|
||||
} |
||||
|
||||
|
||||
|
||||
@Test |
||||
public void read_document_from_root() throws Exception { |
||||
|
||||
com.jayway.jsonpath.json.JsonObject result = JsonPath.read(DOCUMENT, "$.store").toJsonObject(); |
||||
|
||||
assertEquals(2, result.getProperties().size()); |
||||
|
||||
|
||||
} |
||||
|
||||
|
||||
@Test |
||||
public void read_store_book_1() throws Exception { |
||||
|
||||
JsonPath path = JsonPath.compile("$.store.book[1]"); |
||||
|
||||
JsonElement map = path.read(DOCUMENT); |
||||
|
||||
assertEquals("Evelyn Waugh", map.toJsonObject().get("author").toObject()); |
||||
} |
||||
|
||||
@Test |
||||
public void read_store_book_wildcard() throws Exception { |
||||
JsonPath path = JsonPath.compile("$.store.book[*]"); |
||||
|
||||
JsonElement list = path.read(DOCUMENT); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
public void read_store_book_author() throws Exception { |
||||
Iterable<String> l1 = toList(JsonPath.read(DOCUMENT, "$.store.book[0,1].author")); |
||||
assertThat(l1, hasItems("Nigel Rees", "Evelyn Waugh")); |
||||
|
||||
Iterable<String> l2 = toList(JsonPath.read(DOCUMENT, "$.store.book[*].author")); |
||||
assertThat(l2, hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); |
||||
|
||||
Iterable<String> l3 = toList(JsonPath.read(DOCUMENT, "$.['store'].['book'][*].['author']")); |
||||
assertThat(l3, hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); |
||||
|
||||
} |
||||
|
||||
|
||||
private <T> List<T> toList(JsonElement read) throws JsonException { |
||||
List l = new ArrayList<T>(); |
||||
for(JsonElement e: read.toJsonArray()){ |
||||
l.add((T) e.toObject()); |
||||
} |
||||
return l ; |
||||
} |
||||
|
||||
@Test |
||||
public void all_authors() throws Exception { |
||||
Iterable<String> l1 = toList(JsonPath.read(DOCUMENT, "$..author")); |
||||
assertThat(l1, hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); |
||||
} |
||||
|
||||
|
||||
@Test |
||||
public void all_store_properties() throws Exception { |
||||
JsonArray itemsInStore = JsonPath.read(DOCUMENT, "$.store.*").toJsonArray(); |
||||
|
||||
assertEquals(JsonPath.read(itemsInStore, "$.[0].[0].author").toObject(), "Nigel Rees"); |
||||
assertEquals(JsonPath.read(itemsInStore, "$.[0][0].author").toObject(), "Nigel Rees"); |
||||
} |
||||
|
||||
@Test |
||||
public void all_prices_in_store() throws Exception { |
||||
|
||||
assertThat(this.<Double>toList(JsonPath.read(DOCUMENT, "$.store..price")), hasItems(8.95D, 12.99D, 8.99D, 19.95D)); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
public void access_array_by_index_from_tail() throws Exception { |
||||
|
||||
assertThat((String)JsonPath.read(DOCUMENT, "$..book[(@.length-1)].author").toObject(), equalTo("J. R. R. Tolkien")); |
||||
assertThat((String)JsonPath.read(DOCUMENT, "$..book[-1:].author").toObject(), equalTo("J. R. R. Tolkien")); |
||||
} |
||||
|
||||
@Test |
||||
public void read_store_book_index_0_and_1() throws Exception { |
||||
|
||||
assertThat(this.<String>toList(JsonPath.read(DOCUMENT, "$.store.book[0,1].author")), hasItems("Nigel Rees", "Evelyn Waugh")); |
||||
assertTrue(this.<String>toList(JsonPath.read(DOCUMENT, "$.store.book[0,1].author")).size() == 2); |
||||
} |
||||
|
||||
@Test |
||||
public void read_store_book_pull_first_2() throws Exception { |
||||
|
||||
assertThat(this.<String>toList(JsonPath.read(DOCUMENT, "$.store.book[:2].author")), hasItems("Nigel Rees", "Evelyn Waugh")); |
||||
assertTrue(this.<String>toList(JsonPath.read(DOCUMENT, "$.store.book[:2].author")).size() == 2); |
||||
} |
||||
|
||||
|
||||
@Test |
||||
public void read_store_book_filter_by_isbn() throws Exception { |
||||
|
||||
assertThat(this.<String>toList(JsonPath.read(DOCUMENT, "$.store.book[?(@.isbn)].isbn")), hasItems("0-553-21311-3", "0-395-19395-8")); |
||||
assertTrue(this.<String>toList(JsonPath.read(DOCUMENT, "$.store.book[?(@.isbn)].isbn")).size() == 2); |
||||
} |
||||
|
||||
@Test |
||||
public void all_books_cheaper_than_10() throws Exception { |
||||
Object o = JsonPath.read(DOCUMENT, "$.store.book[?(@.price < 10)].title"); |
||||
assertThat(this.<String>toList(JsonPath.read(DOCUMENT, "$.store.book[?(@.price < 10)].title")), hasItems("Sayings of the Century", "Moby Dick")); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
public void all_books_with_category_reference() throws Exception { |
||||
|
||||
Object o = JsonPath.read(DOCUMENT, "$..book[?(@.category = 'reference')].title"); |
||||
assertEquals(JsonPath.read(DOCUMENT, "$..book[?(@.category = 'reference')].title").toObject(), "Sayings of the Century"); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
public void all_members_of_all_documents() throws Exception { |
||||
JsonPath.read(DOCUMENT, "$..*"); |
||||
} |
||||
|
||||
@Test |
||||
public void access_index_out_of_bounds_does_not_throw_exception() throws Exception { |
||||
|
||||
Object res = JsonPath.read(DOCUMENT, "$.store.book[100].author"); |
||||
|
||||
assertNull(res); |
||||
|
||||
JsonElement res2 = JsonPath.read(DOCUMENT, "$.store.book[1, 200].author"); |
||||
|
||||
|
||||
assertEquals(res2.toObject(), "Evelyn Waugh"); |
||||
//assertNull(();
|
||||
} |
||||
|
||||
/* |
||||
@Test(expected = InvalidPathException.class) |
||||
public void invalid_space_path_throws_exception() throws Exception { |
||||
JsonPath.read(DOCUMENT, "space is not good"); |
||||
} |
||||
*/ |
||||
|
||||
@Test(expected = InvalidPathException.class) |
||||
public void invalid_new_path_throws_exception() throws Exception { |
||||
JsonPath.read(DOCUMENT, "new "); |
||||
} |
||||
} |
||||
|
@ -1,30 +1,30 @@
|
||||
package com.jayway.jsonpath; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import static org.junit.Assert.assertFalse; |
||||
import static org.junit.Assert.assertTrue; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 2/3/11 |
||||
* Time: 9:58 PM |
||||
*/ |
||||
public class PathUtilTest { |
||||
|
||||
|
||||
@Test |
||||
public void wildcard_is_not_definite() throws Exception { |
||||
assertFalse(PathUtil.isPathDefinite("$..book[0]")); |
||||
} |
||||
|
||||
@Test |
||||
public void is_definite() throws Exception { |
||||
assertTrue(PathUtil.isPathDefinite("$.definite.this.is")); |
||||
assertTrue(PathUtil.isPathDefinite("$.definite:this.is")); |
||||
assertTrue(PathUtil.isPathDefinite("rows[0].id")); |
||||
} |
||||
|
||||
|
||||
} |
||||
package com.jayway.jsonpath; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import static org.junit.Assert.assertFalse; |
||||
import static org.junit.Assert.assertTrue; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 2/3/11 |
||||
* Time: 9:58 PM |
||||
*/ |
||||
public class PathUtilTest { |
||||
|
||||
|
||||
@Test |
||||
public void wildcard_is_not_definite() throws Exception { |
||||
assertFalse(PathUtil.isPathDefinite("$..book[0]")); |
||||
} |
||||
|
||||
@Test |
||||
public void is_definite() throws Exception { |
||||
assertTrue(PathUtil.isPathDefinite("$.definite.this.is")); |
||||
assertTrue(PathUtil.isPathDefinite("$.definite:this.is")); |
||||
assertTrue(PathUtil.isPathDefinite("rows[0].id")); |
||||
} |
||||
|
||||
|
||||
} |
||||
|
@ -1,69 +1,69 @@
|
||||
package com.jayway.jsonpath; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import static org.hamcrest.Matchers.hasItemInArray; |
||||
import static org.hamcrest.Matchers.hasItems; |
||||
import static org.junit.Assert.assertThat; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 2/2/11 |
||||
* Time: 1:22 PM |
||||
*/ |
||||
public class SplitPathFragmentsTest { |
||||
|
||||
/* |
||||
1. "$..book[-1:].foo.bar" |
||||
2. "$.store.book[*].author" |
||||
3. "$..author" |
||||
4. "$.store.*" |
||||
5. "$.store..price" |
||||
6. "$..book[(@.length-1)]" |
||||
7. "$..book[-1:] |
||||
8. "$..book[0,1]" |
||||
9. "$..book[:2]" |
||||
10. "$..book[?(@.isbn)]" |
||||
11. "$..book[?(@.price<10)]" |
||||
12. "$..*" |
||||
*/ |
||||
|
||||
|
||||
@Test |
||||
public void fragments_are_split_correctly() throws Exception { |
||||
|
||||
assertThat(PathUtil.splitPath("$..book[-1:].foo.bar"), hasItems("$", "..", "[-1:]", "foo", "bar")); |
||||
|
||||
assertThat(PathUtil.splitPath("$.store.book[*].author"), hasItems("$", "store", "book", "[*]", "author")); |
||||
|
||||
assertThat(PathUtil.splitPath("$..author"), hasItems("$", "..", "author")); |
||||
|
||||
assertThat(PathUtil.splitPath("$.store.*"), hasItems("$", "store", "*")); |
||||
|
||||
assertThat(PathUtil.splitPath("$.store..price"), hasItems("$", "store", "..", "price")); |
||||
|
||||
assertThat(PathUtil.splitPath("$..book[(@.length-1)]"), hasItems("$", "..", "book", "[(@.length-1)]")); |
||||
|
||||
assertThat(PathUtil.splitPath("$..book[-1:]"), hasItems("$", "..", "book", "[-1:]")); |
||||
|
||||
assertThat(PathUtil.splitPath("$..book[0,1]"), hasItems("$", "..", "book", "[0,1]")); |
||||
|
||||
assertThat(PathUtil.splitPath("$..book[:2]"), hasItems("$", "..", "book", "[:2]")); |
||||
|
||||
assertThat(PathUtil.splitPath("$..book[?(@.isbn)]"), hasItems("$", "..", "book", "[?(@.isbn)]")); |
||||
|
||||
assertThat(PathUtil.splitPath("$..book[?(@.price<10)]"), hasItems("$", "..", "book", "[?(@.price<10)]")); |
||||
|
||||
assertThat(PathUtil.splitPath("$..*"), hasItems("$", "..", "*")); |
||||
|
||||
assertThat(PathUtil.splitPath("$.[0][1].author"), hasItems("$", "[0]", "[1]", "author")); |
||||
|
||||
assertThat(PathUtil.splitPath("$.[0].[1].author"), hasItems("$", "[0]", "[1]", "author")); |
||||
|
||||
assertThat(PathUtil.splitPath("$.foo:bar.author"), hasItems("$", "foo:bar", "author")); |
||||
} |
||||
|
||||
|
||||
|
||||
} |
||||
package com.jayway.jsonpath; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import static org.hamcrest.Matchers.hasItemInArray; |
||||
import static org.hamcrest.Matchers.hasItems; |
||||
import static org.junit.Assert.assertThat; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 2/2/11 |
||||
* Time: 1:22 PM |
||||
*/ |
||||
public class SplitPathFragmentsTest { |
||||
|
||||
/* |
||||
1. "$..book[-1:].foo.bar" |
||||
2. "$.store.book[*].author" |
||||
3. "$..author" |
||||
4. "$.store.*" |
||||
5. "$.store..price" |
||||
6. "$..book[(@.length-1)]" |
||||
7. "$..book[-1:] |
||||
8. "$..book[0,1]" |
||||
9. "$..book[:2]" |
||||
10. "$..book[?(@.isbn)]" |
||||
11. "$..book[?(@.price<10)]" |
||||
12. "$..*" |
||||
*/ |
||||
|
||||
|
||||
@Test |
||||
public void fragments_are_split_correctly() throws Exception { |
||||
|
||||
assertThat(PathUtil.splitPath("$..book[-1:].foo.bar"), hasItems("$", "..", "[-1:]", "foo", "bar")); |
||||
|
||||
assertThat(PathUtil.splitPath("$.store.book[*].author"), hasItems("$", "store", "book", "[*]", "author")); |
||||
|
||||
assertThat(PathUtil.splitPath("$..author"), hasItems("$", "..", "author")); |
||||
|
||||
assertThat(PathUtil.splitPath("$.store.*"), hasItems("$", "store", "*")); |
||||
|
||||
assertThat(PathUtil.splitPath("$.store..price"), hasItems("$", "store", "..", "price")); |
||||
|
||||
assertThat(PathUtil.splitPath("$..book[(@.length-1)]"), hasItems("$", "..", "book", "[(@.length-1)]")); |
||||
|
||||
assertThat(PathUtil.splitPath("$..book[-1:]"), hasItems("$", "..", "book", "[-1:]")); |
||||
|
||||
assertThat(PathUtil.splitPath("$..book[0,1]"), hasItems("$", "..", "book", "[0,1]")); |
||||
|
||||
assertThat(PathUtil.splitPath("$..book[:2]"), hasItems("$", "..", "book", "[:2]")); |
||||
|
||||
assertThat(PathUtil.splitPath("$..book[?(@.isbn)]"), hasItems("$", "..", "book", "[?(@.isbn)]")); |
||||
|
||||
assertThat(PathUtil.splitPath("$..book[?(@.price<10)]"), hasItems("$", "..", "book", "[?(@.price<10)]")); |
||||
|
||||
assertThat(PathUtil.splitPath("$..*"), hasItems("$", "..", "*")); |
||||
|
||||
assertThat(PathUtil.splitPath("$.[0][1].author"), hasItems("$", "[0]", "[1]", "author")); |
||||
|
||||
assertThat(PathUtil.splitPath("$.[0].[1].author"), hasItems("$", "[0]", "[1]", "author")); |
||||
|
||||
assertThat(PathUtil.splitPath("$.foo:bar.author"), hasItems("$", "foo:bar", "author")); |
||||
} |
||||
|
||||
|
||||
|
||||
} |
||||
|
Loading…
Reference in new issue