mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
commit
b3abaa5b4c
9 changed files with 509 additions and 63 deletions
7
CDL.java
7
CDL.java
|
@ -41,7 +41,7 @@ SOFTWARE.
|
|||
* The names for the elements in the JSONObjects can be taken from the names
|
||||
* in the first row.
|
||||
* @author JSON.org
|
||||
* @version 2015-12-09
|
||||
* @version 2016-05-01
|
||||
*/
|
||||
public class CDL {
|
||||
|
||||
|
@ -69,8 +69,13 @@ public class CDL {
|
|||
for (;;) {
|
||||
c = x.next();
|
||||
if (c == q) {
|
||||
//Handle escaped double-quote
|
||||
if(x.next() != '\"')
|
||||
{
|
||||
x.back();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (c == 0 || c == '\n' || c == '\r') {
|
||||
throw x.syntaxError("Missing close quote '" + q + "'.");
|
||||
}
|
||||
|
|
|
@ -28,10 +28,12 @@ import java.io.IOException;
|
|||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.lang.reflect.Array;
|
||||
import java.math.*;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
|
@ -76,7 +78,7 @@ import java.util.Map;
|
|||
* </ul>
|
||||
*
|
||||
* @author JSON.org
|
||||
* @version 2015-10-29
|
||||
* @version 2016-05-20
|
||||
*/
|
||||
public class JSONArray implements Iterable<Object> {
|
||||
|
||||
|
@ -593,7 +595,9 @@ public class JSONArray implements Iterable<Object> {
|
|||
return myE;
|
||||
}
|
||||
return Enum.valueOf(clazz, val.toString());
|
||||
} catch (IllegalArgumentException | NullPointerException e) {
|
||||
} catch (IllegalArgumentException e) {
|
||||
return defaultValue;
|
||||
} catch (NullPointerException e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
@ -956,6 +960,46 @@ public class JSONArray implements Iterable<Object> {
|
|||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a JSONPointer using an intialization string and tries to
|
||||
* match it to an item within this JSONArray. For example, given a
|
||||
* JSONArray initialized with this document:
|
||||
* <pre>
|
||||
* [
|
||||
* {"b":"c"}
|
||||
* ]
|
||||
* </pre>
|
||||
* and this JSONPointer string:
|
||||
* <pre>
|
||||
* "/0/b"
|
||||
* </pre>
|
||||
* Then this method will return the String "c"
|
||||
* A JSONPointerException may be thrown from code called by this method.
|
||||
*
|
||||
* @param jsonPointer string that can be used to create a JSONPointer
|
||||
* @return the item matched by the JSONPointer, otherwise null
|
||||
*/
|
||||
public Object query(String jsonPointer) {
|
||||
return new JSONPointer(jsonPointer).queryFrom(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Queries and returns a value from this object using {@code jsonPointer}, or
|
||||
* returns null if the query fails due to a missing key.
|
||||
*
|
||||
* @param jsonPointer the string representation of the JSON pointer
|
||||
* @return the queried value or {@code null}
|
||||
* @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax
|
||||
*/
|
||||
public Object optQuery(String jsonPointer) {
|
||||
JSONPointer pointer = new JSONPointer(jsonPointer);
|
||||
try {
|
||||
return pointer.queryFrom(this);
|
||||
} catch (JSONPointerException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an index and close the hole.
|
||||
*
|
||||
|
@ -1128,4 +1172,29 @@ public class JSONArray implements Iterable<Object> {
|
|||
throw new JSONException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a java.util.List containing all of the elements in this array.
|
||||
* If an element in the array is a JSONArray or JSONObject it will also
|
||||
* be converted.
|
||||
* <p>
|
||||
* Warning: This method assumes that the data structure is acyclical.
|
||||
*
|
||||
* @return a java.util.List containing the elements of this array
|
||||
*/
|
||||
public List<Object> toList() {
|
||||
List<Object> results = new ArrayList<Object>(this.myArrayList.size());
|
||||
for (Object element : this.myArrayList) {
|
||||
if (element == null || JSONObject.NULL.equals(element)) {
|
||||
results.add(null);
|
||||
} else if (element instanceof JSONArray) {
|
||||
results.add(((JSONArray) element).toList());
|
||||
} else if (element instanceof JSONObject) {
|
||||
results.add(((JSONObject) element).toMap());
|
||||
} else {
|
||||
results.add(element);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ import java.util.Iterator;
|
|||
* the JsonML transform.
|
||||
*
|
||||
* @author JSON.org
|
||||
* @version 2015-12-09
|
||||
* @version 2016-01-30
|
||||
*/
|
||||
public class JSONML {
|
||||
|
||||
|
@ -174,7 +174,7 @@ public class JSONML {
|
|||
if (!(token instanceof String)) {
|
||||
throw x.syntaxError("Missing value");
|
||||
}
|
||||
newjo.accumulate(attribute, XML.stringToValue((String)token));
|
||||
newjo.accumulate(attribute, JSONObject.stringToValue((String)token));
|
||||
token = null;
|
||||
} else {
|
||||
newjo.accumulate(attribute, "");
|
||||
|
@ -227,7 +227,7 @@ public class JSONML {
|
|||
} else {
|
||||
if (ja != null) {
|
||||
ja.put(token instanceof String
|
||||
? XML.stringToValue((String)token)
|
||||
? JSONObject.stringToValue((String)token)
|
||||
: token);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,7 +30,8 @@ import java.io.Writer;
|
|||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.math.*;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Collection;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
|
@ -92,7 +93,7 @@ import java.util.Set;
|
|||
* </ul>
|
||||
*
|
||||
* @author JSON.org
|
||||
* @version 2015-12-09
|
||||
* @version 2016-05-20
|
||||
*/
|
||||
public class JSONObject {
|
||||
/**
|
||||
|
@ -900,7 +901,9 @@ public class JSONObject {
|
|||
return myE;
|
||||
}
|
||||
return Enum.valueOf(clazz, val.toString());
|
||||
} catch (IllegalArgumentException | NullPointerException e) {
|
||||
} catch (IllegalArgumentException e) {
|
||||
return defaultValue;
|
||||
} catch (NullPointerException e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
@ -1335,6 +1338,46 @@ public class JSONObject {
|
|||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a JSONPointer using an intialization string and tries to
|
||||
* match it to an item within this JSONObject. For example, given a
|
||||
* JSONObject initialized with this document:
|
||||
* <pre>
|
||||
* {
|
||||
* "a":{"b":"c"}
|
||||
* }
|
||||
* </pre>
|
||||
* and this JSONPointer string:
|
||||
* <pre>
|
||||
* "/a/b"
|
||||
* </pre>
|
||||
* Then this method will return the String "c".
|
||||
* A JSONPointerException may be thrown from code called by this method.
|
||||
*
|
||||
* @param jsonPointer string that can be used to create a JSONPointer
|
||||
* @return the item matched by the JSONPointer, otherwise null
|
||||
*/
|
||||
public Object query(String jsonPointer) {
|
||||
return new JSONPointer(jsonPointer).queryFrom(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Queries and returns a value from this object using {@code jsonPointer}, or
|
||||
* returns null if the query fails due to a missing key.
|
||||
*
|
||||
* @param jsonPointer the string representation of the JSON pointer
|
||||
* @return the queried value or {@code null}
|
||||
* @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax
|
||||
*/
|
||||
public Object optQuery(String jsonPointer) {
|
||||
JSONPointer pointer = new JSONPointer(jsonPointer);
|
||||
try {
|
||||
return pointer.queryFrom(this);
|
||||
} catch (JSONPointerException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Produce a string in double quotes with backslash sequences in all the
|
||||
* right places. A backslash will be inserted within </, producing <\/,
|
||||
|
@ -1477,7 +1520,6 @@ public class JSONObject {
|
|||
* @return A simple JSON value.
|
||||
*/
|
||||
public static Object stringToValue(String string) {
|
||||
Double d;
|
||||
if (string.equals("")) {
|
||||
return string;
|
||||
}
|
||||
|
@ -1496,23 +1538,23 @@ public class JSONObject {
|
|||
* produced, then the value will just be a string.
|
||||
*/
|
||||
|
||||
char b = string.charAt(0);
|
||||
if ((b >= '0' && b <= '9') || b == '-') {
|
||||
char initial = string.charAt(0);
|
||||
if ((initial >= '0' && initial <= '9') || initial == '-') {
|
||||
try {
|
||||
if (string.indexOf('.') > -1 || string.indexOf('e') > -1
|
||||
|| string.indexOf('E') > -1) {
|
||||
d = Double.valueOf(string);
|
||||
|| string.indexOf('E') > -1
|
||||
|| "-0".equals(string)) {
|
||||
Double d = Double.valueOf(string);
|
||||
if (!d.isInfinite() && !d.isNaN()) {
|
||||
return d;
|
||||
}
|
||||
} else {
|
||||
Long myLong = new Long(string);
|
||||
if (string.equals(myLong.toString())) {
|
||||
if (myLong == myLong.intValue()) {
|
||||
return myLong.intValue();
|
||||
} else {
|
||||
return myLong;
|
||||
if (myLong.longValue() == myLong.intValue()) {
|
||||
return Integer.valueOf(myLong.intValue());
|
||||
}
|
||||
return myLong;
|
||||
}
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
|
@ -1836,4 +1878,31 @@ public class JSONObject {
|
|||
throw new JSONException(exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a java.util.Map containing all of the entrys in this object.
|
||||
* If an entry in the object is a JSONArray or JSONObject it will also
|
||||
* be converted.
|
||||
* <p>
|
||||
* Warning: This method assumes that the data structure is acyclical.
|
||||
*
|
||||
* @return a java.util.Map containing the entrys of this object
|
||||
*/
|
||||
public Map<String, Object> toMap() {
|
||||
Map<String, Object> results = new HashMap<String, Object>();
|
||||
for (Entry<String, Object> entry : this.map.entrySet()) {
|
||||
Object value;
|
||||
if (entry.getValue() == null || NULL.equals(entry.getValue())) {
|
||||
value = null;
|
||||
} else if (entry.getValue() instanceof JSONObject) {
|
||||
value = ((JSONObject) entry.getValue()).toMap();
|
||||
} else if (entry.getValue() instanceof JSONArray) {
|
||||
value = ((JSONArray) entry.getValue()).toList();
|
||||
} else {
|
||||
value = entry.getValue();
|
||||
}
|
||||
results.put(entry.getKey(), value);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
|
267
JSONPointer.java
Normal file
267
JSONPointer.java
Normal file
|
@ -0,0 +1,267 @@
|
|||
package org.json;
|
||||
|
||||
import static java.lang.String.format;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.*;
|
||||
|
||||
/*
|
||||
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>.
|
||||
*
|
||||
* In a nutshell, JSONPointer allows the user to navigate into a JSON document
|
||||
* using strings, and retrieve targeted objects, like a simple form of XPATH.
|
||||
* Path segments are separated by the '/' char, which signifies the root of
|
||||
* the document when it appears as the first char of the string. Array
|
||||
* elements are navigated using ordinals, counting from 0. JSONPointer strings
|
||||
* may be extended to any arbitrary number of segments. If the navigation
|
||||
* is successful, the matched item is returned. A matched item may be a
|
||||
* JSONObject, a JSONArray, or a JSON value. If the JSONPointer string building
|
||||
* fails, an appropriate exception is thrown. If the navigation fails to find
|
||||
* a match, a JSONPointerException is thrown.
|
||||
*
|
||||
* @author JSON.org
|
||||
* @version 2016-05-14
|
||||
*/
|
||||
public class JSONPointer {
|
||||
|
||||
// used for URL encoding and decoding
|
||||
private static final String ENCODING = "utf-8";
|
||||
|
||||
/**
|
||||
* This class allows the user to build a JSONPointer in steps, using
|
||||
* exactly one segment in each step.
|
||||
*/
|
||||
public static class Builder {
|
||||
|
||||
// Segments for the eventual JSONPointer string
|
||||
private final List<String> refTokens = new ArrayList<String>();
|
||||
|
||||
/**
|
||||
* Creates a {@code JSONPointer} instance using the tokens previously set using the
|
||||
* {@link #append(String)} method calls.
|
||||
*/
|
||||
public JSONPointer build() {
|
||||
return new JSONPointer(refTokens);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an arbitary token to the list of reference tokens. It can be any non-null value.
|
||||
*
|
||||
* Unlike in the case of JSON string or URI fragment representation of JSON pointers, the
|
||||
* argument of this method MUST NOT be escaped. If you want to query the property called
|
||||
* {@code "a~b"} then you should simply pass the {@code "a~b"} string as-is, there is no
|
||||
* need to escape it as {@code "a~0b"}.
|
||||
*
|
||||
* @param token the new token to be appended to the list
|
||||
* @return {@code this}
|
||||
* @throws NullPointerException if {@code token} is null
|
||||
*/
|
||||
public Builder append(String token) {
|
||||
if (token == null) {
|
||||
throw new NullPointerException("token cannot be null");
|
||||
}
|
||||
refTokens.add(token);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an integer to the reference token list. Although not necessarily, mostly this token will
|
||||
* denote an array index.
|
||||
*
|
||||
* @param arrayIndex the array index to be added to the token list
|
||||
* @return {@code this}
|
||||
*/
|
||||
public Builder append(int arrayIndex) {
|
||||
refTokens.add(String.valueOf(arrayIndex));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Static factory method for {@link Builder}. Example usage:
|
||||
*
|
||||
* <pre><code>
|
||||
* JSONPointer pointer = JSONPointer.builder()
|
||||
* .append("obj")
|
||||
* .append("other~key").append("another/key")
|
||||
* .append("\"")
|
||||
* .append(0)
|
||||
* .build();
|
||||
* </code></pre>
|
||||
*
|
||||
* @return a builder instance which can be used to construct a {@code JSONPointer} instance by chained
|
||||
* {@link Builder#append(String)} calls.
|
||||
*/
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
// Segments for the JSONPointer string
|
||||
private final 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 = Collections.emptyList();
|
||||
return;
|
||||
}
|
||||
if (pointer.startsWith("#/")) {
|
||||
pointer = pointer.substring(2);
|
||||
try {
|
||||
pointer = URLDecoder.decode(pointer, ENCODING);
|
||||
} 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));
|
||||
}
|
||||
}
|
||||
|
||||
public JSONPointer(List<String> refTokens) {
|
||||
this.refTokens = new ArrayList<String>(refTokens);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches a JSONArray element by ordinal position
|
||||
* @param current the JSONArray to be evaluated
|
||||
* @param indexToken the array index in string form
|
||||
* @return the matched object. If no matching item is found a
|
||||
* JSONPointerException is thrown
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representing the JSONPointer path value using string
|
||||
* representation
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder rval = new StringBuilder("");
|
||||
for (String token: refTokens) {
|
||||
rval.append('/').append(escape(token));
|
||||
}
|
||||
return rval.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Escapes path segment values to an unambiguous form.
|
||||
* The escape char to be inserted is '~'. The chars to be escaped
|
||||
* are ~, which maps to ~0, and /, which maps to ~1. Backslashes
|
||||
* and double quote chars are also escaped.
|
||||
* @param token the JSONPointer segment value to be escaped
|
||||
* @return the escaped value for the token
|
||||
*/
|
||||
private String escape(String token) {
|
||||
return token.replace("~", "~0")
|
||||
.replace("/", "~1")
|
||||
.replace("\\", "\\\\")
|
||||
.replace("\"", "\\\"");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representing the JSONPointer path value using URI
|
||||
* fragment identifier representation
|
||||
*/
|
||||
public String toURIFragment() {
|
||||
try {
|
||||
StringBuilder rval = new StringBuilder("#");
|
||||
for (String token : refTokens) {
|
||||
rval.append('/').append(URLEncoder.encode(token, ENCODING));
|
||||
}
|
||||
return rval.toString();
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
45
JSONPointerException.java
Normal file
45
JSONPointerException.java
Normal file
|
@ -0,0 +1,45 @@
|
|||
package org.json;
|
||||
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The JSONPointerException is thrown by {@link JSONPointer} if an error occurs
|
||||
* during evaluating a pointer.
|
||||
*
|
||||
* @author JSON.org
|
||||
* @version 2016-05-13
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
|
@ -278,7 +278,11 @@ public class JSONTokener {
|
|||
sb.append('\r');
|
||||
break;
|
||||
case 'u':
|
||||
try {
|
||||
sb.append((char)Integer.parseInt(this.next(4), 16));
|
||||
} catch (NumberFormatException e) {
|
||||
throw this.syntaxError("Illegal escape.", e);
|
||||
}
|
||||
break;
|
||||
case '"':
|
||||
case '\'':
|
||||
|
@ -433,6 +437,16 @@ public class JSONTokener {
|
|||
return new JSONException(message + this.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a JSONException to signal a syntax error.
|
||||
*
|
||||
* @param message The error message.
|
||||
* @param causedBy The throwable that caused the error.
|
||||
* @return A JSONException object, suitable for throwing
|
||||
*/
|
||||
public JSONException syntaxError(String message, Throwable causedBy) {
|
||||
return new JSONException(message + this.toString(), causedBy);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a printable string of this JSONTokener.
|
||||
|
|
11
README
11
README
|
@ -15,7 +15,7 @@ The license includes this restriction: "The software shall be used for good,
|
|||
not evil." If your conscience cannot live with that, then choose a different
|
||||
package.
|
||||
|
||||
The package compiles on Java 1.8.
|
||||
The package compiles on Java 1.6-1.8.
|
||||
|
||||
|
||||
JSONObject.java: The JSONObject can parse text from a String or a JSONTokener
|
||||
|
@ -32,6 +32,10 @@ tokens. It can be constructed from a String, Reader, or InputStream.
|
|||
JSONException.java: The JSONException is the standard exception type thrown
|
||||
by this package.
|
||||
|
||||
JSONPointer.java: Implementation of
|
||||
[JSON Pointer (RFC 6901)](https://tools.ietf.org/html/rfc6901). Supports
|
||||
JSON Pointers both in the form of string representation and URI fragment
|
||||
representation.
|
||||
|
||||
JSONString.java: The JSONString interface requires a toJSONString method,
|
||||
allowing an object to provide its own serialization.
|
||||
|
@ -73,8 +77,13 @@ This package fully supports Integer, Long, and Double Java types. Partial suppor
|
|||
for BigInteger and BigDecimal values in JSONObject and JSONArray objects is provided
|
||||
in the form of get(), opt(), and put() API methods.
|
||||
|
||||
Although 1.6 compatibility is currently supported, it is not a project goal and may be
|
||||
removed in some future release.
|
||||
|
||||
Release history:
|
||||
|
||||
20160212 Java 1.6 compatibility, OSGi bundle. Contains the latest code as of 12 Feb, 2016.
|
||||
|
||||
20151123 JSONObject and JSONArray initialization with generics. Contains the
|
||||
latest code as of 23 Nov, 2015.
|
||||
|
||||
|
|
48
XML.java
48
XML.java
|
@ -31,7 +31,7 @@ import java.util.Iterator;
|
|||
* covert a JSONObject into an XML text.
|
||||
*
|
||||
* @author JSON.org
|
||||
* @version 2016-01-01
|
||||
* @version 2016-01-30
|
||||
*/
|
||||
@SuppressWarnings("boxing")
|
||||
public class XML {
|
||||
|
@ -238,7 +238,7 @@ public class XML {
|
|||
throw x.syntaxError("Missing value");
|
||||
}
|
||||
jsonobject.accumulate(string,
|
||||
XML.stringToValue((String) token));
|
||||
JSONObject.stringToValue((String) token));
|
||||
token = null;
|
||||
} else {
|
||||
jsonobject.accumulate(string, "");
|
||||
|
@ -270,7 +270,7 @@ public class XML {
|
|||
string = (String) token;
|
||||
if (string.length() > 0) {
|
||||
jsonobject.accumulate("content",
|
||||
XML.stringToValue(string));
|
||||
JSONObject.stringToValue(string));
|
||||
}
|
||||
|
||||
} else if (token == LT) {
|
||||
|
@ -297,47 +297,15 @@ public class XML {
|
|||
}
|
||||
|
||||
/**
|
||||
* Try to convert a string into a number, boolean, or null. If the string
|
||||
* can't be converted, return the string. This is much less ambitious than
|
||||
* JSONObject.stringToValue, especially because it does not attempt to
|
||||
* convert plus forms, octal forms, hex forms, or E forms lacking decimal
|
||||
* points.
|
||||
* This method has been deprecated in favor of the
|
||||
* {@link JSONObject.stringToValue(String)} method. Use it instead.
|
||||
*
|
||||
* @deprecated Use {@link JSONObject#stringToValue(String)} instead.
|
||||
* @param string
|
||||
* A String.
|
||||
* @return A simple JSON value.
|
||||
* @return JSON value of this string or the string
|
||||
*/
|
||||
public static Object stringToValue(String string) {
|
||||
if ("true".equalsIgnoreCase(string)) {
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
if ("false".equalsIgnoreCase(string)) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
if ("null".equalsIgnoreCase(string)) {
|
||||
return JSONObject.NULL;
|
||||
}
|
||||
|
||||
// If it might be a number, try converting it, first as a Long, and then
|
||||
// as a Double. If that doesn't work, return the string.
|
||||
try {
|
||||
char initial = string.charAt(0);
|
||||
if (initial == '-' || (initial >= '0' && initial <= '9')) {
|
||||
Long value = new Long(string);
|
||||
if (value.toString().equals(string)) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
try {
|
||||
Double value = new Double(string);
|
||||
if (value.toString().equals(string)) {
|
||||
return value;
|
||||
}
|
||||
} catch (Exception ignoreAlso) {
|
||||
}
|
||||
}
|
||||
return string;
|
||||
return JSONObject.stringToValue(string);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue