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 SummaryConstructors 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 SummaryAll 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.Objectclone, finalize, getClass, notify, notifyAll, wait, wait, wait
 - 
Methods inherited from interface java.lang.IterableforEach, spliterator
 
- 
 
- 
- 
- 
Constructor Detail- 
JsonArraypublic 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
 
 - 
JsonArraypublic JsonArray() Create an empty instance
 - 
JsonArraypublic JsonArray(List list) Create an instance from a List. The List is not copied.- Parameters:
- list- the underlying backing list
 
 - 
JsonArraypublic JsonArray(Buffer buf) Create an instance from a Buffer of JSON.- Parameters:
- buf- the buffer of JSON.
 
 
- 
 - 
Method Detail- 
ofpublic 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.
 
 - 
getStringpublic 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
 
 - 
getNumberpublic 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
 
 - 
getIntegerpublic 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
 
 - 
getLongpublic 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
 
 - 
getDoublepublic 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
 
 - 
getFloatpublic 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
 
 - 
getBooleanpublic 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
 
 - 
getJsonObjectpublic 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
 
 - 
getJsonArraypublic 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
 
 - 
getBinarypublic 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 String
- IllegalArgumentException- if the String value is not a legal Base64 encoded value
 
 - 
getBufferpublic 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 String
- IllegalArgumentException- if the String value is not a legal Base64 encoded value
 
 - 
getInstantpublic 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 String
- DateTimeParseException- if the String value is not a legal ISO 8601 encoded value
 
 - 
getValuepublic Object getValue(int pos) Get the value with the specified key, as an Object with types respecting the limitations of JSON.- Mapwill be wrapped to- JsonObject
- Listwill be wrapped to- JsonArray
- Instantwill be converted to- String
- byte[]will be converted to- String
- Bufferwill be converted to- String
- Enumwill be converted to- String
 - Parameters:
- pos- the position in the array
- Returns:
- the Integer, or null if a null value present
 
 - 
hasNullpublic 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
 
 - 
addNullpublic JsonArray addNull() Add a null value to the JSON array.- Returns:
- a reference to this, so the API can be used fluently
 
 - 
addpublic 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
 
 - 
addpublic JsonArray add(int pos, Object value) Add an Object to the JSON array at given positionpos.- Parameters:
- pos- the position
- value- the value
- Returns:
- a reference to this, so the API can be used fluently
 
 - 
addAllpublic 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
 
 - 
setNullpublic 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
 
 - 
setpublic JsonArray set(int pos, Object value) Set an Object to the JSON array at positionpos.- Parameters:
- pos- position in the array
- value- the value
- Returns:
- a reference to this, so the API can be used fluently
 
 - 
containspublic 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
 
 - 
indexOfpublic 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
 
 - 
removepublic 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
 
 - 
removepublic 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.
 
 - 
sizepublic int size() Get the number of values in this JSON array- Returns:
- the number of items
 
 - 
isEmptypublic boolean isEmpty() Are there zero items in this JSON array?- Returns:
- true if zero, false otherwise
 
 - 
getListpublic 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.
 
 - 
clearpublic JsonArray clear() Remove all entries from the JSON array- Returns:
- a reference to this, so the API can be used fluently
 
 - 
encodepublic String encode() Encode the JSON array to a string- Returns:
- the string encoding
 
 - 
toBufferpublic Buffer toBuffer() Encode this JSON object as buffer.- Returns:
- the buffer encoding.
 
 - 
encodePrettilypublic String encodePrettily() Encode the JSON array prettily as a string- Returns:
- the string encoding
 
 - 
copypublic JsonArray copy() Deep copy of the JSON array.- Specified by:
- copyin interface- Shareable
- Returns:
- a copy where all elements have been copied recursively
- Throws:
- IllegalStateException- when a nested element cannot be copied
 
 - 
copypublic 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
 
 - 
streampublic 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
 
 - 
writeToBufferpublic void writeToBuffer(Buffer buffer) Description copied from interface:ClusterSerializableMethod invoked when serializing this instance.- Specified by:
- writeToBufferin interface- ClusterSerializable
- Parameters:
- buffer- the- Bufferwhere the serialized bytes must be written to
 
 - 
readFromBufferpublic int readFromBuffer(int pos, Buffer buffer)Description copied from interface:ClusterSerializableMethod invoked when deserializing bytes to this instance.- Specified by:
- readFromBufferin interface- ClusterSerializable
- Parameters:
- pos- the position where to start reading the- buffer
- buffer- the- Bufferwhere the serialized bytes must be read from
- Returns:
- the position after the last serialized byte
 
 
- 
 
-