Fix StackOverflow when attempting to serialize ItemMeta with nested maps.
This removes the JSON library included with PlayerVaults and uses SimpleJSON included with Bukkit.
This commit is contained in:
@@ -81,7 +81,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.spigotmc</groupId>
|
<groupId>org.spigotmc</groupId>
|
||||||
<artifactId>spigot-api</artifactId>
|
<artifactId>spigot-api</artifactId>
|
||||||
<version>1.8.3-R0.1-SNAPSHOT</version>
|
<version>1.8.8-R0.1-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.milkbowl.vault</groupId>
|
<groupId>net.milkbowl.vault</groupId>
|
||||||
|
|||||||
@@ -21,14 +21,13 @@ import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
|||||||
import org.bukkit.configuration.serialization.ConfigurationSerialization;
|
import org.bukkit.configuration.serialization.ConfigurationSerialization;
|
||||||
import org.bukkit.inventory.Inventory;
|
import org.bukkit.inventory.Inventory;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.json.JSONArray;
|
import org.json.simple.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.simple.JSONObject;
|
||||||
import org.json.JSONObject;
|
import org.json.simple.JSONValue;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
@@ -41,18 +40,16 @@ import java.util.Map.Entry;
|
|||||||
public class Serialization {
|
public class Serialization {
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static Map<String, Object> toMap(JSONObject object) throws JSONException {
|
public static Map<String, Object> toMap(JSONObject object) {
|
||||||
Map<String, Object> map = new HashMap<>();
|
Map<String, Object> map = new HashMap<>();
|
||||||
Iterator<String> keys = object.keys();
|
for (Object key : object.keySet()) {
|
||||||
while (keys.hasNext()) {
|
map.put(key.toString(), fromJson(object.get(key)));
|
||||||
String key = keys.next();
|
|
||||||
map.put(key, fromJson(object.get(key)));
|
|
||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Object fromJson(Object json) throws JSONException {
|
private static Object fromJson(Object json) {
|
||||||
if (json == JSONObject.NULL) {
|
if (json == null) {
|
||||||
return null;
|
return null;
|
||||||
} else if (json instanceof JSONObject) {
|
} else if (json instanceof JSONObject) {
|
||||||
return toMap((JSONObject) json);
|
return toMap((JSONObject) json);
|
||||||
@@ -63,10 +60,10 @@ public class Serialization {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Object> toList(JSONArray array) throws JSONException {
|
public static List<Object> toList(JSONArray array) {
|
||||||
List<Object> list = new ArrayList<>();
|
List<Object> list = new ArrayList<>();
|
||||||
for (int i = 0; i < array.length(); i++) {
|
for (Object value : array) {
|
||||||
list.add(fromJson(array.get(i)));
|
list.add(fromJson(value));
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
@@ -94,12 +91,8 @@ public class Serialization {
|
|||||||
if (piece.equalsIgnoreCase("null")) {
|
if (piece.equalsIgnoreCase("null")) {
|
||||||
contents.add(null);
|
contents.add(null);
|
||||||
} else {
|
} else {
|
||||||
try {
|
ItemStack item = (ItemStack) deserialize(toMap((JSONObject) JSONValue.parse(piece)));
|
||||||
ItemStack item = (ItemStack) deserialize(toMap(new JSONObject(piece)));
|
|
||||||
contents.add(item);
|
contents.add(item);
|
||||||
} catch (JSONException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ItemStack[] items = new ItemStack[contents.size()];
|
ItemStack[] items = new ItemStack[contents.size()];
|
||||||
|
|||||||
@@ -1,884 +0,0 @@
|
|||||||
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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.StringWriter;
|
|
||||||
import java.io.Writer;
|
|
||||||
import java.lang.reflect.Array;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A JSONArray is an ordered sequence of values. Its external text form is a string wrapped in square brackets with
|
|
||||||
* commas separating the values. The internal form is an object having <code>get</code> and <code>opt</code> methods for
|
|
||||||
* accessing the values by index, and <code>put</code> methods for adding or replacing values. The values can be any of
|
|
||||||
* these types: <code>Boolean</code>, <code>JSONArray</code>, <code>JSONObject</code>, <code>Number</code>,
|
|
||||||
* <code>String</code>, or the <code>JSONObject.NULL object</code>.
|
|
||||||
* <p/>
|
|
||||||
* The constructor can convert a JSON text into a Java object. The <code>toString</code> method converts to JSON text.
|
|
||||||
* <p/>
|
|
||||||
* A <code>get</code> method returns a value if one can be found, and throws an exception if one cannot be found. An
|
|
||||||
* <code>opt</code> method returns a default value instead of throwing an exception, and so is useful for obtaining
|
|
||||||
* optional values.
|
|
||||||
* <p/>
|
|
||||||
* The generic <code>get()</code> and <code>opt()</code> methods return an object which you can cast or query for type.
|
|
||||||
* There are also typed <code>get</code> and <code>opt</code> methods that do type checking and type coercion for you.
|
|
||||||
* <p/>
|
|
||||||
* The texts produced by the <code>toString</code> methods strictly conform to JSON syntax rules. The constructors are
|
|
||||||
* more forgiving in the texts they will accept: <ul> <li>An extra <code>,</code> <small>(comma)</small> may appear
|
|
||||||
* just before the closing bracket.</li> <li>The <code>null</code> value will be inserted when there is <code>,</code>
|
|
||||||
* <small>(comma)</small> elision.</li> <li>Strings may be quoted with <code>'</code> <small>(single
|
|
||||||
* quote)</small>.</li> <li>Strings do not need to be quoted at all if they do not begin with a quote or single quote,
|
|
||||||
* and if they do not contain leading or trailing spaces, and if they do not contain any of these characters: <code>{ }
|
|
||||||
* [ ] / \ : , = ; #</code> and if they do not look like numbers and if they are not the reserved words
|
|
||||||
* <code>true</code>, <code>false</code>, or <code>null</code>.</li> <li>Values can be separated by <code>;</code>
|
|
||||||
* <small>(semicolon)</small> as well as by <code>,</code> <small>(comma)</small>.</li> </ul>
|
|
||||||
*
|
|
||||||
* @author JSON.org
|
|
||||||
* @version 2012-11-13
|
|
||||||
*/
|
|
||||||
public class JSONArray {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The arrayList where the JSONArray's properties are kept.
|
|
||||||
*/
|
|
||||||
private final ArrayList<Object> myArrayList;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct an empty JSONArray.
|
|
||||||
*/
|
|
||||||
public JSONArray() {
|
|
||||||
this.myArrayList = new ArrayList<Object>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct a JSONArray from a JSONTokener.
|
|
||||||
*
|
|
||||||
* @param x A JSONTokener
|
|
||||||
*
|
|
||||||
* @throws JSONException If there is a syntax error.
|
|
||||||
*/
|
|
||||||
public JSONArray(JSONTokener x) throws JSONException {
|
|
||||||
this();
|
|
||||||
if (x.nextClean() != '[') {
|
|
||||||
throw x.syntaxError("A JSONArray text must start with '['");
|
|
||||||
}
|
|
||||||
if (x.nextClean() != ']') {
|
|
||||||
x.back();
|
|
||||||
for (; ; ) {
|
|
||||||
if (x.nextClean() == ',') {
|
|
||||||
x.back();
|
|
||||||
this.myArrayList.add(JSONObject.NULL);
|
|
||||||
} else {
|
|
||||||
x.back();
|
|
||||||
this.myArrayList.add(x.nextValue());
|
|
||||||
}
|
|
||||||
switch (x.nextClean()) {
|
|
||||||
case ';':
|
|
||||||
case ',':
|
|
||||||
if (x.nextClean() == ']') {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
x.back();
|
|
||||||
break;
|
|
||||||
case ']':
|
|
||||||
return;
|
|
||||||
default:
|
|
||||||
throw x.syntaxError("Expected a ',' or ']'");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct a JSONArray from a source JSON text.
|
|
||||||
*
|
|
||||||
* @param source A string that begins with <code>[</code> <small>(left bracket)</small> and ends with
|
|
||||||
* <code>]</code> <small>(right bracket)</small>.
|
|
||||||
*
|
|
||||||
* @throws JSONException If there is a syntax error.
|
|
||||||
*/
|
|
||||||
public JSONArray(String source) throws JSONException {
|
|
||||||
this(new JSONTokener(source));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct a JSONArray from a Collection.
|
|
||||||
*
|
|
||||||
* @param collection A Collection.
|
|
||||||
*/
|
|
||||||
public JSONArray(Collection<?> collection) {
|
|
||||||
this.myArrayList = new ArrayList<Object>();
|
|
||||||
if (collection != null) {
|
|
||||||
Iterator<?> iter = collection.iterator();
|
|
||||||
while (iter.hasNext()) {
|
|
||||||
this.myArrayList.add(JSONObject.wrap(iter.next()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct a JSONArray from an array
|
|
||||||
*
|
|
||||||
* @throws JSONException If not an array.
|
|
||||||
*/
|
|
||||||
public JSONArray(Object array) throws JSONException {
|
|
||||||
this();
|
|
||||||
if (array.getClass().isArray()) {
|
|
||||||
int length = Array.getLength(array);
|
|
||||||
for (int i = 0; i < length; i += 1) {
|
|
||||||
this.put(JSONObject.wrap(Array.get(array, i)));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new JSONException(
|
|
||||||
"JSONArray initial value should be a string or collection or array.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the object value associated with an index.
|
|
||||||
*
|
|
||||||
* @param index The index must be between 0 and length() - 1.
|
|
||||||
*
|
|
||||||
* @return An object value.
|
|
||||||
*
|
|
||||||
* @throws JSONException If there is no value for the index.
|
|
||||||
*/
|
|
||||||
public Object get(int index) throws JSONException {
|
|
||||||
Object object = this.opt(index);
|
|
||||||
if (object == null) {
|
|
||||||
throw new JSONException("JSONArray[" + index + "] not found.");
|
|
||||||
}
|
|
||||||
return object;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the boolean value associated with an index. The string values "true" and "false" are converted to boolean.
|
|
||||||
*
|
|
||||||
* @param index The index must be between 0 and length() - 1.
|
|
||||||
*
|
|
||||||
* @return The truth.
|
|
||||||
*
|
|
||||||
* @throws JSONException If there is no value for the index or if the value is not convertible to boolean.
|
|
||||||
*/
|
|
||||||
public boolean getBoolean(int index) throws JSONException {
|
|
||||||
Object object = this.get(index);
|
|
||||||
if (object.equals(Boolean.FALSE) ||
|
|
||||||
(object instanceof String &&
|
|
||||||
((String) object).equalsIgnoreCase("false"))) {
|
|
||||||
return false;
|
|
||||||
} else if (object.equals(Boolean.TRUE) ||
|
|
||||||
(object instanceof String &&
|
|
||||||
((String) object).equalsIgnoreCase("true"))) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
throw new JSONException("JSONArray[" + index + "] is not a boolean.");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the double value associated with an index.
|
|
||||||
*
|
|
||||||
* @param index The index must be between 0 and length() - 1.
|
|
||||||
*
|
|
||||||
* @return The value.
|
|
||||||
*
|
|
||||||
* @throws JSONException If the key is not found or if the value cannot be converted to a number.
|
|
||||||
*/
|
|
||||||
public double getDouble(int index) throws JSONException {
|
|
||||||
Object object = this.get(index);
|
|
||||||
try {
|
|
||||||
return object instanceof Number
|
|
||||||
? ((Number) object).doubleValue()
|
|
||||||
: Double.parseDouble((String) object);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new JSONException("JSONArray[" + index +
|
|
||||||
"] is not a number.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the int value associated with an index.
|
|
||||||
*
|
|
||||||
* @param index The index must be between 0 and length() - 1.
|
|
||||||
*
|
|
||||||
* @return The value.
|
|
||||||
*
|
|
||||||
* @throws JSONException If the key is not found or if the value is not a number.
|
|
||||||
*/
|
|
||||||
public int getInt(int index) throws JSONException {
|
|
||||||
Object object = this.get(index);
|
|
||||||
try {
|
|
||||||
return object instanceof Number
|
|
||||||
? ((Number) object).intValue()
|
|
||||||
: Integer.parseInt((String) object);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new JSONException("JSONArray[" + index +
|
|
||||||
"] is not a number.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the JSONArray associated with an index.
|
|
||||||
*
|
|
||||||
* @param index The index must be between 0 and length() - 1.
|
|
||||||
*
|
|
||||||
* @return A JSONArray value.
|
|
||||||
*
|
|
||||||
* @throws JSONException If there is no value for the index. or if the value is not a JSONArray
|
|
||||||
*/
|
|
||||||
public JSONArray getJSONArray(int index) throws JSONException {
|
|
||||||
Object object = this.get(index);
|
|
||||||
if (object instanceof JSONArray) {
|
|
||||||
return (JSONArray) object;
|
|
||||||
}
|
|
||||||
throw new JSONException("JSONArray[" + index +
|
|
||||||
"] is not a JSONArray.");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the JSONObject associated with an index.
|
|
||||||
*
|
|
||||||
* @param index subscript
|
|
||||||
*
|
|
||||||
* @return A JSONObject value.
|
|
||||||
*
|
|
||||||
* @throws JSONException If there is no value for the index or if the value is not a JSONObject
|
|
||||||
*/
|
|
||||||
public JSONObject getJSONObject(int index) throws JSONException {
|
|
||||||
Object object = this.get(index);
|
|
||||||
if (object instanceof JSONObject) {
|
|
||||||
return (JSONObject) object;
|
|
||||||
}
|
|
||||||
throw new JSONException("JSONArray[" + index +
|
|
||||||
"] is not a JSONObject.");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the long value associated with an index.
|
|
||||||
*
|
|
||||||
* @param index The index must be between 0 and length() - 1.
|
|
||||||
*
|
|
||||||
* @return The value.
|
|
||||||
*
|
|
||||||
* @throws JSONException If the key is not found or if the value cannot be converted to a number.
|
|
||||||
*/
|
|
||||||
public long getLong(int index) throws JSONException {
|
|
||||||
Object object = this.get(index);
|
|
||||||
try {
|
|
||||||
return object instanceof Number
|
|
||||||
? ((Number) object).longValue()
|
|
||||||
: Long.parseLong((String) object);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new JSONException("JSONArray[" + index +
|
|
||||||
"] is not a number.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the string associated with an index.
|
|
||||||
*
|
|
||||||
* @param index The index must be between 0 and length() - 1.
|
|
||||||
*
|
|
||||||
* @return A string value.
|
|
||||||
*
|
|
||||||
* @throws JSONException If there is no string value for the index.
|
|
||||||
*/
|
|
||||||
public String getString(int index) throws JSONException {
|
|
||||||
Object object = this.get(index);
|
|
||||||
if (object instanceof String) {
|
|
||||||
return (String) object;
|
|
||||||
}
|
|
||||||
throw new JSONException("JSONArray[" + index + "] not a string.");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determine if the value is null.
|
|
||||||
*
|
|
||||||
* @param index The index must be between 0 and length() - 1.
|
|
||||||
*
|
|
||||||
* @return true if the value at the index is null, or if there is no value.
|
|
||||||
*/
|
|
||||||
public boolean isNull(int index) {
|
|
||||||
return JSONObject.NULL.equals(this.opt(index));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Make a string from the contents of this JSONArray. The <code>separator</code> string is inserted between each
|
|
||||||
* element. Warning: This method assumes that the data structure is acyclical.
|
|
||||||
*
|
|
||||||
* @param separator A string that will be inserted between the elements.
|
|
||||||
*
|
|
||||||
* @return a string.
|
|
||||||
*
|
|
||||||
* @throws JSONException If the array contains an invalid number.
|
|
||||||
*/
|
|
||||||
public String join(String separator) throws JSONException {
|
|
||||||
int len = this.length();
|
|
||||||
StringBuffer sb = new StringBuffer();
|
|
||||||
|
|
||||||
for (int i = 0; i < len; i += 1) {
|
|
||||||
if (i > 0) {
|
|
||||||
sb.append(separator);
|
|
||||||
}
|
|
||||||
sb.append(JSONObject.valueToString(this.myArrayList.get(i)));
|
|
||||||
}
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the number of elements in the JSONArray, included nulls.
|
|
||||||
*
|
|
||||||
* @return The length (or size).
|
|
||||||
*/
|
|
||||||
public int length() {
|
|
||||||
return this.myArrayList.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the optional object value associated with an index.
|
|
||||||
*
|
|
||||||
* @param index The index must be between 0 and length() - 1.
|
|
||||||
*
|
|
||||||
* @return An object value, or null if there is no object at that index.
|
|
||||||
*/
|
|
||||||
public Object opt(int index) {
|
|
||||||
return (index < 0 || index >= this.length())
|
|
||||||
? null
|
|
||||||
: this.myArrayList.get(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the optional boolean value associated with an index. It returns false if there is no value at that index, or
|
|
||||||
* if the value is not Boolean.TRUE or the String "true".
|
|
||||||
*
|
|
||||||
* @param index The index must be between 0 and length() - 1.
|
|
||||||
*
|
|
||||||
* @return The truth.
|
|
||||||
*/
|
|
||||||
public boolean optBoolean(int index) {
|
|
||||||
return this.optBoolean(index, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the optional boolean value associated with an index. It returns the defaultValue if there is no value at that
|
|
||||||
* index or if it is not a Boolean or the String "true" or "false" (case insensitive).
|
|
||||||
*
|
|
||||||
* @param index The index must be between 0 and length() - 1.
|
|
||||||
* @param defaultValue A boolean default.
|
|
||||||
*
|
|
||||||
* @return The truth.
|
|
||||||
*/
|
|
||||||
public boolean optBoolean(int index, boolean defaultValue) {
|
|
||||||
try {
|
|
||||||
return this.getBoolean(index);
|
|
||||||
} catch (Exception e) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the optional double value associated with an index. NaN is returned if there is no value for the index, or if
|
|
||||||
* the value is not a number and cannot be converted to a number.
|
|
||||||
*
|
|
||||||
* @param index The index must be between 0 and length() - 1.
|
|
||||||
*
|
|
||||||
* @return The value.
|
|
||||||
*/
|
|
||||||
public double optDouble(int index) {
|
|
||||||
return this.optDouble(index, Double.NaN);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the optional double value associated with an index. The defaultValue is returned if there is no value for the
|
|
||||||
* index, or if the value is not a number and cannot be converted to a number.
|
|
||||||
*
|
|
||||||
* @param index subscript
|
|
||||||
* @param defaultValue The default value.
|
|
||||||
*
|
|
||||||
* @return The value.
|
|
||||||
*/
|
|
||||||
public double optDouble(int index, double defaultValue) {
|
|
||||||
try {
|
|
||||||
return this.getDouble(index);
|
|
||||||
} catch (Exception e) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the optional int value associated with an index. Zero is returned if there is no value for the index, or if
|
|
||||||
* the value is not a number and cannot be converted to a number.
|
|
||||||
*
|
|
||||||
* @param index The index must be between 0 and length() - 1.
|
|
||||||
*
|
|
||||||
* @return The value.
|
|
||||||
*/
|
|
||||||
public int optInt(int index) {
|
|
||||||
return this.optInt(index, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the optional int value associated with an index. The defaultValue is returned if there is no value for the
|
|
||||||
* index, or if the value is not a number and cannot be converted to a number.
|
|
||||||
*
|
|
||||||
* @param index The index must be between 0 and length() - 1.
|
|
||||||
* @param defaultValue The default value.
|
|
||||||
*
|
|
||||||
* @return The value.
|
|
||||||
*/
|
|
||||||
public int optInt(int index, int defaultValue) {
|
|
||||||
try {
|
|
||||||
return this.getInt(index);
|
|
||||||
} catch (Exception e) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the optional JSONArray associated with an index.
|
|
||||||
*
|
|
||||||
* @param index subscript
|
|
||||||
*
|
|
||||||
* @return A JSONArray value, or null if the index has no value, or if the value is not a JSONArray.
|
|
||||||
*/
|
|
||||||
public JSONArray optJSONArray(int index) {
|
|
||||||
Object o = this.opt(index);
|
|
||||||
return o instanceof JSONArray ? (JSONArray) o : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the optional JSONObject associated with an index. Null is returned if the key is not found, or null if the
|
|
||||||
* index has no value, or if the value is not a JSONObject.
|
|
||||||
*
|
|
||||||
* @param index The index must be between 0 and length() - 1.
|
|
||||||
*
|
|
||||||
* @return A JSONObject value.
|
|
||||||
*/
|
|
||||||
public JSONObject optJSONObject(int index) {
|
|
||||||
Object o = this.opt(index);
|
|
||||||
return o instanceof JSONObject ? (JSONObject) o : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the optional long value associated with an index. Zero is returned if there is no value for the index, or if
|
|
||||||
* the value is not a number and cannot be converted to a number.
|
|
||||||
*
|
|
||||||
* @param index The index must be between 0 and length() - 1.
|
|
||||||
*
|
|
||||||
* @return The value.
|
|
||||||
*/
|
|
||||||
public long optLong(int index) {
|
|
||||||
return this.optLong(index, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the optional long value associated with an index. The defaultValue is returned if there is no value for the
|
|
||||||
* index, or if the value is not a number and cannot be converted to a number.
|
|
||||||
*
|
|
||||||
* @param index The index must be between 0 and length() - 1.
|
|
||||||
* @param defaultValue The default value.
|
|
||||||
*
|
|
||||||
* @return The value.
|
|
||||||
*/
|
|
||||||
public long optLong(int index, long defaultValue) {
|
|
||||||
try {
|
|
||||||
return this.getLong(index);
|
|
||||||
} catch (Exception e) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the optional string value associated with an index. It returns an empty string if there is no value at that
|
|
||||||
* index. If the value is not a string and is not null, then it is coverted to a string.
|
|
||||||
*
|
|
||||||
* @param index The index must be between 0 and length() - 1.
|
|
||||||
*
|
|
||||||
* @return A String value.
|
|
||||||
*/
|
|
||||||
public String optString(int index) {
|
|
||||||
return this.optString(index, "");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the optional string associated with an index. The defaultValue is returned if the key is not found.
|
|
||||||
*
|
|
||||||
* @param index The index must be between 0 and length() - 1.
|
|
||||||
* @param defaultValue The default value.
|
|
||||||
*
|
|
||||||
* @return A String value.
|
|
||||||
*/
|
|
||||||
public String optString(int index, String defaultValue) {
|
|
||||||
Object object = this.opt(index);
|
|
||||||
return JSONObject.NULL.equals(object)
|
|
||||||
? defaultValue
|
|
||||||
: object.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Append a boolean value. This increases the array's length by one.
|
|
||||||
*
|
|
||||||
* @param value A boolean value.
|
|
||||||
*
|
|
||||||
* @return this.
|
|
||||||
*/
|
|
||||||
public JSONArray put(boolean value) {
|
|
||||||
this.put(value ? Boolean.TRUE : Boolean.FALSE);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Put a value in the JSONArray, where the value will be a JSONArray which is produced from a Collection.
|
|
||||||
*
|
|
||||||
* @param value A Collection value.
|
|
||||||
*
|
|
||||||
* @return this.
|
|
||||||
*/
|
|
||||||
public JSONArray put(Collection<?> value) {
|
|
||||||
this.put(new JSONArray(value));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Append a double value. This increases the array's length by one.
|
|
||||||
*
|
|
||||||
* @param value A double value.
|
|
||||||
*
|
|
||||||
* @return this.
|
|
||||||
*
|
|
||||||
* @throws JSONException if the value is not finite.
|
|
||||||
*/
|
|
||||||
public JSONArray put(double value) throws JSONException {
|
|
||||||
Double d = new Double(value);
|
|
||||||
JSONObject.testValidity(d);
|
|
||||||
this.put(d);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Append an int value. This increases the array's length by one.
|
|
||||||
*
|
|
||||||
* @param value An int value.
|
|
||||||
*
|
|
||||||
* @return this.
|
|
||||||
*/
|
|
||||||
public JSONArray put(int value) {
|
|
||||||
this.put(new Integer(value));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Append an long value. This increases the array's length by one.
|
|
||||||
*
|
|
||||||
* @param value A long value.
|
|
||||||
*
|
|
||||||
* @return this.
|
|
||||||
*/
|
|
||||||
public JSONArray put(long value) {
|
|
||||||
this.put(new Long(value));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Put a value in the JSONArray, where the value will be a JSONObject which is produced from a Map.
|
|
||||||
*
|
|
||||||
* @param value A Map value.
|
|
||||||
*
|
|
||||||
* @return this.
|
|
||||||
*/
|
|
||||||
public JSONArray put(Map<?, ?> value) {
|
|
||||||
this.put(new JSONObject(value));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Append an object value. This increases the array's length by one.
|
|
||||||
*
|
|
||||||
* @param value An object value. The value should be a Boolean, Double, Integer, JSONArray, JSONObject, Long, or
|
|
||||||
* String, or the JSONObject.NULL object.
|
|
||||||
*
|
|
||||||
* @return this.
|
|
||||||
*/
|
|
||||||
public JSONArray put(Object value) {
|
|
||||||
this.myArrayList.add(value);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Put or replace a boolean value in the JSONArray. If the index is greater than the length of the JSONArray, then
|
|
||||||
* null elements will be added as necessary to pad it out.
|
|
||||||
*
|
|
||||||
* @param index The subscript.
|
|
||||||
* @param value A boolean value.
|
|
||||||
*
|
|
||||||
* @return this.
|
|
||||||
*
|
|
||||||
* @throws JSONException If the index is negative.
|
|
||||||
*/
|
|
||||||
public JSONArray put(int index, boolean value) throws JSONException {
|
|
||||||
this.put(index, value ? Boolean.TRUE : Boolean.FALSE);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Put a value in the JSONArray, where the value will be a JSONArray which is produced from a Collection.
|
|
||||||
*
|
|
||||||
* @param index The subscript.
|
|
||||||
* @param value A Collection value.
|
|
||||||
*
|
|
||||||
* @return this.
|
|
||||||
*
|
|
||||||
* @throws JSONException If the index is negative or if the value is not finite.
|
|
||||||
*/
|
|
||||||
public JSONArray put(int index, Collection<?> value) throws JSONException {
|
|
||||||
this.put(index, new JSONArray(value));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Put or replace a double value. If the index is greater than the length of the JSONArray, then null elements will
|
|
||||||
* be added as necessary to pad it out.
|
|
||||||
*
|
|
||||||
* @param index The subscript.
|
|
||||||
* @param value A double value.
|
|
||||||
*
|
|
||||||
* @return this.
|
|
||||||
*
|
|
||||||
* @throws JSONException If the index is negative or if the value is not finite.
|
|
||||||
*/
|
|
||||||
public JSONArray put(int index, double value) throws JSONException {
|
|
||||||
this.put(index, new Double(value));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Put or replace an int value. If the index is greater than the length of the JSONArray, then null elements will be
|
|
||||||
* added as necessary to pad it out.
|
|
||||||
*
|
|
||||||
* @param index The subscript.
|
|
||||||
* @param value An int value.
|
|
||||||
*
|
|
||||||
* @return this.
|
|
||||||
*
|
|
||||||
* @throws JSONException If the index is negative.
|
|
||||||
*/
|
|
||||||
public JSONArray put(int index, int value) throws JSONException {
|
|
||||||
this.put(index, new Integer(value));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Put or replace a long value. If the index is greater than the length of the JSONArray, then null elements will be
|
|
||||||
* added as necessary to pad it out.
|
|
||||||
*
|
|
||||||
* @param index The subscript.
|
|
||||||
* @param value A long value.
|
|
||||||
*
|
|
||||||
* @return this.
|
|
||||||
*
|
|
||||||
* @throws JSONException If the index is negative.
|
|
||||||
*/
|
|
||||||
public JSONArray put(int index, long value) throws JSONException {
|
|
||||||
this.put(index, new Long(value));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Put a value in the JSONArray, where the value will be a JSONObject that is produced from a Map.
|
|
||||||
*
|
|
||||||
* @param index The subscript.
|
|
||||||
* @param value The Map value.
|
|
||||||
*
|
|
||||||
* @return this.
|
|
||||||
*
|
|
||||||
* @throws JSONException If the index is negative or if the the value is an invalid number.
|
|
||||||
*/
|
|
||||||
public JSONArray put(int index, Map<?, ?> value) throws JSONException {
|
|
||||||
this.put(index, new JSONObject(value));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Put or replace an object value in the JSONArray. If the index is greater than the length of the JSONArray, then
|
|
||||||
* null elements will be added as necessary to pad it out.
|
|
||||||
*
|
|
||||||
* @param index The subscript.
|
|
||||||
* @param value The value to put into the array. The value should be a Boolean, Double, Integer, JSONArray,
|
|
||||||
* JSONObject, Long, or String, or the JSONObject.NULL object.
|
|
||||||
*
|
|
||||||
* @return this.
|
|
||||||
*
|
|
||||||
* @throws JSONException If the index is negative or if the the value is an invalid number.
|
|
||||||
*/
|
|
||||||
public JSONArray put(int index, Object value) throws JSONException {
|
|
||||||
JSONObject.testValidity(value);
|
|
||||||
if (index < 0) {
|
|
||||||
throw new JSONException("JSONArray[" + index + "] not found.");
|
|
||||||
}
|
|
||||||
if (index < this.length()) {
|
|
||||||
this.myArrayList.set(index, value);
|
|
||||||
} else {
|
|
||||||
while (index != this.length()) {
|
|
||||||
this.put(JSONObject.NULL);
|
|
||||||
}
|
|
||||||
this.put(value);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove an index and close the hole.
|
|
||||||
*
|
|
||||||
* @param index The index of the element to be removed.
|
|
||||||
*
|
|
||||||
* @return The value that was associated with the index, or null if there was no value.
|
|
||||||
*/
|
|
||||||
public Object remove(int index) {
|
|
||||||
Object o = this.opt(index);
|
|
||||||
this.myArrayList.remove(index);
|
|
||||||
return o;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Produce a JSONObject by combining a JSONArray of names with the values of this JSONArray.
|
|
||||||
*
|
|
||||||
* @param names A JSONArray containing a list of key strings. These will be paired with the values.
|
|
||||||
*
|
|
||||||
* @return A JSONObject, or null if there are no names or if this JSONArray has no values.
|
|
||||||
*
|
|
||||||
* @throws JSONException If any of the names are null.
|
|
||||||
*/
|
|
||||||
public JSONObject toJSONObject(JSONArray names) throws JSONException {
|
|
||||||
if (names == null || names.length() == 0 || this.length() == 0) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
JSONObject jo = new JSONObject();
|
|
||||||
for (int i = 0; i < names.length(); i += 1) {
|
|
||||||
jo.put(names.getString(i), this.opt(i));
|
|
||||||
}
|
|
||||||
return jo;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Make a JSON text of this JSONArray. For compactness, no unnecessary whitespace is added. If it is not possible to
|
|
||||||
* produce a syntactically correct JSON text then null will be returned instead. This could occur if the array
|
|
||||||
* contains an invalid number.
|
|
||||||
* <p/>
|
|
||||||
* Warning: This method assumes that the data structure is acyclical.
|
|
||||||
*
|
|
||||||
* @return a printable, displayable, transmittable representation of the array.
|
|
||||||
*/
|
|
||||||
public String toString() {
|
|
||||||
try {
|
|
||||||
return this.toString(0);
|
|
||||||
} catch (Exception e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Make a prettyprinted JSON text of this JSONArray. Warning: This method assumes that the data structure is
|
|
||||||
* acyclical.
|
|
||||||
*
|
|
||||||
* @param indentFactor The number of spaces to add to each level of indentation.
|
|
||||||
*
|
|
||||||
* @return a printable, displayable, transmittable representation of the object, beginning with
|
|
||||||
* <code>[</code> <small>(left bracket)</small> and ending with <code>]</code> <small>(right
|
|
||||||
* bracket)</small>.
|
|
||||||
*
|
|
||||||
* @throws JSONException
|
|
||||||
*/
|
|
||||||
public String toString(int indentFactor) throws JSONException {
|
|
||||||
StringWriter sw = new StringWriter();
|
|
||||||
synchronized (sw.getBuffer()) {
|
|
||||||
return this.write(sw, indentFactor, 0).toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Write the contents of the JSONArray as JSON text to a writer. For compactness, no whitespace is added.
|
|
||||||
* <p/>
|
|
||||||
* Warning: This method assumes that the data structure is acyclical.
|
|
||||||
*
|
|
||||||
* @return The writer.
|
|
||||||
*
|
|
||||||
* @throws JSONException
|
|
||||||
*/
|
|
||||||
public Writer write(Writer writer) throws JSONException {
|
|
||||||
return this.write(writer, 0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Write the contents of the JSONArray as JSON text to a writer. For compactness, no whitespace is added.
|
|
||||||
* <p/>
|
|
||||||
* Warning: This method assumes that the data structure is acyclical.
|
|
||||||
*
|
|
||||||
* @param indentFactor The number of spaces to add to each level of indentation.
|
|
||||||
* @param indent The indention of the top level.
|
|
||||||
*
|
|
||||||
* @return The writer.
|
|
||||||
*
|
|
||||||
* @throws JSONException
|
|
||||||
*/
|
|
||||||
Writer write(Writer writer, int indentFactor, int indent)
|
|
||||||
throws JSONException {
|
|
||||||
try {
|
|
||||||
boolean commanate = false;
|
|
||||||
int length = this.length();
|
|
||||||
writer.write('[');
|
|
||||||
|
|
||||||
if (length == 1) {
|
|
||||||
JSONObject.writeValue(writer, this.myArrayList.get(0),
|
|
||||||
indentFactor, indent);
|
|
||||||
} else if (length != 0) {
|
|
||||||
final int newindent = indent + indentFactor;
|
|
||||||
|
|
||||||
for (int i = 0; i < length; i += 1) {
|
|
||||||
if (commanate) {
|
|
||||||
writer.write(',');
|
|
||||||
}
|
|
||||||
if (indentFactor > 0) {
|
|
||||||
writer.write('\n');
|
|
||||||
}
|
|
||||||
JSONObject.indent(writer, newindent);
|
|
||||||
JSONObject.writeValue(writer, this.myArrayList.get(i),
|
|
||||||
indentFactor, newindent);
|
|
||||||
commanate = true;
|
|
||||||
}
|
|
||||||
if (indentFactor > 0) {
|
|
||||||
writer.write('\n');
|
|
||||||
}
|
|
||||||
JSONObject.indent(writer, indent);
|
|
||||||
}
|
|
||||||
writer.write(']');
|
|
||||||
return writer;
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new JSONException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
package org.json;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The JSONException is thrown by the JSON.org classes when things are amiss.
|
|
||||||
*
|
|
||||||
* @author JSON.org
|
|
||||||
* @version 2010-12-24
|
|
||||||
*/
|
|
||||||
public class JSONException extends Exception {
|
|
||||||
|
|
||||||
private static final long serialVersionUID = 0;
|
|
||||||
private Throwable cause;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructs a JSONException with an explanatory message.
|
|
||||||
*
|
|
||||||
* @param message Detail about the reason for the exception.
|
|
||||||
*/
|
|
||||||
public JSONException(String message) {
|
|
||||||
super(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public JSONException(Throwable cause) {
|
|
||||||
super(cause.getMessage());
|
|
||||||
this.cause = cause;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Throwable getCause() {
|
|
||||||
return this.cause;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,17 +0,0 @@
|
|||||||
package org.json;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The <code>JSONString</code> interface allows a <code>toJSONString()</code> method so that a class can change the
|
|
||||||
* behavior of <code>JSONObject.toString()</code>, <code>JSONArray.toString()</code>, and
|
|
||||||
* <code>JSONWriter.value(</code>Object<code>)</code>. The <code>toJSONString</code> method will be used instead of the
|
|
||||||
* default behavior of using the Object's <code>toString()</code> method and quoting the result.
|
|
||||||
*/
|
|
||||||
public interface JSONString {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The <code>toJSONString</code> method allows a class to produce its own JSON serialization.
|
|
||||||
*
|
|
||||||
* @return A strictly syntactically correct JSON text.
|
|
||||||
*/
|
|
||||||
public String toJSONString();
|
|
||||||
}
|
|
||||||
@@ -1,434 +0,0 @@
|
|||||||
package org.json;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* 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 JSONTokener takes a source string and extracts characters and tokens from it. It is used by the JSONObject and
|
|
||||||
* JSONArray constructors to parse JSON source strings.
|
|
||||||
*
|
|
||||||
* @author JSON.org
|
|
||||||
* @version 2012-02-16
|
|
||||||
*/
|
|
||||||
public class JSONTokener {
|
|
||||||
|
|
||||||
private long character;
|
|
||||||
private boolean eof;
|
|
||||||
private long index;
|
|
||||||
private long line;
|
|
||||||
private char previous;
|
|
||||||
private Reader reader;
|
|
||||||
private boolean usePrevious;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct a JSONTokener from a Reader.
|
|
||||||
*
|
|
||||||
* @param reader A reader.
|
|
||||||
*/
|
|
||||||
public JSONTokener(Reader reader) {
|
|
||||||
this.reader = reader.markSupported()
|
|
||||||
? reader
|
|
||||||
: new BufferedReader(reader);
|
|
||||||
this.eof = false;
|
|
||||||
this.usePrevious = false;
|
|
||||||
this.previous = 0;
|
|
||||||
this.index = 0;
|
|
||||||
this.character = 1;
|
|
||||||
this.line = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct a JSONTokener from an InputStream.
|
|
||||||
*/
|
|
||||||
public JSONTokener(InputStream inputStream) throws JSONException {
|
|
||||||
this(new InputStreamReader(inputStream));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct a JSONTokener from a string.
|
|
||||||
*
|
|
||||||
* @param s A source string.
|
|
||||||
*/
|
|
||||||
public JSONTokener(String s) {
|
|
||||||
this(new StringReader(s));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Back up one character. This provides a sort of lookahead capability, so that you can test for a digit or letter
|
|
||||||
* before attempting to parse the next number or identifier.
|
|
||||||
*/
|
|
||||||
public void back() throws JSONException {
|
|
||||||
if (this.usePrevious || this.index <= 0) {
|
|
||||||
throw new JSONException("Stepping back two steps is not supported");
|
|
||||||
}
|
|
||||||
this.index -= 1;
|
|
||||||
this.character -= 1;
|
|
||||||
this.usePrevious = true;
|
|
||||||
this.eof = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the hex value of a character (base16).
|
|
||||||
*
|
|
||||||
* @param c A character between '0' and '9' or between 'A' and 'F' or between 'a' and 'f'.
|
|
||||||
*
|
|
||||||
* @return An int between 0 and 15, or -1 if c was not a hex digit.
|
|
||||||
*/
|
|
||||||
public static int dehexchar(char c) {
|
|
||||||
if (c >= '0' && c <= '9') {
|
|
||||||
return c - '0';
|
|
||||||
}
|
|
||||||
if (c >= 'A' && c <= 'F') {
|
|
||||||
return c - ('A' - 10);
|
|
||||||
}
|
|
||||||
if (c >= 'a' && c <= 'f') {
|
|
||||||
return c - ('a' - 10);
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean end() {
|
|
||||||
return this.eof && !this.usePrevious;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determine if the source string still contains characters that next() can consume.
|
|
||||||
*
|
|
||||||
* @return true if not yet at the end of the source.
|
|
||||||
*/
|
|
||||||
public boolean more() throws JSONException {
|
|
||||||
this.next();
|
|
||||||
if (this.end()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
this.back();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the next character in the source string.
|
|
||||||
*
|
|
||||||
* @return The next character, or 0 if past the end of the source string.
|
|
||||||
*/
|
|
||||||
public char next() throws JSONException {
|
|
||||||
int c;
|
|
||||||
if (this.usePrevious) {
|
|
||||||
this.usePrevious = false;
|
|
||||||
c = this.previous;
|
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
c = this.reader.read();
|
|
||||||
} catch (IOException exception) {
|
|
||||||
throw new JSONException(exception);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c <= 0) { // End of stream
|
|
||||||
this.eof = true;
|
|
||||||
c = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.index += 1;
|
|
||||||
if (this.previous == '\r') {
|
|
||||||
this.line += 1;
|
|
||||||
this.character = c == '\n' ? 0 : 1;
|
|
||||||
} else if (c == '\n') {
|
|
||||||
this.line += 1;
|
|
||||||
this.character = 0;
|
|
||||||
} else {
|
|
||||||
this.character += 1;
|
|
||||||
}
|
|
||||||
this.previous = (char) c;
|
|
||||||
return this.previous;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Consume the next character, and check that it matches a specified character.
|
|
||||||
*
|
|
||||||
* @param c The character to match.
|
|
||||||
*
|
|
||||||
* @return The character.
|
|
||||||
*
|
|
||||||
* @throws JSONException if the character does not match.
|
|
||||||
*/
|
|
||||||
public char next(char c) throws JSONException {
|
|
||||||
char n = this.next();
|
|
||||||
if (n != c) {
|
|
||||||
throw this.syntaxError("Expected '" + c + "' and instead saw '" +
|
|
||||||
n + "'");
|
|
||||||
}
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the next n characters.
|
|
||||||
*
|
|
||||||
* @param n The number of characters to take.
|
|
||||||
*
|
|
||||||
* @return A string of n characters.
|
|
||||||
*
|
|
||||||
* @throws JSONException Substring bounds error if there are not n characters remaining in the source string.
|
|
||||||
*/
|
|
||||||
public String next(int n) throws JSONException {
|
|
||||||
if (n == 0) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
char[] chars = new char[n];
|
|
||||||
int pos = 0;
|
|
||||||
|
|
||||||
while (pos < n) {
|
|
||||||
chars[pos] = this.next();
|
|
||||||
if (this.end()) {
|
|
||||||
throw this.syntaxError("Substring bounds error");
|
|
||||||
}
|
|
||||||
pos += 1;
|
|
||||||
}
|
|
||||||
return new String(chars);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the next char in the string, skipping whitespace.
|
|
||||||
*
|
|
||||||
* @return A character, or 0 if there are no more characters.
|
|
||||||
*
|
|
||||||
* @throws JSONException
|
|
||||||
*/
|
|
||||||
public char nextClean() throws JSONException {
|
|
||||||
for (; ; ) {
|
|
||||||
char c = this.next();
|
|
||||||
if (c == 0 || c > ' ') {
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the characters up to the next close quote character. Backslash processing is done. The formal JSON format
|
|
||||||
* does not allow strings in single quotes, but an implementation is allowed to accept them.
|
|
||||||
*
|
|
||||||
* @param quote The quoting character, either <code>"</code> <small>(double quote)</small> or
|
|
||||||
* <code>'</code> <small>(single quote)</small>.
|
|
||||||
*
|
|
||||||
* @return A String.
|
|
||||||
*
|
|
||||||
* @throws JSONException Unterminated string.
|
|
||||||
*/
|
|
||||||
public String nextString(char quote) throws JSONException {
|
|
||||||
char c;
|
|
||||||
StringBuffer sb = new StringBuffer();
|
|
||||||
for (; ; ) {
|
|
||||||
c = this.next();
|
|
||||||
switch (c) {
|
|
||||||
case 0:
|
|
||||||
case '\n':
|
|
||||||
case '\r':
|
|
||||||
throw this.syntaxError("Unterminated string");
|
|
||||||
case '\\':
|
|
||||||
c = this.next();
|
|
||||||
switch (c) {
|
|
||||||
case 'b':
|
|
||||||
sb.append('\b');
|
|
||||||
break;
|
|
||||||
case 't':
|
|
||||||
sb.append('\t');
|
|
||||||
break;
|
|
||||||
case 'n':
|
|
||||||
sb.append('\n');
|
|
||||||
break;
|
|
||||||
case 'f':
|
|
||||||
sb.append('\f');
|
|
||||||
break;
|
|
||||||
case 'r':
|
|
||||||
sb.append('\r');
|
|
||||||
break;
|
|
||||||
case 'u':
|
|
||||||
sb.append((char) Integer.parseInt(this.next(4), 16));
|
|
||||||
break;
|
|
||||||
case '"':
|
|
||||||
case '\'':
|
|
||||||
case '\\':
|
|
||||||
case '/':
|
|
||||||
sb.append(c);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw this.syntaxError("Illegal escape.");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
if (c == quote) {
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
sb.append(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the text up but not including the specified character or the end of line, whichever comes first.
|
|
||||||
*
|
|
||||||
* @param delimiter A delimiter character.
|
|
||||||
*
|
|
||||||
* @return A string.
|
|
||||||
*/
|
|
||||||
public String nextTo(char delimiter) throws JSONException {
|
|
||||||
StringBuffer sb = new StringBuffer();
|
|
||||||
for (; ; ) {
|
|
||||||
char c = this.next();
|
|
||||||
if (c == delimiter || c == 0 || c == '\n' || c == '\r') {
|
|
||||||
if (c != 0) {
|
|
||||||
this.back();
|
|
||||||
}
|
|
||||||
return sb.toString().trim();
|
|
||||||
}
|
|
||||||
sb.append(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the text up but not including one of the specified delimiter characters or the end of line, whichever comes
|
|
||||||
* first.
|
|
||||||
*
|
|
||||||
* @param delimiters A set of delimiter characters.
|
|
||||||
*
|
|
||||||
* @return A string, trimmed.
|
|
||||||
*/
|
|
||||||
public String nextTo(String delimiters) throws JSONException {
|
|
||||||
char c;
|
|
||||||
StringBuffer sb = new StringBuffer();
|
|
||||||
for (; ; ) {
|
|
||||||
c = this.next();
|
|
||||||
if (delimiters.indexOf(c) >= 0 || c == 0 ||
|
|
||||||
c == '\n' || c == '\r') {
|
|
||||||
if (c != 0) {
|
|
||||||
this.back();
|
|
||||||
}
|
|
||||||
return sb.toString().trim();
|
|
||||||
}
|
|
||||||
sb.append(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the next value. The value can be a Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the
|
|
||||||
* JSONObject.NULL object.
|
|
||||||
*
|
|
||||||
* @return An object.
|
|
||||||
*
|
|
||||||
* @throws JSONException If syntax error.
|
|
||||||
*/
|
|
||||||
public Object nextValue() throws JSONException {
|
|
||||||
char c = this.nextClean();
|
|
||||||
String string;
|
|
||||||
|
|
||||||
switch (c) {
|
|
||||||
case '"':
|
|
||||||
case '\'':
|
|
||||||
return this.nextString(c);
|
|
||||||
case '{':
|
|
||||||
this.back();
|
|
||||||
return new JSONObject(this);
|
|
||||||
case '[':
|
|
||||||
this.back();
|
|
||||||
return new JSONArray(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Handle unquoted text. This could be the values true, false, or
|
|
||||||
* null, or it can be a number. An implementation (such as this one)
|
|
||||||
* is allowed to also accept non-standard forms.
|
|
||||||
*
|
|
||||||
* Accumulate characters until we reach the end of the text or a
|
|
||||||
* formatting character.
|
|
||||||
*/
|
|
||||||
|
|
||||||
StringBuffer sb = new StringBuffer();
|
|
||||||
while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) {
|
|
||||||
sb.append(c);
|
|
||||||
c = this.next();
|
|
||||||
}
|
|
||||||
this.back();
|
|
||||||
|
|
||||||
string = sb.toString().trim();
|
|
||||||
if ("".equals(string)) {
|
|
||||||
throw this.syntaxError("Missing value");
|
|
||||||
}
|
|
||||||
return JSONObject.stringToValue(string);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Skip characters until the next character is the requested character. If the requested character is not found, no
|
|
||||||
* characters are skipped.
|
|
||||||
*
|
|
||||||
* @param to A character to skip to.
|
|
||||||
*
|
|
||||||
* @return The requested character, or zero if the requested character is not found.
|
|
||||||
*/
|
|
||||||
public char skipTo(char to) throws JSONException {
|
|
||||||
char c;
|
|
||||||
try {
|
|
||||||
long startIndex = this.index;
|
|
||||||
long startCharacter = this.character;
|
|
||||||
long startLine = this.line;
|
|
||||||
this.reader.mark(1000000);
|
|
||||||
do {
|
|
||||||
c = this.next();
|
|
||||||
if (c == 0) {
|
|
||||||
this.reader.reset();
|
|
||||||
this.index = startIndex;
|
|
||||||
this.character = startCharacter;
|
|
||||||
this.line = startLine;
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
} while (c != to);
|
|
||||||
} catch (IOException exc) {
|
|
||||||
throw new JSONException(exc);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.back();
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Make a JSONException to signal a syntax error.
|
|
||||||
*
|
|
||||||
* @param message The error message.
|
|
||||||
*
|
|
||||||
* @return A JSONException object, suitable for throwing
|
|
||||||
*/
|
|
||||||
public JSONException syntaxError(String message) {
|
|
||||||
return new JSONException(message + this.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Make a printable string of this JSONTokener.
|
|
||||||
*
|
|
||||||
* @return " at {index} [character {character} line {line}]"
|
|
||||||
*/
|
|
||||||
public String toString() {
|
|
||||||
return " at " + this.index + " [character " + this.character + " line " +
|
|
||||||
this.line + "]";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user