io.vertx.core.json

## Class JsonObject

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

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

      public class JsonObject
      extends Object
      implements Iterable<Map.Entry<String,Object>>, io.vertx.core.shareddata.impl.ClusterSerializable

  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(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 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()` 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.
    `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.
    `JsonObject` | `mergeIn(JsonObject other)` 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.
    `String` | `toString()`
    `void` | `writeToBuffer(Buffer buffer)`

    - ### 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.

  - ### Method Detail

    - #### getString

          public String getString(String key)

      Get the string 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 String

    - #### 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.

      This method should be used in conjunction with `put(String, byte[])`

      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

    - #### getInstant

          public java.time.Instant getInstant(String key)

      Get the instant value with the specified key.

      JSON itself has no notion of a date, this extension complies to the RFC-7493, so this method assumes there is a String value with the key and it contains a ISODATE encoded date, which it decodes if found and returns.

      This method should be used in conjunction with `put(String, java.time.Instant)`

      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

    - #### getValue

          public Object getValue(String key)

      Get the value with the specified key, as an Object
      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

    - #### 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

    - #### 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

    - #### put

          public JsonObject put(String key,
                                Enum value)

      Put an Enum into the JSON object with the specified key.

      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.

      Parameters:
        `key` - the key
        `value` - the value
      Returns:
        a reference to this, so the API can be used fluently

    - #### put

          public JsonObject put(String key,
                                CharSequence value)

      Put an CharSequence 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

    - #### put

          public JsonObject put(String key,
                                String value)

      Put a String 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

    - #### put

          public JsonObject put(String key,
                                Integer value)

      Put an Integer 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

    - #### put

          public JsonObject put(String key,
                                Long value)

      Put a Long 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

    - #### put

          public JsonObject put(String key,
                                Double value)

      Put a Double 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

    - #### put

          public JsonObject put(String key,
                                Float value)

      Put a Float 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

    - #### put

          public JsonObject put(String key,
                                Boolean value)

      Put a Boolean 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

    - #### 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,
                                JsonObject value)

      Put another JSON 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

    - #### put

          public JsonObject put(String key,
                                JsonArray value)

      Put a JSON array 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

    - #### put

          public JsonObject put(String key,
                                byte[] value)

      Put a byte[] into the JSON object with the specified key.

      JSON extension RFC7493, binary will first be Base64 encoded before being put as a String.

      Parameters:
        `key` - the key
        `value` - the value
      Returns:
        a reference to this, so the API can be used fluently

    - #### put

          public JsonObject put(String key,
                                java.time.Instant value)

      Put a Instant into the JSON object with the specified key.

      JSON extension RFC7493, instant will first be encoded to ISODATE String.

      Parameters:
        `key` - the key
        `value` - the value
      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.

      Parameters:
        `other` - the other JSON object
      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.

    - #### copy

          public JsonObject copy()

      Copy the JSON object
      Returns:
        a copy of the object

    - #### getMap

          public Map<String,Object> getMap()

      Get the underlying Map.
      Returns:
        the underlying Map.

    - #### stream

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

      Get a stream of the entries in the JSON object.
      Returns:
        a stream of the entries.

    - #### 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)

      Specified by:
        `writeToBuffer` in interface `io.vertx.core.shareddata.impl.ClusterSerializable`

    - #### readFromBuffer

          public int readFromBuffer(int pos,
                                    Buffer buffer)

      Specified by:
        `readFromBuffer` in interface `io.vertx.core.shareddata.impl.ClusterSerializable`

Copyright © 2016. All rights reserved.