Package com.couchbase.client.java.json
Class JsonArray
java.lang.Object
com.couchbase.client.java.json.JsonValue
com.couchbase.client.java.json.JsonArray
- All Implemented Interfaces:
Serializable
,Iterable<Object>
Represents a JSON array that can be stored and loaded from Couchbase Server.
If boxed return values are unboxed, the calling code needs to make sure to handle potential
NullPointerException
s.
The JsonArray
is backed by a List
and is intended to work similar to it API wise, but to only
allow to store such objects which can be represented by JSON.- Since:
- 2.0
- See Also:
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionadd
(boolean value) add
(double value) add
(int value) add
(long value) add
(JsonObject value) Append anJsonObject
element to theJsonArray
.Append an element to theJsonArray
.addNull()
Append a null element to theJsonArray
.boolean
Returns true if the object is part of the array.static JsonArray
create()
Creates a emptyJsonArray
.static JsonArray
create
(int initialCapacity) Creates a emptyJsonArray
.boolean
static JsonArray
Creates a newJsonArray
and populates it with the values supplied.static JsonArray
static JsonArray
fromJson
(byte[] s) static JsonArray
get
(int index) Retrieves the value by the position in theJsonArray
and does not cast it.getArray
(int index) getBigDecimal
(int index) Retrieves the value by the position in theJsonArray
and casts it toBigDecimal
.getBigInteger
(int index) Retrieves the value by the position in theJsonArray
and casts it toBigInteger
.boolean
getBoolean
(int index) getDouble
(int index) getInt
(int index) getLong
(int index) getNumber
(int index) getObject
(int index) Retrieves the value by the position in theJsonArray
and casts it toJsonObject
.getString
(int index) int
hashCode()
boolean
isEmpty()
Checks if theJsonArray
is empty or not.iterator()
int
size()
Returns the size of theJsonArray
.byte[]
toBytes()
Similar totoString()
but turns this array directly into an encoded byte array.toList()
toString()
Converts theJsonArray
into its JSON string representation.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
create
Creates a emptyJsonArray
.- Returns:
- a empty
JsonArray
.
-
create
Creates a emptyJsonArray
.- Parameters:
initialCapacity
- the initial capacity for this json array.- Returns:
- a empty
JsonArray
.
-
from
Creates a newJsonArray
and populates it with the values supplied. If the type is not supported, aInvalidArgumentException
exception is thrown. -
from
Returns a newJsonArray
containing items from the givenList
, in the same order they were returned by the list's iterator.Sub Maps and Iterables: If possible, contained Maps and Iterables are converted to JsonObject and JsonArray respectively. However, some restrictions apply. Any non-convertible item will raise a
ClassCastException
. If the sub-conversion raises an exception (like an InvalidArgumentException) then it is put as cause for the ClassCastException.- Parameters:
items
- the list of items to be stored in theJsonArray
.- Returns:
- a new
JsonArray
containing the elements from the given list, in the order they were returned by the list's iterator. - Throws:
InvalidArgumentException
- if one or more items is of unsupported type.NullPointerException
- if the given list is null.
-
fromJson
Static method to create aJsonArray
from a JSONString
. Not to be confused withfrom(Object...)
from(aString)} which will populate a new array with the string. The string is expected to be a valid JSON array representation (eg. starting with a '[').- Parameters:
s
- the JSON String to convert to aJsonArray
.- Returns:
- the corresponding
JsonArray
. - Throws:
InvalidArgumentException
- if the conversion cannot be done.
-
fromJson
-
get
Retrieves the value by the position in theJsonArray
and does not cast it.- Parameters:
index
- the index of the value.- Returns:
- the value at index.
- Throws:
IndexOutOfBoundsException
- if the index is negative or too large.
-
add
Append an element to theJsonArray
. Note that the type is checked and aInvalidArgumentException
is thrown if not supported.- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
.
-
addNull
Append a null element to theJsonArray
. This is equivalent to callingadd(Object)
with null orJsonValue.NULL
.- Returns:
- the
JsonArray
.
-
add
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
.
-
getString
- Parameters:
index
- the index of the value.- Returns:
- the value at index.
- Throws:
IndexOutOfBoundsException
- if the index is negative or too large.
-
add
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
.
-
getLong
Retrieves the value by the position in theJsonArray
and casts it toLong
. Note that if value was stored as another numerical type, some truncation or rounding may occur.- Parameters:
index
- the index of the value.- Returns:
- the value at index.
- Throws:
IndexOutOfBoundsException
- if the index is negative or too large.
-
add
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
.
-
getInt
Retrieves the value by the position in theJsonArray
and casts it toInteger
. Note that if value was stored as another numerical type, some truncation or rounding may occur.- Parameters:
index
- the index of the value.- Returns:
- the value at index.
- Throws:
IndexOutOfBoundsException
- if the index is negative or too large.
-
add
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
.
-
getDouble
Retrieves the value by the position in theJsonArray
and casts it toDouble
. Note that if value was stored as another numerical type, some truncation or rounding may occur.- Parameters:
index
- the index of the value.- Returns:
- the value at index.
- Throws:
IndexOutOfBoundsException
- if the index is negative or too large.
-
add
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
.
-
getBoolean
public boolean getBoolean(int index) - Parameters:
index
- the index of the value.- Returns:
- the value at index.
- Throws:
IndexOutOfBoundsException
- if the index is negative or too large.
-
add
Append anJsonObject
element to theJsonArray
.- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
.
-
add
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
. - See Also:
-
getObject
Retrieves the value by the position in theJsonArray
and casts it toJsonObject
.- Parameters:
index
- the index of the value.- Returns:
- the value at index.
- Throws:
IndexOutOfBoundsException
- if the index is negative or too large.
-
add
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
.
-
add
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
. - See Also:
-
add
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
.
-
getArray
- Parameters:
index
- the index of the value.- Returns:
- the value at index.
- Throws:
IndexOutOfBoundsException
- if the index is negative or too large.
-
getBigInteger
Retrieves the value by the position in theJsonArray
and casts it toBigInteger
.- Parameters:
index
- the index of the value.- Returns:
- the value at the given index.
- Throws:
IndexOutOfBoundsException
- if the index is negative or too large.
-
getBigDecimal
Retrieves the value by the position in theJsonArray
and casts it toBigDecimal
.- Parameters:
index
- the index of the value.- Returns:
- the value at the given index.
- Throws:
IndexOutOfBoundsException
- if the index is negative or too large.
-
getNumber
- Parameters:
index
- the index of the value.- Returns:
- the value at the given index.
- Throws:
IndexOutOfBoundsException
- if the index is negative or too large.
-
toList
-
isEmpty
public boolean isEmpty()Checks if theJsonArray
is empty or not.- Returns:
- true if it is, false otherwise.
-
size
public int size()Returns the size of theJsonArray
.- Returns:
- the size.
-
contains
Returns true if the object is part of the array.- Parameters:
value
- the value to check.- Returns:
- true if it is part of the array, false otherwise.
-
iterator
-
toString
Converts theJsonArray
into its JSON string representation. -
toBytes
public byte[] toBytes()Similar totoString()
but turns this array directly into an encoded byte array.- Returns:
- the byte array representing this
JsonArray
.
-
equals
-
hashCode
public int hashCode()
-