public class JsonObject extends Object implements Iterable<Map.Entry<String,Object>>, io.vertx.core.shareddata.impl.ClusterSerializable, Shareable
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 object. The implementation adheres to the RFC-7493 to support Temporal data types as well as binary data.
Please see the documentation for more information.
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()
Copy the 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. |
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
|
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
|
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.
|
JsonObject |
put(String key,
Boolean value)
Put a Boolean into the JSON object with the specified key.
|
JsonObject |
put(String key,
byte[] value)
Put a byte[] into the JSON object with the specified key.
|
JsonObject |
put(String key,
CharSequence value)
Put an CharSequence into the JSON object with the specified key.
|
JsonObject |
put(String key,
Double value)
Put a Double into the JSON object with the specified key.
|
JsonObject |
put(String key,
Enum value)
Put an Enum into the JSON object with the specified key.
|
JsonObject |
put(String key,
Float value)
Put a Float into the JSON object with the specified key.
|
JsonObject |
put(String key,
java.time.Instant value)
Put a Instant into the JSON object with the specified key.
|
JsonObject |
put(String key,
Integer value)
Put an Integer into the JSON object with the specified key.
|
JsonObject |
put(String key,
JsonArray value)
Put a JSON array into the JSON object with the specified key.
|
JsonObject |
put(String key,
JsonObject value)
Put another JSON object into the JSON object with the specified key.
|
JsonObject |
put(String key,
Long value)
Put a Long into the JSON object with the specified key.
|
JsonObject |
put(String key,
Object value)
Put an Object into the JSON object with the specified key.
|
JsonObject |
put(String key,
String value)
Put a String 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) |
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 of the entries in the JSON object.
|
Buffer |
toBuffer()
Encode this JSON object as buffer.
|
String |
toString() |
void |
writeToBuffer(Buffer buffer) |
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 mapFrom(Object obj)
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)
key
- the key to return the value forClassCastException
- if the value is not a Stringpublic 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.
This method should be used in conjunction with put(String, byte[])
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 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.
This method should be used in conjunction with put(String, java.time.Instant)
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)
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 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 put(String key, Enum value)
JSON has no concept of encoding Enums, so the Enum will be converted to a String using the Enum.name()
method and the value put as a String.
key
- the keyvalue
- the valuepublic JsonObject put(String key, CharSequence value)
key
- the keyvalue
- the valuepublic JsonObject put(String key, String value)
key
- the keyvalue
- the valuepublic JsonObject put(String key, Integer value)
key
- the keyvalue
- the valuepublic JsonObject put(String key, Long value)
key
- the keyvalue
- the valuepublic JsonObject put(String key, Double value)
key
- the keyvalue
- the valuepublic JsonObject put(String key, Float value)
key
- the keyvalue
- the valuepublic JsonObject put(String key, Boolean value)
key
- the keyvalue
- the valuepublic JsonObject putNull(String key)
key
- the keypublic JsonObject put(String key, JsonObject value)
key
- the keyvalue
- the valuepublic JsonObject put(String key, JsonArray value)
key
- the keyvalue
- the valuepublic JsonObject put(String key, byte[] value)
JSON extension RFC7493, binary will first be Base64 encoded before being put as a String.
key
- the keyvalue
- the valuepublic JsonObject put(String key, java.time.Instant value)
JSON extension RFC7493, instant will first be encoded to ISO 8601 date and time String such as "2017-04-03T10:25:41Z".
key
- the keyvalue
- the valuepublic 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()
public Map<String,Object> getMap()
Map
as is.
This map may contain values that are not the types returned by the JsonObject
.public java.util.stream.Stream<Map.Entry<String,Object>> stream()
public Iterator<Map.Entry<String,Object>> iterator()
public int size()
public JsonObject clear()
public boolean isEmpty()
public void writeToBuffer(Buffer buffer)
writeToBuffer
in interface io.vertx.core.shareddata.impl.ClusterSerializable
public int readFromBuffer(int pos, Buffer buffer)
readFromBuffer
in interface io.vertx.core.shareddata.impl.ClusterSerializable
Copyright © 2023 Eclipse. All rights reserved.