Package com.oorian.json
Class JsonObject
java.lang.Object
com.oorian.json.JsonValue
com.oorian.json.JsonObject
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 Summary
ConstructorsConstructorDescriptionCreates an empty JsonObject.JsonObject(JsonObject copy) Creates a JsonObject as a shallow copy of another JsonObject.JsonObject(String name, Object value) Creates a JsonObject with a single key-value pair. -
Method Summary
Modifier and TypeMethodDescriptionbooleancontainsKey(String key) Returnstrueif this object contains the specified key.Returns the rawJsonValuefor the specified key.getAsArray(String key) Returns the value for the specified key as a List.getAsBoolean(String key) Returns the value for the specified key as a Boolean.getAsBoolean(String key, boolean defaultValue) Returns the value for the specified key as a boolean, with a default.getAsBoolean(String key, Boolean defaultValue) Returns the value for the specified key as a Boolean, with a default.getAsBooleanArray(String key) Returns the value for the specified key as a list of Booleans.getAsDouble(String key) Returns the value for the specified key as a Double.getAsDouble(String key, double defaultValue) Returns the value for the specified key as a double, with a default.getAsDoubleArray(String key) Returns the value for the specified key as a list of Doubles.getAsFloat(String key) Returns the value for the specified key as a Float.getAsFloat(String key, float defaultValue) Returns the value for the specified key as a float, with a default.getAsFloatArray(String key) Returns the value for the specified key as a list of Floats.Returns the value for the specified key as an Integer.Returns the value for the specified key as an int, with a default.Returns the value for the specified key as an Integer, with a default.getAsIntArray(String key) Returns the value for the specified key as a list of Integers.getAsJsonArray(String key) Returns the value for the specified key as aJsonArray.getAsJsonObject(String key) Returns the value for the specified key as aJsonObject.Returns the value for the specified key as a Long.Returns the value for the specified key as a long, with a default.getAsLongArray(String key) Returns the value for the specified key as a list of Longs.getAsNumber(String key) Returns the value for the specified key as a Number.getAsNumber(String key, Number defaultValue) Returns the value for the specified key as a Number, with a default.getAsShort(String key) Returns the value for the specified key as a Short.getAsShort(String key, short defaultValue) Returns the value for the specified key as a short, with a default.getAsShortArray(String key) Returns the value for the specified key as a list of Shorts.getAsString(String key) Returns the value for the specified key as a String.getAsString(String key, String defaultValue) Returns the value for the specified key as a String, with a default.getAsStringArray(String key) Returns the value for the specified key as a list of Strings.getKeys()Returns a list of all keys in this object.voidinitialize(String json) Replaces this object's contents by parsing a JSON string.booleanisEmpty()Returnstrueif this object contains no key-value pairs.static JsonObjectParses a JSON string into a JsonObject.final JsonObjectAdds or replaces a key-value pair in this object.final JsonObjectRemoves a key-value pair from this object.Returns this value serialized as a JSON string.
-
Constructor Details
-
JsonObject
public JsonObject()Creates an empty JsonObject. -
JsonObject
Creates a JsonObject as a shallow copy of another JsonObject.- Parameters:
copy- the JsonObject to copy.
-
JsonObject
Creates a JsonObject with a single key-value pair.- Parameters:
name- the key name.value- the value.
-
-
Method Details
-
parse
Parses a JSON string into a JsonObject.- Parameters:
json- the JSON string to parse.- Returns:
- the parsed JsonObject.
-
initialize
Replaces this object's contents by parsing a JSON string.- Parameters:
json- the JSON string to parse.
-
containsKey
Returnstrueif this object contains the specified key.- Parameters:
key- the key to check.- Returns:
trueif the key exists.
-
getKeys
Returns a list of all keys in this object.- Returns:
- the list of keys.
-
isEmpty
public boolean isEmpty()Returnstrueif this object contains no key-value pairs.- Returns:
trueif empty.
-
put
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
Removes a key-value pair from this object.- Parameters:
key- the key to remove.- Returns:
- this JsonObject for method chaining.
-
get
Returns the rawJsonValuefor the specified key.- Parameters:
key- the key to look up.- Returns:
- the JsonValue, or
nullif the key does not exist.
-
getAsString
Returns the value for the specified key as a String.- Parameters:
key- the key to look up.- Returns:
- the String value, or
nullif the key is missing or null.
-
getAsString
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
Returns the value for the specified key as a Boolean.- Parameters:
key- the key to look up.- Returns:
- the Boolean value, or
nullif the key is missing or null.
-
getAsBoolean
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
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
Returns the value for the specified key as a Short.- Parameters:
key- the key to look up.- Returns:
- the Short value, or
nullif the key is missing or null.
-
getAsShort
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
Returns the value for the specified key as an Integer.- Parameters:
key- the key to look up.- Returns:
- the Integer value, or
nullif the key is missing or null.
-
getAsInt
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
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
Returns the value for the specified key as a Long.- Parameters:
key- the key to look up.- Returns:
- the Long value, or
nullif the key is missing or null.
-
getAsLong
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
Returns the value for the specified key as a Float.- Parameters:
key- the key to look up.- Returns:
- the Float value, or
nullif the key is missing or null.
-
getAsFloat
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
Returns the value for the specified key as a Double.- Parameters:
key- the key to look up.- Returns:
- the Double value, or
nullif the key is missing or null.
-
getAsDouble
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
Returns the value for the specified key as a Number.- Parameters:
key- the key to look up.- Returns:
- the Number value, or
nullif the key is missing or null.
-
getAsNumber
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
Returns the value for the specified key as aJsonObject.- Parameters:
key- the key to look up.- Returns:
- the nested JsonObject.
-
getAsJsonArray
Returns the value for the specified key as aJsonArray.- Parameters:
key- the key to look up.- Returns:
- the JsonArray.
-
getAsArray
Returns the value for the specified key as a List.- Parameters:
key- the key to look up.- Returns:
- the list of values.
-
getAsStringArray
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
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
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
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
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
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
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
Description copied from class:JsonValueReturns this value serialized as a JSON string.- Specified by:
toJsonStringin classJsonValue- Returns:
- the JSON string representation.
-