1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 16:00:51 -07:00
This commit is contained in:
Douglas Crockford 2013-02-19 04:49:21 -08:00
parent 0759465bdf
commit ae8d12c5f4

View file

@ -81,13 +81,11 @@ import java.util.Map;
*/ */
public class JSONArray { public class JSONArray {
/** /**
* The arrayList where the JSONArray's properties are kept. * The arrayList where the JSONArray's properties are kept.
*/ */
private final ArrayList myArrayList; private final ArrayList myArrayList;
/** /**
* Construct an empty JSONArray. * Construct an empty JSONArray.
*/ */
@ -97,8 +95,11 @@ public class JSONArray {
/** /**
* Construct a JSONArray from a JSONTokener. * Construct a JSONArray from a JSONTokener.
* @param x A JSONTokener *
* @throws JSONException If there is a syntax error. * @param x
* A JSONTokener
* @throws JSONException
* If there is a syntax error.
*/ */
public JSONArray(JSONTokener x) throws JSONException { public JSONArray(JSONTokener x) throws JSONException {
this(); this();
@ -132,22 +133,25 @@ public class JSONArray {
} }
} }
/** /**
* Construct a JSONArray from a source JSON text. * Construct a JSONArray from a source JSON text.
* @param source A string that begins with *
* <code>[</code>&nbsp;<small>(left bracket)</small> * @param source
* and ends with <code>]</code>&nbsp;<small>(right bracket)</small>. * A string that begins with <code>[</code>&nbsp;<small>(left
* @throws JSONException If there is a syntax error. * bracket)</small> and ends with <code>]</code>
* &nbsp;<small>(right bracket)</small>.
* @throws JSONException
* If there is a syntax error.
*/ */
public JSONArray(String source) throws JSONException { public JSONArray(String source) throws JSONException {
this(new JSONTokener(source)); this(new JSONTokener(source));
} }
/** /**
* Construct a JSONArray from a Collection. * Construct a JSONArray from a Collection.
* @param collection A Collection. *
* @param collection
* A Collection.
*/ */
public JSONArray(Collection collection) { public JSONArray(Collection collection) {
this.myArrayList = new ArrayList(); this.myArrayList = new ArrayList();
@ -159,10 +163,11 @@ public class JSONArray {
} }
} }
/** /**
* Construct a JSONArray from an array * Construct a JSONArray from an array
* @throws JSONException If not an array. *
* @throws JSONException
* If not an array.
*/ */
public JSONArray(Object array) throws JSONException { public JSONArray(Object array) throws JSONException {
this(); this();
@ -177,13 +182,14 @@ public class JSONArray {
} }
} }
/** /**
* Get the object value associated with an index. * Get the object value associated with an index.
*
* @param index * @param index
* The index must be between 0 and length() - 1. * The index must be between 0 and length() - 1.
* @return An object value. * @return An object value.
* @throws JSONException If there is no value for the index. * @throws JSONException
* If there is no value for the index.
*/ */
public Object get(int index) throws JSONException { public Object get(int index) throws JSONException {
Object object = this.opt(index); Object object = this.opt(index);
@ -193,132 +199,134 @@ public class JSONArray {
return object; return object;
} }
/** /**
* Get the boolean value associated with an index. * Get the boolean value associated with an index. The string values "true"
* The string values "true" and "false" are converted to boolean. * and "false" are converted to boolean.
* *
* @param index The index must be between 0 and length() - 1. * @param index
* The index must be between 0 and length() - 1.
* @return The truth. * @return The truth.
* @throws JSONException If there is no value for the index or if the * @throws JSONException
* value is not convertible to boolean. * If there is no value for the index or if the value is not
* convertible to boolean.
*/ */
public boolean getBoolean(int index) throws JSONException { public boolean getBoolean(int index) throws JSONException {
Object object = this.get(index); Object object = this.get(index);
if (object.equals(Boolean.FALSE) || if (object.equals(Boolean.FALSE)
(object instanceof String && || (object instanceof String && ((String) object)
((String)object).equalsIgnoreCase("false"))) { .equalsIgnoreCase("false"))) {
return false; return false;
} else if (object.equals(Boolean.TRUE) || } else if (object.equals(Boolean.TRUE)
(object instanceof String && || (object instanceof String && ((String) object)
((String)object).equalsIgnoreCase("true"))) { .equalsIgnoreCase("true"))) {
return true; return true;
} }
throw new JSONException("JSONArray[" + index + "] is not a boolean."); throw new JSONException("JSONArray[" + index + "] is not a boolean.");
} }
/** /**
* Get the double value associated with an index. * Get the double value associated with an index.
* *
* @param index The index must be between 0 and length() - 1. * @param index
* The index must be between 0 and length() - 1.
* @return The value. * @return The value.
* @throws JSONException If the key is not found or if the value cannot * @throws JSONException
* be converted to a number. * If the key is not found or if the value cannot be converted
* to a number.
*/ */
public double getDouble(int index) throws JSONException { public double getDouble(int index) throws JSONException {
Object object = this.get(index); Object object = this.get(index);
try { try {
return object instanceof Number return object instanceof Number ? ((Number) object).doubleValue()
? ((Number)object).doubleValue()
: Double.parseDouble((String) object); : Double.parseDouble((String) object);
} catch (Exception e) { } catch (Exception e) {
throw new JSONException("JSONArray[" + index + throw new JSONException("JSONArray[" + index + "] is not a number.");
"] is not a number.");
} }
} }
/** /**
* Get the int value associated with an index. * Get the int value associated with an index.
* *
* @param index The index must be between 0 and length() - 1. * @param index
* The index must be between 0 and length() - 1.
* @return The value. * @return The value.
* @throws JSONException If the key is not found or if the value is not a number. * @throws JSONException
* If the key is not found or if the value is not a number.
*/ */
public int getInt(int index) throws JSONException { public int getInt(int index) throws JSONException {
Object object = this.get(index); Object object = this.get(index);
try { try {
return object instanceof Number return object instanceof Number ? ((Number) object).intValue()
? ((Number)object).intValue()
: Integer.parseInt((String) object); : Integer.parseInt((String) object);
} catch (Exception e) { } catch (Exception e) {
throw new JSONException("JSONArray[" + index + throw new JSONException("JSONArray[" + index + "] is not a number.");
"] is not a number.");
} }
} }
/** /**
* Get the JSONArray associated with an index. * Get the JSONArray associated with an index.
* @param index The index must be between 0 and length() - 1. *
* @param index
* The index must be between 0 and length() - 1.
* @return A JSONArray value. * @return A JSONArray value.
* @throws JSONException If there is no value for the index. or if the * @throws JSONException
* value is not a JSONArray * If there is no value for the index. or if the value is not a
* JSONArray
*/ */
public JSONArray getJSONArray(int index) throws JSONException { public JSONArray getJSONArray(int index) throws JSONException {
Object object = this.get(index); Object object = this.get(index);
if (object instanceof JSONArray) { if (object instanceof JSONArray) {
return (JSONArray) object; return (JSONArray) object;
} }
throw new JSONException("JSONArray[" + index + throw new JSONException("JSONArray[" + index + "] is not a JSONArray.");
"] is not a JSONArray.");
} }
/** /**
* Get the JSONObject associated with an index. * Get the JSONObject associated with an index.
* @param index subscript *
* @param index
* subscript
* @return A JSONObject value. * @return A JSONObject value.
* @throws JSONException If there is no value for the index or if the * @throws JSONException
* value is not a JSONObject * If there is no value for the index or if the value is not a
* JSONObject
*/ */
public JSONObject getJSONObject(int index) throws JSONException { public JSONObject getJSONObject(int index) throws JSONException {
Object object = this.get(index); Object object = this.get(index);
if (object instanceof JSONObject) { if (object instanceof JSONObject) {
return (JSONObject) object; return (JSONObject) object;
} }
throw new JSONException("JSONArray[" + index + throw new JSONException("JSONArray[" + index + "] is not a JSONObject.");
"] is not a JSONObject.");
} }
/** /**
* Get the long value associated with an index. * Get the long value associated with an index.
* *
* @param index The index must be between 0 and length() - 1. * @param index
* The index must be between 0 and length() - 1.
* @return The value. * @return The value.
* @throws JSONException If the key is not found or if the value cannot * @throws JSONException
* be converted to a number. * If the key is not found or if the value cannot be converted
* to a number.
*/ */
public long getLong(int index) throws JSONException { public long getLong(int index) throws JSONException {
Object object = this.get(index); Object object = this.get(index);
try { try {
return object instanceof Number return object instanceof Number ? ((Number) object).longValue()
? ((Number)object).longValue()
: Long.parseLong((String) object); : Long.parseLong((String) object);
} catch (Exception e) { } catch (Exception e) {
throw new JSONException("JSONArray[" + index + throw new JSONException("JSONArray[" + index + "] is not a number.");
"] is not a number.");
} }
} }
/** /**
* Get the string associated with an index. * Get the string associated with an index.
* @param index The index must be between 0 and length() - 1. *
* @param index
* The index must be between 0 and length() - 1.
* @return A string value. * @return A string value.
* @throws JSONException If there is no string value for the index. * @throws JSONException
* If there is no string value for the index.
*/ */
public String getString(int index) throws JSONException { public String getString(int index) throws JSONException {
Object object = this.get(index); Object object = this.get(index);
@ -328,24 +336,36 @@ public class JSONArray {
throw new JSONException("JSONArray[" + index + "] not a string."); throw new JSONException("JSONArray[" + index + "] not a string.");
} }
/** /**
* Determine if the value is null. * Determine if the value is null.
* @param index The index must be between 0 and length() - 1. *
* @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. * @return true if the value at the index is null, or if there is no value.
*/ */
public boolean isNull(int index) { public boolean isNull(int index) {
return JSONObject.NULL.equals(this.opt(index)); return JSONObject.NULL.equals(this.opt(index));
} }
/**
* Returns an iterator.
*
* @return An iterator.
*/
public Iterator iterator() {
return this.myArrayList.iterator();
}
/** /**
* Make a string from the contents of this JSONArray. The * Make a string from the contents of this JSONArray. The
* <code>separator</code> string is inserted between each element. * <code>separator</code> string is inserted between each element. Warning:
* Warning: This method assumes that the data structure is acyclical. * This method assumes that the data structure is acyclical.
* @param separator A string that will be inserted between the elements. *
* @param separator
* A string that will be inserted between the elements.
* @return a string. * @return a string.
* @throws JSONException If the array contains an invalid number. * @throws JSONException
* If the array contains an invalid number.
*/ */
public String join(String separator) throws JSONException { public String join(String separator) throws JSONException {
int len = this.length(); int len = this.length();
@ -360,7 +380,6 @@ public class JSONArray {
return sb.toString(); return sb.toString();
} }
/** /**
* Get the number of elements in the JSONArray, included nulls. * Get the number of elements in the JSONArray, included nulls.
* *
@ -370,40 +389,40 @@ public class JSONArray {
return this.myArrayList.size(); return this.myArrayList.size();
} }
/** /**
* Get the optional object value associated with an index. * 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 * @param index
* object at that index. * The index must be between 0 and length() - 1.
* @return An object value, or null if there is no object at that index.
*/ */
public Object opt(int index) { public Object opt(int index) {
return (index < 0 || index >= this.length()) return (index < 0 || index >= this.length()) ? null : this.myArrayList
? null .get(index);
: this.myArrayList.get(index);
} }
/** /**
* Get the optional boolean value associated with an index. * Get the optional boolean value associated with an index. It returns false
* It returns false if there is no value at that index, * if there is no value at that index, or if the value is not Boolean.TRUE
* or if the value is not Boolean.TRUE or the String "true". * or the String "true".
* *
* @param index The index must be between 0 and length() - 1. * @param index
* The index must be between 0 and length() - 1.
* @return The truth. * @return The truth.
*/ */
public boolean optBoolean(int index) { public boolean optBoolean(int index) {
return this.optBoolean(index, false); return this.optBoolean(index, false);
} }
/** /**
* Get the optional boolean value associated with an index. * Get the optional boolean value associated with an index. It returns the
* It returns the defaultValue if there is no value at that index or if * defaultValue if there is no value at that index or if it is not a Boolean
* it is not a Boolean or the String "true" or "false" (case insensitive). * or the String "true" or "false" (case insensitive).
* *
* @param index The index must be between 0 and length() - 1. * @param index
* @param defaultValue A boolean default. * The index must be between 0 and length() - 1.
* @param defaultValue
* A boolean default.
* @return The truth. * @return The truth.
*/ */
public boolean optBoolean(int index, boolean defaultValue) { public boolean optBoolean(int index, boolean defaultValue) {
@ -414,27 +433,28 @@ public class JSONArray {
} }
} }
/** /**
* Get the optional double value associated with an index. * Get the optional double value associated with an index. NaN is returned
* NaN is returned if there is no value for the index, * if there is no value for the index, or if the value is not a number and
* or if the value is not a number and cannot be converted to a number. * cannot be converted to a number.
* *
* @param index The index must be between 0 and length() - 1. * @param index
* The index must be between 0 and length() - 1.
* @return The value. * @return The value.
*/ */
public double optDouble(int index) { public double optDouble(int index) {
return this.optDouble(index, Double.NaN); return this.optDouble(index, Double.NaN);
} }
/** /**
* Get the optional double value associated with an index. * Get the optional double value associated with an index. The defaultValue
* The defaultValue is returned if there is no value for the index, * is returned if there is no value for the index, or if the value is not a
* or if the value is not a number and cannot be converted to a number. * number and cannot be converted to a number.
* *
* @param index subscript * @param index
* @param defaultValue The default value. * subscript
* @param defaultValue
* The default value.
* @return The value. * @return The value.
*/ */
public double optDouble(int index, double defaultValue) { public double optDouble(int index, double defaultValue) {
@ -445,26 +465,28 @@ public class JSONArray {
} }
} }
/** /**
* Get the optional int value associated with an index. * Get the optional int value associated with an index. Zero is returned if
* Zero is returned if there is no value for the index, * there is no value for the index, or if the value is not a number and
* or if the value is not a number and cannot be converted to a number. * cannot be converted to a number.
* *
* @param index The index must be between 0 and length() - 1. * @param index
* The index must be between 0 and length() - 1.
* @return The value. * @return The value.
*/ */
public int optInt(int index) { public int optInt(int index) {
return this.optInt(index, 0); return this.optInt(index, 0);
} }
/** /**
* Get the optional int value associated with an index. * Get the optional int value associated with an index. The defaultValue is
* The defaultValue is returned if there is no value for the index, * returned if there is no value for the index, or if the value is not a
* or if the value is not a number and cannot be converted to a number. * number and cannot be converted to a number.
* @param index The index must be between 0 and length() - 1. *
* @param defaultValue The default value. * @param index
* The index must be between 0 and length() - 1.
* @param defaultValue
* The default value.
* @return The value. * @return The value.
*/ */
public int optInt(int index, int defaultValue) { public int optInt(int index, int defaultValue) {
@ -475,25 +497,26 @@ public class JSONArray {
} }
} }
/** /**
* Get the optional JSONArray associated with an index. * Get the optional JSONArray associated with an index.
* @param index subscript *
* @return A JSONArray value, or null if the index has no value, * @param index
* or if the value is not a JSONArray. * subscript
* @return A JSONArray value, or null if the index has no value, or if the
* value is not a JSONArray.
*/ */
public JSONArray optJSONArray(int index) { public JSONArray optJSONArray(int index) {
Object o = this.opt(index); Object o = this.opt(index);
return o instanceof JSONArray ? (JSONArray) o : null; return o instanceof JSONArray ? (JSONArray) o : null;
} }
/** /**
* Get the optional JSONObject associated with an index. * Get the optional JSONObject associated with an index. Null is returned if
* Null is returned if the key is not found, or null if the index has * the key is not found, or null if the index has no value, or if the value
* no value, or if the value is not a JSONObject. * is not a JSONObject.
* *
* @param index The index must be between 0 and length() - 1. * @param index
* The index must be between 0 and length() - 1.
* @return A JSONObject value. * @return A JSONObject value.
*/ */
public JSONObject optJSONObject(int index) { public JSONObject optJSONObject(int index) {
@ -501,26 +524,28 @@ public class JSONArray {
return o instanceof JSONObject ? (JSONObject) o : null; return o instanceof JSONObject ? (JSONObject) o : null;
} }
/** /**
* Get the optional long value associated with an index. * Get the optional long value associated with an index. Zero is returned if
* Zero is returned if there is no value for the index, * there is no value for the index, or if the value is not a number and
* or if the value is not a number and cannot be converted to a number. * cannot be converted to a number.
* *
* @param index The index must be between 0 and length() - 1. * @param index
* The index must be between 0 and length() - 1.
* @return The value. * @return The value.
*/ */
public long optLong(int index) { public long optLong(int index) {
return this.optLong(index, 0); return this.optLong(index, 0);
} }
/** /**
* Get the optional long value associated with an index. * Get the optional long value associated with an index. The defaultValue is
* The defaultValue is returned if there is no value for the index, * returned if there is no value for the index, or if the value is not a
* or if the value is not a number and cannot be converted to a number. * number and cannot be converted to a number.
* @param index The index must be between 0 and length() - 1. *
* @param defaultValue The default value. * @param index
* The index must be between 0 and length() - 1.
* @param defaultValue
* The default value.
* @return The value. * @return The value.
*/ */
public long optLong(int index, long defaultValue) { public long optLong(int index, long defaultValue) {
@ -531,40 +556,40 @@ public class JSONArray {
} }
} }
/** /**
* Get the optional string value associated with an index. It returns an * Get the optional string value associated with an index. It returns an
* empty string if there is no value at that index. If the value * empty string if there is no value at that index. If the value is not a
* is not a string and is not null, then it is coverted to a string. * string and is not null, then it is coverted to a string.
* *
* @param index The index must be between 0 and length() - 1. * @param index
* The index must be between 0 and length() - 1.
* @return A String value. * @return A String value.
*/ */
public String optString(int index) { public String optString(int index) {
return this.optString(index, ""); return this.optString(index, "");
} }
/** /**
* Get the optional string associated with an index. * Get the optional string associated with an index. The defaultValue is
* The defaultValue is returned if the key is not found. * returned if the key is not found.
* *
* @param index The index must be between 0 and length() - 1. * @param index
* @param defaultValue The default value. * The index must be between 0 and length() - 1.
* @param defaultValue
* The default value.
* @return A String value. * @return A String value.
*/ */
public String optString(int index, String defaultValue) { public String optString(int index, String defaultValue) {
Object object = this.opt(index); Object object = this.opt(index);
return JSONObject.NULL.equals(object) return JSONObject.NULL.equals(object) ? defaultValue : object
? defaultValue .toString();
: object.toString();
} }
/** /**
* Append a boolean value. This increases the array's length by one. * Append a boolean value. This increases the array's length by one.
* *
* @param value A boolean value. * @param value
* A boolean value.
* @return this. * @return this.
*/ */
public JSONArray put(boolean value) { public JSONArray put(boolean value) {
@ -572,11 +597,12 @@ public class JSONArray {
return this; return this;
} }
/** /**
* Put a value in the JSONArray, where the value will be a * Put a value in the JSONArray, where the value will be a JSONArray which
* JSONArray which is produced from a Collection. * is produced from a Collection.
* @param value A Collection value. *
* @param value
* A Collection value.
* @return this. * @return this.
*/ */
public JSONArray put(Collection value) { public JSONArray put(Collection value) {
@ -584,12 +610,13 @@ public class JSONArray {
return this; return this;
} }
/** /**
* Append a double value. This increases the array's length by one. * Append a double value. This increases the array's length by one.
* *
* @param value A double value. * @param value
* @throws JSONException if the value is not finite. * A double value.
* @throws JSONException
* if the value is not finite.
* @return this. * @return this.
*/ */
public JSONArray put(double value) throws JSONException { public JSONArray put(double value) throws JSONException {
@ -599,11 +626,11 @@ public class JSONArray {
return this; return this;
} }
/** /**
* Append an int value. This increases the array's length by one. * Append an int value. This increases the array's length by one.
* *
* @param value An int value. * @param value
* An int value.
* @return this. * @return this.
*/ */
public JSONArray put(int value) { public JSONArray put(int value) {
@ -611,11 +638,11 @@ public class JSONArray {
return this; return this;
} }
/** /**
* Append an long value. This increases the array's length by one. * Append an long value. This increases the array's length by one.
* *
* @param value A long value. * @param value
* A long value.
* @return this. * @return this.
*/ */
public JSONArray put(long value) { public JSONArray put(long value) {
@ -623,11 +650,12 @@ public class JSONArray {
return this; return this;
} }
/** /**
* Put a value in the JSONArray, where the value will be a * Put a value in the JSONArray, where the value will be a JSONObject which
* JSONObject which is produced from a Map. * is produced from a Map.
* @param value A Map value. *
* @param value
* A Map value.
* @return this. * @return this.
*/ */
public JSONArray put(Map value) { public JSONArray put(Map value) {
@ -635,11 +663,12 @@ public class JSONArray {
return this; return this;
} }
/** /**
* Append an object value. This increases the array's length by one. * Append an object value. This increases the array's length by one.
* @param value An object value. The value should be a *
* Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the * @param value
* An object value. The value should be a Boolean, Double,
* Integer, JSONArray, JSONObject, Long, or String, or the
* JSONObject.NULL object. * JSONObject.NULL object.
* @return this. * @return this.
*/ */
@ -648,109 +677,128 @@ public class JSONArray {
return this; return this;
} }
/** /**
* Put or replace a boolean value in the JSONArray. If the index is greater * Put or replace a boolean value in the JSONArray. If the index is greater
* than the length of the JSONArray, then null elements will be added as * than the length of the JSONArray, then null elements will be added as
* necessary to pad it out. * necessary to pad it out.
* @param index The subscript. *
* @param value A boolean value. * @param index
* The subscript.
* @param value
* A boolean value.
* @return this. * @return this.
* @throws JSONException If the index is negative. * @throws JSONException
* If the index is negative.
*/ */
public JSONArray put(int index, boolean value) throws JSONException { public JSONArray put(int index, boolean value) throws JSONException {
this.put(index, value ? Boolean.TRUE : Boolean.FALSE); this.put(index, value ? Boolean.TRUE : Boolean.FALSE);
return this; return this;
} }
/** /**
* Put a value in the JSONArray, where the value will be a * Put a value in the JSONArray, where the value will be a JSONArray which
* JSONArray which is produced from a Collection. * is produced from a Collection.
* @param index The subscript. *
* @param value A Collection value. * @param index
* The subscript.
* @param value
* A Collection value.
* @return this. * @return this.
* @throws JSONException If the index is negative or if the value is * @throws JSONException
* not finite. * If the index is negative or if the value is not finite.
*/ */
public JSONArray put(int index, Collection value) throws JSONException { public JSONArray put(int index, Collection value) throws JSONException {
this.put(index, new JSONArray(value)); this.put(index, new JSONArray(value));
return this; return this;
} }
/** /**
* Put or replace a double value. If the index is greater than the length of * Put or replace a double value. If the index is greater than the length of
* the JSONArray, then null elements will be added as necessary to pad * the JSONArray, then null elements will be added as necessary to pad it
* it out. * out.
* @param index The subscript. *
* @param value A double value. * @param index
* The subscript.
* @param value
* A double value.
* @return this. * @return this.
* @throws JSONException If the index is negative or if the value is * @throws JSONException
* not finite. * If the index is negative or if the value is not finite.
*/ */
public JSONArray put(int index, double value) throws JSONException { public JSONArray put(int index, double value) throws JSONException {
this.put(index, new Double(value)); this.put(index, new Double(value));
return this; return this;
} }
/** /**
* Put or replace an int value. If the index is greater than the length of * Put or replace an int value. If the index is greater than the length of
* the JSONArray, then null elements will be added as necessary to pad * the JSONArray, then null elements will be added as necessary to pad it
* it out. * out.
* @param index The subscript. *
* @param value An int value. * @param index
* The subscript.
* @param value
* An int value.
* @return this. * @return this.
* @throws JSONException If the index is negative. * @throws JSONException
* If the index is negative.
*/ */
public JSONArray put(int index, int value) throws JSONException { public JSONArray put(int index, int value) throws JSONException {
this.put(index, new Integer(value)); this.put(index, new Integer(value));
return this; return this;
} }
/** /**
* Put or replace a long value. If the index is greater than the length of * Put or replace a long value. If the index is greater than the length of
* the JSONArray, then null elements will be added as necessary to pad * the JSONArray, then null elements will be added as necessary to pad it
* it out. * out.
* @param index The subscript. *
* @param value A long value. * @param index
* The subscript.
* @param value
* A long value.
* @return this. * @return this.
* @throws JSONException If the index is negative. * @throws JSONException
* If the index is negative.
*/ */
public JSONArray put(int index, long value) throws JSONException { public JSONArray put(int index, long value) throws JSONException {
this.put(index, new Long(value)); this.put(index, new Long(value));
return this; return this;
} }
/** /**
* Put a value in the JSONArray, where the value will be a * Put a value in the JSONArray, where the value will be a JSONObject that
* JSONObject that is produced from a Map. * is produced from a Map.
* @param index The subscript. *
* @param value The Map value. * @param index
* The subscript.
* @param value
* The Map value.
* @return this. * @return this.
* @throws JSONException If the index is negative or if the the value is * @throws JSONException
* an invalid number. * If the index is negative or if the the value is an invalid
* number.
*/ */
public JSONArray put(int index, Map value) throws JSONException { public JSONArray put(int index, Map value) throws JSONException {
this.put(index, new JSONObject(value)); this.put(index, new JSONObject(value));
return this; return this;
} }
/** /**
* Put or replace an object value in the JSONArray. If the index is greater * 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 * than the length of the JSONArray, then null elements will be added as
* necessary to pad it out. * necessary to pad it out.
* @param index The subscript. *
* @param value The value to put into the array. The value should be a * @param index
* Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the * The subscript.
* JSONObject.NULL object. * @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. * @return this.
* @throws JSONException If the index is negative or if the the value is * @throws JSONException
* an invalid number. * If the index is negative or if the the value is an invalid
* number.
*/ */
public JSONArray put(int index, Object value) throws JSONException { public JSONArray put(int index, Object value) throws JSONException {
JSONObject.testValidity(value); JSONObject.testValidity(value);
@ -768,12 +816,13 @@ public class JSONArray {
return this; return this;
} }
/** /**
* Remove an index and close the hole. * 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, * @param index
* or null if there was no value. * 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 remove(int index) {
Object o = this.opt(index); Object o = this.opt(index);
@ -781,15 +830,17 @@ public class JSONArray {
return o; return o;
} }
/** /**
* Produce a JSONObject by combining a JSONArray of names with the values * Produce a JSONObject by combining a JSONArray of names with the values of
* of this JSONArray. * this JSONArray.
* @param names A JSONArray containing a list of key strings. These will be *
* @param names
* A JSONArray containing a list of key strings. These will be
* paired with the values. * paired with the values.
* @return A JSONObject, or null if there are no names or if this JSONArray * @return A JSONObject, or null if there are no names or if this JSONArray
* has no values. * has no values.
* @throws JSONException If any of the names are null. * @throws JSONException
* If any of the names are null.
*/ */
public JSONObject toJSONObject(JSONArray names) throws JSONException { public JSONObject toJSONObject(JSONArray names) throws JSONException {
if (names == null || names.length() == 0 || this.length() == 0) { if (names == null || names.length() == 0 || this.length() == 0) {
@ -802,17 +853,16 @@ public class JSONArray {
return jo; return jo;
} }
/** /**
* Make a JSON text of this JSONArray. For compactness, no * Make a JSON text of this JSONArray. For compactness, no unnecessary
* unnecessary whitespace is added. If it is not possible to produce a * whitespace is added. If it is not possible to produce a syntactically
* syntactically correct JSON text then null will be returned instead. This * correct JSON text then null will be returned instead. This could occur if
* could occur if the array contains an invalid number. * the array contains an invalid number.
* <p> * <p>
* Warning: This method assumes that the data structure is acyclical. * Warning: This method assumes that the data structure is acyclical.
* *
* @return a printable, displayable, transmittable * @return a printable, displayable, transmittable representation of the
* representation of the array. * array.
*/ */
public String toString() { public String toString() {
try { try {
@ -822,16 +872,16 @@ public class JSONArray {
} }
} }
/** /**
* Make a prettyprinted JSON text of this JSONArray. * Make a prettyprinted JSON text of this JSONArray. Warning: This method
* Warning: This method assumes that the data structure is acyclical. * assumes that the data structure is acyclical.
* @param indentFactor The number of spaces to add to each level of *
* indentation. * @param indentFactor
* @return a printable, displayable, transmittable * The number of spaces to add to each level of indentation.
* representation of the object, beginning * @return a printable, displayable, transmittable representation of the
* with <code>[</code>&nbsp;<small>(left bracket)</small> and ending * object, beginning with <code>[</code>&nbsp;<small>(left
* with <code>]</code>&nbsp;<small>(right bracket)</small>. * bracket)</small> and ending with <code>]</code>
* &nbsp;<small>(right bracket)</small>.
* @throws JSONException * @throws JSONException
*/ */
public String toString(int indentFactor) throws JSONException { public String toString(int indentFactor) throws JSONException {