Package com.github.openjson
Class JSONArray
- java.lang.Object
-
- com.github.openjson.JSONArray
-
public class JSONArray extends Object
A dense indexed sequence of values. Values may be any mix ofJSONObjects, otherJSONArrays, Strings, Booleans, Integers, Longs, Doubles,nullorJSONObject.NULL. Values may not beNaNs,infinities, or of any type not listed here.JSONArrayhas the same type coercion behavior and optional/mandatory accessors asJSONObject. See that class' documentation for details. Warning: this class represents null in two incompatible ways: the standard Javanullreference, and the sentinel valueJSONObject.NULL. In particular,getfails if the requested index holds the null reference, but succeeds if it holdsJSONObject.NULL. Instances of this class are not thread safe. Although this class is non-final, it was not designed for inheritance and should not be subclassed. In particular, self-use by overridable methods is not specified. See Effective Java Item 17, "Design and Document or inheritance or else prohibit it" for further information.
-
-
Constructor Summary
Constructors Constructor Description JSONArray()Creates aJSONArraywith no values.JSONArray(JSONTokener readFrom)Creates a newJSONArraywith values from the next array in the tokener.JSONArray(Object array)Creates a newJSONArraywith values from the given primitive array.JSONArray(String json)Creates a newJSONArraywith values from the JSON string.JSONArray(Collection<?> copyFrom)Creates a newJSONArrayby copying all values from the given collection.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidencode(JSONStringer stringer)Encodes this array usingJSONStringerprovidedbooleanequals(Object o)Objectget(int index)Returns the value atindex.booleangetBoolean(int index)Returns the value atindexif it exists and is a boolean or can be coerced to a boolean.doublegetDouble(int index)Returns the value atindexif it exists and is a double or can be coerced to a double.intgetInt(int index)Returns the value atindexif it exists and is an int or can be coerced to an int.JSONArraygetJSONArray(int index)Returns the value atindexif it exists and is aJSONArray.JSONObjectgetJSONObject(int index)Returns the value atindexif it exists and is aJSONObject.longgetLong(int index)Returns the value atindexif it exists and is a long or can be coerced to a long.StringgetString(int index)Returns the value atindexif it exists, coercing it if necessary.inthashCode()booleanisNull(int index)Returns true if this array has no value atindex, or if its value is thenullreference orJSONObject.NULL.Stringjoin(String separator)Returns a new string by alternating this array's values withseparator.intlength()Objectopt(int index)Returns the value atindex, or null if the array has no value atindex.booleanoptBoolean(int index)Returns the value atindexif it exists and is a boolean or can be coerced to a boolean.booleanoptBoolean(int index, boolean fallback)Returns the value atindexif it exists and is a boolean or can be coerced to a boolean.doubleoptDouble(int index)Returns the value atindexif it exists and is a double or can be coerced to a double.doubleoptDouble(int index, double fallback)Returns the value atindexif it exists and is a double or can be coerced to a double.intoptInt(int index)Returns the value atindexif it exists and is an int or can be coerced to an int.intoptInt(int index, int fallback)Returns the value atindexif it exists and is an int or can be coerced to an int.JSONArrayoptJSONArray(int index)Returns the value atindexif it exists and is aJSONArray.JSONObjectoptJSONObject(int index)Returns the value atindexif it exists and is aJSONObject.longoptLong(int index)Returns the value atindexif it exists and is a long or can be coerced to a long.longoptLong(int index, long fallback)Returns the value atindexif it exists and is a long or can be coerced to a long.StringoptString(int index)Returns the value atindexif it exists, coercing it if necessary.StringoptString(int index, String fallback)Returns the value atindexif it exists, coercing it if necessary.JSONArrayput(boolean value)Appendsvalueto the end of this array.JSONArrayput(double value)Appendsvalueto the end of this array.JSONArrayput(int value)Appendsvalueto the end of this array.JSONArrayput(int index, boolean value)Sets the value atindextovalue, null padding this array to the required length if necessary.JSONArrayput(int index, double value)Sets the value atindextovalue, null padding this array to the required length if necessary.JSONArrayput(int index, int value)Sets the value atindextovalue, null padding this array to the required length if necessary.JSONArrayput(int index, long value)Sets the value atindextovalue, null padding this array to the required length if necessary.JSONArrayput(int index, Object value)Sets the value atindextovalue, null padding this array to the required length if necessary.JSONArrayput(int index, Collection<?> value)Sets the value atindextovaluewrapped intoJSONArray, null padding this array to the required length if necessary.JSONArrayput(long value)Appendsvalueto the end of this array.JSONArrayput(Object value)Appendsvalueto the end of this array.JSONArrayput(Collection<?> value)Appendsvaluewrapped byJSONArrayto the end of this array.Objectremove(int index)Removes and returns the value atindex, or null if the array has no value atindex.JSONObjecttoJSONObject(JSONArray names)Returns a new object whose values are the values in this array, and whose names are the values innames.StringtoString()Encodes this array as a compact JSON string, such as:StringtoString(int indentSpaces)Encodes this array as a human readable JSON string for debugging, such as:StringtoString(JSONStringer stringer)Encodes this array usingJSONStringerprovided
-
-
-
Constructor Detail
-
JSONArray
public JSONArray()
Creates aJSONArraywith no values.
-
JSONArray
public JSONArray(Collection<?> copyFrom)
Creates a newJSONArrayby copying all values from the given collection.- Parameters:
copyFrom- a collection whose values are of supported types. Unsupported values are not permitted and will yield an array in an inconsistent state.
-
JSONArray
public JSONArray(JSONTokener readFrom) throws JSONException
Creates a newJSONArraywith values from the next array in the tokener.- Parameters:
readFrom- a tokener whose nextValue() method will yield aJSONArray.- Throws:
JSONException- if the parse fails or doesn't yield aJSONArray.
-
JSONArray
public JSONArray(String json) throws JSONException
Creates a newJSONArraywith values from the JSON string.- Parameters:
json- a JSON-encoded string containing an array.- Throws:
JSONException- if the parse fails or doesn't yield aJSONArray.
-
JSONArray
public JSONArray(Object array) throws JSONException
Creates a newJSONArraywith values from the given primitive array.- Parameters:
array- The values to use.- Throws:
JSONException- if any of the values are non-finite double values (i.e. NaN or infinite)
-
-
Method Detail
-
length
public int length()
- Returns:
- Returns the number of values in this array.
-
put
public JSONArray put(boolean value)
Appendsvalueto the end of this array.- Parameters:
value- The value to append.- Returns:
- this array.
-
put
public JSONArray put(double value) throws JSONException
Appendsvalueto the end of this array.- Parameters:
value- a finite value. May not beNaNsorinfinities.- Returns:
- this array.
- Throws:
JSONException- If the value is unacceptable.
-
put
public JSONArray put(int value)
Appendsvalueto the end of this array.- Parameters:
value- The value to append.- Returns:
- this array.
-
put
public JSONArray put(long value)
Appendsvalueto the end of this array.- Parameters:
value- The value to append.- Returns:
- this array.
-
put
public JSONArray put(Collection<?> value)
Appendsvaluewrapped byJSONArrayto the end of this array.- Parameters:
value- any collection.- Returns:
- this array.
-
put
public JSONArray put(Object value)
Appendsvalueto the end of this array.- Parameters:
value- aJSONObject,JSONArray, String, Boolean, Integer, Long, Double,JSONObject.NULL, ornull. May not beNaNsorinfinities. Unsupported values are not permitted and will cause the array to be in an inconsistent state.- Returns:
- this array.
-
put
public JSONArray put(int index, boolean value) throws JSONException
Sets the value atindextovalue, null padding this array to the required length if necessary. If a value already exists atindex, it will be replaced.- Parameters:
index- Where to put the value.value- The value to set.- Returns:
- this array.
- Throws:
JSONException- This should never happen.
-
put
public JSONArray put(int index, double value) throws JSONException
Sets the value atindextovalue, null padding this array to the required length if necessary. If a value already exists atindex, it will be replaced.- Parameters:
index- Where to put the value.value- a finite value. May not beNaNsorinfinities.- Returns:
- this array.
- Throws:
JSONException- If the value is not a finite value.
-
put
public JSONArray put(int index, int value) throws JSONException
Sets the value atindextovalue, null padding this array to the required length if necessary. If a value already exists atindex, it will be replaced.- Parameters:
index- Where to put the value.value- The value to set.- Returns:
- this array.
- Throws:
JSONException- Should never actually happen.
-
put
public JSONArray put(int index, long value) throws JSONException
Sets the value atindextovalue, null padding this array to the required length if necessary. If a value already exists atindex, it will be replaced.- Parameters:
index- Where to put the value.value- The value to set.- Returns:
- this array.
- Throws:
JSONException- Should never actually happen.
-
put
public JSONArray put(int index, Collection<?> value) throws JSONException
Sets the value atindextovaluewrapped intoJSONArray, null padding this array to the required length if necessary. If a value already exists atindex, it will be replaced.- Parameters:
index- Where to put the value.value- The value to set.- Returns:
- this array.
- Throws:
JSONException- Should never actually happen.
-
put
public JSONArray put(int index, Object value) throws JSONException
Sets the value atindextovalue, null padding this array to the required length if necessary. If a value already exists atindex, it will be replaced.- Parameters:
index- Where to put the value.value- aJSONObject,JSONArray, String, Boolean, Integer, Long, Double,JSONObject.NULL, ornull. May not beNaNsorinfinities.- Returns:
- this array.
- Throws:
JSONException- If the value cannot be represented as a finite double value.
-
isNull
public boolean isNull(int index)
Returns true if this array has no value atindex, or if its value is thenullreference orJSONObject.NULL.- Parameters:
index- Which value to check.- Returns:
- true if the value is null.
-
get
public Object get(int index) throws JSONException
Returns the value atindex.- Parameters:
index- Which value to get.- Returns:
- the value at the specified location.
- Throws:
JSONException- if this array has no value atindex, or if that value is thenullreference. This method returns normally if the value isJSONObject#NULL.
-
opt
public Object opt(int index)
Returns the value atindex, or null if the array has no value atindex.- Parameters:
index- Which value to get.- Returns:
- the value at the specified location.
-
remove
public Object remove(int index)
Removes and returns the value atindex, or null if the array has no value atindex.- Parameters:
index- Which value to remove.- Returns:
- The value previously at the specified location.
-
getBoolean
public boolean getBoolean(int index) throws JSONExceptionReturns the value atindexif it exists and is a boolean or can be coerced to a boolean.- Parameters:
index- Which value to get.- Returns:
- the value at the specified location.
- Throws:
JSONException- if the value atindexdoesn't exist or cannot be coerced to a boolean.
-
optBoolean
public boolean optBoolean(int index)
Returns the value atindexif it exists and is a boolean or can be coerced to a boolean. Returns false otherwise.- Parameters:
index- Which value to get.- Returns:
- the value at the specified location.
-
optBoolean
public boolean optBoolean(int index, boolean fallback)Returns the value atindexif it exists and is a boolean or can be coerced to a boolean. Returnsfallbackotherwise.- Parameters:
index- Which value to get.fallback- the fallback value to return if no value exists.- Returns:
- the value at the specified location or the fallback value.
-
getDouble
public double getDouble(int index) throws JSONExceptionReturns the value atindexif it exists and is a double or can be coerced to a double.- Parameters:
index- Which value to get.- Returns:
- the value at the specified location.
- Throws:
JSONException- if the value atindexdoesn't exist or cannot be coerced to a double.
-
optDouble
public double optDouble(int index)
Returns the value atindexif it exists and is a double or can be coerced to a double. ReturnsNaNotherwise.- Parameters:
index- Which value to get.- Returns:
- the value at the specified location.
-
optDouble
public double optDouble(int index, double fallback)Returns the value atindexif it exists and is a double or can be coerced to a double. Returnsfallbackotherwise.- Parameters:
index- Which value to get.fallback- The fallback value to use if no value is at the specified location.- Returns:
- the value at the specified location or the fallback value.
-
getInt
public int getInt(int index) throws JSONExceptionReturns the value atindexif it exists and is an int or can be coerced to an int.- Parameters:
index- Which value to get.- Returns:
- the value at the specified location.
- Throws:
JSONException- if the value atindexdoesn't exist or cannot be coerced to a int.
-
optInt
public int optInt(int index)
Returns the value atindexif it exists and is an int or can be coerced to an int. Returns 0 otherwise.- Parameters:
index- Which value to get.- Returns:
- the value at the specified location.
-
optInt
public int optInt(int index, int fallback)Returns the value atindexif it exists and is an int or can be coerced to an int. Returnsfallbackotherwise.- Parameters:
index- Which value to get.fallback- The fallback value to use if no value is at the specified location.- Returns:
- the value at the specified location or the fallback value.
-
getLong
public long getLong(int index) throws JSONExceptionReturns the value atindexif it exists and is a long or can be coerced to a long.- Parameters:
index- Which value to get.- Returns:
- the value at the specified location.
- Throws:
JSONException- if the value atindexdoesn't exist or cannot be coerced to a long.
-
optLong
public long optLong(int index)
Returns the value atindexif it exists and is a long or can be coerced to a long. Returns 0 otherwise.- Parameters:
index- Which value to get.- Returns:
- the value at the specified location.
-
optLong
public long optLong(int index, long fallback)Returns the value atindexif it exists and is a long or can be coerced to a long. Returnsfallbackotherwise.- Parameters:
index- Which value to get.fallback- The fallback value to use if no value is at the specified location.- Returns:
- the value at the specified location or the fallback value.
-
getString
public String getString(int index) throws JSONException
Returns the value atindexif it exists, coercing it if necessary.- Parameters:
index- Which value to get.- Returns:
- the value at the specified location.
- Throws:
JSONException- if no such value exists.
-
optString
public String optString(int index)
Returns the value atindexif it exists, coercing it if necessary. Returns the empty string if no such value exists.- Parameters:
index- Which value to get.- Returns:
- the value at the specified location.
-
optString
public String optString(int index, String fallback)
Returns the value atindexif it exists, coercing it if necessary. Returnsfallbackif no such value exists.- Parameters:
index- Which value to get.fallback- The fallback value to use if no value is at the specified location.- Returns:
- the value at the specified location or the fallback value.
-
getJSONArray
public JSONArray getJSONArray(int index) throws JSONException
Returns the value atindexif it exists and is aJSONArray.- Parameters:
index- Which value to get.- Returns:
- the value at the specified location.
- Throws:
JSONException- if the value doesn't exist or is not aJSONArray.
-
optJSONArray
public JSONArray optJSONArray(int index)
Returns the value atindexif it exists and is aJSONArray. Returns null otherwise.- Parameters:
index- Which value to get.- Returns:
- the value at the specified location.
-
getJSONObject
public JSONObject getJSONObject(int index) throws JSONException
Returns the value atindexif it exists and is aJSONObject.- Parameters:
index- Which value to get.- Returns:
- the value at the specified location.
- Throws:
JSONException- if the value doesn't exist or is not aJSONObject.
-
optJSONObject
public JSONObject optJSONObject(int index)
Returns the value atindexif it exists and is aJSONObject. Returns null otherwise.- Parameters:
index- Which value to get.- Returns:
- the value at the specified location.
-
toJSONObject
public JSONObject toJSONObject(JSONArray names) throws JSONException
Returns a new object whose values are the values in this array, and whose names are the values innames. Names and values are paired up by index from 0 through to the shorter array's length. Names that are not strings will be coerced to strings. This method returns null if either array is empty.- Parameters:
names- The names to apply to the returned values.- Returns:
- the newly constructed object.
- Throws:
JSONException- Should not be possible.
-
join
public String join(String separator) throws JSONException
Returns a new string by alternating this array's values withseparator. This array's string values are quoted and have their special characters escaped. For example, the array containing the strings '12" pizza', 'taco' and 'soda' joined on '+' returns this:"12\" pizza"+"taco"+"soda"
- Parameters:
separator- The string used to separate the returned values.- Returns:
- the conjoined values.
- Throws:
JSONException- Only if there is a coding error.
-
toString
public String toString()
Encodes this array as a compact JSON string, such as:[94043,90210]
Note 1: this method will not output any fields with 'null' value. OverrideJSONStringer.entry(java.util.Map.Entry<java.lang.String, java.lang.Object>)method to have nulls printed. Note 2: this method will suppress any internal exceptions. UsetoString(JSONStringer)method directly to handle exceptions manually.
-
toString
public String toString(int indentSpaces) throws JSONException
Encodes this array as a human readable JSON string for debugging, such as:[ 94043, 90210 ]- Parameters:
indentSpaces- the number of spaces to indent for each level of nesting.- Returns:
- The string form of this array.
- Throws:
JSONException- Only if there is a coding error.
-
toString
public String toString(JSONStringer stringer) throws JSONException
Encodes this array usingJSONStringerprovided- Parameters:
stringer- -JSONStringerto be used for serialization- Returns:
- The string representation of this.
- Throws:
JSONException- On internal errors. Shouldn't happen.
-
encode
protected void encode(JSONStringer stringer)
Encodes this array usingJSONStringerprovided- Parameters:
stringer- -JSONStringerto be used for serialization- Throws:
JSONException- On internal errors. Shouldn't happen.
-
-