Class JsonArray
java.lang.Object
io.vertx.core.json.JsonArray
- All Implemented Interfaces:
ClusterSerializable, Shareable, Iterable<Object>
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
ConstructorsConstructorDescriptionCreate an empty instanceCreate an instance from a Buffer of JSON.Create an instance from a String of JSON, this string must be a valid array otherwise an exception will be thrown.Create an instance from a List. -
Method Summary
Modifier and TypeMethodDescriptionAdd an Object to the JSON array at given positionpos.Add an Object to the JSON array.Appends all of the elements in the specified array to the end of this JSON array.addNull()Add a null value to the JSON array.clear()Remove all entries from the JSON arraybooleanReturnstrueif this JSON Array contains the specified value.copy()Deep copy of the JSON array.Deep copy of the JSON array.encode()Encode the JSON array to a stringEncode the JSON array prettily as a stringbooleanbyte[]getBinary(int pos) Get the byte[] at positionposin the array.getBoolean(int pos) Get the Boolean at positionposin the array,getBuffer(int pos) Get the Buffer at positionposin the array.getDouble(int pos) Get the Double at positionposin the array,getFloat(int pos) Get the Float at positionposin the array,getInstant(int pos) Get the Instant at positionposin the array.getInteger(int pos) Get the Integer at positionposin the array,getJsonArray(int pos) Get the JsonArray at positionposin the array.getJsonObject(int pos) Get the JsonObject at positionposin the array.getList()Get the underlyingListas is.getLong(int pos) Get the Long at positionposin the array,getNumber(int pos) Get the Number at positionposin the array,getString(int pos) Get the String at positionposin the array,getValue(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?intReturns 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()Get an Iterator over the values in the JSON arraystatic JsonArrayCreate a JsonArray containing an arbitrary number of values.intreadFromBuffer(int pos, Buffer buffer) Method invoked when deserializing bytes to this instance.remove(int pos) Remove the value at the specified position in the JSON array.booleanRemove the specified value from the JSON array.Set an Object to the JSON array at positionpos.setNull(int pos) Set a null value to the JSON array at positionpos.intsize()Get the number of values in this JSON arraystream()Get a Stream over the entries in the JSON array.toBuffer()Encode this JSON object as buffer.toString()voidwriteToBuffer(Buffer buffer) Method invoked when serializing this instance.Methods inherited from interface Iterable
forEach, spliterator
-
Constructor Details
-
JsonArray
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
Create an instance from a List. The List is not copied.- Parameters:
list- the underlying backing list
-
JsonArray
Create an instance from a Buffer of JSON.- Parameters:
buf- the buffer of JSON.
-
-
Method Details
-
of
Create a JsonArray containing an arbitrary number of values.- Parameters:
values- The objects into JsonArray.- Throws:
NullPointerException- if the args is null.
-
getString
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
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
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
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
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
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
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
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
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
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
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
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
Add a null value to the JSON array.- Returns:
- a reference to this, so the API can be used fluently
-
add
-
add
-
addAll
-
setNull
Set a null value to the JSON array at positionpos.- Returns:
- a reference to this, so the API can be used fluently
-
set
-
contains
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
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
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
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
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
Remove all entries from the JSON array- Returns:
- a reference to this, so the API can be used fluently
-
iterator
-
encode
-
toBuffer
-
encodePrettily
Encode the JSON array prettily as a string- Returns:
- the string encoding
-
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
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
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
-
toString
-
equals
-
hashCode
-
writeToBuffer
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
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
-