java.lang.Object
net.luis.utils.io.data.json.JsonObject
- All Implemented Interfaces:
JsonElement
Represents a json object.
A json object is an ordered set of name/value pairs.
A name is a string and a value can be any json element, including json null.
A json object is an ordered set of name/value pairs.
A name is a string and a value can be any json element, including json null.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Map
<String, JsonElement> The internal map of elements.
The order of the elements is preserved. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs an empty json object.JsonObject
(@NotNull Map<String, ? extends JsonElement> elements) Constructs a json object with the given elements. -
Method Summary
Modifier and TypeMethodDescription@Nullable JsonElement
Adds the given boolean with the given key to this json object.
The boolean will be converted to a json primitive.@Nullable JsonElement
Adds the given byte with the given key to this json object.
The byte will be converted to a json primitive.@Nullable JsonElement
Adds the given double with the given key to this json object.
The double will be converted to a json primitive.@Nullable JsonElement
Adds the given float with the given key to this json object.
The float will be converted to a json primitive.@Nullable JsonElement
Adds the given int with the given key to this json object.
The int will be converted to a json primitive.@Nullable JsonElement
Adds the given long with the given key to this json object.
The long will be converted to a json primitive.@Nullable JsonElement
Adds the given short with the given key to this json object.
The short will be converted to a json primitive.@Nullable JsonElement
Adds the given number with the given key to this json object.
If the number is null, it will be replaced with json null.
The number value will be converted to a json primitive.@Nullable JsonElement
Adds the given string with the given key to this json object.
If the string is null, it will be replaced with json null.
The string value will be converted to a json primitive.@Nullable JsonElement
add
(@NotNull String key, @Nullable JsonElement element) Adds the given element with the given key to this json object.
If the element is null, it will be replaced with json null.
If the key is already present, the element will be replaced.void
addAll
(@NotNull Map<String, ? extends JsonElement> elements) Adds all elements from the given map to this json object.void
addAll
(@NotNull JsonObject object) Adds all elements from the given json object to this json object.void
clear()
Removes all element pairs from this json object.boolean
containsKey
(@Nullable String key) Checks if this json object contains the given key.boolean
containsValue
(@Nullable JsonElement element) Checks if this json object contains the given element.@NotNull @Unmodifiable Collection
<JsonElement> elements()
Returns the collection of values in this json object.@NotNull Set
<Map.Entry<String, JsonElement>> entrySet()
Returns the set of entries in this json object.boolean
void
forEach
(@NotNull BiConsumer<? super String, ? super JsonElement> action) Iterates over the entries of this json object and applies the given action to each entry.@Nullable JsonElement
Gets the element with the given key from this json object.boolean
getAsBoolean
(@NotNull String key) Gets the element with the given key from this json object as a boolean.
The element will be converted to a json primitive and then to a boolean.byte
Gets the element with the given key from this json object as a byte.
The element will be converted to a json primitive and then to a byte.double
getAsDouble
(@NotNull String key) Gets the element with the given key from this json object as a double.
The element will be converted to a json primitive and then to a double.float
getAsFloat
(@NotNull String key) Gets the element with the given key from this json object as a float.
The element will be converted to a json primitive and then to a float.int
getAsInteger
(@NotNull String key) Gets the element with the given key from this json object as an integer.
The element will be converted to a json primitive and then to an integer.@NotNull JsonArray
getAsJsonArray
(@NotNull String key) Gets the element with the given key from this json object as a json array.@NotNull JsonObject
getAsJsonObject
(@NotNull String key) Gets the element with the given key from this json object as a json object.long
Gets the element with the given key from this json object as a long.
The element will be converted to a json primitive and then to a long.@NotNull Number
getAsNumber
(@NotNull String key) Gets the element with the given key from this json object as a number.
The element will be converted to a json primitive and then to a number.short
getAsShort
(@NotNull String key) Gets the element with the given key from this json object as a short.
The element will be converted to a json primitive and then to a short.@NotNull String
getAsString
(@NotNull String key) Gets the element with the given key from this json object as a string.
The element will be converted to a json primitive and then to a string.@NotNull JsonPrimitive
getJsonPrimitive
(@NotNull String key) Gets the element with the given key from this json object as a json primitive.int
hashCode()
boolean
isEmpty()
Checks if this json object is empty.keySet()
Returns the set of keys in this json object.@Nullable JsonElement
Removes the element with the given key from this json object.boolean
replace
(@NotNull String key, @NotNull JsonElement oldElement, @Nullable JsonElement newElement) Replaces the given element with the given key in this json object with the new given element.@Nullable JsonElement
replace
(@NotNull String key, @Nullable JsonElement newElement) Replaces the element with the given key in this json object with the new given element.
If the given element is null, it will be replaced with json null.int
size()
Returns the number of elements in this json object.toString()
@NotNull String
toString
(@NotNull JsonConfig config) Returns a string representation of this json element based on the given json config.
The json config specifies how the json element should be formatted.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface net.luis.utils.io.data.json.JsonElement
getAsJsonArray, getAsJsonObject, getAsJsonPrimitive, isJsonArray, isJsonNull, isJsonObject, isJsonPrimitive
-
Field Details
-
elements
The internal map of elements.
The order of the elements is preserved.
-
-
Constructor Details
-
JsonObject
public JsonObject()Constructs an empty json object. -
JsonObject
Constructs a json object with the given elements.- Parameters:
elements
- The map of elements to add- Throws:
NullPointerException
- If the given elements are null
-
-
Method Details
-
size
public int size()Returns the number of elements in this json object.- Returns:
- The size of this json object
-
isEmpty
public boolean isEmpty()Checks if this json object is empty.- Returns:
- True if this json object is empty, false otherwise
-
containsKey
Checks if this json object contains the given key.- Parameters:
key
- The key to check- Returns:
- True if this json object contains the given key, false otherwise
-
containsValue
Checks if this json object contains the given element.- Parameters:
element
- The element to check- Returns:
- True if this json object contains the given element, false otherwise
-
keySet
Returns the set of keys in this json object.- Returns:
- The keys of this json object
-
elements
Returns the collection of values in this json object.- Returns:
- The values of this json object
-
entrySet
Returns the set of entries in this json object.- Returns:
- The entries of this json object
-
forEach
Iterates over the entries of this json object and applies the given action to each entry.- Parameters:
action
- The action to apply to each entry- Throws:
NullPointerException
- If the given action is null
-
add
@Nullable public @Nullable JsonElement add(@NotNull @NotNull String key, @Nullable @Nullable JsonElement element) Adds the given element with the given key to this json object.
If the element is null, it will be replaced with json null.
If the key is already present, the element will be replaced.- Parameters:
key
- The key to addelement
- The element to add- Returns:
- The previous element associated with the key, or null if the key was not present
- Throws:
NullPointerException
- If the given key is null
-
add
@Nullable public @Nullable JsonElement add(@NotNull @NotNull String key, @Nullable @Nullable String value) Adds the given string with the given key to this json object.
If the string is null, it will be replaced with json null.
The string value will be converted to a json primitive.- Parameters:
key
- The key to addvalue
- The string value to add- Returns:
- The previous element associated with the key, or null if the key was not present
- Throws:
NullPointerException
- If the given key is null- See Also:
-
add
Adds the given boolean with the given key to this json object.
The boolean will be converted to a json primitive.- Parameters:
key
- The key to addvalue
- The boolean value to add- Returns:
- The previous element associated with the key, or null if the key was not present
- Throws:
NullPointerException
- If the given key is null- See Also:
-
add
@Nullable public @Nullable JsonElement add(@NotNull @NotNull String key, @Nullable @Nullable Number value) Adds the given number with the given key to this json object.
If the number is null, it will be replaced with json null.
The number value will be converted to a json primitive.- Parameters:
key
- The key to addvalue
- The number value to add- Returns:
- The previous element associated with the key, or null if the key was not present
- Throws:
NullPointerException
- If the given key is null- See Also:
-
add
Adds the given byte with the given key to this json object.
The byte will be converted to a json primitive.- Parameters:
key
- The key to addvalue
- The byte value to add- Returns:
- The previous element associated with the key, or null if the key was not present
- Throws:
NullPointerException
- If the given key is null- See Also:
-
add
Adds the given short with the given key to this json object.
The short will be converted to a json primitive.- Parameters:
key
- The key to addvalue
- The short value to add- Returns:
- The previous element associated with the key, or null if the key was not present
- Throws:
NullPointerException
- If the given key is null- See Also:
-
add
Adds the given int with the given key to this json object.
The int will be converted to a json primitive.- Parameters:
key
- The key to addvalue
- The int value to add- Returns:
- The previous element associated with the key, or null if the key was not present
- Throws:
NullPointerException
- If the given key is null- See Also:
-
add
Adds the given long with the given key to this json object.
The long will be converted to a json primitive.- Parameters:
key
- The key to addvalue
- The long value to add- Returns:
- The previous element associated with the key, or null if the key was not present
- Throws:
NullPointerException
- If the given key is null- See Also:
-
add
Adds the given float with the given key to this json object.
The float will be converted to a json primitive.- Parameters:
key
- The key to addvalue
- The float value to add- Returns:
- The previous element associated with the key, or null if the key was not present
- Throws:
NullPointerException
- If the given key is null- See Also:
-
add
Adds the given double with the given key to this json object.
The double will be converted to a json primitive.- Parameters:
key
- The key to addvalue
- The double value to add- Returns:
- The previous element associated with the key, or null if the key was not present
- Throws:
NullPointerException
- If the given key is null- See Also:
-
addAll
Adds all elements from the given json object to this json object.- Parameters:
object
- The json object of elements to add- Throws:
NullPointerException
- If the given json object is null
-
addAll
Adds all elements from the given map to this json object.- Parameters:
elements
- The map of elements to add- Throws:
NullPointerException
- If the given elements are null
-
remove
Removes the element with the given key from this json object.- Parameters:
key
- The key to remove- Returns:
- The element associated with the key, or null if the key was not present
-
clear
public void clear()Removes all element pairs from this json object. -
replace
@Nullable public @Nullable JsonElement replace(@NotNull @NotNull String key, @Nullable @Nullable JsonElement newElement) Replaces the element with the given key in this json object with the new given element.
If the given element is null, it will be replaced with json null.- Parameters:
key
- The key to replacenewElement
- The new element to replace with- Returns:
- The previous element associated with the key, or null if the key was not present
- Throws:
NullPointerException
- If the given key is null
-
replace
public boolean replace(@NotNull @NotNull String key, @NotNull @NotNull JsonElement oldElement, @Nullable @Nullable JsonElement newElement) Replaces the given element with the given key in this json object with the new given element.- Parameters:
key
- The key to replaceoldElement
- The old element to replacenewElement
- The new element to replace with- Returns:
- True if the element was replaced, false otherwise
- Throws:
NullPointerException
- If the given key or old element is null
-
get
Gets the element with the given key from this json object.- Parameters:
key
- The key to get- Returns:
- The element associated with the key, or null if the key was not present
- Throws:
NullPointerException
- If the given key is null
-
getAsJsonObject
Gets the element with the given key from this json object as a json object.- Parameters:
key
- The key to get- Returns:
- The element associated with the key as a json object
- Throws:
NullPointerException
- If the given key is nullNoSuchJsonElementException
- If no element was found for the given keyJsonTypeException
- If the element is not a json object- See Also:
-
getAsJsonArray
Gets the element with the given key from this json object as a json array.- Parameters:
key
- The key to get- Returns:
- The element associated with the key as a json array
- Throws:
NullPointerException
- If the given key is nullNoSuchJsonElementException
- If no element was found for the given keyJsonTypeException
- If the element is not a json array- See Also:
-
getJsonPrimitive
Gets the element with the given key from this json object as a json primitive.- Parameters:
key
- The key to get- Returns:
- The element associated with the key as a json primitive
- Throws:
NullPointerException
- If the given key is nullNoSuchJsonElementException
- If no element was found for the given keyJsonTypeException
- If the element is not a json primitive- See Also:
-
getAsString
Gets the element with the given key from this json object as a string.
The element will be converted to a json primitive and then to a string.- Parameters:
key
- The key to get- Returns:
- The element associated with the key as a string
- Throws:
NullPointerException
- If the given key is nullNoSuchJsonElementException
- If no element was found for the given keyJsonTypeException
- If the element is not a string- See Also:
-
getAsBoolean
Gets the element with the given key from this json object as a boolean.
The element will be converted to a json primitive and then to a boolean.- Parameters:
key
- The key to get- Returns:
- The element associated with the key as a boolean
- Throws:
NullPointerException
- If the given key is nullNoSuchJsonElementException
- If no element was found for the given keyJsonTypeException
- If the element is not a boolean- See Also:
-
getAsNumber
Gets the element with the given key from this json object as a number.
The element will be converted to a json primitive and then to a number.- Parameters:
key
- The key to get- Returns:
- The element associated with the key as a number
- Throws:
NullPointerException
- If the given key is nullNoSuchJsonElementException
- If no element was found for the given keyJsonTypeException
- If the element is not a number- See Also:
-
getAsByte
Gets the element with the given key from this json object as a byte.
The element will be converted to a json primitive and then to a byte.- Parameters:
key
- The key to get- Returns:
- The element associated with the key as a byte
- Throws:
NullPointerException
- If the given key is nullNoSuchJsonElementException
- If no element was found for the given keyJsonTypeException
- If the element is not a byte- See Also:
-
getAsShort
Gets the element with the given key from this json object as a short.
The element will be converted to a json primitive and then to a short.- Parameters:
key
- The key to get- Returns:
- The element associated with the key as a short
- Throws:
NullPointerException
- If the given key is nullNoSuchJsonElementException
- If no element was found for the given keyJsonTypeException
- If the element is not a short- See Also:
-
getAsInteger
Gets the element with the given key from this json object as an integer.
The element will be converted to a json primitive and then to an integer.- Parameters:
key
- The key to get- Returns:
- The element associated with the key as an integer
- Throws:
NullPointerException
- If the given key is nullNoSuchJsonElementException
- If no element was found for the given keyJsonTypeException
- If the element is not an integer- See Also:
-
getAsLong
Gets the element with the given key from this json object as a long.
The element will be converted to a json primitive and then to a long.- Parameters:
key
- The key to get- Returns:
- The element associated with the key as a long
- Throws:
NullPointerException
- If the given key is nullNoSuchJsonElementException
- If no element was found for the given keyJsonTypeException
- If the element is not a long- See Also:
-
getAsFloat
Gets the element with the given key from this json object as a float.
The element will be converted to a json primitive and then to a float.- Parameters:
key
- The key to get- Returns:
- The element associated with the key as a float
- Throws:
NullPointerException
- If the given key is nullNoSuchJsonElementException
- If no element was found for the given keyJsonTypeException
- If the element is not a float- See Also:
-
getAsDouble
Gets the element with the given key from this json object as a double.
The element will be converted to a json primitive and then to a double.- Parameters:
key
- The key to get- Returns:
- The element associated with the key as a double
- Throws:
NullPointerException
- If the given key is nullNoSuchJsonElementException
- If no element was found for the given keyJsonTypeException
- If the element is not a double- See Also:
-
equals
-
hashCode
public int hashCode() -
toString
-
toString
Description copied from interface:JsonElement
Returns a string representation of this json element based on the given json config.
The json config specifies how the json element should be formatted.- Specified by:
toString
in interfaceJsonElement
- Parameters:
config
- The json config to use- Returns:
- The string representation of this json element
-