Class JSONArray
java.lang.Object
org.springframework.boot.configurationmetadata.json.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 nonfinal, 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
ConstructorsConstructorDescriptionCreates aJSONArraywith no values.Creates a newJSONArraywith values from the given primitive array.Creates a newJSONArraywith values from the JSON string.JSONArray(Collection copyFrom) Creates a newJSONArrayby copying all values from the given collection.JSONArray(JSONTokener readFrom) Creates a newJSONArraywith values from the next array in the tokener. -
Method Summary
Modifier and TypeMethodDescriptionbooleanget(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.getJSONArray(int index) Returns the value atindexif it exists and is aJSONArray.getJSONObject(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.getString(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.Returns a new string by alternating this array's values withseparator.intlength()Returns the number of values in this array.opt(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.optJSONArray(int index) Returns the value atindexif it exists and is aJSONArray.optJSONObject(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.optString(int index) Returns the value atindexif it exists, coercing it if necessary.Returns the value atindexif it exists, coercing it if necessary.put(boolean value) Appendsvalueto the end of this array.put(double value) Appendsvalueto the end of this array.put(int value) Appendsvalueto the end of this array.put(int index, boolean value) Sets the value atindextovalue, null padding this array to the required length if necessary.put(int index, double value) Sets the value atindextovalue, null padding this array to the required length if necessary.put(int index, int value) Sets the value atindextovalue, null padding this array to the required length if necessary.put(int index, long value) Sets the value atindextovalue, null padding this array to the required length if necessary.Sets the value atindextovalue, null padding this array to the required length if necessary.put(long value) Appendsvalueto the end of this array.Appendsvalueto 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:
-
Constructor Details
-
JSONArray
public JSONArray()Creates aJSONArraywith no values. -
JSONArray
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
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.JSONException- if processing of json failed
-
JSONArray
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
Creates a newJSONArraywith values from the given primitive array.- Parameters:
array- a primitive array- Throws:
JSONException- if processing of json failed
-
-
Method Details
-
length
public int length()Returns the number of values in this array.- Returns:
- the length of this array
-
put
Appendsvalueto the end of this array.- Parameters:
value- the value- Returns:
- this array.
-
put
Appendsvalueto the end of this array.- Parameters:
value- a finite value. May not beNaNsorinfinities.- Returns:
- this array.
- Throws:
JSONException- if processing of json failed
-
put
Appendsvalueto the end of this array.- Parameters:
value- the value- Returns:
- this array.
-
put
Appendsvalueto the end of this array.- Parameters:
value- the value- Returns:
- this array.
-
put
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
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- the index to set the value tovalue- the value- Returns:
- this array.
- Throws:
JSONException- if processing of json failed
-
put
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- the index to set the value tovalue- a finite value. May not beNaNsorinfinities.- Returns:
- this array.
- Throws:
JSONException- if processing of json failed
-
put
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- the index to set the value tovalue- the value- Returns:
- this array.
- Throws:
JSONException- if processing of json failed
-
put
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- the index to set the value tovalue- the value- Returns:
- this array.
- Throws:
JSONException- if processing of json failed
-
put
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- the index to set the value tovalue- aJSONObject,JSONArray, String, Boolean, Integer, Long, Double,JSONObject.NULL, ornull. May not beNaNsorinfinities.- Returns:
- this array.
- Throws:
JSONException- if processing of json failed
-
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- the index to set the value to- Returns:
- true if this array has no value at
index
-
get
Returns the value atindex.- Parameters:
index- the index to get the value from- Returns:
- the value at
index. - 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
Returns the value atindex, or null if the array has no value atindex.- Parameters:
index- the index to get the value from- Returns:
- the value at
indexornull
-
remove
Removes and returns the value atindex, or null if the array has no value atindex.- Parameters:
index- the index of the value to remove- Returns:
- the previous value at
index
-
getBoolean
Returns the value atindexif it exists and is a boolean or can be coerced to a boolean.- Parameters:
index- the index to get the value from- Returns:
- the value at
index - 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- the index to get the value from- Returns:
- the
valueorfalse
-
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- the index to get the value fromfallback- the fallback value- Returns:
- the value at
indexoffallback
-
getDouble
Returns the value atindexif it exists and is a double or can be coerced to a double.- Parameters:
index- the index to get the value from- Returns:
- the
value - 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- the index to get the value from- Returns:
- the
valueorNaN
-
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- the index to get the value fromfallback- the fallback value- Returns:
- the value at
indexoffallback
-
getInt
Returns the value atindexif it exists and is an int or can be coerced to an int.- Parameters:
index- the index to get the value from- Returns:
- the
value - Throws:
JSONException- if the value atindexdoesn't exist or cannot be coerced to an 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- the index to get the value from- Returns:
- the
valueor0
-
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- the index to get the value fromfallback- the fallback value- Returns:
- the value at
indexoffallback
-
getLong
Returns the value atindexif it exists and is a long or can be coerced to a long.- Parameters:
index- the index to get the value from- Returns:
- the
value - 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- the index to get the value from- Returns:
- the
valueor0
-
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- the index to get the value fromfallback- the fallback value- Returns:
- the value at
indexoffallback
-
getString
Returns the value atindexif it exists, coercing it if necessary.- Parameters:
index- the index to get the value from- Returns:
- the
value - Throws:
JSONException- if no such value exists.
-
optString
Returns the value atindexif it exists, coercing it if necessary. Returns the empty string if no such value exists.- Parameters:
index- the index to get the value from- Returns:
- the
valueor an empty string
-
optString
-
getJSONArray
Returns the value atindexif it exists and is aJSONArray.- Parameters:
index- the index to get the value from- Returns:
- the array at
index - Throws:
JSONException- if the value doesn't exist or is not aJSONArray.
-
optJSONArray
Returns the value atindexif it exists and is aJSONArray. Returns null otherwise.- Parameters:
index- the index to get the value from- Returns:
- the array at
indexornull
-
getJSONObject
Returns the value atindexif it exists and is aJSONObject.- Parameters:
index- the index to get the value from- Returns:
- the object at
index - Throws:
JSONException- if the value doesn't exist or is not aJSONObject.
-
optJSONObject
Returns the value atindexif it exists and is aJSONObject. Returns null otherwise.- Parameters:
index- the index to get the value from- Returns:
- the object at
indexornull
-
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 property names- Returns:
- a json object
- Throws:
JSONException- if processing of json failed
-
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 separator to use- Returns:
- the joined value
- Throws:
JSONException- if processing of json failed
-
toString
-
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:
- a human-readable JSON string of this array
- Throws:
JSONException- if processing of json failed
-
equals
-
hashCode
-