Class JsonObject

java.lang.Object
io.helidon.json.JsonValue
io.helidon.json.JsonObject

public final class JsonObject extends JsonValue
Represents a JSON object value containing key-value pairs. Keys and values must not be null. When duplicate member names are parsed, the last value is retained. Discarded values are validated recursively so duplicate names cannot hide malformed string data.
  • Method Details

    • builder

      public static JsonObject.Builder builder()
      Create a new JsonObject.Builder for fluent construction of JsonObject instances.
      Returns:
      a new JsonObject.Builder
    • create

      public static JsonObject create(Map<String, JsonValue> content)
      Create a JsonObject from a map of string keys to JsonValue instances.
      Parameters:
      content - the map containing the object properties
      Returns:
      a new JsonObject
    • empty

      public static JsonObject empty()
      Returns the shared empty JSON object instance.
      Returns:
      the empty JSON object
    • containsKey

      public boolean containsKey(String key)
      Checks if this object contains the specified key.
      Parameters:
      key - the key to check for
      Returns:
      true if the object contains the key, false otherwise
    • value

      public JsonValue value(String key, JsonValue defaultValue)
      Return the value associated with the specified key, or the default value if the key is not present.
      Parameters:
      key - the key to look up
      defaultValue - the value to return if the key is not present
      Returns:
      the value associated with the key, or the default value
    • value

      public Optional<JsonValue> value(String key)
      Return the value associated with the specified key, or the empty value if the key is not present.
      Parameters:
      key - the key to look up
      Returns:
      the value associated with the key, or empty
    • booleanValue

      public Optional<Boolean> booleanValue(String key)
      Return the boolean value associated with the specified key as an Optional.
      Parameters:
      key - the key to look up
      Returns:
      an Optional containing the boolean value, or empty if the key is not present
    • booleanValue

      public boolean booleanValue(String key, boolean defaultValue)
      Return the boolean value associated with the specified key, or the default value if the key is not present.
      Parameters:
      key - the key to look up
      defaultValue - the value to return if the key is not present
      Returns:
      the boolean value associated with the key, or the default value
    • objectValue

      public Optional<JsonObject> objectValue(String key)
      Return the JsonObject value associated with the specified key as an Optional.
      Parameters:
      key - the key to look up
      Returns:
      an Optional containing the JsonObject value, or empty if the key is not present
    • objectValue

      public JsonObject objectValue(String key, JsonObject defaultValue)
      Return the JsonObject value associated with the specified key, or the default value if the key is not present.
      Parameters:
      key - the key to look up
      defaultValue - the value to return if the key is not present
      Returns:
      the JsonObject value associated with the key, or the default value
    • stringValue

      public Optional<String> stringValue(String key)
      Return the string value associated with the specified key as an Optional.
      Parameters:
      key - the key to look up
      Returns:
      an Optional containing the string value, or empty if the key is not present
    • stringValue

      public String stringValue(String key, String defaultValue)
      Return the string value associated with the specified key, or the default value if the key is not present.
      Parameters:
      key - the key to look up
      defaultValue - the value to return if the key is not present
      Returns:
      the string value associated with the key, or the default value
    • byteValue

      public Optional<Byte> byteValue(String key)
      Return the byte value associated with the specified key as an Optional. Conversion follows JsonNumber.byteValue().
      Parameters:
      key - the key to look up
      Returns:
      an Optional containing the byte value, or empty if the key is not present
    • byteValue

      public byte byteValue(String key, byte defaultValue)
      Return the byte value associated with the specified key, or the default value if the key is not present. Conversion follows JsonNumber.byteValue().
      Parameters:
      key - the key to look up
      defaultValue - the value to return if the key is not present
      Returns:
      the byte value associated with the key, or the default value
    • shortValue

      public Optional<Short> shortValue(String key)
      Return the short value associated with the specified key as an Optional. Conversion follows JsonNumber.shortValue().
      Parameters:
      key - the key to look up
      Returns:
      an Optional containing the short value, or empty if the key is not present
    • shortValue

      public short shortValue(String key, short defaultValue)
      Return the short value associated with the specified key, or the default value if the key is not present. Conversion follows JsonNumber.shortValue().
      Parameters:
      key - the key to look up
      defaultValue - the value to return if the key is not present
      Returns:
      the short value associated with the key, or the default value
    • intValue

      public Optional<Integer> intValue(String key)
      Return the integer value associated with the specified key as an Optional. Conversion follows JsonNumber.intValue().
      Parameters:
      key - the key to look up
      Returns:
      an Optional containing the integer value, or empty if the key is not present
    • intValue

      public int intValue(String key, int defaultValue)
      Return the integer value associated with the specified key, or the default value if the key is not present. Conversion follows JsonNumber.intValue().
      Parameters:
      key - the key to look up
      defaultValue - the value to return if the key is not present
      Returns:
      the integer value associated with the key, or the default value
    • longValue

      public Optional<Long> longValue(String key)
      Return the long value associated with the specified key as an Optional. Conversion follows JsonNumber.longValue().
      Parameters:
      key - the key to look up
      Returns:
      an Optional containing the long value, or empty if the key is not present
    • longValue

      public long longValue(String key, long defaultValue)
      Return the long value associated with the specified key, or the default value if the key is not present. Conversion follows JsonNumber.longValue().
      Parameters:
      key - the key to look up
      defaultValue - the value to return if the key is not present
      Returns:
      the long value associated with the key, or the default value
    • floatValue

      public Optional<Float> floatValue(String key)
      Return the float value associated with the specified key as an Optional. Conversion follows JsonNumber.floatValue().
      Parameters:
      key - the key to look up
      Returns:
      an Optional containing the float value, or empty if the key is not present
    • floatValue

      public float floatValue(String key, float defaultValue)
      Return the float value associated with the specified key, or the default value if the key is not present. Conversion follows JsonNumber.floatValue().
      Parameters:
      key - the key to look up
      defaultValue - the value to return if the key is not present
      Returns:
      the float value associated with the key, or the default value
    • doubleValue

      public Optional<Double> doubleValue(String key)
      Return the double value associated with the specified key as an Optional. Conversion follows JsonNumber.doubleValue().
      Parameters:
      key - the key to look up
      Returns:
      an Optional containing the double value, or empty if the key is not present
    • doubleValue

      public double doubleValue(String key, double defaultValue)
      Return the double value associated with the specified key, or the default value if the key is not present. Conversion follows JsonNumber.doubleValue().
      Parameters:
      key - the key to look up
      defaultValue - the value to return if the key is not present
      Returns:
      the double value associated with the key, or the default value
    • bigIntegerValue

      public Optional<BigInteger> bigIntegerValue(String key)
      Return the BigInteger value associated with the specified key as an Optional. Conversion follows JsonNumber.bigIntegerValue().
      Parameters:
      key - the key to look up
      Returns:
      an Optional containing the BigInteger value, or empty if the key is not present
      Throws:
      JsonException - if the conversion exceeds the limits documented by JsonNumber.bigIntegerValue()
    • bigIntegerValue

      public BigInteger bigIntegerValue(String key, BigInteger defaultValue)
      Return the BigInteger value associated with the specified key, or the default value if the key is not present. Conversion follows JsonNumber.bigIntegerValue().
      Parameters:
      key - the key to look up
      defaultValue - the value to return if the key is not present
      Returns:
      the BigInteger value associated with the key, or the default value
      Throws:
      JsonException - if the conversion exceeds the limits documented by JsonNumber.bigIntegerValue()
    • numberValue

      public Optional<BigDecimal> numberValue(String key)
      Return the BigDecimal value associated with the specified key as an Optional.
      Parameters:
      key - the key to look up
      Returns:
      an Optional containing the BigDecimal value, or empty if the key is not present
    • numberValue

      public BigDecimal numberValue(String key, BigDecimal defaultValue)
      Return the BigDecimal value associated with the specified key, or the default value if the key is not present.
      Parameters:
      key - the key to look up
      defaultValue - the value to return if the key is not present
      Returns:
      the BigDecimal value associated with the key, or the default value
    • arrayValue

      public Optional<JsonArray> arrayValue(String key)
      Return the JsonArray value associated with the specified key as an Optional.
      Parameters:
      key - the key to look up
      Returns:
      an Optional containing the JsonArray value, or empty if the key is not present
    • arrayValue

      public JsonArray arrayValue(String key, JsonArray defaultValue)
      Return the JsonArray value associated with the specified key, or the default value if the key is not present.
      Parameters:
      key - the key to look up
      defaultValue - the value to return if the key is not present
      Returns:
      the JsonArray value associated with the key, or the default value
    • keys

      public Set<JsonString> keys()
      Return a set of all keys in this object as JsonString instances.
      Returns:
      a set of JsonString keys
    • keysAsStrings

      public Set<String> keysAsStrings()
      Return an unmodifiable set of all keys in this object as String instances.
      Returns:
      an unmodifiable set of String keys
    • size

      public int size()
      Return the number of properties in this object.
      Returns:
      the size of this object
    • type

      public JsonValueType type()
      Description copied from class: JsonValue
      Return the type of this JSON value.
      Specified by:
      type in class JsonValue
      Returns:
      the JsonValueType of this value
    • toJson

      public void toJson(JsonGenerator generator)
      Description copied from class: JsonValue
      Write this JSON value to the provided generator.
      Specified by:
      toJson in class JsonValue
      Parameters:
      generator - the generator to write to
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object