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
NullPointerExceptions.
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 anJsonObjectelement to theJsonArray.Append an element to theJsonArray.addNull()Append a null element to theJsonArray.booleanReturns true if the object is part of the array.static JsonArraycreate()Creates a emptyJsonArray.static JsonArraycreate(int initialCapacity) Creates a emptyJsonArray.booleanstatic JsonArrayCreates a newJsonArrayand populates it with the values supplied.static JsonArraystatic JsonArrayfromJson(byte[] s) static JsonArrayget(int index) Retrieves the value by the position in theJsonArrayand does not cast it.getArray(int index) getBigDecimal(int index) Retrieves the value by the position in theJsonArrayand casts it toBigDecimal.getBigInteger(int index) Retrieves the value by the position in theJsonArrayand casts it toBigInteger.booleangetBoolean(int index) getDouble(int index) getInt(int index) getLong(int index) getNumber(int index) getObject(int index) Retrieves the value by the position in theJsonArrayand casts it toJsonObject.getString(int index) inthashCode()booleanisEmpty()Checks if theJsonArrayis empty or not.iterator()intsize()Returns the size of theJsonArray.byte[]toBytes()Similar totoString()but turns this array directly into an encoded byte array.toList()toString()Converts theJsonArrayinto its JSON string representation.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods 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 newJsonArrayand populates it with the values supplied. If the type is not supported, aInvalidArgumentExceptionexception is thrown. -
from
Returns a newJsonArraycontaining 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
JsonArraycontaining 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 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:
InvalidArgumentException- if the conversion cannot be done.
-
fromJson
-
get
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
Append an element to theJsonArray. Note that the type is checked and aInvalidArgumentExceptionis 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 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
- Parameters:
value- the value to append.- Returns:
- the
JsonArray.
-
getInt
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
- Parameters:
value- the value to append.- Returns:
- the
JsonArray.
-
getDouble
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
- 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 anJsonObjectelement 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 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
- 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 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
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
- 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 theJsonArrayis 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 theJsonArrayinto 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()
-