mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
125 lines
4.9 KiB
Java
125 lines
4.9 KiB
Java
package org.json;
|
|
|
|
import static java.lang.String.format;
|
|
import static java.util.Collections.emptyList;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.net.URLDecoder;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/*
|
|
Copyright (c) 2002 JSON.org
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
The Software shall be used for Good, not Evil.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|
|
*/
|
|
|
|
/**
|
|
* A JSON Pointer is a simple query language defined for JSON documents by
|
|
* <a href="https://tools.ietf.org/html/rfc6901">RFC 6901</a>.
|
|
*/
|
|
public class JSONPointer {
|
|
|
|
private List<String> refTokens;
|
|
|
|
/**
|
|
* Pre-parses and initializes a new {@code JSONPointer} instance. If you want to
|
|
* evaluate the same JSON Pointer on different JSON documents then it is recommended
|
|
* to keep the {@code JSONPointer} instances due to performance considerations.
|
|
*
|
|
* @param pointer the JSON String or URI Fragment representation of the JSON pointer.
|
|
* @throws IllegalArgumentException if {@code pointer} is not a valid JSON pointer
|
|
*/
|
|
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);
|
|
try {
|
|
pointer = URLDecoder.decode(pointer, "utf-8");
|
|
} catch (UnsupportedEncodingException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
} 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", "~")
|
|
.replace("\\\"", "\"")
|
|
.replace("\\\\", "\\");
|
|
}
|
|
|
|
/**
|
|
* Evaluates this JSON Pointer on the given {@code document}. The {@code document}
|
|
* is usually a {@link JSONObject} or a {@link JSONArray} instance, but the empty
|
|
* JSON Pointer ({@code ""}) can be evaluated on any JSON values and in such case the
|
|
* returned value will be {@code document} itself.
|
|
*
|
|
* @param document the JSON document which should be the subject of querying.
|
|
* @return the result of the evaluation
|
|
* @throws JSONPointerException if an error occurs during evaluation
|
|
*/
|
|
public Object queryFrom(Object 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, token);
|
|
} else {
|
|
throw new JSONPointerException(format(
|
|
"value [%s] is not an array or object therefore its key %s cannot be resolved", current,
|
|
token));
|
|
}
|
|
}
|
|
return current;
|
|
}
|
|
|
|
private Object readByIndexToken(Object current, String indexToken) {
|
|
try {
|
|
int index = Integer.parseInt(indexToken);
|
|
JSONArray currentArr = (JSONArray) current;
|
|
if (index >= currentArr.length()) {
|
|
throw new JSONPointerException(format("index %d is out of bounds - the array has %d elements", index,
|
|
currentArr.length()));
|
|
}
|
|
return currentArr.get(index);
|
|
} catch (NumberFormatException e) {
|
|
throw new JSONPointerException(format("%s is not an array index", indexToken), e);
|
|
}
|
|
}
|
|
}
|