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 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.
Modifier and Type | Method and Description |
---|---|
JsonArray |
add(boolean value)
|
JsonArray |
add(double value)
|
JsonArray |
add(int value)
|
JsonArray |
add(JsonArray value)
|
JsonArray |
add(JsonObject value)
Append an
JsonObject element to the JsonArray . |
JsonArray |
add(List<?> value)
|
JsonArray |
add(long value)
|
JsonArray |
add(Map<String,?> value)
|
JsonArray |
add(Object value)
Append an element to the
JsonArray . |
JsonArray |
add(String value)
|
JsonArray |
addNull()
Append a null element to the
JsonArray . |
static JsonArray |
create()
Creates a empty
JsonArray . |
static JsonArray |
empty()
Creates a empty
JsonArray . |
boolean |
equals(Object o) |
static JsonArray |
from(List<?> items)
|
static JsonArray |
from(Object... items)
Creates a new
JsonArray and populates it with the values supplied. |
static JsonArray |
fromJson(String s)
|
Object |
get(int index)
Retrieves the value by the position in the
JsonArray and does not cast it. |
JsonArray |
getArray(int index)
|
boolean |
getBoolean(int index)
|
Double |
getDouble(int index)
|
Integer |
getInt(int index)
|
Long |
getLong(int index)
|
JsonObject |
getObject(int index)
Retrieves the value by the position in the
JsonArray and casts it to JsonObject . |
String |
getString(int index)
|
int |
hashCode() |
boolean |
isEmpty()
Checks if the
JsonArray is empty or not. |
Iterator<Object> |
iterator() |
int |
size()
Returns the size of the
JsonArray . |
List<Object> |
toList()
|
String |
toString()
Converts the
JsonArray into its JSON string representation. |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
forEach, spliterator
public static JsonArray from(Object... items)
Creates a new JsonArray
and populates it with the values supplied.
If the type is not supported, a IllegalArgumentException
exception is thrown.
public static JsonArray from(List<?> items)
Creates a new JsonArray
and populates it with the values in the supplied List
.
If the type of an item is not supported, an IllegalArgumentException
is thrown. If the list is null, a NullPointerException
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 a ClassCastException
. If the sub-conversion raises an exception (like an IllegalArgumentException) then it is put as cause for the ClassCastException.
items
- the list of items to be stored in the JsonArray
.JsonArray
.IllegalArgumentException
- if at least one item is of unsupported type.NullPointerException
- if the list of items is null.public static JsonArray fromJson(String s)
Static method to create a JsonArray
from a JSON String
.
Not to be confused with from(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 ‘[’).
s
- the JSON String to convert to a JsonArray
.JsonArray
.IllegalArgumentException
- if the conversion cannot be done.public Object get(int index)
Retrieves the value by the position in the JsonArray
and does not cast it.
index
- the index of the value.IndexOutOfBoundsException
- if the index is negative or too large.public JsonArray add(Object value)
Append an element to the JsonArray
.
Note that the type is checked and a IllegalArgumentException
is thrown if not supported.
value
- the value to append.JsonArray
.public JsonArray addNull()
Append a null element to the JsonArray
.
This is equivalent to calling add(Object)
with null or JsonValue.NULL
.
JsonArray
.public JsonArray add(String value)
value
- the value to append.JsonArray
.public String getString(int index)
index
- the index of the value.IndexOutOfBoundsException
- if the index is negative or too large.public JsonArray add(long value)
value
- the value to append.JsonArray
.public Long getLong(int index)
Retrieves the value by the position in the JsonArray
and casts it to Long
.
Note that if value was stored as another numerical type, some truncation or rounding may occur.
index
- the index of the value.IndexOutOfBoundsException
- if the index is negative or too large.public JsonArray add(int value)
value
- the value to append.JsonArray
.public Integer getInt(int index)
Retrieves the value by the position in the JsonArray
and casts it to Integer
.
Note that if value was stored as another numerical type, some truncation or rounding may occur.
index
- the index of the value.IndexOutOfBoundsException
- if the index is negative or too large.public JsonArray add(double value)
value
- the value to append.JsonArray
.public Double getDouble(int index)
Retrieves the value by the position in the JsonArray
and casts it to Double
.
Note that if value was stored as another numerical type, some truncation or rounding may occur.
index
- the index of the value.IndexOutOfBoundsException
- if the index is negative or too large.public JsonArray add(boolean value)
value
- the value to append.JsonArray
.public boolean getBoolean(int index)
index
- the index of the value.IndexOutOfBoundsException
- if the index is negative or too large.public JsonArray add(JsonObject value)
Append an JsonObject
element to the JsonArray
.
value
- the value to append.JsonArray
.public JsonArray add(Map<String,?> value)
Append an JsonObject
element, converted from a List
, to the JsonArray
.
value
- the value to append.JsonArray
.JsonObject.from(Map)
public JsonObject getObject(int index)
Retrieves the value by the position in the JsonArray
and casts it to JsonObject
.
index
- the index of the value.IndexOutOfBoundsException
- if the index is negative or too large.public JsonArray add(JsonArray value)
value
- the value to append.JsonArray
.public JsonArray add(List<?> value)
value
- the value to append.JsonArray
.from(List)
public JsonArray getArray(int index)
index
- the index of the value.IndexOutOfBoundsException
- if the index is negative or too large.public boolean isEmpty()
Checks if the JsonArray
is empty or not.
public int size()
Returns the size of the JsonArray
.
Copyright © 2015 Couchbase, Inc.