Class JsonObject

java.lang.Object
com.oorian.json.JsonValue
com.oorian.json.JsonObject

public class JsonObject extends JsonValue
Represents a JSON object containing key-value pairs.

JsonObject is the primary class for working with JSON data in Oorian. It provides methods for storing, retrieving, and manipulating JSON object properties with type-safe accessors for common data types.

  • Constructor Details

    • JsonObject

      public JsonObject()
      Creates an empty JsonObject.
    • JsonObject

      public JsonObject(JsonObject copy)
      Creates a JsonObject as a shallow copy of another JsonObject.
      Parameters:
      copy - the JsonObject to copy.
    • JsonObject

      public JsonObject(String name, Object value)
      Creates a JsonObject with a single key-value pair.
      Parameters:
      name - the key name.
      value - the value.
  • Method Details

    • parse

      public static JsonObject parse(String json)
      Parses a JSON string into a JsonObject.
      Parameters:
      json - the JSON string to parse.
      Returns:
      the parsed JsonObject.
    • initialize

      public void initialize(String json)
      Replaces this object's contents by parsing a JSON string.
      Parameters:
      json - the JSON string to parse.
    • containsKey

      public boolean containsKey(String key)
      Returns true if this object contains the specified key.
      Parameters:
      key - the key to check.
      Returns:
      true if the key exists.
    • getKeys

      public List<String> getKeys()
      Returns a list of all keys in this object.
      Returns:
      the list of keys.
    • isEmpty

      public boolean isEmpty()
      Returns true if this object contains no key-value pairs.
      Returns:
      true if empty.
    • put

      public final JsonObject put(String key, Object value)
      Adds or replaces a key-value pair in this object.
      Parameters:
      key - the key name.
      value - the value (automatically wrapped to the appropriate JSON type).
      Returns:
      this JsonObject for method chaining.
    • remove

      public final JsonObject remove(String key)
      Removes a key-value pair from this object.
      Parameters:
      key - the key to remove.
      Returns:
      this JsonObject for method chaining.
    • get

      public JsonValue get(String key)
      Returns the raw JsonValue for the specified key.
      Parameters:
      key - the key to look up.
      Returns:
      the JsonValue, or null if the key does not exist.
    • getAsString

      public String getAsString(String key)
      Returns the value for the specified key as a String.
      Parameters:
      key - the key to look up.
      Returns:
      the String value, or null if the key is missing or null.
    • getAsString

      public String getAsString(String key, String defaultValue)
      Returns the value for the specified key as a String, with a default.
      Parameters:
      key - the key to look up.
      defaultValue - the value to return if the key is missing or null.
      Returns:
      the String value, or the default value.
    • getAsBoolean

      public Boolean getAsBoolean(String key)
      Returns the value for the specified key as a Boolean.
      Parameters:
      key - the key to look up.
      Returns:
      the Boolean value, or null if the key is missing or null.
    • getAsBoolean

      public Boolean getAsBoolean(String key, boolean defaultValue)
      Returns the value for the specified key as a boolean, with a default.
      Parameters:
      key - the key to look up.
      defaultValue - the value to return if the key is missing or null.
      Returns:
      the boolean value, or the default value.
    • getAsBoolean

      public Boolean getAsBoolean(String key, Boolean defaultValue)
      Returns the value for the specified key as a Boolean, with a default.
      Parameters:
      key - the key to look up.
      defaultValue - the value to return if the key is missing or null.
      Returns:
      the Boolean value, or the default value.
    • getAsShort

      public Short getAsShort(String key)
      Returns the value for the specified key as a Short.
      Parameters:
      key - the key to look up.
      Returns:
      the Short value, or null if the key is missing or null.
    • getAsShort

      public Short getAsShort(String key, short defaultValue)
      Returns the value for the specified key as a short, with a default.
      Parameters:
      key - the key to look up.
      defaultValue - the value to return if the key is missing or null.
      Returns:
      the short value, or the default value.
    • getAsInt

      public Integer getAsInt(String key)
      Returns the value for the specified key as an Integer.
      Parameters:
      key - the key to look up.
      Returns:
      the Integer value, or null if the key is missing or null.
    • getAsInt

      public Integer getAsInt(String key, int defaultValue)
      Returns the value for the specified key as an int, with a default.
      Parameters:
      key - the key to look up.
      defaultValue - the value to return if the key is missing or null.
      Returns:
      the int value, or the default value.
    • getAsInt

      public Integer getAsInt(String key, Integer defaultValue)
      Returns the value for the specified key as an Integer, with a default.
      Parameters:
      key - the key to look up.
      defaultValue - the value to return if the key is missing or null.
      Returns:
      the Integer value, or the default value.
    • getAsLong

      public Long getAsLong(String key)
      Returns the value for the specified key as a Long.
      Parameters:
      key - the key to look up.
      Returns:
      the Long value, or null if the key is missing or null.
    • getAsLong

      public Long getAsLong(String key, long defaultValue)
      Returns the value for the specified key as a long, with a default.
      Parameters:
      key - the key to look up.
      defaultValue - the value to return if the key is missing or null.
      Returns:
      the long value, or the default value.
    • getAsFloat

      public Float getAsFloat(String key)
      Returns the value for the specified key as a Float.
      Parameters:
      key - the key to look up.
      Returns:
      the Float value, or null if the key is missing or null.
    • getAsFloat

      public Float getAsFloat(String key, float defaultValue)
      Returns the value for the specified key as a float, with a default.
      Parameters:
      key - the key to look up.
      defaultValue - the value to return if the key is missing or null.
      Returns:
      the float value, or the default value.
    • getAsDouble

      public Double getAsDouble(String key)
      Returns the value for the specified key as a Double.
      Parameters:
      key - the key to look up.
      Returns:
      the Double value, or null if the key is missing or null.
    • getAsDouble

      public Double getAsDouble(String key, double defaultValue)
      Returns the value for the specified key as a double, with a default.
      Parameters:
      key - the key to look up.
      defaultValue - the value to return if the key is missing or null.
      Returns:
      the double value, or the default value.
    • getAsNumber

      public Number getAsNumber(String key)
      Returns the value for the specified key as a Number.
      Parameters:
      key - the key to look up.
      Returns:
      the Number value, or null if the key is missing or null.
    • getAsNumber

      public Number getAsNumber(String key, Number defaultValue)
      Returns the value for the specified key as a Number, with a default.
      Parameters:
      key - the key to look up.
      defaultValue - the value to return if the key is missing or null.
      Returns:
      the Number value, or the default value.
    • getAsJsonObject

      public JsonObject getAsJsonObject(String key)
      Returns the value for the specified key as a JsonObject.
      Parameters:
      key - the key to look up.
      Returns:
      the nested JsonObject.
    • getAsJsonArray

      public JsonArray getAsJsonArray(String key)
      Returns the value for the specified key as a JsonArray.
      Parameters:
      key - the key to look up.
      Returns:
      the JsonArray.
    • getAsArray

      public List getAsArray(String key)
      Returns the value for the specified key as a List.
      Parameters:
      key - the key to look up.
      Returns:
      the list of values.
    • getAsStringArray

      public List<String> getAsStringArray(String key)
      Returns the value for the specified key as a list of Strings.
      Parameters:
      key - the key to look up.
      Returns:
      the list of String values.
    • getAsBooleanArray

      public List<Boolean> getAsBooleanArray(String key)
      Returns the value for the specified key as a list of Booleans.
      Parameters:
      key - the key to look up.
      Returns:
      the list of Boolean values.
    • getAsShortArray

      public List<Short> getAsShortArray(String key)
      Returns the value for the specified key as a list of Shorts.
      Parameters:
      key - the key to look up.
      Returns:
      the list of Short values.
    • getAsIntArray

      public List<Integer> getAsIntArray(String key)
      Returns the value for the specified key as a list of Integers.
      Parameters:
      key - the key to look up.
      Returns:
      the list of Integer values.
    • getAsLongArray

      public List<Long> getAsLongArray(String key)
      Returns the value for the specified key as a list of Longs.
      Parameters:
      key - the key to look up.
      Returns:
      the list of Long values.
    • getAsFloatArray

      public List<Float> getAsFloatArray(String key)
      Returns the value for the specified key as a list of Floats.
      Parameters:
      key - the key to look up.
      Returns:
      the list of Float values.
    • getAsDoubleArray

      public List<Double> getAsDoubleArray(String key)
      Returns the value for the specified key as a list of Doubles.
      Parameters:
      key - the key to look up.
      Returns:
      the list of Double values.
    • toJsonString

      public String toJsonString()
      Description copied from class: JsonValue
      Returns this value serialized as a JSON string.
      Specified by:
      toJsonString in class JsonValue
      Returns:
      the JSON string representation.