public class JsonArray extends Object implements Iterable<Object>, io.vertx.core.shareddata.impl.ClusterSerializable, Shareable
Unlike some other languages Java does not have a native understanding of JSON. To enable JSON to be used easily in Vert.x code we use this class to encapsulate the notion of a JSON array. The implementation adheres to the RFC-7493 to support Temporal data types as well as binary data.
Please see the documentation for more information.
Constructor and Description |
---|
JsonArray()
Create an empty instance
|
JsonArray(Buffer buf)
Create an instance from a Buffer of JSON.
|
JsonArray(List list)
Create an instance from a List.
|
JsonArray(String json)
Create an instance from a String of JSON, this string must be a valid array otherwise an exception will be thrown.
|
Modifier and Type | Method and Description |
---|---|
JsonArray |
add(Boolean value)
Add a Boolean to the JSON array.
|
JsonArray |
add(byte[] value)
Add a binary value to the JSON array.
|
JsonArray |
add(CharSequence value)
Add a CharSequence to the JSON array.
|
JsonArray |
add(Double value)
Add a Double to the JSON array.
|
JsonArray |
add(Enum value)
Add an enum to the JSON array.
|
JsonArray |
add(Float value)
Add a Float to the JSON array.
|
JsonArray |
add(java.time.Instant value)
Add a Instant value to the JSON array.
|
JsonArray |
add(Integer value)
Add an Integer to the JSON array.
|
JsonArray |
add(JsonArray value)
Add another JSON array to the JSON array.
|
JsonArray |
add(JsonObject value)
Add a JSON object to the JSON array.
|
JsonArray |
add(Long value)
Add a Long to the JSON array.
|
JsonArray |
add(Object value)
Add an Object to the JSON array.
|
JsonArray |
add(String value)
Add a String to the JSON array.
|
JsonArray |
addAll(JsonArray array)
Appends all of the elements in the specified array to the end of this JSON array.
|
JsonArray |
addNull()
Add a null value to the JSON array.
|
JsonArray |
clear()
Remove all entries from the JSON array
|
boolean |
contains(Object value)
Does the JSON array contain the specified value? This method will scan the entire array until it finds a value
or reaches the end.
|
JsonArray |
copy()
Make a copy of the JSON array
|
String |
encode()
Encode the JSON array to a string
|
String |
encodePrettily()
Encode the JSON array prettily as a string
|
boolean |
equals(Object o) |
byte[] |
getBinary(int pos)
Get the byte[] at position
pos in the array. |
Boolean |
getBoolean(int pos)
Get the Boolean at position
pos in the array, |
Double |
getDouble(int pos)
Get the Double at position
pos in the array, |
Float |
getFloat(int pos)
Get the Float at position
pos in the array, |
java.time.Instant |
getInstant(int pos)
Get the Instant at position
pos in the array. |
Integer |
getInteger(int pos)
Get the Integer at position
pos in the array, |
JsonArray |
getJsonArray(int pos)
Get the JsonArray at position
pos in the array. |
JsonObject |
getJsonObject(int pos)
Get the JsonObject at position
pos in the array. |
List |
getList()
Get the unerlying List
|
Long |
getLong(int pos)
Get the Long at position
pos in the array, |
Number |
getNumber(int pos)
Get the Number at position
pos in the array, |
String |
getString(int pos)
Get the String at position
pos in the array, |
Object |
getValue(int pos)
Get the Object value at position
pos in the array. |
int |
hashCode() |
boolean |
hasNull(int pos)
Is there a null value at position pos?
|
boolean |
isEmpty()
Are there zero items in this JSON array?
|
Iterator<Object> |
iterator()
Get an Iterator over the values in the JSON array
|
int |
readFromBuffer(int pos,
Buffer buffer) |
Object |
remove(int pos)
Remove the value at the specified position in the JSON array.
|
boolean |
remove(Object value)
Remove the specified value from the JSON array.
|
JsonArray |
set(int pos,
Boolean value)
Set a Boolean to the JSON array at position
pos . |
JsonArray |
set(int pos,
byte[] value)
Set a binary value to the JSON array at position
pos . |
JsonArray |
set(int pos,
CharSequence value)
Set a CharSequence to the JSON array at position
pos . |
JsonArray |
set(int pos,
Double value)
Set a Double to the JSON array at position
pos . |
JsonArray |
set(int pos,
Enum value)
Set an enum to the JSON array at position
pos . |
JsonArray |
set(int pos,
Float value)
Set a Float to the JSON array at position
pos . |
JsonArray |
set(int pos,
java.time.Instant value)
Set a Instant value to the JSON array at position
pos . |
JsonArray |
set(int pos,
Integer value)
Set an Integer to the JSON array at position
pos . |
JsonArray |
set(int pos,
JsonArray value)
Set another JSON array to the JSON array at position
pos . |
JsonArray |
set(int pos,
JsonObject value)
Set a JSON object to the JSON array at position
pos . |
JsonArray |
set(int pos,
Long value)
Set a Long to the JSON array at position
pos . |
JsonArray |
set(int pos,
Object value)
Set an Object to the JSON array at position
pos . |
JsonArray |
set(int pos,
String value)
Set a String to the JSON array at position
pos . |
JsonArray |
setNull(int pos)
Set a null value to the JSON array at position
pos . |
int |
size()
Get the number of values in this JSON array
|
java.util.stream.Stream<Object> |
stream()
Get a Stream over the entries in the JSON array
|
Buffer |
toBuffer()
Encode this JSON object as buffer.
|
String |
toString() |
void |
writeToBuffer(Buffer buffer) |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
forEach, spliterator
public JsonArray(String json)
Json.decodeValue(String)
and check the result is
a JSON array.json
- the string of JSONpublic JsonArray()
public JsonArray(List list)
list
- the underlying backing listpublic JsonArray(Buffer buf)
buf
- the buffer of JSON.public String getString(int pos)
pos
in the array,pos
- the position in the arrayClassCastException
- if the value cannot be converted to Stringpublic Number getNumber(int pos)
pos
in the array,pos
- the position in the arrayClassCastException
- if the value is not a Numberpublic Integer getInteger(int pos)
pos
in the array,pos
- the position in the arrayClassCastException
- if the value cannot be converted to Integerpublic Long getLong(int pos)
pos
in the array,pos
- the position in the arrayClassCastException
- if the value cannot be converted to Longpublic Double getDouble(int pos)
pos
in the array,pos
- the position in the arrayClassCastException
- if the value cannot be converted to Doublepublic Float getFloat(int pos)
pos
in the array,pos
- the position in the arrayClassCastException
- if the value cannot be converted to Floatpublic Boolean getBoolean(int pos)
pos
in the array,pos
- the position in the arrayClassCastException
- if the value cannot be converted to Integerpublic JsonObject getJsonObject(int pos)
pos
in the array.pos
- the position in the arrayClassCastException
- if the value cannot be converted to JsonObjectpublic JsonArray getJsonArray(int pos)
pos
in the array.pos
- the position in the arrayClassCastException
- if the value cannot be converted to JsonArraypublic byte[] getBinary(int pos)
pos
in the array.
JSON itself has no notion of a binary, so this method assumes there is a String value and it contains a Base64 encoded binary, which it decodes if found and returns.
This method should be used in conjunction with add(byte[])
pos
- the position in the arrayClassCastException
- if the value cannot be converted to StringIllegalArgumentException
- if the String value is not a legal Base64 encoded valuepublic java.time.Instant getInstant(int pos)
pos
in the array.
JSON itself has no notion of a temporal types, this extension allows ISO 8601 string formatted dates with timezone
always set to zero UTC offset, as denoted by the suffix "Z" to be parsed as a instant value.
YYYY-MM-DDTHH:mm:ss.sssZ
is the default format used by web browser scripting. This extension complies to
the RFC-7493 with all the restrictions mentioned before. The method will then decode and return a instant value.
This method should be used in conjunction with add(Instant)
pos
- the position in the arrayClassCastException
- if the value cannot be converted to Stringjava.time.format.DateTimeParseException
- if the String value is not a legal ISO 8601 encoded valuepublic Object getValue(int pos)
pos
in the array.pos
- the position in the arraypublic boolean hasNull(int pos)
pos
- the position in the arraypublic JsonArray add(Enum value)
JSON has no concept of encoding Enums, so the Enum will be converted to a String using the Enum.name()
method and the value added as a String.
value
- the valuepublic JsonArray add(CharSequence value)
value
- the valuepublic JsonArray add(String value)
value
- the valuepublic JsonArray add(Integer value)
value
- the valuepublic JsonArray add(Long value)
value
- the valuepublic JsonArray add(Double value)
value
- the valuepublic JsonArray add(Float value)
value
- the valuepublic JsonArray add(Boolean value)
value
- the valuepublic JsonArray addNull()
public JsonArray add(JsonObject value)
value
- the valuepublic JsonArray add(JsonArray value)
value
- the valuepublic JsonArray add(byte[] value)
JSON has no notion of binary so the binary will be base64 encoded to a String, and the String added.
value
- the valuepublic JsonArray add(java.time.Instant value)
JSON has no notion of Temporal data so the Instant will be ISOString encoded, and the String added.
value
- the valuepublic JsonArray add(Object value)
value
- the valuepublic JsonArray addAll(JsonArray array)
array
- the arraypublic JsonArray set(int pos, Enum value)
pos
.
JSON has no concept of encoding Enums, so the Enum will be converted to a String using the Enum.name()
method and the value added as a String.
pos
- position in the arrayvalue
- the valuepublic JsonArray set(int pos, CharSequence value)
pos
.pos
- position in the arrayvalue
- the valuepublic JsonArray set(int pos, String value)
pos
.pos
- position in the arrayvalue
- the valuepublic JsonArray set(int pos, Integer value)
pos
.pos
- position in the arrayvalue
- the valuepublic JsonArray set(int pos, Long value)
pos
.pos
- position in the arrayvalue
- the valuepublic JsonArray set(int pos, Double value)
pos
.pos
- position in the arrayvalue
- the valuepublic JsonArray set(int pos, Float value)
pos
.pos
- position in the arrayvalue
- the valuepublic JsonArray set(int pos, Boolean value)
pos
.pos
- position in the arrayvalue
- the valuepublic JsonArray setNull(int pos)
pos
.public JsonArray set(int pos, JsonObject value)
pos
.pos
- position in the arrayvalue
- the valuepublic JsonArray set(int pos, JsonArray value)
pos
.pos
- position in the arrayvalue
- the valuepublic JsonArray set(int pos, byte[] value)
pos
.
JSON has no notion of binary so the binary will be base64 encoded to a String, and the String added.
pos
- position in the arrayvalue
- the valuepublic JsonArray set(int pos, java.time.Instant value)
pos
.
JSON has no notion of Temporal data so the Instant will be ISOString encoded, and the String added.
pos
- position in the arrayvalue
- the valuepublic JsonArray set(int pos, Object value)
pos
.pos
- position in the arrayvalue
- the valuepublic boolean contains(Object value)
value
- the valuepublic boolean remove(Object value)
value
- the value to removepublic Object remove(int pos)
pos
- the position to remove the value atJsonObject
is built from
this Map and returned. It the value is a List, a JsonArray
is built form this List and returned.public int size()
public boolean isEmpty()
public List getList()
public JsonArray clear()
public String encode()
public Buffer toBuffer()
public String encodePrettily()
public JsonArray copy()
public java.util.stream.Stream<Object> stream()
public void writeToBuffer(Buffer buffer)
writeToBuffer
in interface io.vertx.core.shareddata.impl.ClusterSerializable
public int readFromBuffer(int pos, Buffer buffer)
readFromBuffer
in interface io.vertx.core.shareddata.impl.ClusterSerializable
Copyright © 2023 Eclipse. All rights reserved.