Class JsonObject

java.lang.Object
io.vertx.core.json.JsonObject
All Implemented Interfaces:
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 Details

    • 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 Details

    • of

      public static JsonObject of()
      Create a JsonObject containing zero mappings.
      Returns:
      an empty JsonObject.
    • of

      public static JsonObject of(String k1, Object v1)
      Create a JsonObject containing a single mapping.
      Parameters:
      k1 - the mapping's key
      v1 - the mapping's value
      Returns:
      a JsonObject containing the specified mapping.
    • of

      public static JsonObject of(String k1, Object v1, String k2, Object v2)
      Create a JsonObject containing two mappings.
      Parameters:
      k1 - the first mapping's key
      v1 - the first mapping's value
      k2 - the second mapping's key
      v2 - the second 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)
      Create a JsonObject containing three mappings.
      Parameters:
      k1 - the first mapping's key
      v1 - the first mapping's value
      k2 - the second mapping's key
      v2 - the second mapping's value
      k3 - the third mapping's key
      v3 - 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 key
      v1 - the first mapping's value
      k2 - the second mapping's key
      v2 - the second mapping's value
      k3 - the third mapping's key
      v3 - the third mapping's value
      k4 - the fourth mapping's key
      v4 - 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 key
      v1 - the first mapping's value
      k2 - the second mapping's key
      v2 - the second mapping's value
      k3 - the third mapping's key
      v3 - the third mapping's value
      k4 - the fourth mapping's key
      v4 - the fourth mapping's value
      k5 - the fifth mapping's key
      v5 - 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 key
      v1 - the first mapping's value
      k2 - the second mapping's key
      v2 - the second mapping's value
      k3 - the third mapping's key
      v3 - the third mapping's value
      k4 - the fourth mapping's key
      v4 - the fourth mapping's value
      k5 - the fifth mapping's key
      v5 - the fifth mapping's value
      k6 - the sixth mapping's key
      v6 - 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 key
      v1 - the first mapping's value
      k2 - the second mapping's key
      v2 - the second mapping's value
      k3 - the third mapping's key
      v3 - the third mapping's value
      k4 - the fourth mapping's key
      v4 - the fourth mapping's value
      k5 - the fifth mapping's key
      v5 - the fifth mapping's value
      k6 - the sixth mapping's key
      v6 - the sixth mapping's value
      k7 - the seventh mapping's key
      v7 - 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 key
      v1 - the first mapping's value
      k2 - the second mapping's key
      v2 - the second mapping's value
      k3 - the third mapping's key
      v3 - the third mapping's value
      k4 - the fourth mapping's key
      v4 - the fourth mapping's value
      k5 - the fifth mapping's key
      v5 - the fifth mapping's value
      k6 - the sixth mapping's key
      v6 - the sixth mapping's value
      k7 - the seventh mapping's key
      v7 - the seventh mapping's value
      k8 - the eighth mapping's key
      v8 - 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 key
      v1 - the first mapping's value
      k2 - the second mapping's key
      v2 - the second mapping's value
      k3 - the third mapping's key
      v3 - the third mapping's value
      k4 - the fourth mapping's key
      v4 - the fourth mapping's value
      k5 - the fifth mapping's key
      v5 - the fifth mapping's value
      k6 - the sixth mapping's key
      v6 - the sixth mapping's value
      k7 - the seventh mapping's key
      v7 - the seventh mapping's value
      k8 - the eighth mapping's key
      v8 - the eighth mapping's value
      k9 - the ninth mapping's key
      v9 - 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 key
      v1 - the first mapping's value
      k2 - the second mapping's key
      v2 - the second mapping's value
      k3 - the third mapping's key
      v3 - the third mapping's value
      k4 - the fourth mapping's key
      v4 - the fourth mapping's value
      k5 - the fifth mapping's key
      v5 - the fifth mapping's value
      k6 - the sixth mapping's key
      v6 - the sixth mapping's value
      k7 - the seventh mapping's key
      v7 - the seventh mapping's value
      k8 - the eighth mapping's key
      v8 - the eighth mapping's value
      k9 - the ninth mapping's key
      v9 - the ninth mapping's value
      k10 - the tenth mapping's key
      v10 - the tenth mapping's value
      Returns:
      a JsonObject containing the specified mappings.
    • 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))`. invalid input: '<'p/ Returns 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 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
      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 Instant getInstant(String key, 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(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 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 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 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