Class JsonObject

All Implemented Interfaces:
Serializable, Cloneable, Map<String,JsonElement>

@NullMarked public class JsonObject extends LinkedHashMap<String,JsonElement>
JsonObject is a Map of JsonElement. It includes methods for accessing members as various Java types.
See Also:
  • Constructor Details

    • JsonObject

      public JsonObject()
      Constructs an empty JsonObject with the default initial capacity (16) and the default load factor (0.75).
    • JsonObject

      public JsonObject(int initialCapacity)
      Constructs an empty JsonObject with the specified initial capacity and the default load factor (0.75).
      Parameters:
      initialCapacity - the initial capacity
      Throws:
      IllegalArgumentException - if the initial capacity is negative.
    • JsonObject

      public JsonObject(int initialCapacity, float loadFactor)
      Constructs an empty JsonObject with the specified initial capacity and the specified load factor.
      Parameters:
      initialCapacity - the initial capacity
      loadFactor - the load factor
      Throws:
      IllegalArgumentException - if the initial capacity is negative or the load factor is non-positive
  • Method Details

    • getObject

      public JsonObject getObject(String key)
      Returns the JsonObject for the specified key.
      Parameters:
      key - the key of the JsonObject to be returned
      Returns:
      the JsonObject to which the specified key is mapped
      Throws:
      JsonElementTypeException - if JsonElement at index is not of type JsonElementType.JSON_OBJECT
    • getObject

      public JsonObject getObject(String key, JsonObject defaultValue)
      Returns the JsonObject for the specified key. Default value is returned if key is missing.
      Parameters:
      key - the key of the JsonObject to be returned
      defaultValue - the default value to return if key is missing
      Returns:
      the JsonObject to which the specified key is mapped
      Throws:
      JsonElementTypeException - if JsonElement at index is not of type JsonElementType.JSON_OBJECT
    • getArray

      public JsonArray getArray(String key)
      Returns the JsonArray for the specified key.
      Parameters:
      key - the key of the JsonArray to be returned
      Returns:
      the JsonArray to which the specified key is mapped
      Throws:
      JsonElementTypeException - if JsonElement at index is not of type JsonElementType.JSON_ARRAY
    • getArray

      public JsonArray getArray(String key, JsonArray defaultValue)
      Returns the JsonArray for the specified key. Default value is returned if key is missing.
      Parameters:
      key - the key of the JsonArray to be returned
      defaultValue - the default value to return if key is missing
      Returns:
      the JsonArray to which the specified key is mapped
      Throws:
      JsonElementTypeException - if JsonElement at index is not of type JsonElementType.JSON_ARRAY
    • getString

      public String getString(String key)
      Returns the string for the specified key.
      Parameters:
      key - the key of the string to be returned
      Returns:
      the string to which the specified key is mapped
      Throws:
      JsonElementTypeException - if JsonElement at index is not of type JsonElementType.STRING
    • getString

      public String getString(String key, String defaultValue)
      Returns the string for the specified key. Default value is returned if key is missing.
      Parameters:
      key - the key of the string to be returned
      defaultValue - the default value to return if key is missing
      Returns:
      the string to which the specified key is mapped
      Throws:
      JsonElementTypeException - if JsonElement at index is not of type JsonElementType.STRING
    • getDouble

      public double getDouble(String key)
      Returns the double for the specified key.
      Parameters:
      key - the key of the double to be returned
      Returns:
      the double to which the specified key is mapped
      Throws:
      JsonElementTypeException - if JsonElement at index is not of type JsonElementType.DOUBLE
    • getDouble

      public Double getDouble(String key, Double defaultValue)
      Returns the double for the specified key. Default value is returned if key is missing.
      Parameters:
      key - the key of the double to be returned
      defaultValue - the default value to return if key is missing
      Returns:
      the double to which the specified key is mapped
      Throws:
      JsonElementTypeException - if JsonElement at index is not of type JsonElementType.DOUBLE
    • getFloat

      public float getFloat(String key)
      Returns the float for the specified key.
      Parameters:
      key - the key of the float to be returned
      Returns:
      the float to which the specified key is mapped
      Throws:
      JsonElementTypeException - if JsonElement at index is not of type JsonElementType.DOUBLE
    • getFloat

      public Float getFloat(String key, Float defaultValue)
      Returns the float for the specified key. Default value is returned if key is missing.
      Parameters:
      key - the key of the float to be returned
      defaultValue - the default value to return if key is missing
      Returns:
      the float to which the specified key is mapped
      Throws:
      JsonElementTypeException - if JsonElement at index is not of type JsonElementType.DOUBLE
    • getInt

      public int getInt(String key)
      Returns the integer for the specified key.
      Parameters:
      key - the key of the integer to be returned
      Returns:
      the integer to which the specified key is mapped
      Throws:
      JsonElementTypeException - if JsonElement at index is not of type JsonElementType.LONG
    • getInt

      public Integer getInt(String key, Integer defaultValue)
      Returns the integer for the specified key. Default value is returned if key is missing.
      Parameters:
      key - the key of the integer to be returned
      defaultValue - the default value to return if key is missing
      Returns:
      the integer to which the specified key is mapped
      Throws:
      JsonElementTypeException - if JsonElement at index is not of type JsonElementType.LONG
    • getLong

      public long getLong(String key)
      Returns the long for the specified key.
      Parameters:
      key - the key of the long to be returned
      Returns:
      the long to which the specified key is mapped
      Throws:
      JsonElementTypeException - if JsonElement at index is not of type JsonElementType.LONG
    • getLong

      public Long getLong(String key, Long defaultValue)
      Returns the long for the specified key. Default value is returned if key is missing.
      Parameters:
      key - the key of the long to be returned
      defaultValue - the default value to return if key is missing
      Returns:
      the long to which the specified key is mapped
      Throws:
      JsonElementTypeException - if JsonElement at index is not of type JsonElementType.LONG
    • getBoolean

      public boolean getBoolean(String key)
      Returns the boolean for the specified key.
      Parameters:
      key - the key of the boolean to be returned
      Returns:
      the boolean to which the specified key is mapped
      Throws:
      JsonElementTypeException - if JsonElement at index is not of type JsonElementType.BOOLEAN
    • getBoolean

      public Boolean getBoolean(String key, Boolean defaultValue)
      Returns the boolean for the specified key. Default value is returned if key is missing.
      Parameters:
      key - the key of the boolean to be returned
      defaultValue - the default value to return if key is missing
      Returns:
      the boolean to which the specified key is mapped
      Throws:
      JsonElementTypeException - if JsonElement at index is not of type JsonElementType.BOOLEAN
    • isNull

      public boolean isNull(String key)
      Returns true if JsonElementType.NULL is at specified key.
      Parameters:
      key - the key to be checked
      Returns:
      true if JsonElementType.NULL is at specified key
    • put

      public @Nullable JsonElement put(String key, @Nullable JsonElement value)
      Specified by:
      put in interface Map<String,JsonElement>
      Overrides:
      put in class HashMap<String,JsonElement>
    • put

      public JsonObject put(String key, @Nullable JsonObject value)
      Associates a JsonObject with specified key.
      Parameters:
      key - the key to associate the JsonObject with
      value - the JsonObject to associate
      Returns:
      this
    • put

      public JsonObject put(String key, @Nullable JsonArray value)
      Associates a JsonArray with specified key.
      Parameters:
      key - the key to associate the JsonArray with
      value - the JsonArray to associate
      Returns:
      this
    • put

      public JsonObject put(String key, @Nullable String value)
      Associates a string with specified key.
      Parameters:
      key - the key to associate the string with
      value - the string to associate
      Returns:
      this
    • put

      public JsonObject put(String key, @Nullable Integer value)
      Associates a integer with specified key.
      Parameters:
      key - the key to associate the integer with
      value - the integer to associate
      Returns:
      this
    • put

      public JsonObject put(String key, @Nullable Long value)
      Associates a long with specified key.
      Parameters:
      key - the key to associate the long with
      value - the long to associate
      Returns:
      this
    • put

      public JsonObject put(String key, @Nullable Double value)
      Associates a double with specified key.
      Parameters:
      key - the key to associate the double with
      value - the double to associate
      Returns:
      this
    • put

      public JsonObject put(String key, @Nullable Float value)
      Associates a float with specified key.
      Parameters:
      key - the key to associate the float with
      value - the float to associate
      Returns:
      this
    • put

      public JsonObject put(String key, @Nullable Boolean value)
      Associates a boolean with specified key.
      Parameters:
      key - the key to associate the boolean with
      value - the boolean to associate
      Returns:
      this
    • putNull

      public JsonObject putNull(String key)
      Parameters:
      key - the key to put a NULL JSON value into
      Returns:
      this
    • merge

      public JsonObject merge(JsonObject other)
      Merges this JsonObject with another resulting in a new JsonObject.
      1. If key in both and each value is an object then it will recursively merge and add to result
      2. If key in both and each value is not an object, value from the right is added to the result
      3. If key in this only, then this value is added to result
      4. If key in other only, then other value is added to result
      This does not create a deep copy of nested arrays and objects.
      Parameters:
      other - the object to merge with
      Returns:
      a new JsonObject with merged properties
    • toString

      public String toString()
      Formats this JsonObject as a string with no indentation.
      Overrides:
      toString in class AbstractMap<String,JsonElement>
      Returns:
      formatted JSON string
    • toString

      public String toString(int spaces)
      Formats this JsonObject as a string with specified spaces for indentation.
      Parameters:
      spaces - the spaces for each indentation
      Returns:
      formatted JSON string with spaces for indentation