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 potentialNullPointerExceptions. TheJsonArrayis backed by aListand 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:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JsonArrayadd(boolean value)JsonArrayadd(double value)JsonArrayadd(int value)JsonArrayadd(long value)JsonArrayadd(JsonArray value)JsonArrayadd(JsonObject value)Append anJsonObjectelement to theJsonArray.JsonArrayadd(Number value)JsonArrayadd(Object value)Append an element to theJsonArray.JsonArrayadd(String value)JsonArrayadd(List<?> value)JsonArrayadd(Map<String,?> value)JsonArrayaddNull()Append a null element to theJsonArray.static JsonArraycreate()Creates a emptyJsonArray.static JsonArrayempty()Creates a emptyJsonArray.booleanequals(Object o)static JsonArrayfrom(Object... items)Creates a newJsonArrayand populates it with the values supplied.static JsonArrayfrom(List<?> items)static JsonArrayfromJson(String s)Objectget(int index)Retrieves the value by the position in theJsonArrayand does not cast it.JsonArraygetArray(int index)BigDecimalgetBigDecimal(int index)Retrieves the value by the position in theJsonArrayand casts it toBigDecimal.BigIntegergetBigInteger(int index)Retrieves the value by the position in theJsonArrayand casts it toBigInteger.booleangetBoolean(int index)DoublegetDouble(int index)IntegergetInt(int index)LonggetLong(int index)NumbergetNumber(int index)JsonObjectgetObject(int index)Retrieves the value by the position in theJsonArrayand casts it toJsonObject.StringgetString(int index)inthashCode()booleanisEmpty()Checks if theJsonArrayis empty or not.Iterator<Object>iterator()intsize()Returns the size of theJsonArray.List<Object>toList()StringtoString()Converts theJsonArrayinto 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 newJsonArrayand populates it with the values supplied. If the type is not supported, aIllegalArgumentExceptionexception is thrown.
-
from
public static JsonArray from(List<?> items)
Creates a newJsonArrayand populates it with the values in the suppliedList. If the type of an item is not supported, anIllegalArgumentExceptionis thrown. If the list is null, aNullPointerExceptionis 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 aJsonArrayfrom 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 theJsonArrayand 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 aIllegalArgumentExceptionis 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 theJsonArrayand 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 theJsonArrayand 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 theJsonArrayand 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 anJsonObjectelement 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 theJsonArrayand 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 theJsonArrayand 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 theJsonArrayand 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 theJsonArrayis empty or not.- Returns:
- true if it is, false otherwise.
-
size
public int size()
Returns the size of theJsonArray.- Returns:
- the size.
-
-