Package com.github.openjson
Class JSONArray
java.lang.Object
com.github.openjson.JSONArray
A dense indexed sequence of values. Values may be any mix of
JSONObjects
, other JSONArrays
, Strings,
Booleans, Integers, Longs, Doubles, null
or JSONObject.NULL
.
Values may not be NaNs
, infinities
, or of any type not listed here.
JSONArray
has the same type coercion behavior and
optional/mandatory accessors as JSONObject
. See that class'
documentation for details.
Warning: this class represents null in two incompatible
ways: the standard Java null
reference, and the sentinel value JSONObject.NULL
. In particular, get
fails if the requested index
holds the null reference, but succeeds if it holds JSONObject.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.-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates aJSONArray
with no values.JSONArray
(JSONTokener readFrom) Creates a newJSONArray
with values from the next array in the tokener.Creates a newJSONArray
with values from the given primitive array.Creates a newJSONArray
with values from the JSON string.JSONArray
(Collection<?> copyFrom) Creates a newJSONArray
by copying all values from the given collection. -
Method Summary
Modifier and TypeMethodDescription(package private) void
checkedPut
(Object value) Same asput(boolean)
, with added validity checks.protected void
encode
(JSONStringer stringer) Encodes this array usingJSONStringer
providedboolean
get
(int index) Returns the value atindex
.boolean
getBoolean
(int index) Returns the value atindex
if it exists and is a boolean or can be coerced to a boolean.double
getDouble
(int index) Returns the value atindex
if it exists and is a double or can be coerced to a double.int
getInt
(int index) Returns the value atindex
if it exists and is an int or can be coerced to an int.getJSONArray
(int index) Returns the value atindex
if it exists and is aJSONArray
.getJSONObject
(int index) Returns the value atindex
if it exists and is aJSONObject
.long
getLong
(int index) Returns the value atindex
if it exists and is a long or can be coerced to a long.getString
(int index) Returns the value atindex
if it exists, coercing it if necessary.int
hashCode()
boolean
isNull
(int index) Returns true if this array has no value atindex
, or if its value is thenull
reference orJSONObject.NULL
.iterator()
Returns a new string by alternating this array's values withseparator
.int
length()
opt
(int index) Returns the value atindex
, or null if the array has no value atindex
.boolean
optBoolean
(int index) Returns the value atindex
if it exists and is a boolean or can be coerced to a boolean.boolean
optBoolean
(int index, boolean fallback) Returns the value atindex
if it exists and is a boolean or can be coerced to a boolean.double
optDouble
(int index) Returns the value atindex
if it exists and is a double or can be coerced to a double.double
optDouble
(int index, double fallback) Returns the value atindex
if it exists and is a double or can be coerced to a double.int
optInt
(int index) Returns the value atindex
if it exists and is an int or can be coerced to an int.int
optInt
(int index, int fallback) Returns the value atindex
if it exists and is an int or can be coerced to an int.optJSONArray
(int index) Returns the value atindex
if it exists and is aJSONArray
.optJSONObject
(int index) Returns the value atindex
if it exists and is aJSONObject
.long
optLong
(int index) Returns the value atindex
if it exists and is a long or can be coerced to a long.long
optLong
(int index, long fallback) Returns the value atindex
if it exists and is a long or can be coerced to a long.optString
(int index) Returns the value atindex
if it exists, coercing it if necessary.Returns the value atindex
if it exists, coercing it if necessary.put
(boolean value) Appendsvalue
to the end of this array.put
(double value) Appendsvalue
to the end of this array.put
(int value) Appendsvalue
to the end of this array.put
(int index, boolean value) Sets the value atindex
tovalue
, null padding this array to the required length if necessary.put
(int index, double value) Sets the value atindex
tovalue
, null padding this array to the required length if necessary.put
(int index, int value) Sets the value atindex
tovalue
, null padding this array to the required length if necessary.put
(int index, long value) Sets the value atindex
tovalue
, null padding this array to the required length if necessary.Sets the value atindex
tovalue
, null padding this array to the required length if necessary.put
(int index, Collection<?> value) Sets the value atindex
tovalue
wrapped intoJSONArray
, null padding this array to the required length if necessary.put
(long value) Appendsvalue
to the end of this array.Appendsvalue
to the end of this array.put
(Collection<?> value) Appendsvalue
wrapped byJSONArray
to the end of this array.remove
(int index) Removes and returns the value atindex
, or null if the array has no value atindex
.toJSONObject
(JSONArray names) Returns a new object whose values are the values in this array, and whose names are the values innames
.toString()
Encodes this array as a compact JSON string, such as:toString
(int indentSpaces) Encodes this array as a human readable JSON string for debugging, such as:toString
(JSONStringer stringer) Encodes this array usingJSONStringer
providedMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
values
-
-
Constructor Details
-
JSONArray
public JSONArray()Creates aJSONArray
with no values. -
JSONArray
Creates a newJSONArray
by 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
Creates a newJSONArray
with 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
Creates a newJSONArray
with 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
Creates a newJSONArray
with 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 Details
-
length
public int length()- Returns:
- Returns the number of values in this array.
-
put
Appendsvalue
to the end of this array.- Parameters:
value
- The value to append.- Returns:
- this array.
-
put
Appendsvalue
to the end of this array.- Parameters:
value
- a finite value. May not beNaNs
orinfinities
.- Returns:
- this array.
- Throws:
JSONException
- If the value is unacceptable.
-
put
Appendsvalue
to the end of this array.- Parameters:
value
- The value to append.- Returns:
- this array.
-
put
Appendsvalue
to the end of this array.- Parameters:
value
- The value to append.- Returns:
- this array.
-
put
Appendsvalue
wrapped byJSONArray
to the end of this array.- Parameters:
value
- any collection.- Returns:
- this array.
-
put
Appendsvalue
to the end of this array.- Parameters:
value
- aJSONObject
,JSONArray
, String, Boolean, Integer, Long, Double,JSONObject.NULL
, ornull
. May not beNaNs
orinfinities
. Unsupported values are not permitted and will cause the array to be in an inconsistent state.- Returns:
- this array.
-
checkedPut
Same asput(boolean)
, with added validity checks.- Parameters:
value
- The value to append.- Throws:
JSONException
-
put
Sets the value atindex
tovalue
, 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
Sets the value atindex
tovalue
, 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 beNaNs
orinfinities
.- Returns:
- this array.
- Throws:
JSONException
- If the value is not a finite value.
-
put
Sets the value atindex
tovalue
, 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
Sets the value atindex
tovalue
, 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
Sets the value atindex
tovalue
wrapped 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
Sets the value atindex
tovalue
, 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 beNaNs
orinfinities
.- 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 thenull
reference orJSONObject.NULL
.- Parameters:
index
- Which value to check.- Returns:
- true if the value is null.
-
get
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 thenull
reference. This method returns normally if the value isJSONObject#NULL
.
-
opt
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
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
Returns the value atindex
if 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 atindex
doesn't exist or cannot be coerced to a boolean.
-
optBoolean
public boolean optBoolean(int index) Returns the value atindex
if 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 atindex
if it exists and is a boolean or can be coerced to a boolean. Returnsfallback
otherwise.- 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
Returns the value atindex
if 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 atindex
doesn't exist or cannot be coerced to a double.
-
optDouble
public double optDouble(int index) Returns the value atindex
if it exists and is a double or can be coerced to a double. ReturnsNaN
otherwise.- Parameters:
index
- Which value to get.- Returns:
- the value at the specified location.
-
optDouble
public double optDouble(int index, double fallback) Returns the value atindex
if it exists and is a double or can be coerced to a double. Returnsfallback
otherwise.- 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
Returns the value atindex
if 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 atindex
doesn't exist or cannot be coerced to a int.
-
optInt
public int optInt(int index) Returns the value atindex
if 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 atindex
if it exists and is an int or can be coerced to an int. Returnsfallback
otherwise.- 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
Returns the value atindex
if 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 atindex
doesn't exist or cannot be coerced to a long.
-
optLong
public long optLong(int index) Returns the value atindex
if 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 atindex
if it exists and is a long or can be coerced to a long. Returnsfallback
otherwise.- 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
Returns the value atindex
if 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
Returns the value atindex
if 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
Returns the value atindex
if it exists, coercing it if necessary. Returnsfallback
if 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
Returns the value atindex
if 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
Returns the value atindex
if it exists and is aJSONArray
. Returns null otherwise.- Parameters:
index
- Which value to get.- Returns:
- the value at the specified location.
-
getJSONObject
Returns the value atindex
if 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
Returns the value atindex
if it exists and is aJSONObject
. Returns null otherwise.- Parameters:
index
- Which value to get.- Returns:
- the value at the specified location.
-
toJSONObject
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
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
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
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
Encodes this array usingJSONStringer
provided- Parameters:
stringer
- -JSONStringer
to be used for serialization- Returns:
- The string representation of this.
- Throws:
JSONException
- On internal errors. Shouldn't happen.
-
encode
Encodes this array usingJSONStringer
provided- Parameters:
stringer
- -JSONStringer
to be used for serialization- Throws:
JSONException
- On internal errors. Shouldn't happen.
-
iterator
-
equals
-
hashCode
public int hashCode()
-