|
|
@ -26,6 +26,7 @@ public class JSONReaderImpl implements JSONReader { |
|
|
|
private static final JSONParser JSON_PARSER = new JSONParser(); |
|
|
|
private static final JSONParser JSON_PARSER = new JSONParser(); |
|
|
|
|
|
|
|
|
|
|
|
private Object root; |
|
|
|
private Object root; |
|
|
|
|
|
|
|
private String currentPath; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static synchronized JSONReader parse(Reader reader) throws java.text.ParseException, IOException { |
|
|
|
public static synchronized JSONReader parse(Reader reader) throws java.text.ParseException, IOException { |
|
|
@ -130,27 +131,32 @@ public class JSONReaderImpl implements JSONReader { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private <T> T getByPath(Class<T> clazz, String stringPath) { |
|
|
|
private <T> T getByPath(Class<T> clazz, String stringPath) { |
|
|
|
|
|
|
|
currentPath = ""; |
|
|
|
Object current = this.root; |
|
|
|
Object current = this.root; |
|
|
|
JSONPath path = new JSONPath(stringPath); |
|
|
|
JSONPath path = new JSONPath(stringPath); |
|
|
|
|
|
|
|
|
|
|
|
while (path.hasMoreFragments()) { |
|
|
|
while (path.hasMoreFragments()) { |
|
|
|
|
|
|
|
|
|
|
|
JSONPathFragment fragment = path.nextFragment(); |
|
|
|
JSONPathFragment fragment = path.nextFragment(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
currentPath = fragment.appendToPath(currentPath); |
|
|
|
|
|
|
|
|
|
|
|
if (fragment.isArrayIndex()) { |
|
|
|
if (fragment.isArrayIndex()) { |
|
|
|
current = getArray(current).get(fragment.getArrayIndex()); |
|
|
|
current = toArray(current).get(fragment.getArrayIndex()); |
|
|
|
} else if (fragment.isArrayWildcard()) { |
|
|
|
} else if (fragment.isArrayWildcard()) { |
|
|
|
current = getContainerValue(current, path.nextFragment().fragment()); |
|
|
|
current = getContainerValue(current, path.nextFragment()); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
current = getContainerValue(current, fragment.fragment()); |
|
|
|
current = getContainerValue(current, fragment); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return clazz.cast(current); |
|
|
|
return clazz.cast(current); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private JSONArray getArray(Object array) { |
|
|
|
private JSONArray toArray(Object array) { |
|
|
|
return (JSONArray) array; |
|
|
|
return (JSONArray) array; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private JSONObject getDocument(Object document) { |
|
|
|
private JSONObject toDocument(Object document) { |
|
|
|
return (JSONObject) document; |
|
|
|
return (JSONObject) document; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -161,32 +167,30 @@ public class JSONReaderImpl implements JSONReader { |
|
|
|
* will be returned in a List |
|
|
|
* will be returned in a List |
|
|
|
* |
|
|
|
* |
|
|
|
* @param container a json document or array |
|
|
|
* @param container a json document or array |
|
|
|
* @param field the field to extract from the document alt. the documents contained in the array |
|
|
|
* @param fragment the field to extract from the document alt. the documents contained in the array |
|
|
|
* @return a single field value or a List of fields |
|
|
|
* @return a single field value or a List of fields |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private Object getContainerValue(Object container, Object field) { |
|
|
|
private Object getContainerValue(Object container, JSONPathFragment fragment) { |
|
|
|
Object result; |
|
|
|
Object result; |
|
|
|
|
|
|
|
|
|
|
|
if (container instanceof JSONArray) { |
|
|
|
if (container instanceof JSONArray) { |
|
|
|
List list = new LinkedList(); |
|
|
|
List list = new LinkedList(); |
|
|
|
for (Object doc : getArray(container)) { |
|
|
|
for (Object doc : toArray(container)) { |
|
|
|
list.add(getContainerValue(doc, field)); |
|
|
|
list.add(getContainerValue(doc, fragment)); |
|
|
|
} |
|
|
|
} |
|
|
|
result = list; |
|
|
|
result = list; |
|
|
|
|
|
|
|
|
|
|
|
} else if (container instanceof JSONObject) { |
|
|
|
} else if (container instanceof JSONObject) { |
|
|
|
JSONObject document = getDocument(container); |
|
|
|
JSONObject document = toDocument(container); |
|
|
|
|
|
|
|
|
|
|
|
if (!document.containsKey(field)) { |
|
|
|
if (!document.containsKey(fragment.value())) { |
|
|
|
throw new InvalidPathException(); |
|
|
|
throw new InvalidPathException("Invalid path element: " + currentPath + " <=="); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
result = document.get(field); |
|
|
|
result = document.get(fragment.value()); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
throw new UnsupportedOperationException("can not get value from " + container.getClass().getName()); |
|
|
|
throw new InvalidPathException("Invalid path element: " + currentPath + " <=="); |
|
|
|
} |
|
|
|
} |
|
|
|
//notNull(result, "invalid path: " + field);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result; |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|