Class JsonObject

java.lang.Object
net.luis.utils.io.data.json.JsonObject
All Implemented Interfaces:
JsonElement

public class JsonObject extends Object implements 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.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final Map<String,JsonElement>
    The internal map of elements.
    The order of the elements is preserved.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs an empty json object.
    JsonObject(@NotNull Map<String,? extends JsonElement> elements)
    Constructs a json object with the given elements.
  • Method Summary

    Modifier and Type
    Method
    Description
    @Nullable JsonElement
    add(@NotNull String key, boolean value)
    Adds the given boolean with the given key to this json object.
    The boolean will be converted to a json primitive.
    @Nullable JsonElement
    add(@NotNull String key, byte value)
    Adds the given byte with the given key to this json object.
    The byte will be converted to a json primitive.
    @Nullable JsonElement
    add(@NotNull String key, double value)
    Adds the given double with the given key to this json object.
    The double will be converted to a json primitive.
    @Nullable JsonElement
    add(@NotNull String key, float value)
    Adds the given float with the given key to this json object.
    The float will be converted to a json primitive.
    @Nullable JsonElement
    add(@NotNull String key, int value)
    Adds the given int with the given key to this json object.
    The int will be converted to a json primitive.
    @Nullable JsonElement
    add(@NotNull String key, long value)
    Adds the given long with the given key to this json object.
    The long will be converted to a json primitive.
    @Nullable JsonElement
    add(@NotNull String key, short value)
    Adds the given short with the given key to this json object.
    The short will be converted to a json primitive.
    @Nullable JsonElement
    add(@NotNull String key, @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.
    @Nullable JsonElement
    add(@NotNull String key, @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.
    @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
    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>
    Returns the collection of values in this json object.
    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
    get(@NotNull String key)
    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
    getAsByte(@NotNull String key)
    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
    getAsLong(@NotNull String key)
    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
     
    boolean
    Checks if this json object is empty.
    @NotNull Set<String>
    Returns the set of keys in this json object.
    @Nullable JsonElement
    remove(@Nullable String key)
    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
    Returns the number of elements in this json object.
     
    @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

      private final Map<String,JsonElement> elements
      The internal map of elements.
      The order of the elements is preserved.
  • Constructor Details

    • JsonObject

      public JsonObject()
      Constructs an empty json object.
    • JsonObject

      public JsonObject(@NotNull @NotNull Map<String,? extends JsonElement> elements)
      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

      public boolean containsKey(@Nullable @Nullable String key)
      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

      public boolean containsValue(@Nullable @Nullable JsonElement element)
      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

      @NotNull public @NotNull Set<String> keySet()
      Returns the set of keys in this json object.
      Returns:
      The keys of this json object
    • elements

      @NotNull public @NotNull @Unmodifiable Collection<JsonElement> elements()
      Returns the collection of values in this json object.
      Returns:
      The values of this json object
    • entrySet

      @NotNull public @NotNull Set<Map.Entry<String,JsonElement>> entrySet()
      Returns the set of entries in this json object.
      Returns:
      The entries of this json object
    • forEach

      public void forEach(@NotNull @NotNull BiConsumer<? super String,? super JsonElement> action)
      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 add
      element - 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 add
      value - 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

      @Nullable public @Nullable JsonElement add(@NotNull @NotNull String key, boolean value)
      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 add
      value - 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 add
      value - 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

      @Nullable public @Nullable JsonElement add(@NotNull @NotNull String key, byte value)
      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 add
      value - 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

      @Nullable public @Nullable JsonElement add(@NotNull @NotNull String key, short value)
      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 add
      value - 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

      @Nullable public @Nullable JsonElement add(@NotNull @NotNull String key, int value)
      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 add
      value - 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

      @Nullable public @Nullable JsonElement add(@NotNull @NotNull String key, long value)
      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 add
      value - 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

      @Nullable public @Nullable JsonElement add(@NotNull @NotNull String key, float value)
      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 add
      value - 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

      @Nullable public @Nullable JsonElement add(@NotNull @NotNull String key, double value)
      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 add
      value - 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

      public void addAll(@NotNull @NotNull JsonObject object)
      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

      public void addAll(@NotNull @NotNull Map<String,? extends JsonElement> elements)
      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

      @Nullable public @Nullable JsonElement remove(@Nullable @Nullable String key)
      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 replace
      newElement - 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 replace
      oldElement - The old element to replace
      newElement - 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

      @Nullable public @Nullable JsonElement get(@NotNull @NotNull String key)
      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

      @NotNull public @NotNull JsonObject getAsJsonObject(@NotNull @NotNull String key)
      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 null
      NoSuchJsonElementException - If no element was found for the given key
      JsonTypeException - If the element is not a json object
      See Also:
    • getAsJsonArray

      @NotNull public @NotNull JsonArray getAsJsonArray(@NotNull @NotNull String key)
      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 null
      NoSuchJsonElementException - If no element was found for the given key
      JsonTypeException - If the element is not a json array
      See Also:
    • getJsonPrimitive

      @NotNull public @NotNull JsonPrimitive getJsonPrimitive(@NotNull @NotNull String key)
      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 null
      NoSuchJsonElementException - If no element was found for the given key
      JsonTypeException - If the element is not a json primitive
      See Also:
    • getAsString

      @NotNull public @NotNull String getAsString(@NotNull @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.
      Parameters:
      key - The key to get
      Returns:
      The element associated with the key as a string
      Throws:
      NullPointerException - If the given key is null
      NoSuchJsonElementException - If no element was found for the given key
      JsonTypeException - If the element is not a string
      See Also:
    • getAsBoolean

      public boolean getAsBoolean(@NotNull @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.
      Parameters:
      key - The key to get
      Returns:
      The element associated with the key as a boolean
      Throws:
      NullPointerException - If the given key is null
      NoSuchJsonElementException - If no element was found for the given key
      JsonTypeException - If the element is not a boolean
      See Also:
    • getAsNumber

      @NotNull public @NotNull Number getAsNumber(@NotNull @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.
      Parameters:
      key - The key to get
      Returns:
      The element associated with the key as a number
      Throws:
      NullPointerException - If the given key is null
      NoSuchJsonElementException - If no element was found for the given key
      JsonTypeException - If the element is not a number
      See Also:
    • getAsByte

      public byte getAsByte(@NotNull @NotNull String key)
      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 null
      NoSuchJsonElementException - If no element was found for the given key
      JsonTypeException - If the element is not a byte
      See Also:
    • getAsShort

      public short getAsShort(@NotNull @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.
      Parameters:
      key - The key to get
      Returns:
      The element associated with the key as a short
      Throws:
      NullPointerException - If the given key is null
      NoSuchJsonElementException - If no element was found for the given key
      JsonTypeException - If the element is not a short
      See Also:
    • getAsInteger

      public int getAsInteger(@NotNull @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.
      Parameters:
      key - The key to get
      Returns:
      The element associated with the key as an integer
      Throws:
      NullPointerException - If the given key is null
      NoSuchJsonElementException - If no element was found for the given key
      JsonTypeException - If the element is not an integer
      See Also:
    • getAsLong

      public long getAsLong(@NotNull @NotNull String key)
      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 null
      NoSuchJsonElementException - If no element was found for the given key
      JsonTypeException - If the element is not a long
      See Also:
    • getAsFloat

      public float getAsFloat(@NotNull @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.
      Parameters:
      key - The key to get
      Returns:
      The element associated with the key as a float
      Throws:
      NullPointerException - If the given key is null
      NoSuchJsonElementException - If no element was found for the given key
      JsonTypeException - If the element is not a float
      See Also:
    • getAsDouble

      public double getAsDouble(@NotNull @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.
      Parameters:
      key - The key to get
      Returns:
      The element associated with the key as a double
      Throws:
      NullPointerException - If the given key is null
      NoSuchJsonElementException - If no element was found for the given key
      JsonTypeException - If the element is not a double
      See Also:
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toString

      @NotNull public @NotNull String toString(@NotNull @NotNull JsonConfig config)
      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 interface JsonElement
      Parameters:
      config - The json config to use
      Returns:
      The string representation of this json element