- All Implemented Interfaces:
Serializable
,Cloneable
,Map<String,
JsonElement>
JsonObject is a
Map
of JsonElement
. It includes methods for accessing members as various
Java types.- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,
V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object, V extends Object> -
Constructor Summary
ConstructorsConstructorDescriptionConstructs an emptyJsonObject
with the default initial capacity (16) and the default load factor (0.75).JsonObject
(int initialCapacity) Constructs an emptyJsonObject
with the specified initial capacity and the default load factor (0.75).JsonObject
(int initialCapacity, float loadFactor) Constructs an emptyJsonObject
with the specified initial capacity and the specified load factor. -
Method Summary
Modifier and TypeMethodDescriptionReturns theJsonArray
for the specified key.Returns theJsonArray
for the specified key.boolean
getBoolean
(String key) Returns the boolean for the specified key.getBoolean
(String key, Boolean defaultValue) Returns the boolean for the specified key.double
Returns the double for the specified key.Returns the double for the specified key.float
Returns the float for the specified key.Returns the float for the specified key.int
Returns the integer for the specified key.Returns the integer for the specified key.long
Returns the long for the specified key.Returns the long for the specified key.Returns theJsonObject
for the specified key.getObject
(String key, JsonObject defaultValue) Returns theJsonObject
for the specified key.Returns the string for the specified key.Returns the string for the specified key.boolean
Returns true ifJsonElementType.NULL
is at specified key.merge
(JsonObject other) Merges thisJsonObject
with another resulting in a newJsonObject
.Associates a boolean with specified key.Associates a double with specified key.Associates a float with specified key.Associates a integer with specified key.Associates a long with specified key.Associates a string with specified key.Associates aJsonArray
with specified key.@Nullable JsonElement
put
(String key, @Nullable JsonElement value) put
(String key, @Nullable JsonObject value) Associates aJsonObject
with specified key.toString()
Formats thisJsonObject
as a string with no indentation.toString
(int spaces) Formats thisJsonObject
as a string with specified spaces for indentation.Methods inherited from class java.util.LinkedHashMap
clear, containsValue, entrySet, forEach, get, getOrDefault, keySet, removeEldestEntry, replaceAll, values
Methods inherited from class java.util.HashMap
clone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, putAll, putIfAbsent, remove, remove, replace, replace, size
Methods inherited from class java.util.AbstractMap
equals, hashCode
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, containsKey, equals, hashCode, isEmpty, merge, putAll, putIfAbsent, remove, remove, replace, replace, size
-
Constructor Details
-
JsonObject
public JsonObject()Constructs an emptyJsonObject
with the default initial capacity (16) and the default load factor (0.75). -
JsonObject
public JsonObject(int initialCapacity) Constructs an emptyJsonObject
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 emptyJsonObject
with the specified initial capacity and the specified load factor.- Parameters:
initialCapacity
- the initial capacityloadFactor
- the load factor- Throws:
IllegalArgumentException
- if the initial capacity is negative or the load factor is non-positive
-
-
Method Details
-
getObject
Returns theJsonObject
for the specified key.- Parameters:
key
- the key of theJsonObject
to be returned- Returns:
- the
JsonObject
to which the specified key is mapped - Throws:
JsonElementTypeException
- ifJsonElement
at index is not of typeJsonElementType.JSON_OBJECT
-
getObject
Returns theJsonObject
for the specified key. Default value is returned if key is missing.- Parameters:
key
- the key of theJsonObject
to be returneddefaultValue
- the default value to return if key is missing- Returns:
- the
JsonObject
to which the specified key is mapped - Throws:
JsonElementTypeException
- ifJsonElement
at index is not of typeJsonElementType.JSON_OBJECT
-
getArray
Returns theJsonArray
for the specified key.- Parameters:
key
- the key of theJsonArray
to be returned- Returns:
- the
JsonArray
to which the specified key is mapped - Throws:
JsonElementTypeException
- ifJsonElement
at index is not of typeJsonElementType.JSON_ARRAY
-
getArray
Returns theJsonArray
for the specified key. Default value is returned if key is missing.- Parameters:
key
- the key of theJsonArray
to be returneddefaultValue
- the default value to return if key is missing- Returns:
- the
JsonArray
to which the specified key is mapped - Throws:
JsonElementTypeException
- ifJsonElement
at index is not of typeJsonElementType.JSON_ARRAY
-
getString
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
- ifJsonElement
at index is not of typeJsonElementType.STRING
-
getString
Returns the string for the specified key. Default value is returned if key is missing.- Parameters:
key
- the key of the string to be returneddefaultValue
- the default value to return if key is missing- Returns:
- the string to which the specified key is mapped
- Throws:
JsonElementTypeException
- ifJsonElement
at index is not of typeJsonElementType.STRING
-
getDouble
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
- ifJsonElement
at index is not of typeJsonElementType.DOUBLE
-
getDouble
Returns the double for the specified key. Default value is returned if key is missing.- Parameters:
key
- the key of the double to be returneddefaultValue
- the default value to return if key is missing- Returns:
- the double to which the specified key is mapped
- Throws:
JsonElementTypeException
- ifJsonElement
at index is not of typeJsonElementType.DOUBLE
-
getFloat
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
- ifJsonElement
at index is not of typeJsonElementType.DOUBLE
-
getFloat
Returns the float for the specified key. Default value is returned if key is missing.- Parameters:
key
- the key of the float to be returneddefaultValue
- the default value to return if key is missing- Returns:
- the float to which the specified key is mapped
- Throws:
JsonElementTypeException
- ifJsonElement
at index is not of typeJsonElementType.DOUBLE
-
getInt
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
- ifJsonElement
at index is not of typeJsonElementType.LONG
-
getInt
Returns the integer for the specified key. Default value is returned if key is missing.- Parameters:
key
- the key of the integer to be returneddefaultValue
- the default value to return if key is missing- Returns:
- the integer to which the specified key is mapped
- Throws:
JsonElementTypeException
- ifJsonElement
at index is not of typeJsonElementType.LONG
-
getLong
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
- ifJsonElement
at index is not of typeJsonElementType.LONG
-
getLong
Returns the long for the specified key. Default value is returned if key is missing.- Parameters:
key
- the key of the long to be returneddefaultValue
- the default value to return if key is missing- Returns:
- the long to which the specified key is mapped
- Throws:
JsonElementTypeException
- ifJsonElement
at index is not of typeJsonElementType.LONG
-
getBoolean
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
- ifJsonElement
at index is not of typeJsonElementType.BOOLEAN
-
getBoolean
Returns the boolean for the specified key. Default value is returned if key is missing.- Parameters:
key
- the key of the boolean to be returneddefaultValue
- the default value to return if key is missing- Returns:
- the boolean to which the specified key is mapped
- Throws:
JsonElementTypeException
- ifJsonElement
at index is not of typeJsonElementType.BOOLEAN
-
isNull
Returns true ifJsonElementType.NULL
is at specified key.- Parameters:
key
- the key to be checked- Returns:
- true if
JsonElementType.NULL
is at specified key
-
put
- Specified by:
put
in interfaceMap<String,
JsonElement> - Overrides:
put
in classHashMap<String,
JsonElement>
-
put
Associates aJsonObject
with specified key.- Parameters:
key
- the key to associate theJsonObject
withvalue
- theJsonObject
to associate- Returns:
- this
-
put
Associates aJsonArray
with specified key. -
put
Associates a string with specified key.- Parameters:
key
- the key to associate the string withvalue
- the string to associate- Returns:
- this
-
put
Associates a integer with specified key.- Parameters:
key
- the key to associate the integer withvalue
- the integer to associate- Returns:
- this
-
put
Associates a long with specified key.- Parameters:
key
- the key to associate the long withvalue
- the long to associate- Returns:
- this
-
put
Associates a double with specified key.- Parameters:
key
- the key to associate the double withvalue
- the double to associate- Returns:
- this
-
put
Associates a float with specified key.- Parameters:
key
- the key to associate the float withvalue
- the float to associate- Returns:
- this
-
put
Associates a boolean with specified key.- Parameters:
key
- the key to associate the boolean withvalue
- the boolean to associate- Returns:
- this
-
putNull
- Parameters:
key
- the key to put a NULL JSON value into- Returns:
- this
-
merge
Merges thisJsonObject
with another resulting in a newJsonObject
.- If key in both and each value is an object then it will recursively merge and add to result
- If key in both and each value is not an object, value from the right is added to the result
- If key in this only, then this value is added to result
- If key in other only, then other value is added to result
- Parameters:
other
- the object to merge with- Returns:
- a new
JsonObject
with merged properties
-
toString
Formats thisJsonObject
as a string with no indentation.- Overrides:
toString
in classAbstractMap<String,
JsonElement> - Returns:
- formatted JSON string
-
toString
Formats thisJsonObject
as a string with specified spaces for indentation.- Parameters:
spaces
- the spaces for each indentation- Returns:
- formatted JSON string with spaces for indentation
-