Interface JsonObject

All Superinterfaces:
JsonValue

public non-sealed interface JsonObject extends JsonValue
The interface that represents JSON object.

A JsonObject can be created by parsing JSON text using either Json.parse(String) or Json.parse(char[]).

Alternatively, of(Map) can be used to obtain a JsonObject.

Implementations of JsonObject cannot be created from sources that contain duplicate member names. If duplicate names appear while parsing with Json.parse(String), a JsonParseException is thrown. If duplicate member names are detected while creating a JsonObject with of(Map), an IllegalArgumentException is thrown.

Since:
28
External Specifications
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns an unmodifiable map of String to JsonValue if this JsonValue is an instance of JsonObject; otherwise, throws a JsonValueException.
    default JsonValue
    get(String name)
    Returns the JsonValue associated with the given member name if this JsonValue is an instance of JsonObject; otherwise, throws a JsonValueException.
    static JsonObject
    of(Map<String, ? extends JsonValue> map)
    Returns the JsonObject created from the given map of String to JsonValues.
    tryGet(String name)
    Returns an Optional containing the value of a given member of this JsonObject, or an empty Optional if the member is absent; throws JsonValueException if this JsonValue is not a JsonObject.

    Methods declared in interface JsonValue

    asBoolean, asDouble, asInt, asList, asLong, asString, get, toString, tryValue
    Modifier and Type
    Method
    Description
    default boolean
    Returns the boolean value represented by this JsonValue if it is an instance of JsonBoolean; otherwise, throws a JsonValueException.
    default double
    Returns a double if this JsonValue is an instance of JsonNumber that can be converted, as if by Double.parseDouble, to a finite double value; otherwise, throws a JsonValueException.
    default int
    Returns an int if this JsonValue is an instance of JsonNumber that can be converted exactly; otherwise, throws a JsonValueException.
    default List<JsonValue>
    Returns an unmodifiable list of the JsonValues if this JsonValue is an instance of JsonArray; otherwise, throws a JsonValueException.
    default long
    Returns a long if this JsonValue is an instance of JsonNumber that can be converted exactly; otherwise, throws a JsonValueException.
    default String
    Returns the String value represented by this JsonValue if it is an instance of JsonString; otherwise, throws a JsonValueException.
    default JsonValue
    get(int index)
    Returns the JsonValue associated with the given index if this JsonValue is an instance of JsonArray; otherwise, throws a JsonValueException.
    Returns a String representation of this JsonValue that conforms to JSON syntax.
    Returns an Optional containing this JsonValue if it is not an instance of JsonNull, otherwise an empty Optional.
  • Method Details

    • asMap

      Map<String, JsonValue> asMap()
      Returns an unmodifiable map of String to JsonValue if this JsonValue is an instance of JsonObject; otherwise, throws a JsonValueException.
      Specified by:
      asMap in interface JsonValue
      Implementation Note:
      The JDK platform implementation of JsonObject preserves the encounter order of members. When a JsonObject is created by parsing, this corresponds to the order of members in the source JSON text. When created via the of(Map) factory method, the order follows the encounter order of the provided map.
      Returns:
      an unmodifiable map of String to JsonValue if this JsonValue is an instance of JsonObject; otherwise, throws a JsonValueException
    • get

      default JsonValue get(String name)
      Returns the JsonValue associated with the given member name if this JsonValue is an instance of JsonObject; otherwise, throws a JsonValueException.
      Specified by:
      get in interface JsonValue
      Parameters:
      name - the member name
      Returns:
      the JsonValue associated with the given member name if this JsonValue is an instance of JsonObject; otherwise, throws a JsonValueException
      Throws:
      NullPointerException - if the member name is null
      JsonValueException - if there is no association with the member name
    • tryGet

      default Optional<JsonValue> tryGet(String name)
      Returns an Optional containing the value of a given member of this JsonObject, or an empty Optional if the member is absent; throws JsonValueException if this JsonValue is not a JsonObject.
      Specified by:
      tryGet in interface JsonValue
      Parameters:
      name - the member name
      Returns:
      an Optional containing the value of a given member of this JsonObject, or an empty Optional if the member is absent; throws JsonValueException if this JsonValue is not a JsonObject
      Throws:
      NullPointerException - if the member name is null
    • of

      static JsonObject of(Map<String, ? extends JsonValue> map)
      Returns the JsonObject created from the given map of String to JsonValues.
      Parameters:
      map - the map of JsonValues. Non-null.
      Returns:
      the JsonObject created from the given map of String to JsonValues
      Throws:
      IllegalArgumentException - if duplicate member names are given in map, including when they are encountered while iterating over the mappings of an IdentityHashMap.
      NullPointerException - if map is null, contains any keys that are null, or contains any values that are null.