/** * Append a float value. This increases the array's length by one. * This will store the value as a double, since there are no floats in JSON. * * @param value a float value * @throws RuntimeException if the value is not finite. * @return this. */ public JSONArray append(float value) { return append((double) value); }
/** * Return the JSON data formatted with two spaces for indents. * Chosen to do this since it's the most common case (e.g. with println()). * Same as format(2). Use the format() function for more options. */ @Override public String toString() { try { return format(2); } catch (Exception e) { return null; } }
/** Get this entire array as a boolean array. Everything must be a boolean. */ public boolean[] getBooleanArray() { boolean[] outgoing = new boolean[size()]; for (int i = 0; i < size(); i++) { outgoing[i] = getBoolean(i); } return outgoing; }
/** * Get the string associated with an index. * * @webref jsonarray:method * @brief Gets the String value associated with an index * @param index must be between 0 and length() - 1 * @return A string value. * @throws RuntimeException If there is no string value for the index. * @see JSONArray#getInt(int) * @see JSONArray#getFloat(int) * @see JSONArray#getBoolean(int) */ 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."); }
/** * @webref input:files * @param input String to parse as a JSONArray * @see JSONObject * @see PApplet#loadJSONObject(String) * @see PApplet#saveJSONObject(JSONObject, String) */ public JSONArray parseJSONArray(String input) { return new JSONArray(new StringReader(input)); }
/** * Get this entire array as an int array. Everything must be an int. * * @webref jsonarray:method * @brief Gets the entire array as array of ints * @see JSONArray#getStringArray() */ public int[] getIntArray() { int[] outgoing = new int[size()]; for (int i = 0; i < size(); i++) { outgoing[i] = getInt(i); } return outgoing; }
/** * 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 RuntimeException If the index is negative or if the the value is * an invalid number. */ private JSONArray set(int index, Object value) { JSONObject.testValidity(value); if (index < 0) { throw new RuntimeException("JSONArray[" + index + "] not found."); } if (index < this.size()) { this.myArrayList.set(index, value); } else { while (index != this.size()) { this.append(JSONObject.NULL); } this.append(value); } return this; }
((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));
((JSONObject) value).writeInternal(writer, indentFactor, indent); } else if (value instanceof JSONArray) { ((JSONArray) value).writeInternal(writer, indentFactor, indent); } else if (value instanceof Map) { new JSONObject(value).writeInternal(writer, indentFactor, indent); } else if (value instanceof Collection) { new JSONArray(value).writeInternal(writer, indentFactor, indent); } else if (value.getClass().isArray()) { new JSONArray(value).writeInternal(writer, indentFactor, indent); } else if (value instanceof Number) { writer.write(numberToString((Number) value));
/** * Get a value from an index as a float. JSON uses 'double' values * internally, so this is simply getDouble() cast to a float. * * @webref jsonarray:method * @brief Gets the float value associated with an index * @param index must be between 0 and length() - 1 * @see JSONArray#getInt(int) * @see JSONArray#getString(int) * @see JSONArray#getBoolean(int) */ public float getFloat(int index) { return (float) getDouble(index); }
public JSONArray getJSONArray(int index, JSONArray defaultValue) { try { return getJSONArray(index); } catch (Exception e) { return defaultValue; } }
public JSONObject getJSONObject(int index, JSONObject defaultValue) { try { return getJSONObject(index); } catch (Exception e) { return defaultValue; } }
public float getFloat(int index, float defaultValue) { try { return getFloat(index); } catch (Exception e) { return defaultValue; } }
/** * 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 getInt(int index, int defaultValue) { try { return getInt(index); } catch (Exception e) { return defaultValue; } }
/** * 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 getBoolean(int index, boolean defaultValue) { try { return getBoolean(index); } catch (Exception e) { return defaultValue; } }
/** * Get the JSONArray associated with an index. * * @webref jsonobject:method * @brief Gets the JSONArray associated with an index value * @param 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 * @see JSONArray#getJSONObject(int) * @see JSONArray#setJSONObject(int, JSONObject) * @see JSONArray#setJSONArray(int, JSONArray) */ public JSONArray getJSONArray(int index) { Object object = this.get(index); if (object instanceof JSONArray) { return (JSONArray)object; } throw new RuntimeException("JSONArray[" + index + "] is not a JSONArray."); }
/** * @webref input:files * @param input String to parse as a JSONArray * @see JSONObject * @see PApplet#loadJSONObject(String) * @see PApplet#saveJSONObject(JSONObject, String) */ public JSONArray parseJSONArray(String input) { return new JSONArray(new StringReader(input)); }