Class JsonArray
- java.lang.Object
-
- com.couchbase.client.java.document.json.JsonValue
-
- com.couchbase.client.java.document.json.JsonArray
-
- All Implemented Interfaces:
Serializable
,Iterable<Object>
public class JsonArray extends JsonValue implements Iterable<Object>, Serializable
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 potentialNullPointerException
s. TheJsonArray
is backed by aList
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
- Author:
- Michael Nitschinger, Simon Baslé
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JsonArray
add(boolean value)
JsonArray
add(double value)
JsonArray
add(int value)
JsonArray
add(long value)
JsonArray
add(JsonArray value)
JsonArray
add(JsonObject value)
Append anJsonObject
element to theJsonArray
.JsonArray
add(Number value)
JsonArray
add(Object value)
Append an element to theJsonArray
.JsonArray
add(String value)
JsonArray
add(List<?> value)
JsonArray
add(Map<String,?> value)
JsonArray
addNull()
Append a null element to theJsonArray
.static JsonArray
create()
Creates a emptyJsonArray
.static JsonArray
empty()
Creates a emptyJsonArray
.boolean
equals(Object o)
static JsonArray
from(Object... items)
Creates a newJsonArray
and populates it with the values supplied.static JsonArray
from(List<?> items)
static JsonArray
fromJson(String s)
Object
get(int index)
Retrieves the value by the position in theJsonArray
and does not cast it.JsonArray
getArray(int index)
BigDecimal
getBigDecimal(int index)
Retrieves the value by the position in theJsonArray
and casts it toBigDecimal
.BigInteger
getBigInteger(int index)
Retrieves the value by the position in theJsonArray
and casts it toBigInteger
.boolean
getBoolean(int index)
Double
getDouble(int index)
Integer
getInt(int index)
Long
getLong(int index)
Number
getNumber(int index)
JsonObject
getObject(int index)
Retrieves the value by the position in theJsonArray
and casts it toJsonObject
.String
getString(int index)
int
hashCode()
boolean
isEmpty()
Checks if theJsonArray
is empty or not.Iterator<Object>
iterator()
int
size()
Returns the size of theJsonArray
.List<Object>
toList()
String
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 Detail
-
from
public static JsonArray from(Object... items)
Creates a newJsonArray
and populates it with the values supplied. If the type is not supported, aIllegalArgumentException
exception is thrown.
-
from
public static JsonArray from(List<?> items)
Creates a newJsonArray
and populates it with the values in the suppliedList
. If the type of an item is not supported, anIllegalArgumentException
is thrown. If the list is null, aNullPointerException
is thrown, but null items are supported. *Sub Maps and Lists* If possible, Maps and Lists contained in items will be converted to JsonObject and JsonArray respectively. However, same restrictions apply. Any non-convertible collection will raise aClassCastException
. If the sub-conversion raises an exception (like an IllegalArgumentException) then it is put as cause for the ClassCastException.- Parameters:
items
- the list of items to be stored in theJsonArray
.- Returns:
- a populated
JsonArray
. - Throws:
IllegalArgumentException
- if at least one item is of unsupported type.NullPointerException
- if the list of items is null.
-
fromJson
public static JsonArray fromJson(String s)
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:
IllegalArgumentException
- if the conversion cannot be done.
-
get
public Object get(int index)
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
public JsonArray add(Object value)
Append an element to theJsonArray
. Note that the type is checked and aIllegalArgumentException
is thrown if not supported.- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
.
-
addNull
public JsonArray addNull()
Append a null element to theJsonArray
. This is equivalent to callingadd(Object)
with null orJsonValue.NULL
.- Returns:
- the
JsonArray
.
-
add
public JsonArray add(String value)
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
.
-
getString
public String getString(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
public JsonArray add(long value)
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
.
-
getLong
public Long getLong(int index)
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
public JsonArray add(int value)
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
.
-
getInt
public Integer getInt(int index)
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
public JsonArray add(double value)
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
.
-
getDouble
public Double getDouble(int index)
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
public JsonArray add(boolean value)
- 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
public JsonArray add(JsonObject value)
Append anJsonObject
element to theJsonArray
.- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
.
-
add
public JsonArray add(Map<String,?> value)
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
. - See Also:
JsonObject.from(Map)
-
getObject
public JsonObject getObject(int index)
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
public JsonArray add(JsonArray value)
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
.
-
add
public JsonArray add(List<?> value)
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
. - See Also:
from(List)
-
add
public JsonArray add(Number value)
- Parameters:
value
- the value to append.- Returns:
- the
JsonArray
.
-
getArray
public JsonArray getArray(int index)
- Parameters:
index
- the index of the value.- Returns:
- the value at index.
- Throws:
IndexOutOfBoundsException
- if the index is negative or too large.
-
getBigInteger
public BigInteger getBigInteger(int index)
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
public BigDecimal getBigDecimal(int index)
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
public Number getNumber(int index)
- Parameters:
index
- the index of the value.- Returns:
- the value at the given index.
- Throws:
IndexOutOfBoundsException
- if the index is negative or too large.
-
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.
-
-