public class JsonObject extends Object implements Iterable<Map.Entry<String,Object>>, ClusterSerializable, Shareable
Constructor and Description |
---|
JsonObject()
Create a new, empty instance
|
JsonObject(Buffer buf)
Create an instance from a buffer.
|
JsonObject(Map<String,Object> map)
Create an instance from a Map.
|
JsonObject(String json)
Create an instance from a string of JSON
|
Modifier and Type | Method and Description |
---|---|
JsonObject |
clear()
Remove all the entries in this JSON object
|
boolean |
containsKey(String key)
Does the JSON object contain the specified key?
|
JsonObject |
copy()
Deep copy of this JSON object.
|
JsonObject |
copy(java.util.function.Function<Object,?> cloner)
Deep copy of this JSON object.
|
String |
encode()
Encode this JSON object as a string.
|
String |
encodePrettily()
Encode this JSON object a a string, with whitespace to make the object easier to read by a human, or other
sentient organism.
|
boolean |
equals(Object o) |
Set<String> |
fieldNames()
Return the set of field names in the JSON objects
|
byte[] |
getBinary(String key)
Get the binary value with the specified key.
|
byte[] |
getBinary(String key,
byte[] def)
Like
getBinary(String) but specifying a default value to return if there is no entry. |
Boolean |
getBoolean(String key)
Get the Boolean value with the specified key
|
Boolean |
getBoolean(String key,
Boolean def)
Like
getBoolean(String) but specifying a default value to return if there is no entry. |
Buffer |
getBuffer(String key)
Get the
Buffer value with the specified key. |
Buffer |
getBuffer(String key,
Buffer def)
Like
getBuffer(String) but specifying a default value to return if there is no entry. |
Double |
getDouble(String key)
Get the Double value with the specified key
|
Double |
getDouble(String key,
Double def)
Like
getDouble(String) but specifying a default value to return if there is no entry. |
Float |
getFloat(String key)
Get the Float value with the specified key
|
Float |
getFloat(String key,
Float def)
Like
getFloat(String) but specifying a default value to return if there is no entry. |
java.time.Instant |
getInstant(String key)
Get the instant value with the specified key.
|
java.time.Instant |
getInstant(String key,
java.time.Instant def)
Like
getInstant(String) but specifying a default value to return if there is no entry. |
Integer |
getInteger(String key)
Get the Integer value with the specified key
|
Integer |
getInteger(String key,
Integer def)
Like
getInteger(String) but specifying a default value to return if there is no entry. |
JsonArray |
getJsonArray(String key)
Get the JsonArray value with the specified key
|
JsonArray |
getJsonArray(String key,
JsonArray def)
Like
getJsonArray(String) but specifying a default value to return if there is no entry. |
JsonObject |
getJsonObject(String key)
Get the JsonObject value with the specified key
|
JsonObject |
getJsonObject(String key,
JsonObject def)
Like
getJsonObject(String) but specifying a default value to return if there is no entry. |
Long |
getLong(String key)
Get the Long value with the specified key
|
Long |
getLong(String key,
Long def)
Like
getLong(String) but specifying a default value to return if there is no entry. |
Map<String,Object> |
getMap()
Get the underlying
Map as is. |
Number |
getNumber(String key)
Get the Number value with the specified key
|
Number |
getNumber(String key,
Number def)
Like
getNumber(String) but specifying a default value to return if there is no entry. |
String |
getString(String key)
Get the string value with the specified key, special cases are addressed for extended JSON types
Instant ,
byte[] and Enum which can be converted to String. |
String |
getString(String key,
String def)
Like
getString(String) but specifying a default value to return if there is no entry. |
Object |
getValue(String key)
Get the value with the specified key, as an Object with types respecting the limitations of JSON.
|
Object |
getValue(String key,
Object def)
Like
getValue(String) but specifying a default value to return if there is no entry. |
int |
hashCode() |
boolean |
isEmpty()
Is this object entry?
|
Iterator<Map.Entry<String,Object>> |
iterator()
Get an Iterator of the entries in the JSON object.
|
static JsonObject |
mapFrom(Object obj)
Create a JsonObject from the fields of a Java object.
|
<T> T |
mapTo(Class<T> type)
Instantiate a Java object from a JsonObject.
|
JsonObject |
mergeIn(JsonObject other)
Merge in another JSON object.
|
JsonObject |
mergeIn(JsonObject other,
boolean deep)
Merge in another JSON object.
|
JsonObject |
mergeIn(JsonObject other,
int depth)
Merge in another JSON object.
|
static JsonObject |
of()
Create a JsonObject containing zero mappings.
|
static JsonObject |
of(String k1,
Object v1)
Create a JsonObject containing a single mapping.
|
static JsonObject |
of(String k1,
Object v1,
String k2,
Object v2)
Create a JsonObject containing two mappings.
|
static JsonObject |
of(String k1,
Object v1,
String k2,
Object v2,
String k3,
Object v3)
Create a JsonObject containing three mappings.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
JsonObject |
put(String key,
Object value)
Put an Object into the JSON object with the specified key.
|
JsonObject |
putNull(String key)
Put a null value into the JSON object with the specified key.
|
int |
readFromBuffer(int pos,
Buffer buffer)
Method invoked when deserializing bytes to this instance.
|
Object |
remove(String key)
Remove an entry from this object.
|
int |
size()
Get the number of entries in the JSON object
|
java.util.stream.Stream<Map.Entry<String,Object>> |
stream()
Get a Stream over the entries in the JSON object.
|
Buffer |
toBuffer()
Encode this JSON object as buffer.
|
String |
toString() |
void |
writeToBuffer(Buffer buffer)
Method invoked when serializing this instance.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
forEach, spliterator
public JsonObject(String json)
json
- the string of JSONpublic JsonObject()
public JsonObject(Map<String,Object> map)
map
- the map to create the instance from.public JsonObject(Buffer buf)
buf
- the buffer to create the instance from.public static JsonObject of()
public static JsonObject of(String k1, Object v1)
k1
- the mapping's keyv1
- the mapping's valuepublic static JsonObject of(String k1, Object v1, String k2, Object v2)
k1
- the first mapping's keyv1
- the first mapping's valuek2
- the second mapping's keyv2
- the second mapping's valuepublic static JsonObject of(String k1, Object v1, String k2, Object v2, String k3, Object v3)
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 valuepublic static JsonObject of(String k1, Object v1, String k2, Object v2, String k3, Object v3, String k4, Object v4)
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 valuepublic static JsonObject of(String k1, Object v1, String k2, Object v2, String k3, Object v3, String k4, Object v4, String k5, Object v5)
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 valuepublic 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)
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 valuepublic 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)
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 valuepublic 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)
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 valuepublic 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)
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 valuepublic 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)
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 valuepublic static JsonObject mapFrom(Object obj)
null when obj
is null
.
obj
- The object to convert to a JsonObject.IllegalArgumentException
- if conversion fails due to an incompatible type.public <T> T mapTo(Class<T> type)
type
- The type to instantiate from the JsonObject.IllegalArgumentException
- if the type cannot be instantiated.public String getString(String key)
Instant
,
byte[]
and Enum
which can be converted to String.key
- the key to return the value forpublic Number getNumber(String key)
key
- the key to return the value forClassCastException
- if the value is not a Numberpublic Integer getInteger(String key)
key
- the key to return the value forClassCastException
- if the value is not an Integerpublic Long getLong(String key)
key
- the key to return the value forClassCastException
- if the value is not a Longpublic Double getDouble(String key)
key
- the key to return the value forClassCastException
- if the value is not a Doublepublic Float getFloat(String key)
key
- the key to return the value forClassCastException
- if the value is not a Floatpublic Boolean getBoolean(String key)
key
- the key to return the value forClassCastException
- if the value is not a Booleanpublic JsonObject getJsonObject(String key)
key
- the key to return the value forClassCastException
- if the value is not a JsonObjectpublic JsonArray getJsonArray(String key)
key
- the key to return the value forClassCastException
- if the value is not a JsonArraypublic byte[] getBinary(String 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.
key
- the key to return the value forClassCastException
- if the value is not a StringIllegalArgumentException
- if the String value is not a legal Base64 encoded valuepublic Buffer getBuffer(String key)
Buffer
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.
key
- the string to return the value forClassCastException
- if the value is not a stringIllegalArgumentException
- if the value is not a legal Base64 encoded stringpublic java.time.Instant getInstant(String 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.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.
key
- the key to return the value forClassCastException
- if the value is not a Stringjava.time.format.DateTimeParseException
- if the String value is not a legal ISO 8601 encoded valuepublic Object getValue(String key)
Map
will be wrapped to JsonObject
List
will be wrapped to JsonArray
Instant
will be converted to String
byte[]
will be converted to String
Enum
will be converted to String
key
- the key to lookuppublic String getString(String key, String def)
getString(String)
but specifying a default value to return if there is no entry.key
- the key to lookupdef
- the default value to use if the entry is not presentdef
if no entry presentpublic Number getNumber(String key, Number def)
getNumber(String)
but specifying a default value to return if there is no entry.key
- the key to lookupdef
- the default value to use if the entry is not presentdef
if no entry presentpublic Integer getInteger(String key, Integer def)
getInteger(String)
but specifying a default value to return if there is no entry.key
- the key to lookupdef
- the default value to use if the entry is not presentdef
if no entry presentpublic Long getLong(String key, Long def)
getLong(String)
but specifying a default value to return if there is no entry.key
- the key to lookupdef
- the default value to use if the entry is not presentdef
if no entry presentpublic Double getDouble(String key, Double def)
getDouble(String)
but specifying a default value to return if there is no entry.key
- the key to lookupdef
- the default value to use if the entry is not presentdef
if no entry presentpublic Float getFloat(String key, Float def)
getFloat(String)
but specifying a default value to return if there is no entry.key
- the key to lookupdef
- the default value to use if the entry is not presentdef
if no entry presentpublic Boolean getBoolean(String key, Boolean def)
getBoolean(String)
but specifying a default value to return if there is no entry.key
- the key to lookupdef
- the default value to use if the entry is not presentdef
if no entry presentpublic JsonObject getJsonObject(String key, JsonObject def)
getJsonObject(String)
but specifying a default value to return if there is no entry.key
- the key to lookupdef
- the default value to use if the entry is not presentdef
if no entry presentpublic JsonArray getJsonArray(String key, JsonArray def)
getJsonArray(String)
but specifying a default value to return if there is no entry.key
- the key to lookupdef
- the default value to use if the entry is not presentdef
if no entry presentpublic byte[] getBinary(String key, byte[] def)
getBinary(String)
but specifying a default value to return if there is no entry.key
- the key to lookupdef
- the default value to use if the entry is not presentdef
if no entry presentpublic Buffer getBuffer(String key, Buffer def)
getBuffer(String)
but specifying a default value to return if there is no entry.key
- the key to lookupdef
- the default value to use if the entry is not presentdef
if no entry presentpublic java.time.Instant getInstant(String key, java.time.Instant def)
getInstant(String)
but specifying a default value to return if there is no entry.key
- the key to lookupdef
- the default value to use if the entry is not presentdef
if no entry presentpublic Object getValue(String key, Object def)
getValue(String)
but specifying a default value to return if there is no entry.key
- the key to lookupdef
- the default value to use if the entry is not presentdef
if no entry presentpublic boolean containsKey(String key)
key
- the keypublic Set<String> fieldNames()
public JsonObject putNull(String key)
key
- the keypublic JsonObject put(String key, Object value)
key
- the keyvalue
- the valuepublic Object remove(String key)
key
- the keypublic JsonObject mergeIn(JsonObject other)
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.
other
- the other JSON objectpublic JsonObject mergeIn(JsonObject other, boolean deep)
other
- the other JSON objectdeep
- if true, a deep merge is performedpublic JsonObject mergeIn(JsonObject other, int depth)
other
- the other JSON objectdepth
- depth of mergepublic String encode()
public String encodePrettily()
public Buffer toBuffer()
public JsonObject copy()
copy
in interface Shareable
IllegalStateException
- when a nested element cannot be copiedpublic JsonObject copy(java.util.function.Function<Object,?> cloner)
Unlike copy()
that can fail when an unknown element cannot be copied, this method
delegates the copy of such element to the cloner
function and will not fail.
cloner
- a function that copies custom values not supported by the JSON implementationpublic Map<String,Object> getMap()
Map
as is.
This map may contain values that are not the types returned by the JsonObject
and
with an unpredictable representation of the value, e.g you might get a JSON object
as a JsonObject
or as a Map
.
public java.util.stream.Stream<Map.Entry<String,Object>> stream()
getValue(String)
, respecting the JSON requirements.
To stream the raw values, use the storage object stream instead:
jsonObject
.getMap()
.stream()
public Iterator<Map.Entry<String,Object>> iterator()
public int size()
public JsonObject clear()
public boolean isEmpty()
public void writeToBuffer(Buffer buffer)
ClusterSerializable
writeToBuffer
in interface io.vertx.core.shareddata.impl.ClusterSerializable
buffer
- the Buffer
where the serialized bytes must be written topublic int readFromBuffer(int pos, Buffer buffer)
ClusterSerializable
readFromBuffer
in interface io.vertx.core.shareddata.impl.ClusterSerializable
pos
- the position where to start reading the buffer
buffer
- the Buffer
where the serialized bytes must be read fromCopyright © 2024 Eclipse. All rights reserved.