diff --git a/core/src/processing/data/JSON.java b/core/src/processing/data/JSON.java index 79bd54d8c..3d1ff098d 100644 --- a/core/src/processing/data/JSON.java +++ b/core/src/processing/data/JSON.java @@ -107,7 +107,8 @@ public class JSON { * JSONObjects will be avoided by using a key pool to manage unique key * string objects. This is used by JSONObject.put(string, object). */ - private static HashMap keyPool = new HashMap(keyPoolSize); + private static HashMap keyPool = + new HashMap(keyPoolSize); // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . @@ -164,7 +165,8 @@ public class JSON { /** * The map where the JSONObject's properties are kept. */ - private final Map map; +// private final Map map; + private final HashMap map; /** @@ -180,7 +182,7 @@ public class JSON { * Construct an empty JSONObject. */ public JSON() { - this.map = new HashMap(); + this.map = new HashMap(); } @@ -268,15 +270,15 @@ public class JSON { * the JSONObject. * @throws JSONException */ - public JSON(Map map) { - this.map = new HashMap(); + public JSON(Map map) { + this.map = new HashMap(); if (map != null) { Iterator i = map.entrySet().iterator(); while (i.hasNext()) { - Map.Entry e = (Map.Entry)i.next(); + Map.Entry e = (Map.Entry) i.next(); Object value = e.getValue(); if (value != null) { - this.map.put(e.getKey(), wrap(value)); + map.put((String) e.getKey(), wrap(value)); } } } @@ -760,7 +762,7 @@ public class JSON { JSONArray ja = new JSONArray(); Iterator keys = this.keys(); while (keys.hasNext()) { - ja.put(keys.next()); + ja.append(keys.next()); } return ja.length() == 0 ? null : ja; } @@ -1114,7 +1116,7 @@ public class JSON { * @return this. * @throws JSONException */ - public JSON put(String key, Map value) { + public JSON put(String key, HashMap value) { this.put(key, new JSON(value)); return this; } @@ -1141,7 +1143,7 @@ public class JSON { pooled = (String)keyPool.get(key); if (pooled == null) { if (keyPool.size() >= keyPoolSize) { - keyPool = new HashMap(keyPoolSize); + keyPool = new HashMap(keyPoolSize); } keyPool.put(key, key); } else { @@ -1371,7 +1373,7 @@ public class JSON { } JSONArray ja = new JSONArray(); for (int i = 0; i < names.length(); i += 1) { - ja.put(this.opt(names.getString(i))); + ja.append(this.opt(names.getString(i))); } return ja; } diff --git a/core/src/processing/data/JSONArray.java b/core/src/processing/data/JSONArray.java index fe3d41778..2276984a1 100644 --- a/core/src/processing/data/JSONArray.java +++ b/core/src/processing/data/JSONArray.java @@ -86,14 +86,14 @@ public class JSONArray { /** * The arrayList where the JSONArray's properties are kept. */ - private final ArrayList myArrayList; + private final ArrayList myArrayList; /** * Construct an empty JSONArray. */ public JSONArray() { - this.myArrayList = new ArrayList(); + this.myArrayList = new ArrayList(); } /** @@ -101,7 +101,7 @@ public class JSONArray { * @param x A JSONTokener * @throws JSONException If there is a syntax error. */ - public JSONArray(JSONTokener x) { + private JSONArray(JSONTokener x) { this(); if (x.nextClean() != '[') { throw new RuntimeException("A JSONArray text must start with '['"); @@ -111,10 +111,10 @@ public class JSONArray { for (;;) { if (x.nextClean() == ',') { x.back(); - this.myArrayList.add(JSON.NULL); + myArrayList.add(JSON.NULL); } else { x.back(); - this.myArrayList.add(x.nextValue()); + myArrayList.add(x.nextValue()); } switch (x.nextClean()) { case ';': @@ -151,11 +151,11 @@ public class JSONArray { * @param collection A Collection. */ public JSONArray(Collection collection) { - this.myArrayList = new ArrayList(); + myArrayList = new ArrayList(); if (collection != null) { Iterator iter = collection.iterator(); while (iter.hasNext()) { - this.myArrayList.add(JSON.wrap(iter.next())); + myArrayList.add(JSON.wrap(iter.next())); } } } @@ -170,7 +170,7 @@ public class JSONArray { if (array.getClass().isArray()) { int length = Array.getLength(array); for (int i = 0; i < length; i += 1) { - this.put(JSON.wrap(Array.get(array, i))); + this.append(JSON.wrap(Array.get(array, i))); } } else { throw new RuntimeException("JSONArray initial value should be a string or collection or array."); @@ -178,13 +178,27 @@ public class JSONArray { } + /** + * 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. + */ + private Object opt(int index) { + if (index < 0 || index >= this.length()) { + return null; + } + return myArrayList.get(index); + } + + /** * 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) { + private Object get(int index) { Object object = opt(index); if (object == null) { throw new RuntimeException("JSONArray[" + index + "] not found."); @@ -193,6 +207,92 @@ public class JSONArray { } + /** + * 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) { + Object object = this.get(index); + if (object instanceof String) { + return (String)object; + } + throw new RuntimeException("JSONArray[" + index + "] not a string."); + } + + + /** + * 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) { + Object object = this.get(index); + try { + return object instanceof Number + ? ((Number)object).intValue() + : Integer.parseInt((String)object); + } catch (Exception e) { + throw new RuntimeException("JSONArray[" + index + "] is not a number."); + } + } + + + /** + * 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) { + Object object = this.get(index); + try { + return object instanceof Number + ? ((Number)object).longValue() + : Long.parseLong((String)object); + } catch (Exception e) { + throw new RuntimeException("JSONArray[" + index + "] is not a number."); + } + } + + + public float getFloat(int index) { + Object object = this.get(index); + try { + return object instanceof Number + ? ((Number)object).floatValue() + : Float.parseFloat((String)object); + } catch (Exception e) { + throw new RuntimeException("JSONArray[" + index + "] is not a number."); + } + } + + + /** + * 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) { + Object object = this.get(index); + try { + return object instanceof Number + ? ((Number)object).doubleValue() + : Double.parseDouble((String)object); + } catch (Exception e) { + throw new RuntimeException("JSONArray[" + index + "] is not a number."); + } + } + + /** * Get the boolean value associated with an index. * The string values "true" and "false" are converted to boolean. @@ -217,45 +317,6 @@ public class JSONArray { } - /** - * 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) { - Object object = this.get(index); - try { - return object instanceof Number - ? ((Number)object).doubleValue() - : Double.parseDouble((String)object); - } catch (Exception e) { - throw new RuntimeException("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) { - Object object = this.get(index); - try { - return object instanceof Number - ? ((Number)object).intValue() - : Integer.parseInt((String)object); - } catch (Exception e) { - throw new RuntimeException("JSONArray[" + index + "] is not a number."); - } - } - - /** * Get the JSONArray associated with an index. * @param index The index must be between 0 and length() - 1. @@ -263,7 +324,7 @@ public class JSONArray { * @throws JSONException If there is no value for the index. or if the * value is not a JSONArray */ - public JSONArray getJSONArray(int index) { + public JSONArray getArray(int index) { Object object = this.get(index); if (object instanceof JSONArray) { return (JSONArray)object; @@ -279,7 +340,7 @@ public class JSONArray { * @throws JSONException If there is no value for the index or if the * value is not a JSONObject */ - public JSON getJSONObject(int index) { + public JSON getObject(int index) { Object object = this.get(index); if (object instanceof JSON) { return (JSON)object; @@ -288,97 +349,6 @@ public class JSONArray { } - /** - * 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) { - Object object = this.get(index); - try { - return object instanceof Number - ? ((Number)object).longValue() - : Long.parseLong((String)object); - } catch (Exception e) { - throw new RuntimeException("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) { - Object object = this.get(index); - if (object instanceof String) { - return (String)object; - } - throw new RuntimeException("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 JSON.NULL.equals(this.opt(index)); - } - - - /** - * Make a string from the contents of this JSONArray. The - * separator 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) { - int len = this.length(); - StringBuffer sb = new StringBuffer(); - - for (int i = 0; i < len; i += 1) { - if (i > 0) { - sb.append(separator); - } - sb.append(JSON.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 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. - */ - private Object opt(int index) { - if (index < 0 || index >= this.length()) { - return null; - } - return myArrayList.get(index); - } - - // /** // * Get the optional boolean value associated with an index. // * It returns false if there is no value at that index, @@ -562,8 +532,8 @@ public class JSONArray { * @param value A boolean value. * @return this. */ - public JSONArray put(boolean value) { - this.put(value ? Boolean.TRUE : Boolean.FALSE); + public JSONArray append(boolean value) { + this.append(value ? Boolean.TRUE : Boolean.FALSE); return this; } @@ -574,8 +544,8 @@ public class JSONArray { * @param value A Collection value. * @return this. */ - public JSONArray put(Collection value) { - this.put(new JSONArray(value)); + public JSONArray append(Collection value) { + this.append(new JSONArray(value)); return this; } @@ -587,10 +557,10 @@ public class JSONArray { * @throws JSONException if the value is not finite. * @return this. */ - public JSONArray put(double value) { + public JSONArray append(double value) { Double d = new Double(value); JSON.testValidity(d); - this.put(d); + this.append(d); return this; } @@ -601,8 +571,8 @@ public class JSONArray { * @param value An int value. * @return this. */ - public JSONArray put(int value) { - this.put(new Integer(value)); + public JSONArray append(int value) { + this.append(new Integer(value)); return this; } @@ -613,8 +583,8 @@ public class JSONArray { * @param value A long value. * @return this. */ - public JSONArray put(long value) { - this.put(new Long(value)); + public JSONArray append(long value) { + this.append(new Long(value)); return this; } @@ -625,8 +595,8 @@ public class JSONArray { * @param value A Map value. * @return this. */ - public JSONArray put(Map value) { - this.put(new JSON(value)); + public JSONArray append(Map value) { + this.append(new JSON(value)); return this; } @@ -638,7 +608,7 @@ public class JSONArray { * JSONObject.NULL object. * @return this. */ - public JSONArray put(Object value) { + public JSONArray append(Object value) { this.myArrayList.add(value); return this; } @@ -653,8 +623,8 @@ public class JSONArray { * @return this. * @throws JSONException If the index is negative. */ - public JSONArray put(int index, boolean value) { - this.put(index, value ? Boolean.TRUE : Boolean.FALSE); + public JSONArray set(int index, boolean value) { + this.set(index, value ? Boolean.TRUE : Boolean.FALSE); return this; } @@ -668,8 +638,8 @@ public class JSONArray { * @throws JSONException If the index is negative or if the value is * not finite. */ - public JSONArray put(int index, Collection value) { - this.put(index, new JSONArray(value)); + public JSONArray set(int index, Collection value) { + this.set(index, new JSONArray(value)); return this; } @@ -684,8 +654,8 @@ public class JSONArray { * @throws JSONException If the index is negative or if the value is * not finite. */ - public JSONArray put(int index, double value) { - this.put(index, new Double(value)); + public JSONArray set(int index, double value) { + this.set(index, new Double(value)); return this; } @@ -699,8 +669,8 @@ public class JSONArray { * @return this. * @throws JSONException If the index is negative. */ - public JSONArray put(int index, int value) { - this.put(index, new Integer(value)); + public JSONArray set(int index, int value) { + this.set(index, new Integer(value)); return this; } @@ -714,8 +684,8 @@ public class JSONArray { * @return this. * @throws JSONException If the index is negative. */ - public JSONArray put(int index, long value) { - this.put(index, new Long(value)); + public JSONArray set(int index, long value) { + this.set(index, new Long(value)); return this; } @@ -729,8 +699,8 @@ public class JSONArray { * @throws JSONException If the index is negative or if the the value is * an invalid number. */ - public JSONArray put(int index, Map value) { - this.put(index, new JSON(value)); + public JSONArray set(int index, Map value) { + this.set(index, new JSON(value)); return this; } @@ -747,7 +717,7 @@ public class JSONArray { * @throws JSONException If the index is negative or if the the value is * an invalid number. */ - public JSONArray put(int index, Object value) { + public JSONArray set(int index, Object value) { JSON.testValidity(value); if (index < 0) { throw new RuntimeException("JSONArray[" + index + "] not found."); @@ -756,46 +726,66 @@ public class JSONArray { this.myArrayList.set(index, value); } else { while (index != this.length()) { - this.put(JSON.NULL); + this.append(JSON.NULL); } - this.put(value); + this.append(value); } return this; } + /** + * Get the number of elements in the JSONArray, included nulls. + * + * @return The length (or size). + */ + public int length() { + return myArrayList.size(); + } + + + /** + * 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 JSON.NULL.equals(this.opt(index)); + } + + /** * 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) { + public Object removeIndex(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 JSON toJSONObject(JSONArray names) { - if (names == null || names.length() == 0 || this.length() == 0) { - return null; - } - JSON jo = new JSON(); - for (int i = 0; i < names.length(); i += 1) { - jo.put(names.getString(i), this.opt(i)); - } - return jo; - } +// /** +// * 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 JSON toJSONObject(JSONArray names) { +// if (names == null || names.length() == 0 || this.length() == 0) { +// return null; +// } +// JSON jo = new JSON(); +// for (int i = 0; i < names.length(); i += 1) { +// jo.put(names.getString(i), this.opt(i)); +// } +// return jo; +// } /** @@ -898,4 +888,26 @@ public class JSONArray { throw new RuntimeException(e); } } + + + /** + * Make a string from the contents of this JSONArray. The + * separator 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) { + int len = this.length(); + StringBuffer sb = new StringBuffer(); + + for (int i = 0; i < len; i += 1) { + if (i > 0) { + sb.append(separator); + } + sb.append(JSON.valueToString(this.myArrayList.get(i))); + } + return sb.toString(); + } } diff --git a/core/src/processing/data/JSONTokener.java b/core/src/processing/data/JSONTokener.java index c9bdb3889..7b4737ece 100644 --- a/core/src/processing/data/JSONTokener.java +++ b/core/src/processing/data/JSONTokener.java @@ -187,7 +187,7 @@ public class JSONTokener { public char next(char c) { char n = this.next(); if (n != c) { - syntaxError("Expected '" + c + "' and instead saw '" + n + "'"); + throw new RuntimeException("Expected '" + c + "' and instead saw '" + n + "'"); } return n; } @@ -213,7 +213,7 @@ public class JSONTokener { while (pos < n) { chars[pos] = this.next(); if (this.end()) { - syntaxError("Substring bounds error"); + throw new RuntimeException("Substring bounds error"); } pos += 1; } @@ -256,8 +256,7 @@ public class JSONTokener { case 0: case '\n': case '\r': - syntaxError("Unterminated string"); - break; + throw new RuntimeException("Unterminated string"); case '\\': c = this.next(); switch (c) { @@ -286,7 +285,7 @@ public class JSONTokener { sb.append(c); break; default: - syntaxError("Illegal escape."); + throw new RuntimeException("Illegal escape."); } break; default: @@ -384,7 +383,7 @@ public class JSONTokener { string = sb.toString().trim(); if ("".equals(string)) { - syntaxError("Missing value"); + throw new RuntimeException("Missing value"); } return JSON.stringToValue(string); } @@ -423,17 +422,6 @@ public class JSONTokener { } - /** - * Make a JSONException to signal a syntax error. - * - * @param message The error message. - * @return A JSONException object, suitable for throwing - */ - private void syntaxError(String message) { - throw new RuntimeException(message + this.toString()); - } - - /** * Make a printable string of this JSONTokener. *