Package io.vertx.core.json
Class JsonArray
- java.lang.Object
-
- io.vertx.core.json.JsonArray
-
- All Implemented Interfaces:
ClusterSerializable,Shareable,Iterable<Object>
public class JsonArray extends Object implements Iterable<Object>, ClusterSerializable, Shareable
A representation of a JSON array in Java. 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.- Author:
- Tim Fox
-
-
Constructor Summary
Constructors Constructor Description JsonArray()Create an empty instanceJsonArray(Buffer buf)Create an instance from a Buffer of JSON.JsonArray(String json)Create an instance from a String of JSON, this string must be a valid array otherwise an exception will be thrown.JsonArray(List list)Create an instance from a List.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JsonArrayadd(int pos, Object value)Add an Object to the JSON array at given positionpos.JsonArrayadd(Object value)Add an Object to the JSON array.JsonArrayaddAll(JsonArray array)Appends all of the elements in the specified array to the end of this JSON array.JsonArrayaddNull()Add a null value to the JSON array.JsonArrayclear()Remove all entries from the JSON arraybooleancontains(Object value)Returnstrueif this JSON Array contains the specified value.JsonArraycopy()Deep copy of the JSON array.JsonArraycopy(Function<Object,?> cloner)Deep copy of the JSON array.Stringencode()Encode the JSON array to a stringStringencodePrettily()Encode the JSON array prettily as a stringbooleanequals(Object o)byte[]getBinary(int pos)Get the byte[] at positionposin the array.BooleangetBoolean(int pos)Get the Boolean at positionposin the array,BuffergetBuffer(int pos)Get the Buffer at positionposin the array.DoublegetDouble(int pos)Get the Double at positionposin the array,FloatgetFloat(int pos)Get the Float at positionposin the array,InstantgetInstant(int pos)Get the Instant at positionposin the array.IntegergetInteger(int pos)Get the Integer at positionposin the array,JsonArraygetJsonArray(int pos)Get the JsonArray at positionposin the array.JsonObjectgetJsonObject(int pos)Get the JsonObject at positionposin the array.ListgetList()Get the underlyingListas is.LonggetLong(int pos)Get the Long at positionposin the array,NumbergetNumber(int pos)Get the Number at positionposin the array,StringgetString(int pos)Get the String at positionposin the array,ObjectgetValue(int pos)Get the value with the specified key, as an Object with types respecting the limitations of JSON.inthashCode()booleanhasNull(int pos)Is there a null value at position pos?intindexOf(Object value)Returns the index of the last occurrence of the specified value in this JSON array, or -1 if this JSON array does not contain the value.booleanisEmpty()Are there zero items in this JSON array?Iterator<Object>iterator()Get an Iterator over the values in the JSON arraystatic JsonArrayof(Object... values)Create a JsonArray containing an arbitrary number of values.intreadFromBuffer(int pos, Buffer buffer)Method invoked when deserializing bytes to this instance.Objectremove(int pos)Remove the value at the specified position in the JSON array.booleanremove(Object value)Remove the specified value from the JSON array.JsonArrayset(int pos, Object value)Set an Object to the JSON array at positionpos.JsonArraysetNull(int pos)Set a null value to the JSON array at positionpos.intsize()Get the number of values in this JSON arrayStream<Object>stream()Get a Stream over the entries in the JSON array.BuffertoBuffer()Encode this JSON object as buffer.StringtoString()voidwriteToBuffer(Buffer buffer)Method invoked when serializing this instance.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Constructor Detail
-
JsonArray
public JsonArray(String json)
Create an instance from a String of JSON, this string must be a valid array otherwise an exception will be thrown. If you are unsure of the value, you should use insteadJson.decodeValue(String)and check the result is a JSON array.- Parameters:
json- the string of JSON
-
JsonArray
public JsonArray()
Create an empty instance
-
JsonArray
public JsonArray(List list)
Create an instance from a List. The List is not copied.- Parameters:
list- the underlying backing list
-
JsonArray
public JsonArray(Buffer buf)
Create an instance from a Buffer of JSON.- Parameters:
buf- the buffer of JSON.
-
-
Method Detail
-
of
public static JsonArray of(Object... values)
Create a JsonArray containing an arbitrary number of values.- Parameters:
values- The objects into JsonArray.- Throws:
NullPointerException- if the args is null.
-
getString
public String getString(int pos)
Get the String at positionposin the array,- Parameters:
pos- the position in the array- Returns:
- the String (or String representation), or null if a null value present
-
getNumber
public Number getNumber(int pos)
Get the Number at positionposin the array,- Parameters:
pos- the position in the array- Returns:
- the Number, or null if a null value present
- Throws:
ClassCastException- if the value is not a Number
-
getInteger
public Integer getInteger(int pos)
Get the Integer at positionposin the array,- Parameters:
pos- the position in the array- Returns:
- the Integer, or null if a null value present
- Throws:
ClassCastException- if the value cannot be converted to Integer
-
getLong
public Long getLong(int pos)
Get the Long at positionposin the array,- Parameters:
pos- the position in the array- Returns:
- the Long, or null if a null value present
- Throws:
ClassCastException- if the value cannot be converted to Long
-
getDouble
public Double getDouble(int pos)
Get the Double at positionposin the array,- Parameters:
pos- the position in the array- Returns:
- the Double, or null if a null value present
- Throws:
ClassCastException- if the value cannot be converted to Double
-
getFloat
public Float getFloat(int pos)
Get the Float at positionposin the array,- Parameters:
pos- the position in the array- Returns:
- the Float, or null if a null value present
- Throws:
ClassCastException- if the value cannot be converted to Float
-
getBoolean
public Boolean getBoolean(int pos)
Get the Boolean at positionposin the array,- Parameters:
pos- the position in the array- Returns:
- the Boolean, or null if a null value present
- Throws:
ClassCastException- if the value cannot be converted to Integer
-
getJsonObject
public JsonObject getJsonObject(int pos)
Get the JsonObject at positionposin the array.- Parameters:
pos- the position in the array- Returns:
- the JsonObject, or null if a null value present
- Throws:
ClassCastException- if the value cannot be converted to JsonObject
-
getJsonArray
public JsonArray getJsonArray(int pos)
Get the JsonArray at positionposin the array.- Parameters:
pos- the position in the array- Returns:
- the Integer, or null if a null value present
- Throws:
ClassCastException- if the value cannot be converted to JsonArray
-
getBinary
public byte[] getBinary(int pos)
Get the byte[] at positionposin 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.- Parameters:
pos- the position in the array- Returns:
- the byte[], or null if a null value present
- Throws:
ClassCastException- if the value cannot be converted to StringIllegalArgumentException- if the String value is not a legal Base64 encoded value
-
getBuffer
public Buffer getBuffer(int pos)
Get the Buffer at positionposin 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.- Parameters:
pos- the position in the array- Returns:
- the byte[], or null if a null value present
- Throws:
ClassCastException- if the value cannot be converted to StringIllegalArgumentException- if the String value is not a legal Base64 encoded value
-
getInstant
public Instant getInstant(int pos)
Get the Instant at positionposin 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.sssZis 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.- Parameters:
pos- the position in the array- Returns:
- the Instant, or null if a null value present
- Throws:
ClassCastException- if the value cannot be converted to StringDateTimeParseException- if the String value is not a legal ISO 8601 encoded value
-
getValue
public Object getValue(int pos)
Get the value with the specified key, as an Object with types respecting the limitations of JSON.Mapwill be wrapped toJsonObjectListwill be wrapped toJsonArrayInstantwill be converted toStringbyte[]will be converted toStringBufferwill be converted toStringEnumwill be converted toString
- Parameters:
pos- the position in the array- Returns:
- the Integer, or null if a null value present
-
hasNull
public boolean hasNull(int pos)
Is there a null value at position pos?- Parameters:
pos- the position in the array- Returns:
- true if null value present, false otherwise
-
addNull
public JsonArray addNull()
Add a null value to the JSON array.- Returns:
- a reference to this, so the API can be used fluently
-
add
public JsonArray add(Object value)
Add an Object to the JSON array.- Parameters:
value- the value- Returns:
- a reference to this, so the API can be used fluently
-
add
public JsonArray add(int pos, Object value)
Add an Object to the JSON array at given positionpos.- Parameters:
pos- the positionvalue- the value- Returns:
- a reference to this, so the API can be used fluently
-
addAll
public JsonArray addAll(JsonArray array)
Appends all of the elements in the specified array to the end of this JSON array.- Parameters:
array- the array- Returns:
- a reference to this, so the API can be used fluently
-
setNull
public JsonArray setNull(int pos)
Set a null value to the JSON array at positionpos.- Returns:
- a reference to this, so the API can be used fluently
-
set
public JsonArray set(int pos, Object value)
Set an Object to the JSON array at positionpos.- Parameters:
pos- position in the arrayvalue- the value- Returns:
- a reference to this, so the API can be used fluently
-
contains
public boolean contains(Object value)
Returnstrueif this JSON Array contains the specified value. More formally, returnstrueif and only if this JSON array contains at least one entryentrysuch thatObjects.equals(value, entry).- Parameters:
value- the value whose presence in this JSON array is to be tested- Returns:
- true if it contains the value, false if not
-
indexOf
public int indexOf(Object value)
Returns the index of the last occurrence of the specified value in this JSON array, or -1 if this JSON array does not contain the value. More formally, returns the highest indexisuch thatObjects.equals(value, get(i)), or -1 if there is no such index.- Parameters:
value- the value whose index in this JSON array is to be returned- Returns:
- the index of the value in the array, or -1 if the value is not in the array
-
remove
public boolean remove(Object value)
Remove the specified value from the JSON array. This method will scan the entire array until it finds a value or reaches the end.- Parameters:
value- the value to remove- Returns:
- true if it removed it, false if not found
-
remove
public Object remove(int pos)
Remove the value at the specified position in the JSON array.- Parameters:
pos- the position to remove the value at- Returns:
- the removed value if removed, null otherwise. If the value is a Map, a
JsonObjectis built from this Map and returned. It the value is a List, aJsonArrayis built form this List and returned.
-
size
public int size()
Get the number of values in this JSON array- Returns:
- the number of items
-
isEmpty
public boolean isEmpty()
Are there zero items in this JSON array?- Returns:
- true if zero, false otherwise
-
getList
public List getList()
Get the underlyingListas is. This list may contain values that are not the types returned by theJsonArrayand with an unpredictable representation of the value, e.g you might get a JSON object as aJsonObjector as aMap.- Returns:
- the underlying List.
-
clear
public JsonArray clear()
Remove all entries from the JSON array- Returns:
- a reference to this, so the API can be used fluently
-
encode
public String encode()
Encode the JSON array to a string- Returns:
- the string encoding
-
toBuffer
public Buffer toBuffer()
Encode this JSON object as buffer.- Returns:
- the buffer encoding.
-
encodePrettily
public String encodePrettily()
Encode the JSON array prettily as a string- Returns:
- the string encoding
-
copy
public JsonArray copy()
Deep copy of the JSON array.- Specified by:
copyin interfaceShareable- Returns:
- a copy where all elements have been copied recursively
- Throws:
IllegalStateException- when a nested element cannot be copied
-
copy
public JsonArray copy(Function<Object,?> cloner)
Deep copy of the JSON array.Unlike
copy()that can fail when an unknown element cannot be copied, this method delegates the copy of such element to theclonerfunction and will not fail.- Parameters:
cloner- a function that copies custom values not supported by the JSON implementation- Returns:
- a copy where all elements have been copied recursively
-
stream
public Stream<Object> stream()
Get a Stream over the entries in the JSON array. The values in the stream will follow the same rules as defined ingetValue(int), respecting the JSON requirements. To stream the raw values, use the storage object stream instead:jsonArray .getList() .stream()- Returns:
- a Stream
-
writeToBuffer
public void writeToBuffer(Buffer buffer)
Description copied from interface:ClusterSerializableMethod invoked when serializing this instance.- Specified by:
writeToBufferin interfaceClusterSerializable- Parameters:
buffer- theBufferwhere the serialized bytes must be written to
-
readFromBuffer
public int readFromBuffer(int pos, Buffer buffer)Description copied from interface:ClusterSerializableMethod invoked when deserializing bytes to this instance.- Specified by:
readFromBufferin interfaceClusterSerializable- Parameters:
pos- the position where to start reading thebufferbuffer- theBufferwhere the serialized bytes must be read from- Returns:
- the position after the last serialized byte
-
-