kalle
13 years ago
51 changed files with 1255 additions and 1156 deletions
@ -0,0 +1,25 @@
|
||||
package com.jayway.jsonpath; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 11/8/11 |
||||
* Time: 4:01 PM |
||||
*/ |
||||
public class InvalidJsonException extends RuntimeException{ |
||||
|
||||
public InvalidJsonException() { |
||||
} |
||||
|
||||
public InvalidJsonException(String message) { |
||||
super(message); |
||||
} |
||||
|
||||
public InvalidJsonException(String message, Throwable cause) { |
||||
super(message, cause); |
||||
} |
||||
|
||||
public InvalidJsonException(Throwable cause) { |
||||
super(cause); |
||||
} |
||||
} |
@ -1,61 +0,0 @@
|
||||
package com.jayway.jsonpath; |
||||
|
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* User: kalle stenflo |
||||
* Date: 2/4/11 |
||||
* Time: 1:01 PM |
||||
*/ |
||||
public class JsonUtil { |
||||
/** |
||||
* checks if object is <code>instanceof</code> <code>java.util.List</code> or <code>java.util.Map</code> |
||||
* |
||||
* @param obj object to check |
||||
* @return true if List or Map |
||||
*/ |
||||
public static boolean isContainer(Object obj) { |
||||
return (isList(obj) || isMap(obj)); |
||||
} |
||||
|
||||
/** |
||||
* checks if object is <code>instanceof</code> <code>java.util.List</code> |
||||
* |
||||
* @param obj object to check |
||||
* @return true if List |
||||
*/ |
||||
public static boolean isList(Object obj) { |
||||
return (obj instanceof List); |
||||
} |
||||
|
||||
/** |
||||
* checks if object is <code>instanceof</code> <code>java.util.Map</code> |
||||
* |
||||
* @param obj object to check |
||||
* @return true if Map |
||||
*/ |
||||
public static boolean isMap(Object obj) { |
||||
return (obj instanceof Map); |
||||
} |
||||
|
||||
/** |
||||
* converts casts to <code>java.util.List</code> |
||||
* |
||||
* @param obj |
||||
* @return the list |
||||
*/ |
||||
public static List<Object> toList(Object obj) { |
||||
return (List) obj; |
||||
} |
||||
|
||||
/** |
||||
* converts casts to <code>java.util.Map</code> |
||||
* |
||||
* @param obj |
||||
* @return the Map |
||||
*/ |
||||
public static Map<Object, Object> toMap(Object obj) { |
||||
return (Map) obj; |
||||
} |
||||
} |
@ -0,0 +1,45 @@
|
||||
package com.jayway.jsonpath; |
||||
|
||||
import com.jayway.jsonpath.spi.JsonProvider; |
||||
import com.jayway.jsonpath.spi.impl.DefaultJsonProvider; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 11/8/11 |
||||
* Time: 7:40 PM |
||||
*/ |
||||
public class PathReader { |
||||
|
||||
private Object jsonModel; |
||||
|
||||
private JsonProvider jsonProvider; |
||||
|
||||
private PathReader(String json) { |
||||
this.jsonProvider = new DefaultJsonProvider(); |
||||
this.jsonModel = jsonProvider.parse(json); |
||||
} |
||||
|
||||
private PathReader(Object jsonModel) { |
||||
this.jsonProvider = new DefaultJsonProvider(); |
||||
this.jsonModel = jsonModel; |
||||
} |
||||
|
||||
public static PathReader create(String json) { |
||||
return new PathReader(json); |
||||
} |
||||
|
||||
public <T> T read(String jsonPath) { |
||||
return (T)JsonPath.compile(jsonProvider, jsonPath).read(jsonModel); |
||||
} |
||||
|
||||
public <T> T read(JsonPath jsonPath) { |
||||
return (T)jsonPath.read(jsonModel); |
||||
} |
||||
|
||||
public PathReader get(String jsonPath){ |
||||
Object subModel = read(jsonPath); |
||||
|
||||
return new PathReader(subModel); |
||||
} |
||||
} |
@ -1,210 +0,0 @@
|
||||
package com.jayway.jsonpath; |
||||
|
||||
import java.util.LinkedList; |
||||
import java.util.List; |
||||
import java.util.Queue; |
||||
|
||||
/** |
||||
* User: kalle stenflo |
||||
* Date: 2/2/11 |
||||
* Time: 2:08 PM |
||||
*/ |
||||
public class PathUtil { |
||||
|
||||
/** |
||||
* Checks if a path points to a single item or if it potentially returns multiple items |
||||
* <p/> |
||||
* a path is considered <strong>not</strong> definite if it contains a scan fragment ".." |
||||
* or an array position fragment that is not based on a single index |
||||
* <p/> |
||||
* <p/> |
||||
* absolute path examples: |
||||
* <p/> |
||||
* $store.book |
||||
* $store.book[1].value |
||||
* <p/> |
||||
* not absolute path examples |
||||
* <p/> |
||||
* $..book |
||||
* $.store.book[1,2] |
||||
* $.store.book[?(@.category = 'fiction')] |
||||
* |
||||
* @param jsonPath the path to check |
||||
* @return true if path is definite (points to single item) |
||||
*/ |
||||
public static boolean isPathDefinite(String jsonPath) { |
||||
return !jsonPath.replaceAll("\"[^\"\\\\\\n\r]*\"", "").matches(".*(\\.\\.|\\*|\\[[\\\\/]|\\?|,|:\\s?\\]|\\[\\s?:|>|\\(|<|=|\\+).*"); |
||||
} |
||||
|
||||
/** |
||||
* Splits a path into fragments |
||||
* <p/> |
||||
* the path <code>$.store.book[1].category</code> returns ["$", "store", "book", "[1]", "category"] |
||||
* |
||||
* @param jsonPath path to split |
||||
* @return fragments |
||||
*/ |
||||
public static List<String> splitPath(String jsonPath) { |
||||
|
||||
if (!jsonPath.startsWith("$") && !jsonPath.startsWith("$[")) { |
||||
jsonPath = "$." + jsonPath; |
||||
} |
||||
|
||||
LinkedList<String> fragments = new LinkedList<String>(); |
||||
|
||||
Queue<Character> pathQueue = new LinkedList<Character>(); |
||||
for (char b : jsonPath.toCharArray()) { |
||||
pathQueue.add(b); |
||||
} |
||||
|
||||
while (!pathQueue.isEmpty()) { |
||||
skip(pathQueue, ' '); |
||||
char current = pathQueue.peek(); |
||||
|
||||
switch (current) { |
||||
case '$': |
||||
fragments.add(Character.toString(current)); |
||||
pathQueue.poll(); |
||||
break; |
||||
|
||||
case '.': |
||||
pathQueue.poll(); |
||||
if (pathQueue.peek().equals('.')) { |
||||
pathQueue.poll(); |
||||
fragments.add(".."); |
||||
|
||||
assertNotInvalidPeek(pathQueue, '.'); |
||||
} |
||||
break; |
||||
|
||||
case '[': |
||||
fragments.add(extract(pathQueue, true, ']')); |
||||
break; |
||||
|
||||
default: |
||||
fragments.add(extract(pathQueue, false, '[', '.')); |
||||
|
||||
} |
||||
} |
||||
return fragments; |
||||
} |
||||
|
||||
|
||||
private static String extract(Queue<Character> pathQueue, boolean includeSopChar, char... stopChars) { |
||||
StringBuilder sb = new StringBuilder(); |
||||
while (!pathQueue.isEmpty() && (!isStopChar(pathQueue.peek().charValue(), stopChars))) { |
||||
|
||||
char c = pathQueue.poll(); |
||||
|
||||
if (isStopChar(c, stopChars)) { |
||||
if (includeSopChar) { |
||||
sb.append(c); |
||||
} |
||||
} else { |
||||
sb.append(c); |
||||
} |
||||
} |
||||
if (includeSopChar) { |
||||
assertValidPeek(pathQueue, false, stopChars); |
||||
sb.append(pathQueue.poll()); |
||||
} else { |
||||
assertValidPeek(pathQueue, true, stopChars); |
||||
} |
||||
return clean(sb); |
||||
} |
||||
|
||||
private static String clean(StringBuilder sb) { |
||||
|
||||
String src = sb.toString(); |
||||
|
||||
src = trim(src, "'"); |
||||
src = trim(src, ")"); |
||||
src = trim(src, "("); |
||||
src = trimLeft(src, "?"); |
||||
src = trimLeft(src, "@"); |
||||
|
||||
if (src.length() > 5 && src.subSequence(0, 2).equals("['")) { |
||||
src = src.substring(2); |
||||
src = src.substring(0, src.length() - 2); |
||||
} |
||||
|
||||
return src.trim(); |
||||
} |
||||
|
||||
private static String trim(String src, String trim) { |
||||
return trimLeft(trimRight(src, trim), trim); |
||||
} |
||||
|
||||
private static String trimRight(String src, String trim) { |
||||
String scanFor = trim + " "; |
||||
if (src.contains(scanFor)) { |
||||
while (src.contains(scanFor)) { |
||||
src = src.replace(scanFor, trim); |
||||
} |
||||
} |
||||
return src; |
||||
} |
||||
|
||||
private static String trimLeft(String src, String trim) { |
||||
String scanFor = " " + trim; |
||||
if (src.contains(scanFor)) { |
||||
while (src.contains(scanFor)) { |
||||
src = src.replace(scanFor, trim); |
||||
} |
||||
} |
||||
return src; |
||||
} |
||||
|
||||
private static boolean isStopChar(char c, char... scanFor) { |
||||
boolean found = false; |
||||
for (char check : scanFor) { |
||||
if (check == c) { |
||||
found = true; |
||||
break; |
||||
} |
||||
} |
||||
return found; |
||||
} |
||||
|
||||
private static void skip(Queue<Character> pathQueue, char target) { |
||||
if (pathQueue.isEmpty()) { |
||||
return; |
||||
} |
||||
while (pathQueue.peek().charValue() == target) { |
||||
pathQueue.poll(); |
||||
} |
||||
} |
||||
|
||||
private static void assertNotInvalidPeek(Queue<Character> pathQueue, char... invalidChars) { |
||||
if (pathQueue.isEmpty()) { |
||||
return; |
||||
} |
||||
char peek = pathQueue.peek(); |
||||
for (char check : invalidChars) { |
||||
if (check == peek) { |
||||
throw new InvalidPathException("Char: " + peek + " at current position is not valid!"); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private static void assertValidPeek(Queue<Character> pathQueue, boolean acceptEmpty, char... validChars) { |
||||
if (pathQueue.isEmpty() && acceptEmpty) { |
||||
return; |
||||
} |
||||
if (pathQueue.isEmpty()) { |
||||
throw new InvalidPathException("Path is incomplete"); |
||||
} |
||||
boolean found = false; |
||||
char peek = pathQueue.peek(); |
||||
for (char check : validChars) { |
||||
if (check == peek) { |
||||
found = true; |
||||
break; |
||||
} |
||||
} |
||||
if (!found) { |
||||
throw new InvalidPathException("Path is invalid"); |
||||
} |
||||
} |
||||
|
||||
} |
@ -1,39 +0,0 @@
|
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
import java.util.List; |
||||
|
||||
import static java.lang.String.format; |
||||
|
||||
/** |
||||
* User: kalle stenflo |
||||
* Date: 2/9/11 |
||||
* Time: 12:28 PM |
||||
*/ |
||||
public class FilterOutput { |
||||
|
||||
private final Object result; |
||||
|
||||
public FilterOutput(Object result) { |
||||
this.result = result; |
||||
} |
||||
|
||||
|
||||
public boolean isList(){ |
||||
|
||||
return (result instanceof List); |
||||
|
||||
} |
||||
|
||||
public Object getResult() { |
||||
return result; |
||||
} |
||||
public List<Object> getResultAsList() { |
||||
if(!isList()){ |
||||
throw new RuntimeException(format("Can not convert a %s to a %s", result.getClass().getName(), List.class.getName())); |
||||
} |
||||
return (List<Object>)result; |
||||
} |
||||
|
||||
|
||||
|
||||
} |
@ -1,13 +0,0 @@
|
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 2/2/11 |
||||
* Time: 2:01 PM |
||||
*/ |
||||
public abstract class JsonPathFilterBase { |
||||
public abstract FilterOutput apply(FilterOutput filterItems); |
||||
} |
@ -1,47 +0,0 @@
|
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
import com.jayway.jsonpath.InvalidPathException; |
||||
|
||||
import java.util.LinkedList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* User: kallestenflo |
||||
* Date: 2/2/11 |
||||
* Time: 2:00 PM |
||||
*/ |
||||
public class JsonPathFilterChain { |
||||
|
||||
private List<JsonPathFilterBase> filters; |
||||
|
||||
public JsonPathFilterChain(List<String> pathFragments) { |
||||
filters = configureFilters(pathFragments); |
||||
} |
||||
|
||||
private List<JsonPathFilterBase> configureFilters(List<String> pathFragments) { |
||||
|
||||
List<JsonPathFilterBase> configured = new LinkedList<JsonPathFilterBase>(); |
||||
|
||||
for (String pathFragment : pathFragments) { |
||||
configured.add(JsonPathFilterFactory.createFilter(pathFragment)); |
||||
} |
||||
return configured; |
||||
} |
||||
|
||||
public FilterOutput filter(Object root) { |
||||
|
||||
FilterOutput out = new FilterOutput(root); |
||||
|
||||
for (JsonPathFilterBase filter : filters) { |
||||
if (filter == null) { |
||||
throw new InvalidPathException(); |
||||
} |
||||
if(out.getResult() == null){ |
||||
return null; |
||||
} |
||||
out = filter.apply(out); |
||||
} |
||||
|
||||
return out; |
||||
} |
||||
} |
@ -1,37 +0,0 @@
|
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
/** |
||||
* User: kallestenflo |
||||
* Date: 2/2/11 |
||||
* Time: 2:03 PM |
||||
*/ |
||||
public class JsonPathFilterFactory { |
||||
|
||||
public static JsonPathFilterBase createFilter(String pathFragment) { |
||||
|
||||
if (RootFilter.PATTERN.matcher(pathFragment).matches()) { |
||||
return new RootFilter(); |
||||
} else if (ListIndexFilter.PATTERN.matcher(pathFragment).matches()) { |
||||
return new ListIndexFilter(pathFragment); |
||||
} else if (ListFrontFilter.PATTERN.matcher(pathFragment).matches()) { |
||||
return new ListFrontFilter(pathFragment); |
||||
} else if (ListWildcardFilter.PATTERN.matcher(pathFragment).matches()) { |
||||
return new ListWildcardFilter(); |
||||
} else if (ListTailFilter.PATTERN.matcher(pathFragment).matches()) { |
||||
return new ListTailFilter(pathFragment); |
||||
} else if (ListPropertyFilter.PATTERN.matcher(pathFragment).matches()) { |
||||
return new ListPropertyFilter(pathFragment); |
||||
} else if (ListEvalFilter.PATTERN.matcher(pathFragment).matches()) { |
||||
return new ListEvalFilter(pathFragment); |
||||
} else if (TraverseFilter.PATTERN.matcher(pathFragment).matches()) { |
||||
return new TraverseFilter(); |
||||
} else if (WildcardPropertyFilter.PATTERN.matcher(pathFragment).matches()) { |
||||
return new WildcardPropertyFilter(); |
||||
} else if (PropertyFilter.PATTERN.matcher(pathFragment).matches()) { |
||||
return new PropertyFilter(pathFragment); |
||||
} |
||||
return null; |
||||
|
||||
} |
||||
|
||||
} |
@ -1,72 +0,0 @@
|
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
import com.jayway.jsonpath.JsonUtil; |
||||
import com.jayway.jsonpath.eval.ExpressionEvaluator; |
||||
import net.minidev.json.JSONArray; |
||||
|
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.regex.Matcher; |
||||
import java.util.regex.Pattern; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 2/15/11 |
||||
* Time: 8:27 PM |
||||
*/ |
||||
public class ListEvalFilter extends JsonPathFilterBase { |
||||
|
||||
public static final Pattern PATTERN = Pattern.compile("\\[\\s?\\?\\s?\\(\\s?@.(.*?)\\s?([=<>]+)\\s?(.*)\\s?\\)\\s?\\]"); //[?( @.title< 'ko')]
|
||||
|
||||
private final String pathFragment; |
||||
|
||||
public ListEvalFilter(String pathFragment) { |
||||
this.pathFragment = pathFragment; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public FilterOutput apply(FilterOutput filterItems) { |
||||
|
||||
List<Object> result = new JSONArray(); |
||||
|
||||
for (Object item : filterItems.getResultAsList()) { |
||||
if (isMatch(item)) { |
||||
result.add(item); |
||||
} |
||||
} |
||||
return new FilterOutput(result); |
||||
} |
||||
|
||||
private boolean isMatch(Object check) { |
||||
Matcher matcher = PATTERN.matcher(pathFragment); |
||||
|
||||
if (matcher.matches()) { |
||||
String property = matcher.group(1); |
||||
String operator = matcher.group(2); |
||||
String expected = matcher.group(3); |
||||
|
||||
if (!JsonUtil.isMap(check)) { |
||||
return false; |
||||
} |
||||
Map obj = JsonUtil.toMap(check); |
||||
|
||||
if (!obj.containsKey(property)) { |
||||
return false; |
||||
} |
||||
|
||||
Object propertyValue = obj.get(property); |
||||
|
||||
if (JsonUtil.isContainer(propertyValue)) { |
||||
return false; |
||||
} |
||||
|
||||
String expression = propertyValue + " " + operator + " " + expected; |
||||
|
||||
return ExpressionEvaluator.eval(propertyValue, operator, expected); |
||||
|
||||
} |
||||
return false; |
||||
} |
||||
} |
@ -1,67 +0,0 @@
|
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
import net.minidev.json.JSONArray; |
||||
|
||||
import java.util.LinkedList; |
||||
import java.util.List; |
||||
import java.util.regex.Matcher; |
||||
import java.util.regex.Pattern; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 2/15/11 |
||||
* Time: 8:20 PM |
||||
*/ |
||||
public class ListFrontFilter extends JsonPathFilterBase { |
||||
|
||||
|
||||
public static final Pattern PATTERN = Pattern.compile("\\[\\s?:(\\d+)\\s?\\]"); //[ :2 ]
|
||||
|
||||
private final String pathFragment; |
||||
|
||||
public ListFrontFilter(String pathFragment) { |
||||
this.pathFragment = pathFragment; |
||||
} |
||||
|
||||
@Override |
||||
public FilterOutput apply(FilterOutput filterItems) { |
||||
|
||||
List<Object> result = new JSONArray(); |
||||
|
||||
Integer[] index = getListPullIndex(); |
||||
for (int i : index) { |
||||
if (indexIsInRange(filterItems.getResultAsList(), i)) { |
||||
result.add(filterItems.getResultAsList().get(i)); |
||||
} |
||||
} |
||||
return new FilterOutput(result); |
||||
} |
||||
|
||||
|
||||
private Integer[] getListPullIndex() { |
||||
Matcher matcher = PATTERN.matcher(pathFragment); |
||||
if (matcher.matches()) { |
||||
|
||||
int pullCount = Integer.parseInt(matcher.group(1)); |
||||
|
||||
List<Integer> result = new LinkedList<Integer>(); |
||||
|
||||
for (int y = 0; y < pullCount; y++) { |
||||
result.add(y); |
||||
} |
||||
return result.toArray(new Integer[0]); |
||||
} |
||||
throw new IllegalArgumentException("invalid list index"); |
||||
} |
||||
|
||||
private boolean indexIsInRange(List list, int index) { |
||||
if (index < 0) { |
||||
return false; |
||||
} else if (index > list.size() - 1) { |
||||
return false; |
||||
} else { |
||||
return true; |
||||
} |
||||
} |
||||
} |
@ -1,72 +0,0 @@
|
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
import net.minidev.json.JSONArray; |
||||
|
||||
import java.util.LinkedList; |
||||
import java.util.List; |
||||
import java.util.regex.Pattern; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 2/15/11 |
||||
* Time: 8:02 PM |
||||
*/ |
||||
public class ListIndexFilter extends JsonPathFilterBase { |
||||
|
||||
public static final Pattern PATTERN = Pattern.compile("\\[(\\s?\\d+\\s?,?)+\\]"); //[1] OR [1,2,3]
|
||||
|
||||
private final String pathFragment; |
||||
|
||||
public ListIndexFilter(String pathFragment) { |
||||
this.pathFragment = pathFragment; |
||||
} |
||||
|
||||
@Override |
||||
public FilterOutput apply(FilterOutput filterItems) { |
||||
|
||||
Object result = null; |
||||
|
||||
Integer[] index = getArrayIndex(); |
||||
if (index.length > 1) { |
||||
List<Object> tmp = new JSONArray(); |
||||
for (int i : index) { |
||||
if (indexIsInRange(filterItems.getResultAsList(), i)) { |
||||
tmp.add(filterItems.getResultAsList().get(i)); |
||||
} |
||||
} |
||||
result = tmp; |
||||
} else { |
||||
if (indexIsInRange(filterItems.getResultAsList(), index[0])) { |
||||
result = filterItems.getResultAsList().get(index[0]); |
||||
} |
||||
} |
||||
return new FilterOutput(result); |
||||
} |
||||
|
||||
private boolean indexIsInRange(List list, int index) { |
||||
if (index < 0) { |
||||
return false; |
||||
} else if (index > list.size() - 1) { |
||||
return false; |
||||
} else { |
||||
return true; |
||||
} |
||||
} |
||||
|
||||
|
||||
private Integer[] getArrayIndex() { |
||||
|
||||
String prepared = pathFragment.replaceAll(" ", ""); |
||||
prepared = prepared.substring(1, prepared.length() - 1); |
||||
|
||||
List<Integer> index = new LinkedList<Integer>(); |
||||
|
||||
String[] split = prepared.split(","); |
||||
|
||||
for (String s : split) { |
||||
index.add(Integer.parseInt(s)); |
||||
} |
||||
return index.toArray(new Integer[0]); |
||||
} |
||||
} |
@ -1,52 +0,0 @@
|
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
import com.jayway.jsonpath.JsonUtil; |
||||
import net.minidev.json.JSONArray; |
||||
|
||||
import java.util.List; |
||||
import java.util.regex.Matcher; |
||||
import java.util.regex.Pattern; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 2/15/11 |
||||
* Time: 8:23 PM |
||||
*/ |
||||
public class ListPropertyFilter extends JsonPathFilterBase { |
||||
|
||||
public static final Pattern PATTERN = Pattern.compile("\\[\\s?\\?\\s?\\(\\s?@\\.(\\w+)\\s?\\)\\s?\\]"); //[?(@.title)]
|
||||
|
||||
private final String pathFragment; |
||||
|
||||
public ListPropertyFilter(String pathFragment) { |
||||
this.pathFragment = pathFragment; |
||||
} |
||||
|
||||
@Override |
||||
public FilterOutput apply(FilterOutput filterItems) { |
||||
|
||||
List<Object> result = new JSONArray(); |
||||
|
||||
String prop = getFilterProperty(); |
||||
|
||||
for (Object item : filterItems.getResultAsList()) { |
||||
|
||||
if (JsonUtil.isMap(item)) { |
||||
if (JsonUtil.toMap(item).containsKey(prop)) { |
||||
result.add(item); |
||||
} |
||||
} |
||||
} |
||||
return new FilterOutput(result); |
||||
} |
||||
|
||||
|
||||
private String getFilterProperty() { |
||||
Matcher matcher = PATTERN.matcher(pathFragment); |
||||
if (matcher.matches()) { |
||||
return matcher.group(1); |
||||
} |
||||
throw new IllegalArgumentException("invalid list filter property"); |
||||
} |
||||
} |
@ -1,56 +0,0 @@
|
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
import java.util.List; |
||||
import java.util.regex.Matcher; |
||||
import java.util.regex.Pattern; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 2/15/11 |
||||
* Time: 8:16 PM |
||||
*/ |
||||
public class ListTailFilter extends JsonPathFilterBase { |
||||
|
||||
|
||||
private static final Pattern LIST_TAIL_PATTERN_SHORT = Pattern.compile("\\[\\s*-\\s*(\\d+):\\s*\\]"); // [(@.length - 12)] OR [-13:]
|
||||
private static final Pattern LIST_TAIL_PATTERN_LONG = Pattern.compile("\\[\\s*\\(\\s*@\\.length\\s*-\\s*(\\d+)\\s*\\)\\s*\\]"); //[(@.length-1)]
|
||||
|
||||
public static final Pattern PATTERN = Pattern.compile("(" + LIST_TAIL_PATTERN_SHORT.pattern() + "|" + LIST_TAIL_PATTERN_LONG.pattern() + ")"); |
||||
|
||||
private final String pathFragment; |
||||
|
||||
public ListTailFilter(String pathFragment) { |
||||
this.pathFragment = pathFragment; |
||||
} |
||||
|
||||
@Override |
||||
public FilterOutput apply(FilterOutput filterItems) { |
||||
|
||||
int index = getTailIndex(filterItems.getResultAsList().size()); |
||||
|
||||
return new FilterOutput(filterItems.getResultAsList().get(index)); |
||||
} |
||||
|
||||
|
||||
private int getTailIndex(int arraySize) { |
||||
|
||||
Matcher matcher = LIST_TAIL_PATTERN_SHORT.matcher(pathFragment); |
||||
if (matcher.matches()) { |
||||
|
||||
int index = Integer.parseInt(matcher.group(1)); |
||||
|
||||
return arraySize - index; |
||||
} |
||||
matcher = LIST_TAIL_PATTERN_LONG.matcher(pathFragment); |
||||
if (matcher.matches()) { |
||||
|
||||
int index = Integer.parseInt(matcher.group(1)); |
||||
|
||||
return arraySize - index; |
||||
} |
||||
|
||||
throw new IllegalArgumentException("invalid list index"); |
||||
|
||||
} |
||||
} |
@ -1,19 +0,0 @@
|
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
import java.util.regex.Pattern; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 2/15/11 |
||||
* Time: 8:09 PM |
||||
*/ |
||||
public class ListWildcardFilter extends JsonPathFilterBase{ |
||||
|
||||
public static final Pattern PATTERN = Pattern.compile("\\[\\*\\]"); |
||||
|
||||
@Override |
||||
public FilterOutput apply(FilterOutput filterItems) { |
||||
return new FilterOutput(filterItems.getResultAsList()); |
||||
} |
||||
} |
@ -1,54 +0,0 @@
|
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
import com.jayway.jsonpath.JsonUtil; |
||||
import net.minidev.json.JSONArray; |
||||
|
||||
import java.util.List; |
||||
import java.util.regex.Pattern; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 2/2/11 |
||||
* Time: 2:32 PM |
||||
*/ |
||||
public class PropertyFilter extends JsonPathFilterBase { |
||||
|
||||
public final static Pattern PATTERN = Pattern.compile("(.*)|\\['(.*?)'\\]"); |
||||
|
||||
|
||||
private final String pathFragment; |
||||
|
||||
public PropertyFilter(String pathFragment) { |
||||
this.pathFragment = pathFragment; |
||||
} |
||||
|
||||
@Override |
||||
public FilterOutput apply(FilterOutput filter) { |
||||
|
||||
List<Object> result = new JSONArray(); |
||||
|
||||
|
||||
if (filter.isList()) { |
||||
for (Object current : filter.getResultAsList()) { |
||||
if (JsonUtil.toMap(current).containsKey(pathFragment)) { |
||||
|
||||
Object o = JsonUtil.toMap(current).get(pathFragment); |
||||
if (JsonUtil.isList(o)) { |
||||
result.addAll(JsonUtil.toList(o)); |
||||
|
||||
} else { |
||||
result.add(JsonUtil.toMap(current).get(pathFragment)); |
||||
} |
||||
} |
||||
} |
||||
return new FilterOutput(result); |
||||
} else { |
||||
Object mapValue = null; |
||||
if (JsonUtil.toMap(filter.getResult()).containsKey(pathFragment)) { |
||||
mapValue = JsonUtil.toMap(filter.getResult()).get(pathFragment); |
||||
} |
||||
return new FilterOutput(mapValue); |
||||
} |
||||
} |
||||
} |
@ -1,20 +0,0 @@
|
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
import java.util.List; |
||||
import java.util.regex.Pattern; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 2/2/11 |
||||
* Time: 2:31 PM |
||||
*/ |
||||
public class RootFilter extends JsonPathFilterBase{ |
||||
|
||||
public final static Pattern PATTERN = Pattern.compile("\\$"); |
||||
|
||||
@Override |
||||
public FilterOutput apply(FilterOutput root) { |
||||
return root; |
||||
} |
||||
} |
@ -1,48 +0,0 @@
|
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
import com.jayway.jsonpath.JsonUtil; |
||||
import net.minidev.json.JSONArray; |
||||
|
||||
import java.util.List; |
||||
import java.util.regex.Pattern; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 2/2/11 |
||||
* Time: 2:33 PM |
||||
*/ |
||||
public class TraverseFilter extends JsonPathFilterBase { |
||||
|
||||
public final static Pattern PATTERN = Pattern.compile("\\.\\."); |
||||
|
||||
@Override |
||||
public FilterOutput apply(FilterOutput filter) { |
||||
List<Object> result = new JSONArray(); |
||||
|
||||
traverse(filter.getResult(), result); |
||||
|
||||
return new FilterOutput(result); |
||||
} |
||||
|
||||
private void traverse(Object container, List<Object> result) { |
||||
|
||||
if (JsonUtil.isMap(container)) { |
||||
result.add(container); |
||||
|
||||
for (Object value : JsonUtil.toMap(container).values()) { |
||||
if (JsonUtil.isContainer(value)) { |
||||
traverse(value, result); |
||||
} |
||||
} |
||||
} else if (JsonUtil.isList(container)) { |
||||
|
||||
for (Object value : JsonUtil.toList(container)) { |
||||
if (JsonUtil.isContainer(value)) { |
||||
traverse(value, result); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
@ -1,39 +0,0 @@
|
||||
package com.jayway.jsonpath.filter; |
||||
|
||||
import com.jayway.jsonpath.JsonUtil; |
||||
import net.minidev.json.JSONArray; |
||||
|
||||
import java.util.List; |
||||
import java.util.regex.Pattern; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 2/15/11 |
||||
* Time: 8:42 PM |
||||
*/ |
||||
public class WildcardPropertyFilter extends JsonPathFilterBase { |
||||
|
||||
public final static Pattern PATTERN = Pattern.compile("\\*"); |
||||
|
||||
|
||||
@Override |
||||
public FilterOutput apply(FilterOutput filter) { |
||||
|
||||
List<Object> result = new JSONArray(); |
||||
|
||||
if (filter.isList()) { |
||||
for (Object current : filter.getResultAsList()) { |
||||
for (Object value : JsonUtil.toMap(current).values()) { |
||||
result.add(value); |
||||
} |
||||
} |
||||
} else { |
||||
for (Object value : JsonUtil.toMap(filter.getResult()).values()) { |
||||
result.add(value); |
||||
} |
||||
} |
||||
return new FilterOutput(result); |
||||
|
||||
} |
||||
} |
@ -1,4 +1,4 @@
|
||||
package com.jayway.jsonpath.eval; |
||||
package com.jayway.jsonpath.reader.filter.eval; |
||||
|
||||
/** |
||||
* User: kalle stenflo |
@ -0,0 +1,87 @@
|
||||
package com.jayway.jsonpath.spi; |
||||
|
||||
import com.jayway.jsonpath.InvalidJsonException; |
||||
|
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 11/8/11 |
||||
* Time: 3:51 PM |
||||
*/ |
||||
public abstract class JsonProvider { |
||||
|
||||
public abstract Mode getMode(); |
||||
|
||||
public abstract Object parse(String json) throws InvalidJsonException; |
||||
|
||||
public abstract Map<String, Object> createMap(); |
||||
|
||||
public abstract List<Object> createList(); |
||||
|
||||
/** |
||||
* checks if object is <code>instanceof</code> <code>java.util.List</code> or <code>java.util.Map</code> |
||||
* |
||||
* @param obj object to check |
||||
* @return true if List or Map |
||||
*/ |
||||
public boolean isContainer(Object obj) { |
||||
return (isList(obj) || isMap(obj)); |
||||
} |
||||
|
||||
/** |
||||
* checks if object is <code>instanceof</code> <code>java.util.List</code> |
||||
* |
||||
* @param obj object to check |
||||
* @return true if List |
||||
*/ |
||||
public boolean isList(Object obj) { |
||||
return (obj instanceof List); |
||||
} |
||||
|
||||
/** |
||||
* Converts give object to a List |
||||
* |
||||
* @param list |
||||
* @return |
||||
*/ |
||||
public List<Object> toList(Object list) { |
||||
return (List<Object>) list; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Converts given object to a Map |
||||
* |
||||
* @param map |
||||
* @return |
||||
*/ |
||||
public Map<String, Object> toMap(Object map) { |
||||
return (Map<String, Object>) map; |
||||
} |
||||
|
||||
/** |
||||
* Extracts a value from a Map |
||||
* |
||||
* @param map |
||||
* @param key |
||||
* @return |
||||
*/ |
||||
public Object getMapValue(Object map, String key) { |
||||
return toMap(map).get(key); |
||||
} |
||||
|
||||
/** |
||||
* checks if object is <code>instanceof</code> <code>java.util.Map</code> |
||||
* |
||||
* @param obj object to check |
||||
* @return true if Map |
||||
*/ |
||||
public boolean isMap(Object obj) { |
||||
return (obj instanceof Map); |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,37 @@
|
||||
package com.jayway.jsonpath.spi; |
||||
|
||||
|
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 11/8/11 |
||||
* Time: 7:22 PM |
||||
*/ |
||||
public enum Mode { |
||||
|
||||
SLACK(-1), |
||||
STRICT(1); |
||||
|
||||
private final int mode; |
||||
|
||||
Mode(int mode) { |
||||
this.mode = mode; |
||||
} |
||||
|
||||
public int intValue(){ |
||||
return mode; |
||||
} |
||||
|
||||
public Mode parse(int mode){ |
||||
if(mode == -1){ |
||||
return SLACK; |
||||
} else if(mode == 1){ |
||||
return STRICT; |
||||
} else { |
||||
throw new IllegalArgumentException("Mode " + mode + " not supported"); |
||||
} |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,57 @@
|
||||
package com.jayway.jsonpath.spi.impl; |
||||
|
||||
import com.jayway.jsonpath.InvalidJsonException; |
||||
import com.jayway.jsonpath.spi.JsonProvider; |
||||
import com.jayway.jsonpath.spi.Mode; |
||||
import net.minidev.json.JSONArray; |
||||
import net.minidev.json.JSONObject; |
||||
import net.minidev.json.parser.JSONParser; |
||||
import net.minidev.json.parser.ParseException; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 11/8/11 |
||||
* Time: 3:56 PM |
||||
*/ |
||||
public class DefaultJsonProvider extends JsonProvider { |
||||
|
||||
private Mode mode; |
||||
|
||||
private JSONParser parser; |
||||
|
||||
public DefaultJsonProvider() { |
||||
this(Mode.SLACK); |
||||
} |
||||
|
||||
public DefaultJsonProvider(Mode mode) { |
||||
this.mode = mode; |
||||
this.parser = new JSONParser(mode.intValue()); |
||||
} |
||||
|
||||
public Map<String, Object> createMap() { |
||||
return new JSONObject(); |
||||
} |
||||
|
||||
public List<Object> createList() { |
||||
return new JSONArray(); |
||||
} |
||||
|
||||
public Object parse(String json) { |
||||
try { |
||||
return parser.parse(json); |
||||
} catch (ParseException e) { |
||||
throw new InvalidJsonException(e); |
||||
} catch (IOException e) { |
||||
throw new InvalidJsonException(e); |
||||
} |
||||
} |
||||
|
||||
public Mode getMode() { |
||||
return mode; |
||||
} |
||||
} |
@ -0,0 +1,65 @@
|
||||
package com.jayway.jsonpath; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 11/8/11 |
||||
* Time: 7:52 PM |
||||
*/ |
||||
public class PathReaderTest { |
||||
|
||||
public final static String DOCUMENT = |
||||
"{ \"store\": {\n" + |
||||
" \"book\": [ \n" + |
||||
" { \"category\": \"reference\",\n" + |
||||
" \"author\": \"Nigel Rees\",\n" + |
||||
" \"title\": \"Sayings of the Century\",\n" + |
||||
" \"price\": 8.95\n" + |
||||
" },\n" + |
||||
" { \"category\": \"fiction\",\n" + |
||||
" \"author\": \"Evelyn Waugh\",\n" + |
||||
" \"title\": \"Sword of Honour\",\n" + |
||||
" \"price\": 12.99\n" + |
||||
" },\n" + |
||||
" { \"category\": \"fiction\",\n" + |
||||
" \"author\": \"Herman Melville\",\n" + |
||||
" \"title\": \"Moby Dick\",\n" + |
||||
" \"isbn\": \"0-553-21311-3\",\n" + |
||||
" \"price\": 8.99\n" + |
||||
" },\n" + |
||||
" { \"category\": \"fiction\",\n" + |
||||
" \"author\": \"J. R. R. Tolkien\",\n" + |
||||
" \"title\": \"The Lord of the Rings\",\n" + |
||||
" \"isbn\": \"0-395-19395-8\",\n" + |
||||
" \"price\": 22.99\n" + |
||||
" }\n" + |
||||
" ],\n" + |
||||
" \"bicycle\": {\n" + |
||||
" \"color\": \"red\",\n" + |
||||
" \"price\": 19.95,\n" + |
||||
" \"foo:bar\": \"fooBar\",\n" + |
||||
" \"dot.notation\": \"new\"\n" + |
||||
" }\n" + |
||||
" }\n" + |
||||
"}"; |
||||
|
||||
@Test |
||||
public void a_path_can_be_read() throws Exception { |
||||
|
||||
PathReader pathReader = PathReader.create(DOCUMENT); |
||||
|
||||
Map<String, Object> book = pathReader.read("store.book[1]"); |
||||
|
||||
PathReader pathReader1 = pathReader.get("store.book[1]"); |
||||
|
||||
String author = pathReader1.read("author"); |
||||
|
||||
System.out.println("d"); |
||||
|
||||
} |
||||
|
||||
} |
@ -0,0 +1,60 @@
|
||||
package com.jayway.jsonpath.util; |
||||
|
||||
import javax.script.Invocable; |
||||
import javax.script.ScriptEngine; |
||||
import javax.script.ScriptEngineManager; |
||||
import javax.script.ScriptException; |
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.util.Scanner; |
||||
|
||||
/** |
||||
* Created by IntelliJ IDEA. |
||||
* User: kallestenflo |
||||
* Date: 11/8/11 |
||||
* Time: 1:01 PM |
||||
*/ |
||||
public class ScriptEngineJsonPath { |
||||
|
||||
private static ScriptEngineManager manager = new ScriptEngineManager(); |
||||
private static ScriptEngine engine = manager.getEngineByName("JavaScript"); |
||||
|
||||
private static final String JSON_PATH_SCRIPT = readScript("jsonpath-0.8.0.js"); |
||||
private static final String JSON_SCRIPT = readScript("json.js"); |
||||
private static final String WRAPPER_SCRIPT = readScript("wrapper.js"); |
||||
|
||||
static { |
||||
try { |
||||
engine.eval(JSON_PATH_SCRIPT); |
||||
engine.eval(JSON_SCRIPT); |
||||
engine.eval(WRAPPER_SCRIPT); |
||||
} catch (ScriptException e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
} |
||||
|
||||
public static String eval(String json, String path) throws Exception { |
||||
Invocable inv = (Invocable) engine; |
||||
Object obj = engine.get("WRAPPER"); |
||||
return (String)inv.invokeMethod(obj, "jsonPath", json, path); |
||||
} |
||||
|
||||
|
||||
private static String readScript(String script) { |
||||
InputStream is = null; |
||||
try { |
||||
is = ScriptEngineJsonPath.class.getClassLoader().getSystemResourceAsStream("js/" + script); |
||||
|
||||
return new Scanner(is).useDelimiter("\\A").next(); |
||||
} finally { |
||||
if (is != null) { |
||||
try { |
||||
is.close(); |
||||
} catch (IOException e) { |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,530 @@
|
||||
/* |
||||
json.js |
||||
2011-08-30 |
||||
|
||||
Public Domain |
||||
|
||||
No warranty expressed or implied. Use at your own risk. |
||||
|
||||
This file has been superceded by http://www.JSON.org/json2.js
|
||||
|
||||
See http://www.JSON.org/js.html
|
||||
|
||||
This code should be minified before deployment. |
||||
See http://javascript.crockford.com/jsmin.html
|
||||
|
||||
USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO |
||||
NOT CONTROL. |
||||
|
||||
This file adds these methods to JavaScript: |
||||
|
||||
object.toJSONString(whitelist) |
||||
This method produce a JSON text from a JavaScript value. |
||||
It must not contain any cyclical references. Illegal values |
||||
will be excluded. |
||||
|
||||
The default conversion for dates is to an ISO string. You can |
||||
add a toJSONString method to any date object to get a different |
||||
representation. |
||||
|
||||
The object and array methods can take an optional whitelist |
||||
argument. A whitelist is an array of strings. If it is provided, |
||||
keys in objects not found in the whitelist are excluded. |
||||
|
||||
string.parseJSON(filter) |
||||
This method parses a JSON text to produce an object or |
||||
array. It can throw a SyntaxError exception. |
||||
|
||||
The optional filter parameter is a function which can filter and |
||||
transform the results. It receives each of the keys and values, and |
||||
its return value is used instead of the original value. If it |
||||
returns what it received, then structure is not modified. If it |
||||
returns undefined then the member is deleted. |
||||
|
||||
Example: |
||||
|
||||
// Parse the text. If a key contains the string 'date' then
|
||||
// convert the value to a date.
|
||||
|
||||
myData = text.parseJSON(function (key, value) { |
||||
return key.indexOf('date') >= 0 ? new Date(value) : value; |
||||
}); |
||||
|
||||
This file will break programs with improper for..in loops. See |
||||
http://yuiblog.com/blog/2006/09/26/for-in-intrigue/
|
||||
|
||||
This file creates a global JSON object containing two methods: stringify |
||||
and parse. |
||||
|
||||
JSON.stringify(value, replacer, space) |
||||
value any JavaScript value, usually an object or array. |
||||
|
||||
replacer an optional parameter that determines how object |
||||
values are stringified for objects. It can be a |
||||
function or an array of strings. |
||||
|
||||
space an optional parameter that specifies the indentation |
||||
of nested structures. If it is omitted, the text will |
||||
be packed without extra whitespace. If it is a number, |
||||
it will specify the number of spaces to indent at each |
||||
level. If it is a string (such as '\t' or ' '), |
||||
it contains the characters used to indent at each level. |
||||
|
||||
This method produces a JSON text from a JavaScript value. |
||||
|
||||
When an object value is found, if the object contains a toJSON |
||||
method, its toJSON method will be called and the result will be |
||||
stringified. A toJSON method does not serialize: it returns the |
||||
value represented by the name/value pair that should be serialized, |
||||
or undefined if nothing should be serialized. The toJSON method |
||||
will be passed the key associated with the value, and this will be |
||||
bound to the object holding the key. |
||||
|
||||
For example, this would serialize Dates as ISO strings. |
||||
|
||||
Date.prototype.toJSON = function (key) { |
||||
function f(n) { |
||||
// Format integers to have at least two digits.
|
||||
return n < 10 ? '0' + n : n; |
||||
} |
||||
|
||||
return this.getUTCFullYear() + '-' + |
||||
f(this.getUTCMonth() + 1) + '-' + |
||||
f(this.getUTCDate()) + 'T' + |
||||
f(this.getUTCHours()) + ':' + |
||||
f(this.getUTCMinutes()) + ':' + |
||||
f(this.getUTCSeconds()) + 'Z'; |
||||
}; |
||||
|
||||
You can provide an optional replacer method. It will be passed the |
||||
key and value of each member, with this bound to the containing |
||||
object. The value that is returned from your method will be |
||||
serialized. If your method returns undefined, then the member will |
||||
be excluded from the serialization. |
||||
|
||||
If the replacer parameter is an array of strings, then it will be |
||||
used to select the members to be serialized. It filters the results |
||||
such that only members with keys listed in the replacer array are |
||||
stringified. |
||||
|
||||
Values that do not have JSON representations, such as undefined or |
||||
functions, will not be serialized. Such values in objects will be |
||||
dropped; in arrays they will be replaced with null. You can use |
||||
a replacer function to replace those with JSON values. |
||||
JSON.stringify(undefined) returns undefined. |
||||
|
||||
The optional space parameter produces a stringification of the |
||||
value that is filled with line breaks and indentation to make it |
||||
easier to read. |
||||
|
||||
If the space parameter is a non-empty string, then that string will |
||||
be used for indentation. If the space parameter is a number, then |
||||
the indentation will be that many spaces. |
||||
|
||||
Example: |
||||
|
||||
text = JSON.stringify(['e', {pluribus: 'unum'}]); |
||||
// text is '["e",{"pluribus":"unum"}]'
|
||||
|
||||
|
||||
text = JSON.stringify(['e', {pluribus: 'unum'}], null, '\t'); |
||||
// text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'
|
||||
|
||||
text = JSON.stringify([new Date()], function (key, value) { |
||||
return this[key] instanceof Date ? |
||||
'Date(' + this[key] + ')' : value; |
||||
}); |
||||
// text is '["Date(---current time---)"]'
|
||||
|
||||
|
||||
JSON.parse(text, reviver) |
||||
This method parses a JSON text to produce an object or array. |
||||
It can throw a SyntaxError exception. |
||||
|
||||
The optional reviver parameter is a function that can filter and |
||||
transform the results. It receives each of the keys and values, |
||||
and its return value is used instead of the original value. |
||||
If it returns what it received, then the structure is not modified. |
||||
If it returns undefined then the member is deleted. |
||||
|
||||
Example: |
||||
|
||||
// Parse the text. Values that look like ISO date strings will
|
||||
// be converted to Date objects.
|
||||
|
||||
myData = JSON.parse(text, function (key, value) { |
||||
var a; |
||||
if (typeof value === 'string') { |
||||
a = |
||||
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value); |
||||
if (a) { |
||||
return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], |
||||
+a[5], +a[6])); |
||||
} |
||||
} |
||||
return value; |
||||
}); |
||||
|
||||
myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) { |
||||
var d; |
||||
if (typeof value === 'string' && |
||||
value.slice(0, 5) === 'Date(' && |
||||
value.slice(-1) === ')') { |
||||
d = new Date(value.slice(5, -1)); |
||||
if (d) { |
||||
return d; |
||||
} |
||||
} |
||||
return value; |
||||
}); |
||||
|
||||
|
||||
This is a reference implementation. You are free to copy, modify, or |
||||
redistribute. |
||||
*/ |
||||
|
||||
/*jslint evil: true, regexp: true, unparam: true */ |
||||
|
||||
/*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply, |
||||
call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours, |
||||
getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join, |
||||
lastIndex, length, parse, parseJSON, prototype, push, replace, slice, |
||||
stringify, test, toJSON, toJSONString, toString, valueOf |
||||
*/ |
||||
|
||||
|
||||
// Create a JSON object only if one does not already exist. We create the
|
||||
// methods in a closure to avoid creating global variables.
|
||||
|
||||
var JSON; |
||||
if (!JSON) { |
||||
JSON = {}; |
||||
} |
||||
|
||||
(function () { |
||||
'use strict'; |
||||
|
||||
function f(n) { |
||||
// Format integers to have at least two digits.
|
||||
return n < 10 ? '0' + n : n; |
||||
} |
||||
|
||||
if (typeof Date.prototype.toJSON !== 'function') { |
||||
|
||||
Date.prototype.toJSON = function (key) { |
||||
|
||||
return isFinite(this.valueOf()) ? |
||||
this.getUTCFullYear() + '-' + |
||||
f(this.getUTCMonth() + 1) + '-' + |
||||
f(this.getUTCDate()) + 'T' + |
||||
f(this.getUTCHours()) + ':' + |
||||
f(this.getUTCMinutes()) + ':' + |
||||
f(this.getUTCSeconds()) + 'Z' : null; |
||||
}; |
||||
|
||||
String.prototype.toJSON = |
||||
Number.prototype.toJSON = |
||||
Boolean.prototype.toJSON = function (key) { |
||||
return this.valueOf(); |
||||
}; |
||||
} |
||||
|
||||
var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, |
||||
escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, |
||||
gap, |
||||
indent, |
||||
meta = { // table of character substitutions
|
||||
'\b': '\\b', |
||||
'\t': '\\t', |
||||
'\n': '\\n', |
||||
'\f': '\\f', |
||||
'\r': '\\r', |
||||
'"' : '\\"', |
||||
'\\': '\\\\' |
||||
}, |
||||
rep; |
||||
|
||||
|
||||
function quote(string) { |
||||
|
||||
// If the string contains no control characters, no quote characters, and no
|
||||
// backslash characters, then we can safely slap some quotes around it.
|
||||
// Otherwise we must also replace the offending characters with safe escape
|
||||
// sequences.
|
||||
|
||||
escapable.lastIndex = 0; |
||||
return escapable.test(string) ? '"' + string.replace(escapable, function (a) { |
||||
var c = meta[a]; |
||||
return typeof c === 'string' ? c : |
||||
'\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); |
||||
}) + '"' : '"' + string + '"'; |
||||
} |
||||
|
||||
|
||||
function str(key, holder) { |
||||
|
||||
// Produce a string from holder[key].
|
||||
|
||||
var i, // The loop counter.
|
||||
k, // The member key.
|
||||
v, // The member value.
|
||||
length, |
||||
mind = gap, |
||||
partial, |
||||
value = holder[key]; |
||||
|
||||
// If the value has a toJSON method, call it to obtain a replacement value.
|
||||
|
||||
if (value && typeof value === 'object' && |
||||
typeof value.toJSON === 'function') { |
||||
value = value.toJSON(key); |
||||
} |
||||
|
||||
// If we were called with a replacer function, then call the replacer to
|
||||
// obtain a replacement value.
|
||||
|
||||
if (typeof rep === 'function') { |
||||
value = rep.call(holder, key, value); |
||||
} |
||||
|
||||
// What happens next depends on the value's type.
|
||||
|
||||
switch (typeof value) { |
||||
case 'string': |
||||
return quote(value); |
||||
|
||||
case 'number': |
||||
|
||||
// JSON numbers must be finite. Encode non-finite numbers as null.
|
||||
|
||||
return isFinite(value) ? String(value) : 'null'; |
||||
|
||||
case 'boolean': |
||||
case 'null': |
||||
|
||||
// If the value is a boolean or null, convert it to a string. Note:
|
||||
// typeof null does not produce 'null'. The case is included here in
|
||||
// the remote chance that this gets fixed someday.
|
||||
|
||||
return String(value); |
||||
|
||||
// If the type is 'object', we might be dealing with an object or an array or
|
||||
// null.
|
||||
|
||||
case 'object': |
||||
|
||||
// Due to a specification blunder in ECMAScript, typeof null is 'object',
|
||||
// so watch out for that case.
|
||||
|
||||
if (!value) { |
||||
return 'null'; |
||||
} |
||||
|
||||
// Make an array to hold the partial results of stringifying this object value.
|
||||
|
||||
gap += indent; |
||||
partial = []; |
||||
|
||||
// Is the value an array?
|
||||
|
||||
if (Object.prototype.toString.apply(value) === '[object Array]') { |
||||
|
||||
// The value is an array. Stringify every element. Use null as a placeholder
|
||||
// for non-JSON values.
|
||||
|
||||
length = value.length; |
||||
for (i = 0; i < length; i += 1) { |
||||
partial[i] = str(i, value) || 'null'; |
||||
} |
||||
|
||||
// Join all of the elements together, separated with commas, and wrap them in
|
||||
// brackets.
|
||||
|
||||
v = partial.length === 0 ? '[]' : gap ? |
||||
'[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' : |
||||
'[' + partial.join(',') + ']'; |
||||
gap = mind; |
||||
return v; |
||||
} |
||||
|
||||
// If the replacer is an array, use it to select the members to be stringified.
|
||||
|
||||
if (rep && typeof rep === 'object') { |
||||
length = rep.length; |
||||
for (i = 0; i < length; i += 1) { |
||||
k = rep[i]; |
||||
if (typeof k === 'string') { |
||||
v = str(k, value); |
||||
if (v) { |
||||
partial.push(quote(k) + (gap ? ': ' : ':') + v); |
||||
} |
||||
} |
||||
} |
||||
} else { |
||||
|
||||
// Otherwise, iterate through all of the keys in the object.
|
||||
|
||||
for (k in value) { |
||||
if (Object.prototype.hasOwnProperty.call(value, k)) { |
||||
v = str(k, value); |
||||
if (v) { |
||||
partial.push(quote(k) + (gap ? ': ' : ':') + v); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
// Join all of the member texts together, separated with commas,
|
||||
// and wrap them in braces.
|
||||
|
||||
v = partial.length === 0 ? '{}' : gap ? |
||||
'{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' : |
||||
'{' + partial.join(',') + '}'; |
||||
gap = mind; |
||||
return v; |
||||
} |
||||
} |
||||
|
||||
// If the JSON object does not yet have a stringify method, give it one.
|
||||
|
||||
if (typeof JSON.stringify !== 'function') { |
||||
JSON.stringify = function (value, replacer, space) { |
||||
|
||||
// The stringify method takes a value and an optional replacer, and an optional
|
||||
// space parameter, and returns a JSON text. The replacer can be a function
|
||||
// that can replace values, or an array of strings that will select the keys.
|
||||
// A default replacer method can be provided. Use of the space parameter can
|
||||
// produce text that is more easily readable.
|
||||
|
||||
var i; |
||||
gap = ''; |
||||
indent = ''; |
||||
|
||||
// If the space parameter is a number, make an indent string containing that
|
||||
// many spaces.
|
||||
|
||||
if (typeof space === 'number') { |
||||
for (i = 0; i < space; i += 1) { |
||||
indent += ' '; |
||||
} |
||||
|
||||
// If the space parameter is a string, it will be used as the indent string.
|
||||
|
||||
} else if (typeof space === 'string') { |
||||
indent = space; |
||||
} |
||||
|
||||
// If there is a replacer, it must be a function or an array.
|
||||
// Otherwise, throw an error.
|
||||
|
||||
rep = replacer; |
||||
if (replacer && typeof replacer !== 'function' && |
||||
(typeof replacer !== 'object' || |
||||
typeof replacer.length !== 'number')) { |
||||
throw new Error('JSON.stringify'); |
||||
} |
||||
|
||||
// Make a fake root object containing our value under the key of ''.
|
||||
// Return the result of stringifying the value.
|
||||
|
||||
return str('', {'': value}); |
||||
}; |
||||
} |
||||
|
||||
|
||||
// If the JSON object does not yet have a parse method, give it one.
|
||||
|
||||
if (typeof JSON.parse !== 'function') { |
||||
JSON.parse = function (text, reviver) { |
||||
|
||||
// The parse method takes a text and an optional reviver function, and returns
|
||||
// a JavaScript value if the text is a valid JSON text.
|
||||
|
||||
var j; |
||||
|
||||
function walk(holder, key) { |
||||
|
||||
// The walk method is used to recursively walk the resulting structure so
|
||||
// that modifications can be made.
|
||||
|
||||
var k, v, value = holder[key]; |
||||
if (value && typeof value === 'object') { |
||||
for (k in value) { |
||||
if (Object.prototype.hasOwnProperty.call(value, k)) { |
||||
v = walk(value, k); |
||||
if (v !== undefined) { |
||||
value[k] = v; |
||||
} else { |
||||
delete value[k]; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
return reviver.call(holder, key, value); |
||||
} |
||||
|
||||
|
||||
// Parsing happens in four stages. In the first stage, we replace certain
|
||||
// Unicode characters with escape sequences. JavaScript handles many characters
|
||||
// incorrectly, either silently deleting them, or treating them as line endings.
|
||||
|
||||
text = String(text); |
||||
cx.lastIndex = 0; |
||||
if (cx.test(text)) { |
||||
text = text.replace(cx, function (a) { |
||||
return '\\u' + |
||||
('0000' + a.charCodeAt(0).toString(16)).slice(-4); |
||||
}); |
||||
} |
||||
|
||||
// In the second stage, we run the text against regular expressions that look
|
||||
// for non-JSON patterns. We are especially concerned with '()' and 'new'
|
||||
// because they can cause invocation, and '=' because it can cause mutation.
|
||||
// But just to be safe, we want to reject all unexpected forms.
|
||||
|
||||
// We split the second stage into 4 regexp operations in order to work around
|
||||
// crippling inefficiencies in IE's and Safari's regexp engines. First we
|
||||
// replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
|
||||
// replace all simple value tokens with ']' characters. Third, we delete all
|
||||
// open brackets that follow a colon or comma or that begin the text. Finally,
|
||||
// we look to see that the remaining characters are only whitespace or ']' or
|
||||
// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
|
||||
|
||||
if (/^[\],:{}\s]*$/ |
||||
.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@') |
||||
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']') |
||||
.replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { |
||||
|
||||
// In the third stage we use the eval function to compile the text into a
|
||||
// JavaScript structure. The '{' operator is subject to a syntactic ambiguity
|
||||
// in JavaScript: it can begin a block or an object literal. We wrap the text
|
||||
// in parens to eliminate the ambiguity.
|
||||
|
||||
j = eval('(' + text + ')'); |
||||
|
||||
// In the optional fourth stage, we recursively walk the new structure, passing
|
||||
// each name/value pair to a reviver function for possible transformation.
|
||||
|
||||
return typeof reviver === 'function' ? |
||||
walk({'': j}, '') : j; |
||||
} |
||||
|
||||
// If the text is not JSON parseable, then a SyntaxError is thrown.
|
||||
|
||||
throw new SyntaxError('JSON.parse'); |
||||
}; |
||||
} |
||||
|
||||
// Augment the basic prototypes if they have not already been augmented.
|
||||
// These forms are obsolete. It is recommended that JSON.stringify and
|
||||
// JSON.parse be used instead.
|
||||
|
||||
if (!Object.prototype.toJSONString) { |
||||
Object.prototype.toJSONString = function (filter) { |
||||
return JSON.stringify(this, filter); |
||||
}; |
||||
Object.prototype.parseJSON = function (filter) { |
||||
return JSON.parse(this, filter); |
||||
}; |
||||
} |
||||
}()); |
@ -0,0 +1,87 @@
|
||||
/* JSONPath 0.8.0 - XPath for JSON |
||||
* |
||||
* Copyright (c) 2007 Stefan Goessner (goessner.net) |
||||
* Licensed under the MIT (MIT-LICENSE.txt) licence. |
||||
*/ |
||||
function jsonPath(obj, expr, arg) { |
||||
var P = { |
||||
resultType: arg && arg.resultType || "VALUE", |
||||
result: [], |
||||
normalize: function(expr) { |
||||
var subx = []; |
||||
return expr.replace(/[\['](\??\(.*?\))[\]']/g, function($0,$1){return "[#"+(subx.push($1)-1)+"]";}) |
||||
.replace(/'?\.'?|\['?/g, ";") |
||||
.replace(/;;;|;;/g, ";..;") |
||||
.replace(/;$|'?\]|'$/g, "") |
||||
.replace(/#([0-9]+)/g, function($0,$1){return subx[$1];}); |
||||
}, |
||||
asPath: function(path) { |
||||
var x = path.split(";"), p = "$"; |
||||
for (var i=1,n=x.length; i<n; i++) |
||||
p += /^[0-9*]+$/.test(x[i]) ? ("["+x[i]+"]") : ("['"+x[i]+"']"); |
||||
return p; |
||||
}, |
||||
store: function(p, v) { |
||||
if (p) P.result[P.result.length] = P.resultType == "PATH" ? P.asPath(p) : v; |
||||
return !!p; |
||||
}, |
||||
trace: function(expr, val, path) { |
||||
if (expr) { |
||||
var x = expr.split(";"), loc = x.shift(); |
||||
x = x.join(";"); |
||||
if (val && val.hasOwnProperty(loc)) |
||||
P.trace(x, val[loc], path + ";" + loc); |
||||
else if (loc === "*") |
||||
P.walk(loc, x, val, path, function(m,l,x,v,p) { P.trace(m+";"+x,v,p); }); |
||||
else if (loc === "..") { |
||||
P.trace(x, val, path); |
||||
P.walk(loc, x, val, path, function(m,l,x,v,p) { typeof v[m] === "object" && P.trace("..;"+x,v[m],p+";"+m); }); |
||||
} |
||||
else if (/,/.test(loc)) { // [name1,name2,...]
|
||||
for (var s=loc.split(/'?,'?/),i=0,n=s.length; i<n; i++) |
||||
P.trace(s[i]+";"+x, val, path); |
||||
} |
||||
else if (/^\(.*?\)$/.test(loc)) // [(expr)]
|
||||
P.trace(P.eval(loc, val, path.substr(path.lastIndexOf(";")+1))+";"+x, val, path); |
||||
else if (/^\?\(.*?\)$/.test(loc)) // [?(expr)]
|
||||
P.walk(loc, x, val, path, function(m,l,x,v,p) { if (P.eval(l.replace(/^\?\((.*?)\)$/,"$1"),v[m],m)) P.trace(m+";"+x,v,p); }); |
||||
else if (/^(-?[0-9]*):(-?[0-9]*):?([0-9]*)$/.test(loc)) // [start:end:step] phyton slice syntax
|
||||
P.slice(loc, x, val, path); |
||||
} |
||||
else |
||||
P.store(path, val); |
||||
}, |
||||
walk: function(loc, expr, val, path, f) { |
||||
if (val instanceof Array) { |
||||
for (var i=0,n=val.length; i<n; i++) |
||||
if (i in val) |
||||
f(i,loc,expr,val,path); |
||||
} |
||||
else if (typeof val === "object") { |
||||
for (var m in val) |
||||
if (val.hasOwnProperty(m)) |
||||
f(m,loc,expr,val,path); |
||||
} |
||||
}, |
||||
slice: function(loc, expr, val, path) { |
||||
if (val instanceof Array) { |
||||
var len=val.length, start=0, end=len, step=1; |
||||
loc.replace(/^(-?[0-9]*):(-?[0-9]*):?(-?[0-9]*)$/g, function($0,$1,$2,$3){start=parseInt($1||start);end=parseInt($2||end);step=parseInt($3||step);}); |
||||
start = (start < 0) ? Math.max(0,start+len) : Math.min(len,start); |
||||
end = (end < 0) ? Math.max(0,end+len) : Math.min(len,end); |
||||
for (var i=start; i<end; i+=step) |
||||
P.trace(i+";"+expr, val, path); |
||||
} |
||||
}, |
||||
eval: function(x, _v, _vname) { |
||||
try { return $ && _v && eval(x.replace(/@/g, "_v")); } |
||||
catch(e) { throw new SyntaxError("jsonPath: " + e.message + ": " + x.replace(/@/g, "_v").replace(/\^/g, "_a")); } |
||||
} |
||||
}; |
||||
|
||||
var $ = obj; |
||||
if (expr && obj && (P.resultType == "VALUE" || P.resultType == "PATH")) { |
||||
P.trace(P.normalize(expr).replace(/^\$;/,""), obj, "$"); |
||||
return P.result.length ? P.result : false; |
||||
} |
||||
}
|
@ -0,0 +1,57 @@
|
||||
<html> |
||||
<head> |
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> |
||||
<script src="jsonpath-0.8.0.js"></script> |
||||
<script> |
||||
$(document).ready(function() { |
||||
window.doc = { "store": { |
||||
"book": [ |
||||
{ "category": "reference", |
||||
"author": "Nigel Rees", |
||||
"title": "Sayings of the Century", |
||||
"price": 8.95 |
||||
}, |
||||
{ "category": "fiction", |
||||
"author": "Evelyn Waugh", |
||||
"title": "Sword of Honour", |
||||
"price": 12.99 |
||||
}, |
||||
{ "category": "fiction", |
||||
"author": "Herman Melville", |
||||
"title": "Moby Dick", |
||||
"isbn": "0-553-21311-3", |
||||
"price": 8.99 |
||||
}, |
||||
{ "category": "fiction", |
||||
"author": "J. R. R. Tolkien", |
||||
"title": "The Lord of the Rings", |
||||
"isbn": "0-395-19395-8", |
||||
"price": 22.99 |
||||
} |
||||
], |
||||
"bicycle": { |
||||
"color": "red", |
||||
"price": 19.95, |
||||
"dot.notation": "new" |
||||
} |
||||
} |
||||
}; |
||||
|
||||
|
||||
$('#btnEval').click(function(e) { |
||||
$('#result').empty(); |
||||
var path = $('#txtPath').attr('value'); |
||||
var res = jsonPath(window.doc, path); |
||||
$('#result').append("PATH : " + path + "<hr/>"); |
||||
$('#result').append(JSON.stringify(res, null, 4)); |
||||
}); |
||||
|
||||
|
||||
}); |
||||
</script> |
||||
</head> |
||||
<body> |
||||
<input id="txtPath" type="text" value="$.store.book[?(@.price<10)].title"><button id="btnEval">eval</button> |
||||
<pre id="result"></pre> |
||||
</body> |
||||
</html> |
Loading…
Reference in new issue