mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
initial implementation of JSONPointer
This commit is contained in:
parent
25a87975be
commit
612e41950c
2 changed files with 82 additions and 0 deletions
61
JSONPointer.java
Normal file
61
JSONPointer.java
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
package org.json;
|
||||||
|
|
||||||
|
import static java.lang.String.format;
|
||||||
|
import static java.util.Collections.emptyList;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class JSONPointer {
|
||||||
|
|
||||||
|
private List<String> refTokens;
|
||||||
|
|
||||||
|
public JSONPointer(String pointer) {
|
||||||
|
if (pointer == null) {
|
||||||
|
throw new NullPointerException("pointer cannot be null");
|
||||||
|
}
|
||||||
|
if (pointer.isEmpty()) {
|
||||||
|
refTokens = emptyList();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (pointer.startsWith("#/")) {
|
||||||
|
pointer = pointer.substring(2);
|
||||||
|
} else if (pointer.startsWith("/")) {
|
||||||
|
pointer = pointer.substring(1);
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("a JSON pointer should start with '/' or '#/'");
|
||||||
|
}
|
||||||
|
refTokens = new ArrayList<String>();
|
||||||
|
for (String token : pointer.split("/")) {
|
||||||
|
refTokens.add(unescape(token));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String unescape(String token) {
|
||||||
|
return token.replace("~1", "/").replace("~0", "~");
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object queryFrom(JSONObject document) {
|
||||||
|
if (refTokens.isEmpty()) {
|
||||||
|
return document;
|
||||||
|
}
|
||||||
|
Object current = document;
|
||||||
|
for (String token : refTokens) {
|
||||||
|
if (current instanceof JSONObject) {
|
||||||
|
current = ((JSONObject) current).opt(unescape(token));
|
||||||
|
} else if (current instanceof JSONArray) {
|
||||||
|
current = readByIndexToken(current, unescape(token));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object readByIndexToken(Object current, String indexToken) {
|
||||||
|
try {
|
||||||
|
return ((JSONArray) current).opt(Integer.parseInt(unescape(indexToken)));
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
throw new JSONPointerException(format("%s is not an array index", unescape(indexToken)), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
21
JSONPointerException.java
Normal file
21
JSONPointerException.java
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
package org.json;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The JSONPointerException is thrown by {@link JSONPointer} if an error occurs
|
||||||
|
* during evaluating a pointer.
|
||||||
|
*
|
||||||
|
* @author erosb
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class JSONPointerException extends JSONException {
|
||||||
|
private static final long serialVersionUID = 8872944667561856751L;
|
||||||
|
|
||||||
|
public JSONPointerException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public JSONPointerException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue