io.vertx.core.json

## Class JsonObject

- java.lang.Object
- - io.vertx.core.json.JsonObject

- All Implemented Interfaces:
    io.vertx.core.shareddata.impl.ClusterSerializable, Shareable, Iterable<Map.Entry<String,Object>>

      public class JsonObject
      extends Object
      implements Iterable<Map.Entry<String,Object>>, ClusterSerializable, Shareable

  A representation of a JSON object 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 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.
  Author:
    Tim Fox

- - ### Constructor Summary

    Constructors
    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

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    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.
    `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.

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

    - ### Methods inherited from interface java.lang.Iterable

      `forEach, spliterator`

- - ### Constructor Detail

    - #### JsonObject

          public JsonObject(String json)

      Create an instance from a string of JSON
      Parameters:
        `json` - the string of JSON

    - #### JsonObject

          public JsonObject()

      Create a new, empty instance

    - #### JsonObject

          public JsonObject(Map<String,Object> map)

      Create an instance from a Map. The Map is not copied.
      Parameters:
        `map` - the map to create the instance from.

    - #### JsonObject

          public JsonObject(Buffer buf)

      Create an instance from a buffer.
      Parameters:
        `buf` - the buffer to create the instance from.

  - ### Method Detail

    - #### mapFrom

          public static JsonObject mapFrom(Object obj)

      Create a JsonObject from the fields of a Java object. Faster than calling `new JsonObject(Json.encode(obj))`.

      null when `obj` is `null`.

      Parameters:
        `obj` - The object to convert to a JsonObject.
      Throws:
        `IllegalArgumentException` - if conversion fails due to an incompatible type.

    - #### mapTo

          public <T> T mapTo(Class<T> type)

      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

          public 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.
      Parameters:
        `key` - the key to return the value for
      Returns:
        the value string representation or null if no value for that key

    - #### getNumber

          public Number getNumber(String key)

      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

          public Integer getInteger(String key)

      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

          public Long getLong(String key)

      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

          public Double getDouble(String key)

      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

          public Float getFloat(String key)

      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

          public Boolean getBoolean(String key)

      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

          public JsonObject getJsonObject(String key)

      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

          public JsonArray getJsonArray(String key)

      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

          public byte[] getBinary(String key)

      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 String
        `IllegalArgumentException` - if the String value is not a legal Base64 encoded value

    - #### getBuffer

          public Buffer getBuffer(String key)

      Get the `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.
      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 string
        `IllegalArgumentException` - if the value is not a legal Base64 encoded string

    - #### getInstant

          public java.time.Instant getInstant(String key)

      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.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.
      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 String
        `java.time.format.DateTimeParseException` - if the String value is not a legal ISO 8601 encoded value

    - #### getValue

          public Object getValue(String key)

      Get the value with the specified key, as an Object with types respecting the limitations of JSON.
      - `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`
      Parameters:
        `key` - the key to lookup
      Returns:
        the value

    - #### getString

          public String getString(String key,
                                  String def)

      Like `getString(String)` but specifying a default value to return if there is no entry.
      Parameters:
        `key` - the key to lookup
        `def` - the default value to use if the entry is not present
      Returns:
        the value or `def` if no entry present

    - #### getNumber

          public Number getNumber(String key,
                                  Number def)

      Like `getNumber(String)` but specifying a default value to return if there is no entry.
      Parameters:
        `key` - the key to lookup
        `def` - the default value to use if the entry is not present
      Returns:
        the value or `def` if no entry present

    - #### getInteger

          public Integer getInteger(String key,
                                    Integer def)

      Like `getInteger(String)` but specifying a default value to return if there is no entry.
      Parameters:
        `key` - the key to lookup
        `def` - the default value to use if the entry is not present
      Returns:
        the value or `def` if no entry present

    - #### getLong

          public Long getLong(String key,
                              Long def)

      Like `getLong(String)` but specifying a default value to return if there is no entry.
      Parameters:
        `key` - the key to lookup
        `def` - the default value to use if the entry is not present
      Returns:
        the value or `def` if no entry present

    - #### getDouble

          public Double getDouble(String key,
                                  Double def)

      Like `getDouble(String)` but specifying a default value to return if there is no entry.
      Parameters:
        `key` - the key to lookup
        `def` - the default value to use if the entry is not present
      Returns:
        the value or `def` if no entry present

    - #### getFloat

          public Float getFloat(String key,
                                Float def)

      Like `getFloat(String)` but specifying a default value to return if there is no entry.
      Parameters:
        `key` - the key to lookup
        `def` - the default value to use if the entry is not present
      Returns:
        the value or `def` if no entry present

    - #### getBoolean

          public Boolean getBoolean(String key,
                                    Boolean def)

      Like `getBoolean(String)` but specifying a default value to return if there is no entry.
      Parameters:
        `key` - the key to lookup
        `def` - the default value to use if the entry is not present
      Returns:
        the value or `def` if no entry present

    - #### getJsonObject

          public JsonObject getJsonObject(String key,
                                          JsonObject def)

      Like `getJsonObject(String)` but specifying a default value to return if there is no entry.
      Parameters:
        `key` - the key to lookup
        `def` - the default value to use if the entry is not present
      Returns:
        the value or `def` if no entry present

    - #### getJsonArray

          public JsonArray getJsonArray(String key,
                                        JsonArray def)

      Like `getJsonArray(String)` but specifying a default value to return if there is no entry.
      Parameters:
        `key` - the key to lookup
        `def` - the default value to use if the entry is not present
      Returns:
        the value or `def` if no entry present

    - #### getBinary

          public byte[] getBinary(String key,
                                  byte[] def)

      Like `getBinary(String)` but specifying a default value to return if there is no entry.
      Parameters:
        `key` - the key to lookup
        `def` - the default value to use if the entry is not present
      Returns:
        the value or `def` if no entry present

    - #### getBuffer

          public Buffer getBuffer(String key,
                                  Buffer def)

      Like `getBuffer(String)` but specifying a default value to return if there is no entry.
      Parameters:
        `key` - the key to lookup
        `def` - the default value to use if the entry is not present
      Returns:
        the value or `def` if no entry present

    - #### getInstant

          public 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.
      Parameters:
        `key` - the key to lookup
        `def` - the default value to use if the entry is not present
      Returns:
        the value or `def` if no entry present

    - #### getValue

          public Object getValue(String key,
                                 Object def)

      Like `getValue(String)` but specifying a default value to return if there is no entry.
      Parameters:
        `key` - the key to lookup
        `def` - the default value to use if the entry is not present
      Returns:
        the value or `def` if no entry present

    - #### containsKey

          public boolean containsKey(String key)

      Does the JSON object contain the specified key?
      Parameters:
        `key` - the key
      Returns:
        true if it contains the key, false if not.

    - #### fieldNames

          public Set<String> fieldNames()

      Return the set of field names in the JSON objects
      Returns:
        the set of field names

    - #### putNull

          public JsonObject putNull(String key)

      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

          public JsonObject put(String key,
                                Object value)

      Put an Object into the JSON object with the specified key.
      Parameters:
        `key` - the key
        `value` - the value
      Returns:
        a reference to this, so the API can be used fluently

    - #### remove

          public Object remove(String key)

      Remove an entry from this object.
      Parameters:
        `key` - the key
      Returns:
        the value that was removed, or null if none

    - #### mergeIn

          public JsonObject mergeIn(JsonObject other)

      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

          public JsonObject mergeIn(JsonObject other,
                                    boolean deep)

      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 object
        `deep` - if true, a deep merge is performed
      Returns:
        a reference to this, so the API can be used fluently

    - #### mergeIn

          public JsonObject mergeIn(JsonObject other,
                                    int depth)

      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 object
        `depth` - depth of merge
      Returns:
        a reference to this, so the API can be used fluently

    - #### encode

          public String encode()

      Encode this JSON object as a string.
      Returns:
        the string encoding.

    - #### encodePrettily

          public 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.
      Returns:
        the pretty string encoding.

    - #### toBuffer

          public Buffer toBuffer()

      Encode this JSON object as buffer.
      Returns:
        the buffer encoding.

    - #### copy

          public JsonObject copy()

      Deep copy of this JSON object.
      Specified by:
        `copy` in interface `Shareable`
      Returns:
        a copy where all elements have been copied recursively
      Throws:
        `IllegalStateException` - when a nested element cannot be copied

    - #### copy

          public JsonObject copy(java.util.function.Function<Object,?> cloner)

      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 the `cloner` function 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

          public Map<String,Object> getMap()

      Get the underlying `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`.
      Returns:
        the underlying Map.

    - #### stream

          public java.util.stream.Stream<Map.Entry<String,Object>> stream()

      Get a Stream over the entries in the JSON object. The values in the stream will follow the same rules as defined in `getValue(String)`, respecting the JSON requirements. To stream the raw values, use the storage object stream instead:

             jsonObject
               .getMap()
               .stream()

      Returns:
        a Stream

    - #### iterator

          public Iterator<Map.Entry<String,Object>> iterator()

      Get an Iterator of the entries in the JSON object.
      Specified by:
        `iterator` in interface `Iterable<Map.Entry<String,Object>>`
      Returns:
        an Iterator of the entries

    - #### size

          public int size()

      Get the number of entries in the JSON object
      Returns:
        the number of entries

    - #### clear

          public JsonObject 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

          public String toString()

      Overrides:
        `toString` in class `Object`

    - #### equals

          public boolean equals(Object o)

      Overrides:
        `equals` in class `Object`

    - #### hashCode

          public int hashCode()

      Overrides:
        `hashCode` in class `Object`

    - #### writeToBuffer

          public void writeToBuffer(Buffer buffer)

      Description copied from interface: `ClusterSerializable`
      Method invoked when serializing this instance.
      Specified by:
        `writeToBuffer` in interface `io.vertx.core.shareddata.impl.ClusterSerializable`
      Parameters:
        `buffer` - the `Buffer` where the serialized bytes must be written to

    - #### readFromBuffer

          public int readFromBuffer(int pos,
                                    Buffer buffer)

      Description copied from interface: `ClusterSerializable`
      Method invoked when deserializing bytes to this instance.
      Specified by:
        `readFromBuffer` in interface `io.vertx.core.shareddata.impl.ClusterSerializable`
      Parameters:
        `pos` - the position where to start reading the `buffer`
        `buffer` - the `Buffer` where the serialized bytes must be read from
      Returns:
        the position after the last serialized byte

Copyright © 2022 Eclipse. All rights reserved.