Class JsonObject
- Author:
- Tim Fox
-
Constructor Summary
ConstructorsConstructorDescriptionCreate a new, empty instanceJsonObject(Buffer buf) Create an instance from a buffer.JsonObject(String json) Create an instance from a string of JSONJsonObject(Map<String, Object> map) Create an instance from a Map. -
Method Summary
Modifier and TypeMethodDescriptionclear()Remove all the entries in this JSON objectbooleancontainsKey(String key) Does the JSON object contain the specified key?copy()Deep copy of this JSON object.Deep copy of this JSON object.encode()Encode this JSON object as a string.Encode this JSON object a a string, with whitespace to make the object easier to read by a human, or other sentient organism.booleanReturn the set of field names in the JSON objectsbyte[]Get the binary value with the specified key.byte[]LikegetBinary(String)but specifying a default value to return if there is no entry.getBoolean(String key) Get the Boolean value with the specified keygetBoolean(String key, Boolean def) LikegetBoolean(String)but specifying a default value to return if there is no entry.Get theBuffervalue with the specified key.LikegetBuffer(String)but specifying a default value to return if there is no entry.Get the Double value with the specified keyLikegetDouble(String)but specifying a default value to return if there is no entry.Get the Float value with the specified keyLikegetFloat(String)but specifying a default value to return if there is no entry.getInstant(String key) Get the instant value with the specified key.getInstant(String key, Instant def) LikegetInstant(String)but specifying a default value to return if there is no entry.getInteger(String key) Get the Integer value with the specified keygetInteger(String key, Integer def) LikegetInteger(String)but specifying a default value to return if there is no entry.getJsonArray(String key) Get the JsonArray value with the specified keygetJsonArray(String key, JsonArray def) LikegetJsonArray(String)but specifying a default value to return if there is no entry.getJsonObject(String key) Get the JsonObject value with the specified keygetJsonObject(String key, JsonObject def) LikegetJsonObject(String)but specifying a default value to return if there is no entry.Get the Long value with the specified keyLikegetLong(String)but specifying a default value to return if there is no entry.getMap()Get the underlyingMapas is.Get the Number value with the specified keyLikegetNumber(String)but specifying a default value to return if there is no entry.Get the string value with the specified key, special cases are addressed for extended JSON typesInstant,byte[]andEnumwhich can be converted to String.LikegetString(String)but specifying a default value to return if there is no entry.Get the value with the specified key, as an Object with types respecting the limitations of JSON.LikegetValue(String)but specifying a default value to return if there is no entry.inthashCode()booleanisEmpty()Is this object entry?iterator()Get an Iterator of the entries in the JSON object.static JsonObjectCreate a JsonObject from the fields of a Java object.<T> TInstantiate a Java object from a JsonObject.mergeIn(JsonObject other) Merge in another JSON object.mergeIn(JsonObject other, boolean deep) Merge in another JSON object.mergeIn(JsonObject other, int depth) Merge in another JSON object.static JsonObjectof()Create a JsonObject containing zero mappings.static JsonObjectCreate a JsonObject containing a single mapping.static JsonObjectCreate a JsonObject containing two mappings.static JsonObjectCreate a JsonObject containing three mappings.static JsonObjectCreate a JsonObject containing four mappings.static JsonObjectof(String k1, Object v1, String k2, Object v2, String k3, Object v3, String k4, Object v4, String k5, Object v5) Create a JsonObject containing five mappings.static JsonObjectof(String k1, Object v1, String k2, Object v2, String k3, Object v3, String k4, Object v4, String k5, Object v5, String k6, Object v6) Create a JsonObject containing six mappings.static JsonObjectof(String k1, Object v1, String k2, Object v2, String k3, Object v3, String k4, Object v4, String k5, Object v5, String k6, Object v6, String k7, Object v7) Create a JsonObject containing seven mappings.static JsonObjectof(String k1, Object v1, String k2, Object v2, String k3, Object v3, String k4, Object v4, String k5, Object v5, String k6, Object v6, String k7, Object v7, String k8, Object v8) Create a JsonObject containing eight mappings.static JsonObjectof(String k1, Object v1, String k2, Object v2, String k3, Object v3, String k4, Object v4, String k5, Object v5, String k6, Object v6, String k7, Object v7, String k8, Object v8, String k9, Object v9) Create a JsonObject containing nine mappings.static JsonObjectof(String k1, Object v1, String k2, Object v2, String k3, Object v3, String k4, Object v4, String k5, Object v5, String k6, Object v6, String k7, Object v7, String k8, Object v8, String k9, Object v9, String k10, Object v10) Create a JsonObject containing ten mappings.Put an Object into the JSON object with the specified key.Put a null value into the JSON object with the specified key.intreadFromBuffer(int pos, Buffer buffer) Method invoked when deserializing bytes to this instance.Remove an entry from this object.intsize()Get the number of entries in the JSON objectstream()Get a Stream over the entries in the JSON object.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
-
JsonObject
Create an instance from a string of JSON- Parameters:
json- the string of JSON
-
JsonObject
public JsonObject()Create a new, empty instance -
JsonObject
-
JsonObject
Create an instance from a buffer.- Parameters:
buf- the buffer to create the instance from.
-
-
Method Details
-
of
Create a JsonObject containing zero mappings.- Returns:
- an empty JsonObject.
-
of
Create a JsonObject containing a single mapping.- Parameters:
k1- the mapping's keyv1- the mapping's value- Returns:
- a JsonObject containing the specified mapping.
-
of
Create a JsonObject containing two mappings.- Parameters:
k1- the first mapping's keyv1- the first mapping's valuek2- the second mapping's keyv2- the second mapping's value- Returns:
- a JsonObject containing the specified mappings.
-
of
Create a JsonObject containing three mappings.- Parameters:
k1- the first mapping's keyv1- the first mapping's valuek2- the second mapping's keyv2- the second mapping's valuek3- the third mapping's keyv3- the third mapping's value- Returns:
- a JsonObject containing the specified mappings.
-
of
public static JsonObject of(String k1, Object v1, String k2, Object v2, String k3, Object v3, String k4, Object v4) Create a JsonObject containing four mappings.- Parameters:
k1- the first mapping's keyv1- the first mapping's valuek2- the second mapping's keyv2- the second mapping's valuek3- the third mapping's keyv3- the third mapping's valuek4- the fourth mapping's keyv4- the fourth mapping's value- Returns:
- a JsonObject containing the specified mappings.
-
of
public static JsonObject of(String k1, Object v1, String k2, Object v2, String k3, Object v3, String k4, Object v4, String k5, Object v5) Create a JsonObject containing five mappings.- Parameters:
k1- the first mapping's keyv1- the first mapping's valuek2- the second mapping's keyv2- the second mapping's valuek3- the third mapping's keyv3- the third mapping's valuek4- the fourth mapping's keyv4- the fourth mapping's valuek5- the fifth mapping's keyv5- the fifth mapping's value- Returns:
- a JsonObject containing the specified mappings.
-
of
public static JsonObject of(String k1, Object v1, String k2, Object v2, String k3, Object v3, String k4, Object v4, String k5, Object v5, String k6, Object v6) Create a JsonObject containing six mappings.- Parameters:
k1- the first mapping's keyv1- the first mapping's valuek2- the second mapping's keyv2- the second mapping's valuek3- the third mapping's keyv3- the third mapping's valuek4- the fourth mapping's keyv4- the fourth mapping's valuek5- the fifth mapping's keyv5- the fifth mapping's valuek6- the sixth mapping's keyv6- the sixth mapping's value- Returns:
- a JsonObject containing the specified mappings.
-
of
public static JsonObject of(String k1, Object v1, String k2, Object v2, String k3, Object v3, String k4, Object v4, String k5, Object v5, String k6, Object v6, String k7, Object v7) Create a JsonObject containing seven mappings.- Parameters:
k1- the first mapping's keyv1- the first mapping's valuek2- the second mapping's keyv2- the second mapping's valuek3- the third mapping's keyv3- the third mapping's valuek4- the fourth mapping's keyv4- the fourth mapping's valuek5- the fifth mapping's keyv5- the fifth mapping's valuek6- the sixth mapping's keyv6- the sixth mapping's valuek7- the seventh mapping's keyv7- the seventh mapping's value- Returns:
- a JsonObject containing the specified mappings.
-
of
public static JsonObject of(String k1, Object v1, String k2, Object v2, String k3, Object v3, String k4, Object v4, String k5, Object v5, String k6, Object v6, String k7, Object v7, String k8, Object v8) Create a JsonObject containing eight mappings.- Parameters:
k1- the first mapping's keyv1- the first mapping's valuek2- the second mapping's keyv2- the second mapping's valuek3- the third mapping's keyv3- the third mapping's valuek4- the fourth mapping's keyv4- the fourth mapping's valuek5- the fifth mapping's keyv5- the fifth mapping's valuek6- the sixth mapping's keyv6- the sixth mapping's valuek7- the seventh mapping's keyv7- the seventh mapping's valuek8- the eighth mapping's keyv8- the eighth mapping's value- Returns:
- a JsonObject containing the specified mappings.
-
of
public static JsonObject of(String k1, Object v1, String k2, Object v2, String k3, Object v3, String k4, Object v4, String k5, Object v5, String k6, Object v6, String k7, Object v7, String k8, Object v8, String k9, Object v9) Create a JsonObject containing nine mappings.- Parameters:
k1- the first mapping's keyv1- the first mapping's valuek2- the second mapping's keyv2- the second mapping's valuek3- the third mapping's keyv3- the third mapping's valuek4- the fourth mapping's keyv4- the fourth mapping's valuek5- the fifth mapping's keyv5- the fifth mapping's valuek6- the sixth mapping's keyv6- the sixth mapping's valuek7- the seventh mapping's keyv7- the seventh mapping's valuek8- the eighth mapping's keyv8- the eighth mapping's valuek9- the ninth mapping's keyv9- the ninth mapping's value- Returns:
- a JsonObject containing the specified mappings.
-
of
public static JsonObject of(String k1, Object v1, String k2, Object v2, String k3, Object v3, String k4, Object v4, String k5, Object v5, String k6, Object v6, String k7, Object v7, String k8, Object v8, String k9, Object v9, String k10, Object v10) Create a JsonObject containing ten mappings.- Parameters:
k1- the first mapping's keyv1- the first mapping's valuek2- the second mapping's keyv2- the second mapping's valuek3- the third mapping's keyv3- the third mapping's valuek4- the fourth mapping's keyv4- the fourth mapping's valuek5- the fifth mapping's keyv5- the fifth mapping's valuek6- the sixth mapping's keyv6- the sixth mapping's valuek7- the seventh mapping's keyv7- the seventh mapping's valuek8- the eighth mapping's keyv8- the eighth mapping's valuek9- the ninth mapping's keyv9- the ninth mapping's valuek10- the tenth mapping's keyv10- the tenth mapping's value- Returns:
- a JsonObject containing the specified mappings.
-
mapFrom
Create a JsonObject from the fields of a Java object. Faster than calling `new JsonObject(Json.encode(obj))`. invalid input: '<'p/ Returnsnullwhenobjisnull.- Parameters:
obj- The object to convert to a JsonObject.- Throws:
IllegalArgumentException- if conversion fails due to an incompatible type.
-
mapTo
Instantiate a Java object from a JsonObject. Faster than calling `Json.decodeValue(Json.encode(jsonObject), type)`.- Parameters:
type- The type to instantiate from the JsonObject.- Throws:
IllegalArgumentException- if the type cannot be instantiated.
-
getString
Get the string value with the specified key, special cases are addressed for extended JSON typesInstant,byte[]andEnumwhich can be converted to String.- Parameters:
key- the key to return the value for- Returns:
- the value string representation or null if no value for that key
-
getNumber
Get the Number value with the specified key- Parameters:
key- the key to return the value for- Returns:
- the value or null if no value for that key
- Throws:
ClassCastException- if the value is not a Number
-
getInteger
Get the Integer value with the specified key- Parameters:
key- the key to return the value for- Returns:
- the value or null if no value for that key
- Throws:
ClassCastException- if the value is not an Integer
-
getLong
Get the Long value with the specified key- Parameters:
key- the key to return the value for- Returns:
- the value or null if no value for that key
- Throws:
ClassCastException- if the value is not a Long
-
getDouble
Get the Double value with the specified key- Parameters:
key- the key to return the value for- Returns:
- the value or null if no value for that key
- Throws:
ClassCastException- if the value is not a Double
-
getFloat
Get the Float value with the specified key- Parameters:
key- the key to return the value for- Returns:
- the value or null if no value for that key
- Throws:
ClassCastException- if the value is not a Float
-
getBoolean
Get the Boolean value with the specified key- Parameters:
key- the key to return the value for- Returns:
- the value or null if no value for that key
- Throws:
ClassCastException- if the value is not a Boolean
-
getJsonObject
Get the JsonObject value with the specified key- Parameters:
key- the key to return the value for- Returns:
- the value or null if no value for that key
- Throws:
ClassCastException- if the value is not a JsonObject
-
getJsonArray
Get the JsonArray value with the specified key- Parameters:
key- the key to return the value for- Returns:
- the value or null if no value for that key
- Throws:
ClassCastException- if the value is not a JsonArray
-
getBinary
Get the binary value with the specified key.JSON itself has no notion of a binary, this extension complies to the RFC-7493, so this method assumes there is a String value with the key and it contains a Base64 encoded binary, which it decodes if found and returns.
- Parameters:
key- the key to return the value for- Returns:
- the value or null if no value for that key
- Throws:
ClassCastException- if the value is not a StringIllegalArgumentException- if the String value is not a legal Base64 encoded value
-
getBuffer
Get theBuffervalue with the specified key.JSON itself has no notion of a binary, this extension complies to the RFC-7493, so this method assumes there is a String value with the key and it contains a Base64 encoded binary, which it decodes if found and returns.
- Parameters:
key- the string to return the value for- Returns:
- the value or null if no value for that key
- Throws:
ClassCastException- if the value is not a stringIllegalArgumentException- if the value is not a legal Base64 encoded string
-
getInstant
Get the instant value with the specified key.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:
key- the key to return the value for- Returns:
- the value or null if no value for that key
- Throws:
ClassCastException- if the value is not a 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 toStringEnumwill be converted toString
- Parameters:
key- the key to lookup- Returns:
- the value
-
getString
LikegetString(String)but specifying a default value to return if there is no entry.- Parameters:
key- the key to lookupdef- the default value to use if the entry is not present- Returns:
- the value or
defif no entry present
-
getNumber
LikegetNumber(String)but specifying a default value to return if there is no entry.- Parameters:
key- the key to lookupdef- the default value to use if the entry is not present- Returns:
- the value or
defif no entry present
-
getInteger
LikegetInteger(String)but specifying a default value to return if there is no entry.- Parameters:
key- the key to lookupdef- the default value to use if the entry is not present- Returns:
- the value or
defif no entry present
-
getLong
LikegetLong(String)but specifying a default value to return if there is no entry.- Parameters:
key- the key to lookupdef- the default value to use if the entry is not present- Returns:
- the value or
defif no entry present
-
getDouble
LikegetDouble(String)but specifying a default value to return if there is no entry.- Parameters:
key- the key to lookupdef- the default value to use if the entry is not present- Returns:
- the value or
defif no entry present
-
getFloat
LikegetFloat(String)but specifying a default value to return if there is no entry.- Parameters:
key- the key to lookupdef- the default value to use if the entry is not present- Returns:
- the value or
defif no entry present
-
getBoolean
LikegetBoolean(String)but specifying a default value to return if there is no entry.- Parameters:
key- the key to lookupdef- the default value to use if the entry is not present- Returns:
- the value or
defif no entry present
-
getJsonObject
LikegetJsonObject(String)but specifying a default value to return if there is no entry.- Parameters:
key- the key to lookupdef- the default value to use if the entry is not present- Returns:
- the value or
defif no entry present
-
getJsonArray
LikegetJsonArray(String)but specifying a default value to return if there is no entry.- Parameters:
key- the key to lookupdef- the default value to use if the entry is not present- Returns:
- the value or
defif no entry present
-
getBinary
LikegetBinary(String)but specifying a default value to return if there is no entry.- Parameters:
key- the key to lookupdef- the default value to use if the entry is not present- Returns:
- the value or
defif no entry present
-
getBuffer
LikegetBuffer(String)but specifying a default value to return if there is no entry.- Parameters:
key- the key to lookupdef- the default value to use if the entry is not present- Returns:
- the value or
defif no entry present
-
getInstant
LikegetInstant(String)but specifying a default value to return if there is no entry.- Parameters:
key- the key to lookupdef- the default value to use if the entry is not present- Returns:
- the value or
defif no entry present
-
getValue
LikegetValue(String)but specifying a default value to return if there is no entry.- Parameters:
key- the key to lookupdef- the default value to use if the entry is not present- Returns:
- the value or
defif no entry present
-
containsKey
Does the JSON object contain the specified key?- Parameters:
key- the key- Returns:
- true if it contains the key, false if not.
-
fieldNames
-
putNull
Put a null value into the JSON object with the specified key.- Parameters:
key- the key- Returns:
- a reference to this, so the API can be used fluently
-
put
Put an Object into the JSON object with the specified key.- Parameters:
key- the keyvalue- the value- Returns:
- a reference to this, so the API can be used fluently
-
remove
-
mergeIn
Merge in another JSON object.This is the equivalent of putting all the entries of the other JSON object into this object. This is not a deep merge, entries containing (sub) JSON objects will be replaced entirely.
- Parameters:
other- the other JSON object- Returns:
- a reference to this, so the API can be used fluently
-
mergeIn
Merge in another JSON object. A deep merge (recursive) matches (sub) JSON objects in the existing tree and replaces all matching entries. JsonArrays are treated like any other entry, i.e. replaced entirely.- Parameters:
other- the other JSON objectdeep- if true, a deep merge is performed- Returns:
- a reference to this, so the API can be used fluently
-
mergeIn
Merge in another JSON object. The merge is deep (recursive) to the specified level. If depth is 0, no merge is performed, if depth is greater than the depth of one of the objects, a full deep merge is performed.- Parameters:
other- the other JSON objectdepth- depth of merge- Returns:
- a reference to this, so the API can be used fluently
-
encode
-
encodePrettily
Encode this JSON object a a string, with whitespace to make the object easier to read by a human, or other sentient organism.- Returns:
- the pretty string encoding.
-
toBuffer
-
copy
Deep copy of this JSON object.- 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 this JSON object.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
-
getMap
Get the underlyingMapas is.This map may contain values that are not the types returned by the
JsonObjectand with an unpredictable representation of the value, e.g you might get a JSON object as aJsonObjector as aMap.- Returns:
- the underlying Map.
-
stream
Get a Stream over the entries in the JSON object. The values in the stream will follow the same rules as defined ingetValue(String), respecting the JSON requirements.To stream the raw values, use the storage object stream instead:
jsonObject .getMap() .stream()- Returns:
- a Stream
-
iterator
-
size
public int size()Get the number of entries in the JSON object- Returns:
- the number of entries
-
clear
Remove all the entries in this JSON object -
isEmpty
public boolean isEmpty()Is this object entry?- Returns:
- true if it has zero entries, false if not.
-
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
-