entry : map.entrySet()) {
- Object value = entry.getValue();
- if (value != null) {
- this.map.put(entry.getKey(), wrap(value));
- }
- }
- }
- }
-
- /**
- * Construct a JSONObject from an Object using bean getters. It reflects on all of the public methods of the object.
- * For each of the methods with no parameters and a name starting with "get"
or "is"
- * followed by an uppercase letter, the method is invoked, and a key and the value returned from the getter method
- * are put into the new JSONObject.
- *
- * The key is formed by removing the "get"
or "is"
prefix. If the second remaining
- * character is not upper case, then the first character is converted to lower case.
- *
- * For example, if an object has a method named "getPluginName"
, and if the result of calling
- * object.getPluginName()
is "Larry Fine"
, then the JSONObject will contain "name": "Larry
- * Fine"
.
- *
- * @param bean An object that has getter methods that should be used to make a JSONObject.
- */
- public JSONObject(Object bean) {
- this();
- populateMap(bean);
- }
-
- /**
- * Construct a JSONObject from an Object, using reflection to find the public members. The resulting JSONObject's
- * keys will be the strings from the names array, and the values will be the field values associated with those keys
- * in the object. If a key is not found or not visible, then it will not be copied into the new JSONObject.
- *
- * @param object An object that has fields that should be used to make a JSONObject.
- * @param names An array of strings, the names of the fields to be obtained from the object.
- */
- public JSONObject(Object object, String[] names) {
- this();
- Class c = object.getClass();
- for (String name : names) {
- try {
- putOpt(name, c.getField(name).get(object));
- } catch (JSONException | SecurityException | NoSuchFieldException | IllegalArgumentException | IllegalAccessException ignore) {
- }
- }
- }
-
- /**
- * Construct a JSONObject from a source JSON text string. This is the most commonly used JSONObject constructor.
- *
- * @param source A string beginning with {
(left brace) and ending with
- * }
(right brace) .
- * @throws JSONException If there is a syntax error in the source string or a duplicated key.
- */
- public JSONObject(String source) throws JSONException {
- this(new JSONTokener(source));
- }
-
- /**
- * Construct a JSONObject from a ResourceBundle.
- *
- * @param baseName The ResourceBundle base name.
- * @param locale The Locale to load the ResourceBundle for.
- * @throws JSONException If any JSONExceptions are detected.
- */
- public JSONObject(String baseName, Locale locale) throws JSONException {
- this();
- ResourceBundle bundle = ResourceBundle
- .getBundle(baseName, locale, Thread.currentThread().getContextClassLoader());
- // Iterate through the keys in the bundle.
- Enumeration keys = bundle.getKeys();
- while (keys.hasMoreElements()) {
- Object key = keys.nextElement();
- if (key != null) {
- // Go through the path, ensuring that there is a nested
- // JSONObject for each
- // segment except the last. Add the value using the last
- // segment's name into
- // the deepest nested JSONObject.
- String[] path = ((String) key).split("\\.");
- int last = path.length - 1;
- JSONObject target = this;
- for (int i = 0; i < last; i += 1) {
- String segment = path[i];
- JSONObject nextTarget = target.optJSONObject(segment);
- if (nextTarget == null) {
- nextTarget = new JSONObject();
- target.put(segment, nextTarget);
- }
- target = nextTarget;
- }
- target.put(path[last], bundle.getString((String) key));
- }
- }
- }
-
- /**
- * Produce a string from a double. The string "null" will be returned if the number is not finite.
- *
- * @param d A double.
- * @return A String.
- */
- public static String doubleToString(double d) {
- if (Double.isInfinite(d) || Double.isNaN(d)) {
- return "null";
- }
- // Shave off trailing zeros and decimal point, if possible.
- String string = Double.toString(d);
- if ((string.indexOf('.') > 0) && (string.indexOf('e') < 0) && (string.indexOf('E') < 0)) {
- while (string.endsWith("0")) {
- string = string.substring(0, string.length() - 1);
- }
- if (string.endsWith(".")) {
- string = string.substring(0, string.length() - 1);
- }
- }
- return string;
- }
-
- /**
- * Get an array of field names from a JSONObject.
- *
- * @return An array of field names, or null if there are no names.
- */
- public static String[] getNames(JSONObject jo) {
- int length = jo.length();
- if (length == 0) {
- return null;
- }
- Iterator iterator = jo.keys();
- String[] names = new String[length];
- int i = 0;
- while (iterator.hasNext()) {
- names[i] = iterator.next();
- i += 1;
- }
- return names;
- }
-
- /**
- * Get an array of field names from an Object.
- *
- * @return An array of field names, or null if there are no names.
- */
- public static String[] getNames(Object object) {
- if (object == null) {
- return null;
- }
- Class klass = object.getClass();
- Field[] fields = klass.getFields();
- int length = fields.length;
- if (length == 0) {
- return null;
- }
- return IntStream.range(0, length).mapToObj(i -> fields[i].getName()).toArray(String[]::new);
- }
-
- /**
- * Produce a string from a Number.
- *
- * @param number A Number
- * @return A String.
- * @throws JSONException If n is a non-finite number.
- */
- public static String numberToString(Number number) throws JSONException {
- if (number == null) {
- throw new JSONException("Null pointer");
- }
- testValidity(number);
- // Shave off trailing zeros and decimal point, if possible.
- String string = number.toString();
- if ((string.indexOf('.') > 0) && (string.indexOf('e') < 0) && (string.indexOf('E') < 0)) {
- while (string.endsWith("0")) {
- string = string.substring(0, string.length() - 1);
- }
- if (string.endsWith(".")) {
- string = string.substring(0, string.length() - 1);
- }
- }
- return string;
- }
-
- /**
- * Produce a string in double quotes with backslash sequences in all the right places. A backslash will be inserted
- * control character or an unescaped quote or backslash.
- *
- * @param string A String
- * @return A String correctly formatted for insertion in a JSON text.
- */
- public static String quote(String string) {
- StringWriter sw = new StringWriter();
- synchronized (sw.getBuffer()) {
- try {
- return quote(string, sw).toString();
- } catch (IOException ignored) {
- // will never happen - we are writing to a string writer
- return "";
- }
- }
- }
-
- public static Writer quote(String string, Writer w) throws IOException {
- if ((string == null) || string.isEmpty()) {
- w.write("\"\"");
- return w;
- }
- char b;
- char c = 0;
- String hhhh;
- int i;
- int len = string.length();
- w.write('"');
- for (i = 0; i < len; i += 1) {
- b = c;
- c = string.charAt(i);
- switch (c) {
- case '\\':
- case '"':
- w.write('\\');
- w.write(c);
- break;
- case '/':
- if (b == '<') {
- w.write('\\');
- }
- w.write(c);
- break;
- case '\b':
- w.write("\\b");
- break;
- case '\t':
- w.write("\\t");
- break;
- case '\n':
- w.write("\\n");
- break;
- case '\f':
- w.write("\\f");
- break;
- case '\r':
- w.write("\\r");
- break;
- default:
- if ((c < ' ') || ((c >= '\u0080') && (c < '\u00a0')) || ((c >= '\u2000') && (c
- < '\u2100'))) {
- w.write("\\u");
- hhhh = Integer.toHexString(c);
- w.write("0000", 0, 4 - hhhh.length());
- w.write(hhhh);
- } else {
- w.write(c);
- }
- }
- }
- w.write('"');
- return w;
- }
-
- /**
- * Try to convert a string into a number, boolean, or null. If the string can't be converted, return the string.
- *
- * @param string A String.
- * @return A simple JSON value.
- */
- public static Object stringToValue(String string) {
- Double d;
- if (string.isEmpty()) {
- return string;
- }
- if (string.equalsIgnoreCase("true")) {
- return Boolean.TRUE;
- }
- if (string.equalsIgnoreCase("false")) {
- return Boolean.FALSE;
- }
- if (string.equalsIgnoreCase("null")) {
- return JSONObject.NULL;
- }
- /*
- * If it might be a number, try converting it. If a number cannot be
- * produced, then the value will just be a string.
- */
- char b = string.charAt(0);
- if (((b >= '0') && (b <= '9')) || (b == '-')) {
- try {
- if ((string.indexOf('.') > -1) || (string.indexOf('e') > -1) || (string.indexOf('E')
- > -1)) {
- d = Double.valueOf(string);
- if (!d.isInfinite() && !d.isNaN()) {
- return d;
- }
- } else {
- Long myLong = Long.valueOf(string);
- if (string.equals(myLong.toString())) {
- if (myLong == myLong.intValue()) {
- return myLong.intValue();
- } else {
- return myLong;
- }
- }
- }
- } catch (NumberFormatException ignore) {
- }
- }
- return string;
- }
-
- /**
- * Throw an exception if the object is a NaN or infinite number.
- *
- * @param o The object to test.
- * @throws JSONException If o is a non-finite number.
- */
- public static void testValidity(Object o) throws JSONException {
- if (o != null) {
- if (o instanceof Double) {
- if (((Double) o).isInfinite() || ((Double) o).isNaN()) {
- throw new JSONException("JSON does not allow non-finite numbers.");
- }
- } else if (o instanceof Float) {
- if (((Float) o).isInfinite() || ((Float) o).isNaN()) {
- throw new JSONException("JSON does not allow non-finite numbers.");
- }
- }
- }
- }
-
- /**
- * Make a JSON text of an Object value. If the object has an value.toJSONString() method, then that method will be
- * used to produce the JSON text. The method is required to produce a strictly conforming text. If the object does
- * not contain a toJSONString method (which is the most common case), then a text will be produced by other means.
- * If the value is an array or Collection, then a JSONArray will be made from it and its toJSONString method will be
- * called. If the value is a MAP, then a JSONObject will be made from it and its toJSONString method will be called.
- * Otherwise, the value's toString method will be called, and the result will be quoted.
- *
- *
- * Warning: This method assumes that the data structure is acyclical.
- *
- * @param value The value to be serialized.
- * @return a printable, displayable, transmittable representation of the object, beginning with
- * {
(left brace) and ending with }
(right
- * brace) .
- * @throws JSONException If the value is or contains an invalid number.
- */
- public static String valueToString(Object value) throws JSONException {
- if ((value == null) || value.equals(null)) {
- return "null";
- }
- if (value instanceof JSONString) {
- Object object;
- try {
- object = ((JSONString) value).toJSONString();
- } catch (Exception e) {
- throw new JSONException(e);
- }
- if (object != null) {
- return (String) object;
- }
- throw new JSONException("Bad value from toJSONString: " + object);
- }
- if (value instanceof Number) {
- return numberToString((Number) value);
- }
- if ((value instanceof Boolean) || (value instanceof JSONObject)
- || (value instanceof JSONArray)) {
- return value.toString();
- }
- if (value instanceof Map) {
- return new JSONObject((Map) value).toString();
- }
- if (value instanceof Collection) {
- return new JSONArray((Collection) value).toString();
- }
- if (value.getClass().isArray()) {
- return new JSONArray(value).toString();
- }
- return quote(value.toString());
- }
-
- /**
- * Wrap an object, if necessary. If the object is null, return the NULL object. If it is an array or collection,
- * wrap it in a JSONArray. If it is a map, wrap it in a JSONObject. If it is a standard property (Double, String, et
- * al) then it is already wrapped. Otherwise, if it comes from one of the java packages, turn it into a string. And
- * if it doesn't, try to wrap it in a JSONObject. If the wrapping fails, then null is returned.
- *
- * @param object The object to wrap
- * @return The wrapped value
- */
- public static Object wrap(Object object) {
- try {
- if (object == null) {
- return NULL;
- }
- if ((object instanceof JSONObject) || (object instanceof JSONArray) || NULL
- .equals(object) || (object instanceof JSONString) || (object instanceof Byte)
- || (object instanceof Character) || (object instanceof Short)
- || (object instanceof Integer) || (object instanceof Long)
- || (object instanceof Boolean) || (object instanceof Float)
- || (object instanceof Double) || (object instanceof String)) {
- return object;
- }
- if (object instanceof Collection) {
- return new JSONArray((Collection) object);
- }
- if (object.getClass().isArray()) {
- return new JSONArray(object);
- }
- if (object instanceof Map) {
- return new JSONObject((Map) object);
- }
- Package objectPackage = object.getClass().getPackage();
-
- String objectPackageName = objectPackage != null ? objectPackage.getName() : "";
-
- if (objectPackageName.startsWith("java.") || objectPackageName.startsWith("javax.") || (
- object.getClass().getClassLoader() == null)) {
- return object.toString();
- }
- return new JSONObject(object);
- } catch (JSONException ignored) {
- return null;
- }
- }
-
- static final Writer writeValue(Writer writer, Object value, int indentFactor, int indent)
- throws JSONException, IOException {
- if ((value == null) || value.equals(null)) {
- writer.write("null");
- } else if (value instanceof JSONObject) {
- ((JSONObject) value).write(writer, indentFactor, indent);
- } else if (value instanceof JSONArray) {
- ((JSONArray) value).write(writer, indentFactor, indent);
- } else if (value instanceof Map) {
- new JSONObject((Map) value).write(writer, indentFactor, indent);
- } else if (value instanceof Collection) {
- new JSONArray((Collection) value).write(writer, indentFactor, indent);
- } else if (value.getClass().isArray()) {
- new JSONArray(value).write(writer, indentFactor, indent);
- } else if (value instanceof Number) {
- writer.write(numberToString((Number) value));
- } else if (value instanceof Boolean) {
- writer.write(value.toString());
- } else if (value instanceof JSONString) {
- Object o;
- try {
- o = ((JSONString) value).toJSONString();
- } catch (Exception e) {
- throw new JSONException(e);
- }
- writer.write(o != null ? o.toString() : quote(value.toString()));
- } else {
- quote(value.toString(), writer);
- }
- return writer;
- }
-
- static final void indent(Writer writer, int indent) throws IOException {
- for (int i = 0; i < indent; i += 1) {
- writer.write(' ');
- }
- }
-
- /**
- * Accumulate values under a key. It is similar to the put method except that if there is already an object stored
- * under the key then a JSONArray is stored under the key to hold all of the accumulated values. If there is already
- * a JSONArray, then the new value is appended to it. In contrast, the put method replaces the previous value.
- *
- * If only one value is accumulated that is not a JSONArray, then the result will be the same as using put. But if
- * multiple values are accumulated, then the result will be like append.
- *
- * @param key A key string.
- * @param value An object to be accumulated under the key.
- * @return this.
- * @throws JSONException If the value is an invalid number or if the key is null.
- */
- public JSONObject accumulate(String key, Object value) throws JSONException {
- testValidity(value);
- Object object = opt(key);
- if (object == null) {
- this.put(key, value instanceof JSONArray ? new JSONArray().put(value) : value);
- } else if (object instanceof JSONArray) {
- ((JSONArray) object).put(value);
- } else {
- this.put(key, new JSONArray().put(object).put(value));
- }
- return this;
- }
-
- /**
- * Append values to the array under a key. If the key does not exist in the JSONObject, then the key is put in the
- * JSONObject with its value being a JSONArray containing the value parameter. If the key was already associated
- * with a JSONArray, then the value parameter is appended to it.
- *
- * @param key A key string.
- * @param value An object to be accumulated under the key.
- * @return this.
- * @throws JSONException If the key is null or if the current value associated with the key is not a JSONArray.
- */
- public JSONObject append(String key, Object value) throws JSONException {
- testValidity(value);
- Object object = opt(key);
- if (object == null) {
- this.put(key, new JSONArray().put(value));
- } else if (object instanceof JSONArray) {
- this.put(key, ((JSONArray) object).put(value));
- } else {
- throw new JSONException("JSONObject[" + key + "] is not a JSONArray.");
- }
- return this;
- }
-
- /**
- * Get the value object associated with a key.
- *
- * @param key A key string.
- * @return The object associated with the key.
- * @throws JSONException if the key is not found.
- */
- public Object get(String key) throws JSONException {
- if (key == null) {
- throw new JSONException("Null key.");
- }
- Object object = opt(key);
- if (object == null) {
- throw new JSONException("JSONObject[" + quote(key) + "] not found.");
- }
- return object;
- }
-
- /**
- * Get the boolean value associated with a key.
- *
- * @param key A key string.
- * @return The truth.
- * @throws JSONException if the value is not a Boolean or the String "true" or "false".
- */
- public boolean getBoolean(String key) throws JSONException {
- Object object = get(key);
- 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("JSONObject[" + quote(key) + "] is not a Boolean.");
- }
-
- /**
- * Get the double value associated with a key.
- *
- * @param key A key string.
- * @return The numeric value.
- * @throws JSONException if the key is not found or if the value is not a Number object and cannot be converted to a
- * number.
- */
- public double getDouble(String key) throws JSONException {
- Object object = get(key);
- try {
- return object instanceof Number ?
- ((Number) object).doubleValue() :
- Double.parseDouble((String) object);
- } catch (NumberFormatException ignored) {
- throw new JSONException("JSONObject[" + quote(key) + "] is not a number.");
- }
- }
-
- /**
- * Get the int value associated with a key.
- *
- * @param key A key string.
- * @return The integer value.
- * @throws JSONException if the key is not found or if the value cannot be converted to an integer.
- */
- public int getInt(String key) throws JSONException {
- Object object = get(key);
- try {
- return object instanceof Number ?
- ((Number) object).intValue() :
- Integer.parseInt((String) object);
- } catch (NumberFormatException ignored) {
- throw new JSONException("JSONObject[" + quote(key) + "] is not an int.");
- }
- }
-
- /**
- * Get the JSONArray value associated with a key.
- *
- * @param key A key string.
- * @return A JSONArray which is the value.
- * @throws JSONException if the key is not found or if the value is not a JSONArray.
- */
- public JSONArray getJSONArray(String key) throws JSONException {
- Object object = get(key);
- if (object instanceof JSONArray) {
- return (JSONArray) object;
- }
- throw new JSONException("JSONObject[" + quote(key) + "] is not a JSONArray.");
- }
-
- /**
- * Get the JSONObject value associated with a key.
- *
- * @param key A key string.
- * @return A JSONObject which is the value.
- * @throws JSONException if the key is not found or if the value is not a JSONObject.
- */
- public JSONObject getJSONObject(String key) throws JSONException {
- Object object = get(key);
- if (object instanceof JSONObject) {
- return (JSONObject) object;
- }
- throw new JSONException("JSONObject[" + quote(key) + "] is not a JSONObject.");
- }
-
- /**
- * Get the long value associated with a key.
- *
- * @param key A key string.
- * @return The long value.
- * @throws JSONException if the key is not found or if the value cannot be converted to a long.
- */
- public long getLong(String key) throws JSONException {
- Object object = get(key);
- try {
- return object instanceof Number ?
- ((Number) object).longValue() :
- Long.parseLong((String) object);
- } catch (NumberFormatException ignored) {
- throw new JSONException("JSONObject[" + quote(key) + "] is not a long.");
- }
- }
-
- /**
- * Get the string associated with a key.
- *
- * @param key A key string.
- * @return A string which is the value.
- * @throws JSONException if there is no string value for the key.
- */
- public String getString(String key) throws JSONException {
- Object object = get(key);
- if (object instanceof String) {
- return (String) object;
- }
- throw new JSONException("JSONObject[" + quote(key) + "] not a string.");
- }
-
- /**
- * Determine if the JSONObject contains a specific key.
- *
- * @param key A key string.
- * @return true if the key exists in the JSONObject.
- */
- public boolean has(String key) {
- return this.map.containsKey(key);
- }
-
- /**
- * Increment a property of a JSONObject. If there is no such property, create one with a value of 1. If there is
- * such a property, and if it is an Integer, Long, Double, or Float, then add one to it.
- *
- * @param key A key string.
- * @return this.
- * @throws JSONException If there is already a property with this name that is not an Integer, Long, Double, or
- * Float.
- */
- public JSONObject increment(String key) throws JSONException {
- Object value = opt(key);
- if (value == null) {
- this.put(key, 1);
- } else if (value instanceof Integer) {
- this.put(key, (Integer) value + 1);
- } else if (value instanceof Long) {
- this.put(key, (Long) value + 1);
- } else if (value instanceof Double) {
- this.put(key, (Double) value + 1);
- } else if (value instanceof Float) {
- this.put(key, (Float) value + 1);
- } else {
- throw new JSONException("Unable to increment [" + quote(key) + "].");
- }
- return this;
- }
-
- /**
- * Determine if the value associated with the key is null or if there is no value.
- *
- * @param key A key string.
- * @return true if there is no value associated with the key or if the value is the JSONObject.NULL object.
- */
- public boolean isNull(String key) {
- return JSONObject.NULL.equals(opt(key));
- }
-
- /**
- * Get an enumeration of the keys of the JSONObject.
- *
- * @return An iterator of the keys.
- */
- public Iterator keys() {
- return keySet().iterator();
- }
-
- /**
- * Get a set of keys of the JSONObject.
- *
- * @return A keySet.
- */
- public Set keySet() {
- return this.map.keySet();
- }
-
- /**
- * Get the number of keys stored in the JSONObject.
- *
- * @return The number of keys in the JSONObject.
- */
- public int length() {
- return this.map.size();
- }
-
- /**
- * Produce a JSONArray containing the names of the elements of this JSONObject.
- *
- * @return A JSONArray containing the key strings, or null if the JSONObject is empty.
- */
- public JSONArray names() {
- JSONArray ja = new JSONArray();
- Iterator keys = keys();
- while (keys.hasNext()) {
- ja.put(keys.next());
- }
- return ja.length() == 0 ? null : ja;
- }
-
- /**
- * Get an optional value associated with a key.
- *
- * @param key A key string.
- * @return An object which is the value, or null if there is no value.
- */
- public Object opt(String key) {
- return key == null ? null : this.map.get(key);
- }
-
- /**
- * Get an optional boolean associated with a key. It returns false if there is no such key, or if the value is not
- * Boolean.TRUE or the String "true".
- *
- * @param key A key string.
- * @return The truth.
- */
- public boolean optBoolean(String key) {
- return this.optBoolean(key, false);
- }
-
- /**
- * Get an optional boolean associated with a key. It returns the defaultValue if there is no such key, or if it is
- * not a Boolean or the String "true" or "false" (case insensitive).
- *
- * @param key A key string.
- * @param defaultValue The default.
- * @return The truth.
- */
- public boolean optBoolean(String key, boolean defaultValue) {
- try {
- return getBoolean(key);
- } catch (JSONException ignored) {
- return defaultValue;
- }
- }
-
- /**
- * Get an optional double associated with a key, or NaN if there is no such key or if its value is not a number. If
- * the value is a string, an attempt will be made to evaluate it as a number.
- *
- * @param key A string which is the key.
- * @return An object which is the value.
- */
- public double optDouble(String key) {
- return this.optDouble(key, Double.NaN);
- }
-
- /**
- * Get an optional double associated with a key, or the defaultValue if there is no such key or if its value is not
- * a number. If the value is a string, an attempt will be made to evaluate it as a number.
- *
- * @param key A key string.
- * @param defaultValue The default.
- * @return An object which is the value.
- */
- public double optDouble(String key, double defaultValue) {
- try {
- return getDouble(key);
- } catch (JSONException ignored) {
- return defaultValue;
- }
- }
-
- /**
- * Get an optional int value associated with a key, or zero if there is no such key or if the value is not a number.
- * If the value is a string, an attempt will be made to evaluate it as a number.
- *
- * @param key A key string.
- * @return An object which is the value.
- */
- public int optInt(String key) {
- return this.optInt(key, 0);
- }
-
- /**
- * Get an optional int value associated with a key, or the default if there is no such key or if the value is not a
- * number. If the value is a string, an attempt will be made to evaluate it as a number.
- *
- * @param key A key string.
- * @param defaultValue The default.
- * @return An object which is the value.
- */
- public int optInt(String key, int defaultValue) {
- try {
- return getInt(key);
- } catch (JSONException ignored) {
- return defaultValue;
- }
- }
-
- /**
- * Get an optional JSONArray associated with a key. It returns null if there is no such key, or if its value is not
- * a JSONArray.
- *
- * @param key A key string.
- * @return A JSONArray which is the value.
- */
- public JSONArray optJSONArray(String key) {
- Object o = opt(key);
- return o instanceof JSONArray ? (JSONArray) o : null;
- }
-
- /**
- * Get an optional JSONObject associated with a key. It returns null if there is no such key, or if its value is not
- * a JSONObject.
- *
- * @param key A key string.
- * @return A JSONObject which is the value.
- */
- public JSONObject optJSONObject(String key) {
- Object object = opt(key);
- return object instanceof JSONObject ? (JSONObject) object : null;
- }
-
- /**
- * Get an optional long value associated with a key, or zero if there is no such key or if the value is not a
- * number. If the value is a string, an attempt will be made to evaluate it as a number.
- *
- * @param key A key string.
- * @return An object which is the value.
- */
- public long optLong(String key) {
- return this.optLong(key, 0);
- }
-
- /**
- * Get an optional long value associated with a key, or the default if there is no such key or if the value is not a
- * number. If the value is a string, an attempt will be made to evaluate it as a number.
- *
- * @param key A key string.
- * @param defaultValue The default.
- * @return An object which is the value.
- */
- public long optLong(String key, long defaultValue) {
- try {
- return getLong(key);
- } catch (JSONException ignored) {
- return defaultValue;
- }
- }
-
- /**
- * Get an optional string associated with a key. It returns an empty string if there is no such key. If the value is
- * not a string and is not null, then it is converted to a string.
- *
- * @param key A key string.
- * @return A string which is the value.
- */
- public String optString(String key) {
- return this.optString(key, "");
- }
-
- /**
- * Get an optional string associated with a key. It returns the defaultValue if there is no such key.
- *
- * @param key A key string.
- * @param defaultValue The default.
- * @return A string which is the value.
- */
- public String optString(String key, String defaultValue) {
- Object object = opt(key);
- return NULL.equals(object) ? defaultValue : object.toString();
- }
-
- private void populateMap(Object bean) {
- Class klass = bean.getClass();
- // If klass is a System class then set includeSuperClass to false.
- boolean includeSuperClass = klass.getClassLoader() != null;
- Method[] methods = includeSuperClass ? klass.getMethods() : klass.getDeclaredMethods();
- for (Method method : methods) {
- try {
- if (Modifier.isPublic(method.getModifiers())) {
- String name = method.getName();
- String key = "";
- if (name.startsWith("get")) {
- if ("getClass".equals(name) || "getDeclaringClass".equals(name)) {
- key = "";
- } else {
- key = name.substring(3);
- }
- } else if (name.startsWith("is")) {
- key = name.substring(2);
- }
- if (!key.isEmpty() && Character.isUpperCase(key.charAt(0)) && (
- method.getParameterTypes().length == 0)) {
- if (key.length() == 1) {
- key = key.toLowerCase();
- } else if (!Character.isUpperCase(key.charAt(1))) {
- key = key.substring(0, 1).toLowerCase() + key.substring(1);
- }
- Object result = method.invoke(bean, (Object[]) null);
- if (result != null) {
- this.map.put(key, wrap(result));
- }
- }
- }
- } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ignore) {
- }
- }
- }
-
- /**
- * Put a key/boolean pair in the JSONObject.
- *
- * @param key A key string.
- * @param value A boolean which is the value.
- * @return this.
- * @throws JSONException If the key is null.
- */
- public JSONObject put(String key, boolean value) throws JSONException {
- this.put(key, value ? Boolean.TRUE : Boolean.FALSE);
- return this;
- }
-
- /**
- * Put a key/value pair in the JSONObject, where the value will be a JSONArray which is produced from a Collection.
- *
- * @param key A key string.
- * @param value A Collection value.
- * @return this.
- * @throws JSONException
- */
- public JSONObject put(String key, Collection value) throws JSONException {
- this.put(key, new JSONArray(value));
- return this;
- }
-
- /**
- * Put a key/double pair in the JSONObject.
- *
- * @param key A key string.
- * @param value A double which is the value.
- * @return this.
- * @throws JSONException If the key is null or if the number is invalid.
- */
- public JSONObject put(String key, double value) throws JSONException {
- this.put(key, new Double(value));
- return this;
- }
-
- /**
- * Put a key/int pair in the JSONObject.
- *
- * @param key A key string.
- * @param value An int which is the value.
- * @return this.
- * @throws JSONException If the key is null.
- */
- public JSONObject put(String key, int value) throws JSONException {
- this.put(key, Integer.valueOf(value));
- return this;
- }
-
- /**
- * Put a key/long pair in the JSONObject.
- *
- * @param key A key string.
- * @param value A long which is the value.
- * @return this.
- * @throws JSONException If the key is null.
- */
- public JSONObject put(String key, long value) throws JSONException {
- this.put(key, Long.valueOf(value));
- return this;
- }
-
- /**
- * Put a key/value pair in the JSONObject, where the value will be a JSONObject which is produced from a Map.
- *
- * @param key A key string.
- * @param value A Map value.
- * @return this.
- * @throws JSONException
- */
- public JSONObject put(String key, Map value) throws JSONException {
- this.put(key, new JSONObject(value));
- return this;
- }
-
- /**
- * Put a key/value pair in the JSONObject. If the value is null, then the key will be removed from the JSONObject if
- * it is present.
- *
- * @param key A key string.
- * @param value An object which is the value. It should be of one of these types: Boolean, Double, Integer,
- * JSONArray, JSONObject, Long, String, or the JSONObject.NULL object.
- * @return this.
- * @throws JSONException If the value is non-finite number or if the key is null.
- */
- public JSONObject put(String key, Object value) throws JSONException {
- if (key == null) {
- throw new NullPointerException("Null key.");
- }
- if (value != null) {
- testValidity(value);
- this.map.put(key, value);
- } else {
- remove(key);
- }
- return this;
- }
-
- /**
- * Put a key/value pair in the JSONObject, but only if the key and the value are both non-null, and only if there is
- * not already a member with that name.
- *
- * @param key string
- * @param value object
- * @return this.
- * @throws JSONException if the key is a duplicate
- */
- public JSONObject putOnce(String key, Object value) throws JSONException {
- if ((key != null) && (value != null)) {
- if (opt(key) != null) {
- throw new JSONException("Duplicate key \"" + key + "\"");
- }
- this.put(key, value);
- }
- return this;
- }
-
- /**
- * Put a key/value pair in the JSONObject, but only if the key and the value are both non-null.
- *
- * @param key A key string.
- * @param value An object which is the value. It should be of one of these types: Boolean, Double, Integer,
- * JSONArray, JSONObject, Long, String, or the JSONObject.NULL object.
- * @return this.
- * @throws JSONException If the value is a non-finite number.
- */
- public JSONObject putOpt(String key, Object value) throws JSONException {
- if ((key != null) && (value != null)) {
- this.put(key, value);
- }
- return this;
- }
-
- /**
- * Remove a name and its value, if present.
- *
- * @param key The name to be removed.
- * @return The value that was associated with the name, or null if there was no value.
- */
- public Object remove(String key) {
- return this.map.remove(key);
- }
-
- /**
- * Determine if two JSONObjects are similar. They must contain the same set of names which must be associated with
- * similar values.
- *
- * @param other The other JSONObject
- * @return true if they are equal
- */
- public boolean similar(Object other) {
- try {
- if (!(other instanceof JSONObject)) {
- return false;
- }
- Set set = keySet();
- if (!set.equals(((JSONObject) other).keySet())) {
- return false;
- }
- for (String name : set) {
- Object valueThis = get(name);
- Object valueOther = ((JSONObject) other).get(name);
- if (valueThis instanceof JSONObject) {
- if (!((JSONObject) valueThis).similar(valueOther)) {
- return false;
- }
- } else if (valueThis instanceof JSONArray) {
- if (!((JSONArray) valueThis).similar(valueOther)) {
- return false;
- }
- } else if (!valueThis.equals(valueOther)) {
- return false;
- }
- }
- return true;
- } catch (JSONException ignored) {
- return false;
- }
- }
-
- /**
- * Produce a JSONArray containing the values of the members of this JSONObject.
- *
- * @param names A JSONArray containing a list of key strings. This determines the sequence of the values in the
- * result.
- * @return A JSONArray of values.
- * @throws JSONException If any of the values are non-finite numbers.
- */
- public JSONArray toJSONArray(JSONArray names) throws JSONException {
- if ((names == null) || (names.length() == 0)) {
- return null;
- }
- JSONArray ja = new JSONArray();
- for (int i = 0; i < names.length(); i += 1) {
- ja.put(opt(names.getString(i)));
- }
- return ja;
- }
-
- /**
- * Make a JSON text of this JSONObject. For compactness, no whitespace is added. If this would not result in a
- * syntactically correct JSON text, then null will be returned instead.
- *
- * Warning: This method assumes that the data structure is acyclical.
- *
- * @return a printable, displayable, portable, transmittable representation of the object, beginning with
- * {
(left brace) and ending with }
(right
- * brace) .
- */
- @Override public String toString() {
- try {
- return this.toString(0);
- } catch (JSONException ignored) {
- return null;
- }
- }
-
- /**
- * Make a prettyprinted JSON text of this JSONObject.
- *
- * 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, portable, transmittable representation of the object, beginning with
- * {
(left brace) and ending with }
(right
- * brace) .
- * @throws JSONException If the object contains an invalid number.
- */
- public String toString(int indentFactor) throws JSONException {
- StringWriter w = new StringWriter();
- synchronized (w.getBuffer()) {
- return this.write(w, indentFactor, 0).toString();
- }
- }
-
- /**
- * Write the contents of the JSONObject as JSON text to a writer. For compactness, no whitespace is added.
- *
- * 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 JSONObject as JSON text to a writer. For compactness, no whitespace is added.
- *
- * Warning: This method assumes that the data structure is acyclical.
- *
- * @return The writer.
- * @throws JSONException
- */
- Writer write(Writer writer, int indentFactor, int indent) throws JSONException {
- try {
- boolean commanate = false;
- int length = length();
- Iterator keys = keys();
- writer.write('{');
- if (length == 1) {
- Object key = keys.next();
- writer.write(quote(key.toString()));
- writer.write(':');
- if (indentFactor > 0) {
- writer.write(' ');
- }
- writeValue(writer, this.map.get(key), indentFactor, indent);
- } else if (length != 0) {
- int newindent = indent + indentFactor;
- while (keys.hasNext()) {
- Object key = keys.next();
- if (commanate) {
- writer.write(',');
- }
- if (indentFactor > 0) {
- writer.write('\n');
- }
- indent(writer, newindent);
- writer.write(quote(key.toString()));
- writer.write(':');
- if (indentFactor > 0) {
- writer.write(' ');
- }
- writeValue(writer, this.map.get(key), indentFactor, newindent);
- commanate = true;
- }
- if (indentFactor > 0) {
- writer.write('\n');
- }
- indent(writer, indent);
- }
- writer.write('}');
- return writer;
- } catch (IOException exception) {
- throw new JSONException(exception);
- }
- }
-
- /**
- * JSONObject.NULL is equivalent to the value that JavaScript calls null, whilst Java's null is equivalent to the
- * value that JavaScript calls undefined.
- */
- private static final class Null {
- /**
- * There is only intended to be a single instance of the NULL object, so the clone method returns itself.
- *
- * @return NULL.
- */
- @Override protected final Object clone() {
- try {
- return super.clone();
- } catch (CloneNotSupportedException ignored) {
- return this;
- }
- }
-
- /**
- * A Null object is equal to the null value and to itself.
- *
- * @param object An object to test for nullness.
- * @return true if the object parameter is the JSONObject.NULL object or null.
- */
- @Override public boolean equals(Object object) {
- return (object == null) || (object == this);
- }
-
- /**
- * Get the "null" string value.
- *
- * @return The string "null".
- */
- @Override public String toString() {
- return "null";
- }
- }
-}
diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/json/JSONString.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/json/JSONString.java
deleted file mode 100644
index 019776e58..000000000
--- a/Core/src/main/java/com/github/intellectualsites/plotsquared/json/JSONString.java
+++ /dev/null
@@ -1,16 +0,0 @@
-package com.github.intellectualsites.plotsquared.json;
-
-/**
- * The JSONString
interface allows a toJSONString()
method so that a class can change the
- * behavior of JSONObject.toString()
, JSONArray.toString()
, and
- * JSONWriter.value(
Object)
. The toJSONString
method will be used instead of the
- * default behavior of using the Object's toString()
method and quoting the result.
- */
-public interface JSONString {
- /**
- * The toJSONString
method allows a class to produce its own JSON serialization.
- *
- * @return A strictly syntactically correct JSON text.
- */
- String toJSONString();
-}
diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/json/JSONTokener.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/json/JSONTokener.java
deleted file mode 100644
index 195ceeb5d..000000000
--- a/Core/src/main/java/com/github/intellectualsites/plotsquared/json/JSONTokener.java
+++ /dev/null
@@ -1,389 +0,0 @@
-package com.github.intellectualsites.plotsquared.json;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.Reader;
-import java.io.StringReader;
-
-/**
- * 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 2014-05-03
- */
-public class JSONTokener {
- private final Reader reader;
- private long character;
- private boolean eof;
- private long index;
- private long line;
- private char previous;
- private boolean usePrevious;
-
- /**
- * Construct a JSONTokener from a Reader.
- *
- * @param reader A reader.
- */
- public JSONTokener(final Reader reader) {
- this.reader = reader.markSupported() ? reader : new BufferedReader(reader);
- eof = false;
- usePrevious = false;
- previous = 0;
- index = 0;
- character = 1;
- line = 1;
- }
-
- /**
- * Construct a JSONTokener from an InputStream.
- *
- * @param inputStream The source.
- */
- public JSONTokener(final InputStream inputStream) throws JSONException {
- this(new InputStreamReader(inputStream));
- }
-
- /**
- * Construct a JSONTokener from a string.
- *
- * @param s A source string.
- */
- public JSONTokener(final String s) {
- this(new StringReader(s));
- }
-
- /**
- * 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(final 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;
- }
-
- /**
- * 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 (usePrevious || (index <= 0)) {
- throw new JSONException("Stepping back two steps is not supported");
- }
- index -= 1;
- character -= 1;
- usePrevious = true;
- eof = false;
- }
-
- public boolean end() {
- return eof && !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 (end()) {
- return false;
- }
- 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 (usePrevious) {
- usePrevious = false;
- c = previous;
- } else {
- try {
- c = reader.read();
- } catch (final IOException exception) {
- throw new JSONException(exception);
- }
- if (c <= 0) { // End of stream
- eof = true;
- c = 0;
- }
- }
- index += 1;
- if (previous == '\r') {
- line += 1;
- character = c == '\n' ? 0 : 1;
- } else if (c == '\n') {
- line += 1;
- character = 0;
- } else {
- character += 1;
- }
- previous = (char) c;
- return 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(final char c) throws JSONException {
- final char n = this.next();
- if (n != c) {
- throw 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(final int n) throws JSONException {
- if (n == 0) {
- return "";
- }
- final char[] chars = new char[n];
- int pos = 0;
- while (pos < n) {
- chars[pos] = this.next();
- if (end()) {
- throw 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 (; ; ) {
- final 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 "
(double quote) or '
- * (single quote) .
- * @return A String.
- * @throws JSONException Unterminated string.
- */
- public String nextString(final char quote) throws JSONException {
- char c;
- final StringBuilder sb = new StringBuilder();
- for (; ; ) {
- c = this.next();
- switch (c) {
- case 0:
- case '\n':
- case '\r':
- throw 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 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(final char delimiter) throws JSONException {
- final StringBuilder sb = new StringBuilder();
- for (; ; ) {
- final char c = this.next();
- if ((c == delimiter) || (c == 0) || (c == '\n') || (c == '\r')) {
- if (c != 0) {
- 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(final String delimiters) throws JSONException {
- char c;
- final StringBuilder sb = new StringBuilder();
- for (; ; ) {
- c = this.next();
- if ((delimiters.indexOf(c) >= 0) || (c == 0) || (c == '\n') || (c == '\r')) {
- if (c != 0) {
- 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 = nextClean();
- String string;
- switch (c) {
- case '"':
- case '\'':
- return nextString(c);
- case '{':
- back();
- return new JSONObject(this);
- case '[':
- 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.
- */
- final StringBuilder sb = new StringBuilder();
- while ((c >= ' ') && (",:]}/\\\"[{;=#".indexOf(c) < 0)) {
- sb.append(c);
- c = this.next();
- }
- back();
- string = sb.toString().trim();
- if (string.isEmpty()) {
- throw 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(final char to) throws JSONException {
- char c;
- try {
- final long startIndex = index;
- final long startCharacter = character;
- final long startLine = line;
- reader.mark(1000000);
- do {
- c = this.next();
- if (c == 0) {
- reader.reset();
- index = startIndex;
- character = startCharacter;
- line = startLine;
- return 0;
- }
- } while (c != to);
- } catch (final IOException exception) {
- throw new JSONException(exception);
- }
- 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(final String message) {
- return new JSONException(message + toString());
- }
-
- /**
- * Make a printable string of this JSONTokener.
- *
- * @return " at {index} [character {character} line {line}]"
- */
- @Override public String toString() {
- return " at " + index + " [character " + character + " line " + line + "]";
- }
-}
diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/json/Property.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/json/Property.java
deleted file mode 100644
index 7ace1989b..000000000
--- a/Core/src/main/java/com/github/intellectualsites/plotsquared/json/Property.java
+++ /dev/null
@@ -1,52 +0,0 @@
-package com.github.intellectualsites.plotsquared.json;
-
-import java.util.Enumeration;
-import java.util.Iterator;
-import java.util.Properties;
-
-/**
- * Converts a Property file data into JSONObject and back.
- *
- * @author JSON.org
- * @version 2014-05-03
- */
-public class Property {
- /**
- * Converts a property file object into a JSONObject. The property file object is a table of name value pairs.
- *
- * @param properties java.util.Properties
- * @return JSONObject
- * @throws JSONException
- */
- public static JSONObject toJSONObject(final java.util.Properties properties)
- throws JSONException {
- final JSONObject jo = new JSONObject();
- if ((properties != null) && !properties.isEmpty()) {
- final Enumeration enumProperties = properties.propertyNames();
- while (enumProperties.hasMoreElements()) {
- final String name = (String) enumProperties.nextElement();
- jo.put(name, properties.getProperty(name));
- }
- }
- return jo;
- }
-
- /**
- * Converts the JSONObject into a property file object.
- *
- * @param jo JSONObject
- * @return java.util.Properties
- * @throws JSONException
- */
- public static Properties toProperties(final JSONObject jo) throws JSONException {
- final Properties properties = new Properties();
- if (jo != null) {
- final Iterator keys = jo.keys();
- while (keys.hasNext()) {
- final String name = keys.next();
- properties.put(name, jo.getString(name));
- }
- }
- return properties;
- }
-}
diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/json/XML.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/json/XML.java
deleted file mode 100644
index 51a0cdb10..000000000
--- a/Core/src/main/java/com/github/intellectualsites/plotsquared/json/XML.java
+++ /dev/null
@@ -1,375 +0,0 @@
-package com.github.intellectualsites.plotsquared.json;
-
-import java.util.Iterator;
-
-/**
- * This provides static methods to convert an XML text into a JSONObject, and to covert a JSONObject into an XML text.
- *
- * @author JSON.org
- * @version 2014-05-03
- */
-class XML {
-
- static final Character AMP = '&';
- static final Character APOS = '\'';
- static final Character BANG = '!';
- static final Character EQ = '=';
- static final Character GT = '>';
- static final Character LT = '<';
- static final Character QUEST = '?';
- static final Character QUOT = '"';
- static final Character SLASH = '/';
-
- static String escape(String string) {
- StringBuilder sb = new StringBuilder(string.length());
- for (int i = 0, length = string.length(); i < length; i++) {
- char c = string.charAt(i);
- switch (c) {
- case '&':
- sb.append("&");
- break;
- case '<':
- sb.append("<");
- break;
- case '>':
- sb.append(">");
- break;
- case '"':
- sb.append(""");
- break;
- case '\'':
- sb.append("'");
- break;
- default:
- sb.append(c);
- }
- }
- return sb.toString();
- }
-
- /**
- * Throw an exception if the string contains whitespace. Whitespace is not allowed in tagNames and attributes.
- *
- * @param string A string.
- * @throws JSONException
- */
- static void noSpace(String string) throws JSONException {
- int length = string.length();
- if (length == 0) {
- throw new JSONException("Empty string.");
- }
- for (char c : string.toCharArray()) {
- if (Character.isWhitespace(c)) {
- throw new JSONException('\'' + string + "' contains a space character.");
- }
- }
- }
-
- /**
- * Scan the content following the named tag, attaching it to the context.
- *
- * @param x The XMLTokener containing the source string.
- * @param context The JSONObject that will include the new material.
- * @param name The tag name.
- * @return true if the close tag is processed.
- * @throws JSONException
- */
- private static boolean parse(XMLTokener x, JSONObject context, String name)
- throws JSONException {
- // Test for and skip past these forms:
- //
- //
- //
- // ... ?>
- // Report errors for these forms:
- // <>
- // <=
- // <<
- Object token = x.nextToken();
- // ");
- return false;
- }
- x.back();
- } else if (c == '[') {
- token = x.nextToken();
- if ("CDATA".equals(token)) {
- if (x.next() == '[') {
- string = x.nextCDATA();
- if (!string.isEmpty()) {
- context.accumulate("content", string);
- }
- return false;
- }
- }
- throw x.syntaxError("Expected 'CDATA['");
- }
- int i = 1;
- do {
- token = x.nextMeta();
- if (token == null) {
- throw x.syntaxError("Missing '>' after ' 0);
- return false;
- } else if (token == QUEST) {
- //
- x.skipPast("?>");
- return false;
- } else if (token == SLASH) {
- // Close tag
- token = x.nextToken();
- if (name == null) {
- throw x.syntaxError("Mismatched close tag " + token);
- }
- if (!token.equals(name)) {
- throw x.syntaxError("Mismatched " + name + " and " + token);
- }
- if (x.nextToken() != GT) {
- throw x.syntaxError("Misshaped close tag");
- }
- return true;
- } else if (token instanceof Character) {
- throw x.syntaxError("Misshaped tag");
- // Open tag <
- } else {
- String tagName = (String) token;
- token = null;
- JSONObject jsonobject = new JSONObject();
- for (; ; ) {
- if (token == null) {
- token = x.nextToken();
- }
- // attribute = value
- if (token instanceof String) {
- string = (String) token;
- token = x.nextToken();
- if (token == EQ) {
- token = x.nextToken();
- if (!(token instanceof String)) {
- throw x.syntaxError("Missing value");
- }
- jsonobject.accumulate(string, XML.stringToValue((String) token));
- token = null;
- } else {
- jsonobject.accumulate(string, "");
- }
- // Empty tag <.../>
- } else if (token == SLASH) {
- if (x.nextToken() != GT) {
- throw x.syntaxError("Misshaped tag");
- }
- if (jsonobject.length() > 0) {
- context.accumulate(tagName, jsonobject);
- } else {
- context.accumulate(tagName, "");
- }
- return false;
- // Content, between <...> and
- } else if (token == GT) {
- for (; ; ) {
- token = x.nextContent();
- if (token == null) {
- if (tagName != null) {
- throw x.syntaxError("Unclosed tag " + tagName);
- }
- return false;
- } else if (token instanceof String) {
- string = (String) token;
- if (!string.isEmpty()) {
- jsonobject.accumulate("content", XML.stringToValue(string));
- }
- // Nested element
- } else if (token == LT) {
- if (parse(x, jsonobject, tagName)) {
- if (jsonobject.length() == 0) {
- context.accumulate(tagName, "");
- } else if ((jsonobject.length() == 1) && (jsonobject.opt("content")
- != null)) {
- context.accumulate(tagName, jsonobject.opt("content"));
- } else {
- context.accumulate(tagName, jsonobject);
- }
- return false;
- }
- }
- }
- } else {
- throw x.syntaxError("Misshaped tag");
- }
- }
- }
- }
-
- /**
- * 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.
- *
- * @param string A String.
- * @return A simple JSON value.
- */
- 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 = Long.valueOf(string);
- if (value.toString().equals(string)) {
- return value;
- }
- }
- } catch (NumberFormatException ignore) {
- try {
- Double value = Double.valueOf(string);
- if (value.toString().equals(string)) {
- return value;
- }
- } catch (NumberFormatException ignored) {
- }
- }
- return string;
- }
-
- public static JSONObject toJSONObject(String string) throws JSONException {
- JSONObject jo = new JSONObject();
- XMLTokener x = new XMLTokener(string);
- while (x.more() && x.skipPast("<")) {
- parse(x, jo, null);
- }
- return jo;
- }
-
- /**
- * Convert a JSONObject into a well-formed, element-normal XML string.
- *
- * @param object A JSONObject.
- * @return A string.
- * @throws JSONException
- */
- public static String toString(Object object) throws JSONException {
- return toString(object, null);
- }
-
- /**
- * Convert a JSONObject into a well-formed, element-normal XML string.
- *
- * @param object A JSONObject.
- * @param tagName The optional name of the enclosing tag.
- * @return A string.
- * @throws JSONException
- */
- public static String toString(Object object, String tagName) throws JSONException {
- StringBuilder sb = new StringBuilder();
- int i;
- JSONArray ja;
- int length;
- String string;
- if (object instanceof JSONObject) {
- // Emit
- if (tagName != null) {
- sb.append('<');
- sb.append(tagName);
- sb.append('>');
- }
- // Loop thru the keys.
- JSONObject jo = (JSONObject) object;
- Iterator keys = jo.keys();
- while (keys.hasNext()) {
- String key = keys.next();
- Object value = jo.opt(key);
- if (value == null) {
- value = "";
- }
- string = value instanceof String ? (String) value : null;
- // Emit content in body
- if ("content".equals(key)) {
- if (value instanceof JSONArray) {
- ja = (JSONArray) value;
- length = ja.length();
- for (i = 0; i < length; i += 1) {
- if (i > 0) {
- sb.append('\n');
- }
- sb.append(escape(ja.get(i).toString()));
- }
- } else {
- sb.append(escape(value.toString()));
- }
- // Emit an array of similar keys
- } else if (value instanceof JSONArray) {
- ja = (JSONArray) value;
- length = ja.length();
- for (i = 0; i < length; i += 1) {
- value = ja.get(i);
- if (value instanceof JSONArray) {
- sb.append('<');
- sb.append(key);
- sb.append('>');
- sb.append(toString(value));
- sb.append("");
- sb.append(key);
- sb.append('>');
- } else {
- sb.append(toString(value, key));
- }
- }
- } else if ("".equals(value)) {
- sb.append('<');
- sb.append(key);
- sb.append("/>");
- // Emit a new tag
- } else {
- sb.append(toString(value, key));
- }
- }
- if (tagName != null) {
- // Emit the close tag
- sb.append("");
- sb.append(tagName);
- sb.append('>');
- }
- return sb.toString();
- // XML does not have good support for arrays. If an array appears in
- // a place
- // where XML is lacking, synthesize an element.
- } else {
- if (object.getClass().isArray()) {
- object = new JSONArray(object);
- }
- if (object instanceof JSONArray) {
- ja = (JSONArray) object;
- length = ja.length();
- for (i = 0; i < length; i += 1) {
- sb.append(toString(ja.opt(i), tagName == null ? "array" : tagName));
- }
- return sb.toString();
- } else {
- string = escape(object.toString());
- return (tagName == null) ?
- '"' + string + '"' :
- string.isEmpty() ?
- '<' + tagName + "/>" :
- '<' + tagName + '>' + string + "" + tagName + '>';
- }
- }
- }
-}
diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/json/XMLTokener.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/json/XMLTokener.java
deleted file mode 100644
index 96d65541f..000000000
--- a/Core/src/main/java/com/github/intellectualsites/plotsquared/json/XMLTokener.java
+++ /dev/null
@@ -1,315 +0,0 @@
-package com.github.intellectualsites.plotsquared.json;
-
-import java.util.HashMap;
-
-/**
- * The XMLTokener extends the JSONTokener to provide additional methods for the parsing of XML texts.
- *
- * @author JSON.org
- * @version 2014-05-03
- */
-public class XMLTokener extends JSONTokener {
- /**
- * The table of entity values. It initially contains Character values for amp, apos, gt, lt, quot.
- */
- public static final HashMap entity;
-
- static {
- entity = new HashMap<>(8);
- entity.put("amp", XML.AMP);
- entity.put("apos", XML.APOS);
- entity.put("gt", XML.GT);
- entity.put("lt", XML.LT);
- entity.put("quot", XML.QUOT);
- }
-
- /**
- * Construct an XMLTokener from a string.
- *
- * @param s A source string.
- */
- public XMLTokener(final String s) {
- super(s);
- }
-
- /**
- * Get the text in the CDATA block.
- *
- * @return The string up to the ]]>
.
- * @throws JSONException If the ]]>
is not found.
- */
- public String nextCDATA() throws JSONException {
- final StringBuilder sb = new StringBuilder();
- for (; ; ) {
- char c = next();
- if (end()) {
- throw syntaxError("Unclosed CDATA");
- }
- sb.append(c);
- int i = sb.length() - 3;
- if ((i >= 0) && (sb.charAt(i) == ']') && (sb.charAt(i + 1) == ']') && (sb.charAt(i + 2)
- == '>')) {
- sb.setLength(i);
- return sb.toString();
- }
- }
- }
-
- /**
- * Get the next XML outer token, trimming whitespace. There are two kinds of tokens: the '<' character which begins
- * a markup tag, and the content text between markup tags.
- *
- * @return A string, or a '<' Character, or null if there is no more source text.
- * @throws JSONException
- */
- public Object nextContent() throws JSONException {
- char c;
- do {
- c = next();
- } while (Character.isWhitespace(c));
- if (c == 0) {
- return null;
- }
- if (c == '<') {
- return XML.LT;
- }
- StringBuilder sb = new StringBuilder();
- for (; ; ) {
- if ((c == '<') || (c == 0)) {
- back();
- return sb.toString().trim();
- }
- if (c == '&') {
- sb.append(nextEntity('&'));
- } else {
- sb.append(c);
- }
- c = next();
- }
- }
-
- /**
- * Return the next entity. These entities are translated to Characters: & " > <
- * "
.
- *
- * @param ampersand An ampersand character.
- * @return A Character or an entity String if the entity is not recognized.
- * @throws JSONException If missing ';' in XML entity.
- */
- public Object nextEntity(final char ampersand) throws JSONException {
- final StringBuilder sb = new StringBuilder();
- for (; ; ) {
- final char c = next();
- if (Character.isLetterOrDigit(c) || (c == '#')) {
- sb.append(Character.toLowerCase(c));
- } else if (c == ';') {
- break;
- } else {
- throw syntaxError("Missing ';' in XML entity: &" + sb);
- }
- }
- final String string = sb.toString();
- final Object object = entity.get(string);
- return object != null ? object : ampersand + string + ';';
- }
-
- /**
- * Returns the next XML meta token. This is used for skipping over and ...?> structures.
- *
- * @return Syntax characters (< > / = ! ?
) are returned as Character, and strings and names are
- * returned as Boolean. We don't care what the values actually are.
- * @throws JSONException If a string is not properly closed or if the XML is badly structured.
- */
- public Object nextMeta() throws JSONException {
- char c;
- do {
- c = next();
- } while (Character.isWhitespace(c));
- char q;
- switch (c) {
- case 0:
- throw syntaxError("Misshaped meta tag");
- case '<':
- return XML.LT;
- case '>':
- return XML.GT;
- case '/':
- return XML.SLASH;
- case '=':
- return XML.EQ;
- case '!':
- return XML.BANG;
- case '?':
- return XML.QUEST;
- case '"':
- case '\'':
- q = c;
- for (; ; ) {
- c = next();
- if (c == 0) {
- throw syntaxError("Unterminated string");
- }
- if (c == q) {
- return Boolean.TRUE;
- }
- }
- default:
- for (; ; ) {
- c = next();
- if (Character.isWhitespace(c)) {
- return Boolean.TRUE;
- }
- switch (c) {
- case 0:
- case '<':
- case '>':
- case '/':
- case '=':
- case '!':
- case '?':
- case '"':
- case '\'':
- back();
- return Boolean.TRUE;
- }
- }
- }
- }
-
- /**
- * Get the next XML Token. These tokens are found inside of angle brackets. It may be one of these characters:
- * / >= ! ?
or it may be a string wrapped in single quotes or double quotes, or it may be a name.
- *
- * @return a String or a Character.
- * @throws JSONException If the XML is not well formed.
- */
- public Object nextToken() throws JSONException {
- char c;
- do {
- c = next();
- } while (Character.isWhitespace(c));
- char q;
- StringBuilder sb;
- switch (c) {
- case 0:
- throw syntaxError("Misshaped element");
- case '<':
- throw syntaxError("Misplaced '<'");
- case '>':
- return XML.GT;
- case '/':
- return XML.SLASH;
- case '=':
- return XML.EQ;
- case '!':
- return XML.BANG;
- case '?':
- return XML.QUEST;
- // Quoted string
- case '"':
- case '\'':
- q = c;
- sb = new StringBuilder();
- for (; ; ) {
- c = next();
- if (c == 0) {
- throw syntaxError("Unterminated string");
- }
- if (c == q) {
- return sb.toString();
- }
- if (c == '&') {
- sb.append(nextEntity('&'));
- } else {
- sb.append(c);
- }
- }
- default:
- // Name
- sb = new StringBuilder();
- for (; ; ) {
- sb.append(c);
- c = next();
- if (Character.isWhitespace(c)) {
- return sb.toString();
- }
- switch (c) {
- case 0:
- return sb.toString();
- case '>':
- case '/':
- case '=':
- case '!':
- case '?':
- case '[':
- case ']':
- back();
- return sb.toString();
- case '<':
- case '"':
- case '\'':
- throw syntaxError("Bad character in a name");
- }
- }
- }
- }
-
- /**
- * Skip characters until past the requested string. If it is not found, we are left at the end of the source with a
- * result of false.
- *
- * @param to A string to skip past.
- * @throws JSONException
- */
- public boolean skipPast(final String to) throws JSONException {
- char c;
- int i;
- final int length = to.length();
- final char[] circle = new char[length];
- /*
- * First fill the circle buffer with as many characters as are in the
- * to string. If we reach an early end, bail.
- */
- for (i = 0; i < length; i += 1) {
- c = next();
- if (c == 0) {
- return false;
- }
- circle[i] = c;
- }
- /* We will loop, possibly for all of the remaining characters. */
- for (int offset = 0; ; ) {
- int j = offset;
- boolean b = true;
- /* Compare the circle buffer with the to string. */
- for (i = 0; i < length; i += 1) {
- if (circle[j] != to.charAt(i)) {
- b = false;
- break;
- }
- j += 1;
- if (j >= length) {
- j -= length;
- }
- }
- /* If we exit the loop with b intact, then victory is ours. */
- if (b) {
- return true;
- }
- /* Get the next character. If there isn't one, then defeat is ours. */
- c = next();
- if (c == 0) {
- return false;
- }
- /*
- * Shove the character in the circle buffer and advance the
- * circle offset. The offset is mod n.
- */
- circle[offset] = c;
- offset += 1;
- if (offset >= length) {
- offset -= length;
- }
- }
- }
-}
diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/SchematicHandler.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/SchematicHandler.java
index 650451260..4e553083c 100644
--- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/SchematicHandler.java
+++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/SchematicHandler.java
@@ -1,7 +1,5 @@
package com.github.intellectualsites.plotsquared.plot.util;
-import com.github.intellectualsites.plotsquared.json.JSONArray;
-import com.github.intellectualsites.plotsquared.json.JSONException;
import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.config.Settings;
import com.github.intellectualsites.plotsquared.plot.generator.ClassicPlotWorld;
@@ -26,6 +24,8 @@ import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BaseBlock;
import org.jetbrains.annotations.NotNull;
+import org.json.JSONArray;
+import org.json.JSONException;
import java.io.BufferedReader;
import java.io.File;
diff --git a/build.gradle b/build.gradle
index f09d0d5fd..4f88692d5 100644
--- a/build.gradle
+++ b/build.gradle
@@ -75,6 +75,8 @@ subprojects {
exclude(module: "mockito-core")
exclude(module: "dummypermscompat")
}
+ compile group: 'org.json', name: 'json', version: '20190722'
+
implementation("net.kyori:text-api:3.0.2")
implementation("net.kyori:text-serializer-gson:3.0.2")
implementation("net.kyori:text-serializer-legacy:3.0.2")
@@ -111,6 +113,7 @@ subprojects {
include(dependency("net.kyori:text-api:3.0.2"))
}
relocate("io.papermc.lib", "com.github.intellectualsites.plotsquared.bukkit.paperlib")
+ relocate("org.json", "com.github.intellectualsites.plotsquared.json")
// relocate('org.mcstats', 'com.plotsquared.stats')
archiveFileName = "${parent.name}-${project.name}-${parent.version}.jar"
destinationDirectory = file "../target"