- 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 emptyJsonObjectwith the default initial capacity (16) and the default load factor (0.75).JsonObject(int initialCapacity) Constructs an emptyJsonObjectwith the specified initial capacity and the default load factor (0.75).JsonObject(int initialCapacity, float loadFactor) Constructs an emptyJsonObjectwith the specified initial capacity and the specified load factor. -
Method Summary
Modifier and TypeMethodDescriptionReturns theJsonArrayfor the specified key.Returns theJsonArrayfor the specified key.@Nullable JsonArraygetArrayOrNull(String key) Returns theJsonArrayfor the specified key.booleangetBoolean(String key) Returns the boolean for the specified key.booleangetBoolean(String key, boolean defaultValue) Returns the boolean for the specified key.@Nullable BooleangetBooleanOrNull(String key) Returns the boolean for the specified key.doubleReturns the double for the specified key.doubleReturns the double for the specified key.@Nullable DoublegetDoubleOrNull(String key) Returns the double for the specified key.floatReturns the float for the specified key.floatReturns the float for the specified key.@Nullable FloatgetFloatOrNull(String key) Returns the float for the specified key.intReturns the integer for the specified key.intReturns the integer for the specified key.@Nullable IntegergetIntOrNull(String key) Returns the integer for the specified key.longReturns the long for the specified key.longReturns the long for the specified key.@Nullable LonggetLongOrNull(String key) Returns the long for the specified key.Returns theJsonObjectfor the specified key.getObject(String key, JsonObject defaultValue) Returns theJsonObjectfor the specified key.@Nullable JsonObjectgetObjectOrNull(String key) Returns theJsonObjectfor the specified key.Returns the string for the specified key.Returns the string for the specified key.@Nullable StringgetStringOrNull(String key) Returns the string for the specified key.booleanReturns true ifJsonElementType.NULLis at a specified key.merge(JsonObject other) Merges thisJsonObjectwith another resulting in a newJsonObject.Associates a boolean with a specified key.Associates a double with a specified key.Associates a float with a specified key.Associates an integer with a specified key.Associates alongwith a specified key.Associates a boolean with a specified key.Associates a double with a specified key.Associates a float with a specified key.Associates an integer with a specified key.Associates alongwith a specified key.Associates a string with a specified key.Associates aJsonArraywith a specified key.@Nullable JsonElementput(String key, @Nullable JsonElement value) put(String key, @Nullable JsonObject value) Associates aJsonObjectwith a specified key.toString()Formats thisJsonObjectas a string with no indentation.toString(int spaces) Formats thisJsonObjectas a string with specified spaces for indentation.Methods inherited from class java.util.LinkedHashMap
clear, containsValue, entrySet, forEach, get, getOrDefault, keySet, removeEldestEntry, replaceAll, valuesMethods inherited from class java.util.HashMap
clone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, putAll, putIfAbsent, remove, remove, replace, replace, sizeMethods inherited from class java.util.AbstractMap
equals, hashCodeMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods 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 emptyJsonObjectwith the default initial capacity (16) and the default load factor (0.75). -
JsonObject
public JsonObject(int initialCapacity) Constructs an emptyJsonObjectwith 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 emptyJsonObjectwith 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 theJsonObjectfor the specified key.- Parameters:
key- the key of theJsonObjectto be returned- Returns:
- the
JsonObjectto which the specified key is mapped - Throws:
JsonElementTypeException- ifJsonElementat index is not of typeJsonElementType.JSON_OBJECT
-
getObject
Returns theJsonObjectfor the specified key. The default value is returned if the key is missing.- Parameters:
key- the key of theJsonObjectto be returneddefaultValue- the default value to return if the key is missing- Returns:
- the
JsonObjectto which the specified key is mapped - Throws:
JsonElementTypeException- ifJsonElementat index is not of typeJsonElementType.JSON_OBJECT
-
getObjectOrNull
Returns theJsonObjectfor the specified key. Returns null if the key is missing or if the value isJsonElementType.NULL.- Parameters:
key- the key of theJsonObjectto be returned- Returns:
- the
JsonObjectto which the specified key is mapped or null if the key is missing or if the value isJsonElementType.NULL - Throws:
JsonElementTypeException- ifJsonElementat index is not of typeJsonElementType.JSON_OBJECT
-
getArray
Returns theJsonArrayfor the specified key.- Parameters:
key- the key of theJsonArrayto be returned- Returns:
- the
JsonArrayto which the specified key is mapped - Throws:
JsonElementTypeException- ifJsonElementat index is not of typeJsonElementType.JSON_ARRAY
-
getArray
Returns theJsonArrayfor the specified key. The default value is returned if the key is missing.- Parameters:
key- the key of theJsonArrayto be returneddefaultValue- the default value to return if the key is missing- Returns:
- the
JsonArrayto which the specified key is mapped - Throws:
JsonElementTypeException- ifJsonElementat index is not of typeJsonElementType.JSON_ARRAY
-
getArrayOrNull
Returns theJsonArrayfor the specified key. Returns null if the key is missing or if the value isJsonElementType.NULL.- Parameters:
key- the key of theJsonArrayto be returned- Returns:
- the
JsonArrayto which the specified key is mapped or null if the key is missing or if the value isJsonElementType.NULL - Throws:
JsonElementTypeException- ifJsonElementat 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- ifJsonElementat index is not of typeJsonElementType.STRING
-
getString
Returns the string for the specified key. The default value is returned if the key is missing.- Parameters:
key- the key of the string to be returneddefaultValue- the default value to return if the key is missing- Returns:
- the string to which the specified key is mapped
- Throws:
JsonElementTypeException- ifJsonElementat index is not of typeJsonElementType.STRING
-
getStringOrNull
Returns the string for the specified key. Returns null if the key is missing or if the value isJsonElementType.NULL.- Parameters:
key- the key of the string to be returned- Returns:
- the string to which the specified key is mapped or null if the key is missing or if the value is
JsonElementType.NULL - Throws:
JsonElementTypeException- ifJsonElementat 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- ifJsonElementat index is not of typeJsonElementType.DOUBLE
-
getDouble
Returns the double for the specified key. The default value is returned if the key is missing.- Parameters:
key- the key of the double to be returneddefaultValue- the default value to return if the key is missing- Returns:
- the double to which the specified key is mapped
- Throws:
JsonElementTypeException- ifJsonElementat index is not of typeJsonElementType.DOUBLE
-
getDoubleOrNull
Returns the double for the specified key. Returns null if the key is missing or if the value isJsonElementType.NULL.- Parameters:
key- the key of the double to be returned- Returns:
- the double to which the specified key is mapped or null if the key is missing or if the value is
JsonElementType.NULL - Throws:
JsonElementTypeException- ifJsonElementat 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- ifJsonElementat index is not of typeJsonElementType.DOUBLE
-
getFloat
Returns the float for the specified key. The default value is returned if the key is missing.- Parameters:
key- the key of the float to be returneddefaultValue- the default value to return if the key is missing- Returns:
- the float to which the specified key is mapped
- Throws:
JsonElementTypeException- ifJsonElementat index is not of typeJsonElementType.DOUBLE
-
getFloatOrNull
Returns the float for the specified key. Returns null if the key is missing or if the value isJsonElementType.NULL.- Parameters:
key- the key of the float to be returned- Returns:
- the float to which the specified key is mapped or null if the key is missing or if the value is
JsonElementType.NULL - Throws:
JsonElementTypeException- ifJsonElementat 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- ifJsonElementat index is not of typeJsonElementType.LONG
-
getInt
Returns the integer for the specified key. The default value is returned if the key is missing.- Parameters:
key- the key of the integer to be returneddefaultValue- the default value to return if the key is missing- Returns:
- the integer to which the specified key is mapped
- Throws:
JsonElementTypeException- ifJsonElementat index is not of typeJsonElementType.LONG
-
getIntOrNull
Returns the integer for the specified key. Returns null if the key is missing or if the value isJsonElementType.NULL.- Parameters:
key- the key of the integer to be returned- Returns:
- the integer to which the specified key is mapped or null if the key is missing or if the value is
JsonElementType.NULL - Throws:
JsonElementTypeException- ifJsonElementat 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- ifJsonElementat index is not of typeJsonElementType.LONG
-
getLong
Returns the long for the specified key. The default value is returned if the key is missing.- Parameters:
key- the key of the long to be returneddefaultValue- the default value to return if the key is missing- Returns:
- the long to which the specified key is mapped
- Throws:
JsonElementTypeException- ifJsonElementat index is not of typeJsonElementType.LONG
-
getLongOrNull
Returns the long for the specified key. Returns null if the key is missing or if the value isJsonElementType.NULL.- Parameters:
key- the key of the long to be returned- Returns:
- the long to which the specified key is mapped or null if the key is missing or if the value is
JsonElementType.NULL - Throws:
JsonElementTypeException- ifJsonElementat 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- ifJsonElementat index is not of typeJsonElementType.BOOLEAN
-
getBoolean
Returns the boolean for the specified key. The default value is returned if the key is missing.- Parameters:
key- the key of the boolean to be returneddefaultValue- the default value to return if the key is missing- Returns:
- the boolean to which the specified key is mapped
- Throws:
JsonElementTypeException- ifJsonElementat index is not of typeJsonElementType.BOOLEAN
-
getBooleanOrNull
Returns the boolean for the specified key. Returns null if the key is missing or if the value isJsonElementType.NULL.- Parameters:
key- the key of the boolean to be returned- Returns:
- the boolean to which the specified key is mapped or null if the key is missing or if the value is
JsonElementType.NULL - Throws:
JsonElementTypeException- ifJsonElementat index is not of typeJsonElementType.BOOLEAN
-
isNull
Returns true ifJsonElementType.NULLis at a specified key.- Parameters:
key- the key to be checked- Returns:
- true if
JsonElementType.NULLis at a specified key
-
put
- Specified by:
putin interfaceMap<String,JsonElement> - Overrides:
putin classHashMap<String,JsonElement>
-
put
Associates aJsonObjectwith a specified key.- Parameters:
key- the key to associate theJsonObjectwithvalue- theJsonObjectto associate- Returns:
- this
-
put
Associates aJsonArraywith a specified key. -
put
Associates a string with a specified key.- Parameters:
key- the key to associate the string withvalue- the string to associate- Returns:
- this
-
put
Associates an integer with a specified key.- Parameters:
key- the key to associate the integer withvalue- the integer to associate- Returns:
- this
-
put
Associates an integer with a specified key.- Parameters:
key- the key to associate the integer withvalue- the integer to associate- Returns:
- this
-
put
Associates alongwith a specified key.- Parameters:
key- the key to associate the long withvalue- the long to associate- Returns:
- this
-
put
Associates alongwith a specified key.- Parameters:
key- the key to associate the long withvalue- the long to associate- Returns:
- this
-
put
Associates a double with a specified key.- Parameters:
key- the key to associate the double withvalue- the double to associate- Returns:
- this
-
put
Associates a double with a specified key.- Parameters:
key- the key to associate the double withvalue- the double to associate- Returns:
- this
-
put
Associates a float with a specified key.- Parameters:
key- the key to associate the float withvalue- the float to associate- Returns:
- this
-
put
Associates a float with a specified key.- Parameters:
key- the key to associate the float withvalue- the float to associate- Returns:
- this
-
put
Associates a boolean with a specified key.- Parameters:
key- the key to associate the boolean withvalue- the boolean to associate- Returns:
- this
-
put
Associates a boolean with a 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 thisJsonObjectwith another resulting in a newJsonObject.- If the key in both and each value is an object, then it will recursively merge and add to the result
- If the key in both and each value is not an object, a value from the right is added to the result
- If the key is in this only, then this value is added to the result
- If the key is in other only, then other value is added to the result
- Parameters:
other- the object to merge with- Returns:
- a new
JsonObjectwith merged properties
-
toString
Formats thisJsonObjectas a string with no indentation.- Overrides:
toStringin classAbstractMap<String,JsonElement> - Returns:
- formatted JSON string
-
toString
Formats thisJsonObjectas a string with specified spaces for indentation.- Parameters:
spaces- the spaces for each indentation- Returns:
- formatted JSON string with spaces for indentation
-