Module json5

Class JSONObject

    • Constructor Detail

      • JSONObject

        public JSONObject()
        Constructs a new JSONObject
      • JSONObject

        public JSONObject​(String source)
        Constructs a new JSONObject from a string
        Parameters:
        source - a string
      • JSONObject

        public JSONObject​(JSONParser parser)
        Constructs a new JSONObject from a JSONParser
        Parameters:
        parser - a JSONParser
    • Method Detail

      • copy

        public JSONObject copy()
        Creates a shallow copy of the JSONObject
        Returns:
        the new JSONObject
        Since:
        2.0.0
      • deepCopy

        public JSONObject deepCopy()
        Creates a deep copy of the JSONObject
        Returns:
        the new JSONObject
        Since:
        2.0.0
      • toMap

        public Map<String,​Object> toMap()
        Converts the JSONObject into a map. All JSONObjects and JSONArrays contained within this JSONObject will be converted into their Map or List form as well
        Returns:
        a map of the entries of this object
      • keySet

        public Set<String> keySet()
        Returns a set of keys of the JSONObject. Modifying the set will modify the JSONObject
        Returns:
        a set of keys
        See Also:
        Map.keySet()
      • entrySet

        public Set<Map.Entry<String,​Object>> entrySet()
        Returns a set of entries of the JSONObject. Modifying the set or an entry will modify the JSONObject

        Use with caution. Inserting objects of unknown types may cause issues when trying to use the JSONObject

        Returns:
        a set of entries
        See Also:
        Map.entrySet()
      • forEach

        public void forEach​(BiConsumer<String,​Object> action)
        Iterates over the whole JSONObject and performs the given action for each element.

        For each entry in the object, the predicate receives the key and its associated value.

        Parameters:
        action - The action for each entry
        Since:
        2.0.0
      • length

        public int length()
        Returns the number of entries in the JSONObject
        Returns:
        the number of entries
      • clear

        public void clear()
        Removes all values from this JSONObject
        Since:
        1.2.0
      • remove

        public void remove​(String key)
        Removes a key from a JSONObject
        Parameters:
        key - the key to be removed
        Throws:
        JSONException - if the key does not exist
        Since:
        1.2.0
      • removeIf

        public JSONObject removeIf​(BiPredicate<String,​Object> predicate)
        Removes all entries from a JSONObject where the predicate returns true.

        For each entry in the object, the predicate receives the key and its associated value.

        Parameters:
        predicate - the predicate
        Returns:
        this JSONObject
        Since:
        2.0.0
      • removeIf

        public JSONObject removeIf​(String key,
                                   Predicate<Object> predicate)
        Removes a key from a JSONObject if the predicate returns true.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Since:
        2.0.0
      • removeBooleanIf

        public JSONObject removeBooleanIf​(String key,
                                          Predicate<Boolean> predicate)
        Removes a key from a JSONObject if the predicate returns true.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a boolean
        Since:
        2.0.0
      • removeByteIf

        public JSONObject removeByteIf​(String key,
                                       Predicate<Byte> predicate)
        Removes a key from a JSONObject if the predicate returns true.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a byte
        Since:
        2.0.0
      • removeShortIf

        public JSONObject removeShortIf​(String key,
                                        Predicate<Short> predicate)
        Removes a key from a JSONObject if the predicate returns true.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a short
        Since:
        2.0.0
      • removeIntIf

        public JSONObject removeIntIf​(String key,
                                      Predicate<Integer> predicate)
        Removes a key from a JSONObject if the predicate returns true.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not an int
        Since:
        2.0.0
      • removeLongIf

        public JSONObject removeLongIf​(String key,
                                       Predicate<Long> predicate)
        Removes a key from a JSONObject if the predicate returns true.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a long
        Since:
        2.0.0
      • removeFloatIf

        public JSONObject removeFloatIf​(String key,
                                        Predicate<Float> predicate)
        Removes a key from a JSONObject if the predicate returns true.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a float
        Since:
        2.0.0
      • removeDoubleIf

        public JSONObject removeDoubleIf​(String key,
                                         Predicate<Double> predicate)
        Removes a key from a JSONObject if the predicate returns true.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a double
        Since:
        2.0.0
      • removeByteExactIf

        public JSONObject removeByteExactIf​(String key,
                                            Predicate<Byte> predicate)
        Removes a key from a JSONObject if the predicate returns true.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a byte, or if the value does not fit into a byte
        Since:
        2.0.0
      • removeShortExactIf

        public JSONObject removeShortExactIf​(String key,
                                             Predicate<Short> predicate)
        Removes a key from a JSONObject if the predicate returns true.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a short, or if the value does not fit into a short
        Since:
        2.0.0
      • removeIntExactIf

        public JSONObject removeIntExactIf​(String key,
                                           Predicate<Integer> predicate)
        Removes a key from a JSONObject if the predicate returns true.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not an int, or if the value does not fit into an int
        Since:
        2.0.0
      • removeLongExactIf

        public JSONObject removeLongExactIf​(String key,
                                            Predicate<Long> predicate)
        Removes a key from a JSONObject if the predicate returns true.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a long, or if the value does not fit into a long
        Since:
        2.0.0
      • removeFloatExactIf

        public JSONObject removeFloatExactIf​(String key,
                                             Predicate<Float> predicate)
        Removes a key from a JSONObject if the predicate returns true.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a float, or if the value does not fit into a float
        Since:
        2.0.0
      • removeDoubleExactIf

        public JSONObject removeDoubleExactIf​(String key,
                                              Predicate<Double> predicate)
        Removes a key from a JSONObject if the predicate returns true.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a double, or if the value does not fit into a double
        Since:
        2.0.0
      • removeObjectIf

        public JSONObject removeObjectIf​(String key,
                                         Predicate<JSONObject> predicate)
        Removes a key from a JSONObject if the predicate returns true.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a JSONObject
        Since:
        2.0.0
      • removeArrayIf

        public JSONObject removeArrayIf​(String key,
                                        Predicate<JSONArray> predicate)
        Removes a key from a JSONObject if the predicate returns true.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a JSONArray
        Since:
        2.0.0
      • removeInstantIf

        public JSONObject removeInstantIf​(String key,
                                          Predicate<Instant> predicate)
        Removes a key from a JSONObject if the predicate returns true.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not an instant
        Since:
        2.0.0
      • removeKeys

        public JSONObject removeKeys​(JSONObject obj)
        Removes all the keys if the same key exists within the other JSONObject too.

        This does not compare the values.

        Parameters:
        obj - the other JSONObject
        Returns:
        this JSONObject
        Since:
        2.0.0
      • retainIf

        public JSONObject retainIf​(BiPredicate<String,​Object> predicate)
        Removes all entries from a JSONObject where the predicate returns false.

        For each entry in the object, the predicate receives the key and its associated value.

        Parameters:
        predicate - the predicate
        Returns:
        this JSONObject
        Since:
        2.0.0
      • retainIf

        public JSONObject retainIf​(String key,
                                   Predicate<Object> predicate)
        Removes a key from a JSONObject if the predicate returns false.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Since:
        2.0.0
      • retainBooleanIf

        public JSONObject retainBooleanIf​(String key,
                                          Predicate<Boolean> predicate)
        Removes a key from a JSONObject if the predicate returns false.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a boolean
        Since:
        2.0.0
      • retainByteIf

        public JSONObject retainByteIf​(String key,
                                       Predicate<Byte> predicate)
        Removes a key from a JSONObject if the predicate returns false.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a byte
        Since:
        2.0.0
      • retainShortIf

        public JSONObject retainShortIf​(String key,
                                        Predicate<Short> predicate)
        Removes a key from a JSONObject if the predicate returns false.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a short
        Since:
        2.0.0
      • retainIntIf

        public JSONObject retainIntIf​(String key,
                                      Predicate<Integer> predicate)
        Removes a key from a JSONObject if the predicate returns false.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not an int
        Since:
        2.0.0
      • retainLongIf

        public JSONObject retainLongIf​(String key,
                                       Predicate<Long> predicate)
        Removes a key from a JSONObject if the predicate returns false.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a long
        Since:
        2.0.0
      • retainFloatIf

        public JSONObject retainFloatIf​(String key,
                                        Predicate<Float> predicate)
        Removes a key from a JSONObject if the predicate returns false.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a float
        Since:
        2.0.0
      • retainDoubleIf

        public JSONObject retainDoubleIf​(String key,
                                         Predicate<Double> predicate)
        Removes a key from a JSONObject if the predicate returns false.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a double
        Since:
        2.0.0
      • retainByteExactIf

        public JSONObject retainByteExactIf​(String key,
                                            Predicate<Byte> predicate)
        Removes a key from a JSONObject if the predicate returns false.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a byte, or if the value does not fit into a byte
        Since:
        2.0.0
      • retainShortExactIf

        public JSONObject retainShortExactIf​(String key,
                                             Predicate<Short> predicate)
        Removes a key from a JSONObject if the predicate returns false.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a short, or if the value does not fit into a short
        Since:
        2.0.0
      • retainIntExactIf

        public JSONObject retainIntExactIf​(String key,
                                           Predicate<Integer> predicate)
        Removes a key from a JSONObject if the predicate returns false.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not an int, or if the value does not fit into an int
        Since:
        2.0.0
      • retainLongExactIf

        public JSONObject retainLongExactIf​(String key,
                                            Predicate<Long> predicate)
        Removes a key from a JSONObject if the predicate returns false.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a long, or if the value does not fit into a long
        Since:
        2.0.0
      • retainFloatExactIf

        public JSONObject retainFloatExactIf​(String key,
                                             Predicate<Float> predicate)
        Removes a key from a JSONObject if the predicate returns false.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a float, or if the value does not fit into a float
        Since:
        2.0.0
      • retainDoubleExactIf

        public JSONObject retainDoubleExactIf​(String key,
                                              Predicate<Double> predicate)
        Removes a key from a JSONObject if the predicate returns false.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a double, or if the value does not fit into a double
        Since:
        2.0.0
      • retainObjectIf

        public JSONObject retainObjectIf​(String key,
                                         Predicate<JSONObject> predicate)
        Removes a key from a JSONObject if the predicate returns false.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a JSONObject
        Since:
        2.0.0
      • retainArrayIf

        public JSONObject retainArrayIf​(String key,
                                        Predicate<JSONArray> predicate)
        Removes a key from a JSONObject if the predicate returns false.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a JSONArray
        Since:
        2.0.0
      • retainInstantIf

        public JSONObject retainInstantIf​(String key,
                                          Predicate<Instant> predicate)
        Removes a key from a JSONObject if the predicate returns false.
        Parameters:
        key - the key
        predicate - the predicate
        Returns:
        this JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not an instant
        Since:
        2.0.0
      • retainKeys

        public JSONObject retainKeys​(JSONObject obj)
        Retains only the keys if the same key exists within the other JSONObject too.

        This does not compare the values.

        Parameters:
        obj - the other JSONObject
        Returns:
        this JSONObject
        Since:
        2.0.0
      • has

        public boolean has​(String key)
        Checks if a key exists within the JSONObject
        Parameters:
        key - the key
        Returns:
        whether the key exists
      • isNull

        public boolean isNull​(String key)
        Checks if the value with the specified key is null
        Parameters:
        key - the key
        Returns:
        whether the value is null
        Throws:
        JSONException - if the key does not exist
      • isBoolean

        public boolean isBoolean​(String key)
        Checks if the value with the specified key is a boolean
        Parameters:
        key - the key
        Returns:
        whether the value is a boolean
        Throws:
        JSONException - if the key does not exist
      • isString

        public boolean isString​(String key)
        Checks if the value with the specified key is a string
        Parameters:
        key - the key
        Returns:
        whether the value is a string
        Throws:
        JSONException - if the key does not exist
      • isNumber

        public boolean isNumber​(String key)
        Checks if the value with the specified key is a number
        Parameters:
        key - the key
        Returns:
        whether the value is a number
        Throws:
        JSONException - if the key does not exist
      • isObject

        public boolean isObject​(String key)
        Checks if the value with the specified key is a JSONObject
        Parameters:
        key - the key
        Returns:
        whether the value is a JSONObject
        Throws:
        JSONException - if the key does not exist
      • isArray

        public boolean isArray​(String key)
        Checks if the value with the specified key is a JSONArray
        Parameters:
        key - the key
        Returns:
        whether the value is a JSONArray
        Throws:
        JSONException - if the key does not exist
      • isInstant

        public boolean isInstant​(String key)
        Checks if the value with the specified key can be converted to an Instant.
        Parameters:
        key - the key
        Returns:
        whether the value is an Instant
        Throws:
        JSONException - if the key does not exist
        Since:
        1.1.0
        See Also:
        parseInstant(Object)
      • get

        public Object get​(String key)
        Returns the value for a given key
        Parameters:
        key - the key
        Returns:
        the value
        Throws:
        JSONException - if the key does not exist
      • getBoolean

        public boolean getBoolean​(String key)
        Returns the value as a boolean for a given key
        Parameters:
        key - the key
        Returns:
        the boolean
        Throws:
        JSONException - if the key does not exist, or if the value is not a boolean
      • getString

        public String getString​(String key)
        Returns the value as a string for a given key
        Parameters:
        key - the key
        Returns:
        the string
        Throws:
        JSONException - if the key does not exist, or if the value is not a string
      • getNumber

        public Number getNumber​(String key)
        Returns the value as a number for a given key
        Parameters:
        key - the key
        Returns:
        the number
        Throws:
        JSONException - if the key does not exist, or if the value is not a number
      • getByte

        public byte getByte​(String key)
        Returns the value as a byte for a given key
        Parameters:
        key - the key
        Returns:
        the byte
        Throws:
        JSONException - if the key does not exist, or if the value is not a byte
      • getShort

        public short getShort​(String key)
        Returns the value as a short for a given key
        Parameters:
        key - the key
        Returns:
        the short
        Throws:
        JSONException - if the key does not exist, or if the value is not a short
      • getInt

        public int getInt​(String key)
        Returns the value as an int for a given key
        Parameters:
        key - the key
        Returns:
        the int
        Throws:
        JSONException - if the key does not exist, or if the value is not an int
      • getLong

        public long getLong​(String key)
        Returns the value as a long for a given key
        Parameters:
        key - the key
        Returns:
        the long
        Throws:
        JSONException - if the key does not exist, or if the value is not a long
      • getFloat

        public float getFloat​(String key)
        Returns the value as a float for a given key
        Parameters:
        key - the key
        Returns:
        the float
        Throws:
        JSONException - if the key does not exist, or if the value is not a float
      • getDouble

        public double getDouble​(String key)
        Returns the value as a double for a given key
        Parameters:
        key - the key
        Returns:
        the double
        Throws:
        JSONException - if the key does not exist, or if the value is not a double
      • getByteExact

        public byte getByteExact​(String key)
        Returns the exact value as a byte for a given key. This fails if the value does not fit into a byte
        Parameters:
        key - the key
        Returns:
        the byte
        Throws:
        JSONException - if the key does not exist, the value is not a byte, or if the value does not fit into a byte
      • getShortExact

        public short getShortExact​(String key)
        Returns the exact value as a short for a given key. This fails if the value does not fit into a short
        Parameters:
        key - the key
        Returns:
        the short
        Throws:
        JSONException - if the key does not exist, the value is not a short, or if the value does not fit into a short
      • getIntExact

        public int getIntExact​(String key)
        Returns the exact value as an int for a given key. This fails if the value does not fit into an int
        Parameters:
        key - the key
        Returns:
        the int
        Throws:
        JSONException - if the key does not exist, the value is not an int, or if the value does not fit into an int
      • getLongExact

        public long getLongExact​(String key)
        Returns the exact value as a long for a given key. This fails if the value does not fit into a long
        Parameters:
        key - the key
        Returns:
        the long
        Throws:
        JSONException - if the key does not exist, the value is not a long, or if the value does not fit into a long
      • getFloatExact

        public float getFloatExact​(String key)
        Returns the exact value as a float for a given key. This fails if the value does not fit into a float
        Parameters:
        key - the key
        Returns:
        the float
        Throws:
        JSONException - if the key does not exist, the value is not a float, or if the value does not fit into a float
      • getDoubleExact

        public double getDoubleExact​(String key)
        Returns the exact value as a double for a given key. This fails if the value does not fit into a double
        Parameters:
        key - the key
        Returns:
        the double
        Throws:
        JSONException - if the key does not exist, the value is not a double, or if the value does not fit into a double
      • getObject

        public JSONObject getObject​(String key)
        Returns the value as a JSONObject for a given key
        Parameters:
        key - the key
        Returns:
        the JSONObject
        Throws:
        JSONException - if the key does not exist, or if the value is not a JSONObject
      • getArray

        public JSONArray getArray​(String key)
        Returns the value as a JSONArray for a given key
        Parameters:
        key - the key
        Returns:
        the JSONArray
        Throws:
        JSONException - if the key does not exist, or if the value is not a JSONArray
      • getInstant

        public Instant getInstant​(String key)
        Returns the value as an Instant for a given key.
        Parameters:
        key - the key
        Returns:
        the Instant
        Throws:
        JSONException - if the key does not exist, or if the value is not an Instant
        Since:
        1.1.0
        See Also:
        parseInstant(Object)
      • get

        public Object get​(String key,
                          Object defaults)
        Returns the value for a given key, or the default value if the operation is not possible
        Parameters:
        key - the key
        defaults - the default value
        Returns:
        the value
      • getBoolean

        public boolean getBoolean​(String key,
                                  boolean defaults)
        Returns the value as a boolean for a given key, or the default value if the operation is not possible
        Parameters:
        key - the key
        defaults - the default value
        Returns:
        the boolean
      • getString

        public String getString​(String key,
                                String defaults)
        Returns the value as a string for a given key, or the default value if the operation is not possible
        Parameters:
        key - the key
        defaults - the default value
        Returns:
        the string
      • getNumber

        public Number getNumber​(String key,
                                Number defaults)
        Returns the value as a number for a given key, or the default value if the operation is not possible
        Parameters:
        key - the key
        defaults - the default value
        Returns:
        the number
      • getByte

        public byte getByte​(String key,
                            byte defaults)
        Returns the value as a byte for a given key, or the default value if the operation is not possible
        Parameters:
        key - the key
        defaults - the default value
        Returns:
        the byte
      • getShort

        public short getShort​(String key,
                              short defaults)
        Returns the value as a short for a given key, or the default value if the operation is not possible
        Parameters:
        key - the key
        defaults - the default value
        Returns:
        the short
      • getInt

        public int getInt​(String key,
                          int defaults)
        Returns the value as an int for a given key, or the default value if the operation is not possible
        Parameters:
        key - the key
        defaults - the default value
        Returns:
        the int
      • getLong

        public long getLong​(String key,
                            long defaults)
        Returns the value as a long for a given key, or the default value if the operation is not possible
        Parameters:
        key - the key
        defaults - the default value
        Returns:
        the long
      • getFloat

        public float getFloat​(String key,
                              float defaults)
        Returns the value as a float for a given key, or the default value if the operation is not possible
        Parameters:
        key - the key
        defaults - the default value
        Returns:
        the float
      • getDouble

        public double getDouble​(String key,
                                double defaults)
        Returns the value as a double for a given key, or the default value if the operation is not possible
        Parameters:
        key - the key
        defaults - the default value
        Returns:
        the double
      • getByteExact

        public byte getByteExact​(String key,
                                 byte defaults)
        Returns the exact value as a byte for a given key, or the default value if the operation is not possible
        Parameters:
        key - the key
        defaults - the default value
        Returns:
        the byte
      • getShortExact

        public short getShortExact​(String key,
                                   short defaults)
        Returns the exact value as a short for a given key, or the default value if the operation is not possible
        Parameters:
        key - the key
        defaults - the default value
        Returns:
        the short
      • getIntExact

        public int getIntExact​(String key,
                               int defaults)
        Returns the exact value as an int for a given key, or the default value if the operation is not possible
        Parameters:
        key - the key
        defaults - the default value
        Returns:
        the int
      • getLongExact

        public long getLongExact​(String key,
                                 long defaults)
        Returns the exact value as a long for a given key, or the default value if the operation is not possible
        Parameters:
        key - the key
        defaults - the default value
        Returns:
        the long
      • getFloatExact

        public float getFloatExact​(String key,
                                   float defaults)
        Returns the exact value as a float for a given key, or the default value if the operation is not possible
        Parameters:
        key - the key
        defaults - the default value
        Returns:
        the float
      • getDoubleExact

        public double getDoubleExact​(String key,
                                     double defaults)
        Returns the exact value as a double for a given key, or the default value if the operation is not possible
        Parameters:
        key - the key
        defaults - the default value
        Returns:
        the double
      • getObject

        public JSONObject getObject​(String key,
                                    JSONObject defaults)
        Returns the value as a JSONObject for a given key, or the default value if the operation is not possible
        Parameters:
        key - the key
        defaults - the default value
        Returns:
        the JSONObject
      • getArray

        public JSONArray getArray​(String key,
                                  JSONArray defaults)
        Returns the value as a JSONArray for a given key, or the default value if the operation is not possible
        Parameters:
        key - the key
        defaults - the default value
        Returns:
        the JSONArray
      • getInstant

        public Instant getInstant​(String key,
                                  Instant defaults)
        Returns the value as an Instant for a given key, or the default value if the operation is not possible
        Parameters:
        key - the key
        defaults - the default value
        Returns:
        the Instant
        Since:
        1.1.0
      • putAll

        public JSONObject putAll​(JSONObject obj)
        Adds the values of the given JSONObject to this JSONObject. Afterwards, changes in nested JSONObjects or JSONArrays of one object are reflected in the other object too.

        This effectively shallow copies one JSONObject into another.

        Parameters:
        obj - the other JSONObject
        Returns:
        this JSONObject
        Since:
        2.0.0
      • putAllDeep

        public JSONObject putAllDeep​(JSONObject obj)
        Adds the values of the given JSONObject to this JSONObject. For all nested JSONObjects and JSONArrays, deep copies are created. Afterwards, changes in nested JSONObjects or JSONArrays of one object are not reflected in the other object.

        This effectively deep copies one JSONObject into another.

        Parameters:
        obj - the other JSONObject
        Returns:
        this JSONObject
        Since:
        2.0.0
      • set

        public JSONObject set​(String key,
                              Object value)
        Sets the value at a given key
        Parameters:
        key - the key
        value - the new value
        Returns:
        this JSONObject
      • setIfAbsent

        public JSONObject setIfAbsent​(String key,
                                      Object value)
        Sets the value at a given key if there is no value associated with the key yet
        Parameters:
        key - the key
        value - the new value
        Returns:
        this JSONObject
        Since:
        2.0.0
      • setIfPresent

        public JSONObject setIfPresent​(String key,
                                       Object value)
        Sets the value at a given key if there is already a value associated with the key
        Parameters:
        key - the key
        value - the new value
        Returns:
        this JSONObject
        Since:
        2.0.0
      • compute

        public JSONObject compute​(String key,
                                  BiFunction<String,​Object,​Object> remappingFunction)
        Replaces or sets the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value, or null if there is currently no value associated with the key.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Since:
        2.0.0
      • computeBoolean

        public JSONObject computeBoolean​(String key,
                                         BiFunction<String,​Boolean,​Object> remappingFunction)
        Replaces or sets the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a boolean, or null if there is currently no value associated with the key.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a boolean
        Since:
        2.0.0
      • computeByte

        public JSONObject computeByte​(String key,
                                      BiFunction<String,​Byte,​Object> remappingFunction)
        Replaces or sets the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a byte, or null if there is currently no value associated with the key.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a byte
        Since:
        2.0.0
      • computeShort

        public JSONObject computeShort​(String key,
                                       BiFunction<String,​Short,​Object> remappingFunction)
        Replaces or sets the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a short, or null if there is currently no value associated with the key.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a short
        Since:
        2.0.0
      • computeInt

        public JSONObject computeInt​(String key,
                                     BiFunction<String,​Integer,​Object> remappingFunction)
        Replaces or sets the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as an int, or null if there is currently no value associated with the key.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not an int
        Since:
        2.0.0
      • computeLong

        public JSONObject computeLong​(String key,
                                      BiFunction<String,​Long,​Object> remappingFunction)
        Replaces or sets the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a long, or null if there is currently no value associated with the key.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a long
        Since:
        2.0.0
      • computeFloat

        public JSONObject computeFloat​(String key,
                                       BiFunction<String,​Float,​Object> remappingFunction)
        Replaces or sets the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a float, or null if there is currently no value associated with the key.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a float
        Since:
        2.0.0
      • computeDouble

        public JSONObject computeDouble​(String key,
                                        BiFunction<String,​Double,​Object> remappingFunction)
        Replaces or sets the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a double, or null if there is currently no value associated with the key.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a double
        Since:
        2.0.0
      • computeByteExact

        public JSONObject computeByteExact​(String key,
                                           BiFunction<String,​Byte,​Object> remappingFunction)
        Replaces or sets the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a byte (exact value), or null if there is currently no value associated with the key.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a byte, or if the value does not fit into a byte
        Since:
        2.0.0
      • computeShortExact

        public JSONObject computeShortExact​(String key,
                                            BiFunction<String,​Short,​Object> remappingFunction)
        Replaces or sets the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a short (exact value), or null if there is currently no value associated with the key.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a short, or if the value does not fit into a short
        Since:
        2.0.0
      • computeIntExact

        public JSONObject computeIntExact​(String key,
                                          BiFunction<String,​Integer,​Object> remappingFunction)
        Replaces or sets the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as an int (exact value), or null if there is currently no value associated with the key.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not an int, or if the value does not fit into an int
        Since:
        2.0.0
      • computeLongExact

        public JSONObject computeLongExact​(String key,
                                           BiFunction<String,​Long,​Object> remappingFunction)
        Replaces or sets the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a long (exact value), or null if there is currently no value associated with the key.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a long, or if the value does not fit into a long
        Since:
        2.0.0
      • computeFloatExact

        public JSONObject computeFloatExact​(String key,
                                            BiFunction<String,​Float,​Object> remappingFunction)
        Replaces or sets the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a float (exact value), or null if there is currently no value associated with the key.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a float, or if the value does not fit into a float
        Since:
        2.0.0
      • computeDoubleExact

        public JSONObject computeDoubleExact​(String key,
                                             BiFunction<String,​Double,​Object> remappingFunction)
        Replaces or sets the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a double (exact value), or null if there is currently no value associated with the key.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a double, or if the value does not fit into a double
        Since:
        2.0.0
      • computeObject

        public JSONObject computeObject​(String key,
                                        BiFunction<String,​JSONObject,​Object> remappingFunction)
        Replaces or sets the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a JSONObject, or null if there is currently no value associated with the key.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a JSONObject
        Since:
        2.0.0
      • computeArray

        public JSONObject computeArray​(String key,
                                       BiFunction<String,​JSONArray,​Object> remappingFunction)
        Replaces or sets the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a JSONArray, or null if there is currently no value associated with the key.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a JSONArray
        Since:
        2.0.0
      • computeInstant

        public JSONObject computeInstant​(String key,
                                         BiFunction<String,​Instant,​Object> remappingFunction)
        Replaces or sets the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as an instant, or null if there is currently no value associated with the key.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not an instant
        Since:
        2.0.0
      • computeIfPresent

        public JSONObject computeIfPresent​(String key,
                                           BiFunction<String,​Object,​Object> remappingFunction)
        Replaces the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Since:
        2.0.0
      • computeBooleanIfPresent

        public JSONObject computeBooleanIfPresent​(String key,
                                                  BiFunction<String,​Boolean,​Object> remappingFunction)
        Replaces the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a boolean.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a boolean
        Since:
        2.0.0
      • computeByteIfPresent

        public JSONObject computeByteIfPresent​(String key,
                                               BiFunction<String,​Byte,​Object> remappingFunction)
        Replaces the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a byte.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a byte
        Since:
        2.0.0
      • computeShortIfPresent

        public JSONObject computeShortIfPresent​(String key,
                                                BiFunction<String,​Short,​Object> remappingFunction)
        Replaces the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a short.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a short
        Since:
        2.0.0
      • computeIntIfPresent

        public JSONObject computeIntIfPresent​(String key,
                                              BiFunction<String,​Integer,​Object> remappingFunction)
        Replaces the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as an int.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not an int
        Since:
        2.0.0
      • computeLongIfPresent

        public JSONObject computeLongIfPresent​(String key,
                                               BiFunction<String,​Long,​Object> remappingFunction)
        Replaces the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a long.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a long
        Since:
        2.0.0
      • computeFloatIfPresent

        public JSONObject computeFloatIfPresent​(String key,
                                                BiFunction<String,​Float,​Object> remappingFunction)
        Replaces the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a float.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a float
        Since:
        2.0.0
      • computeDoubleIfPresent

        public JSONObject computeDoubleIfPresent​(String key,
                                                 BiFunction<String,​Double,​Object> remappingFunction)
        Replaces the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a double.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a double
        Since:
        2.0.0
      • computeByteExactIfPresent

        public JSONObject computeByteExactIfPresent​(String key,
                                                    BiFunction<String,​Byte,​Object> remappingFunction)
        Replaces the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a byte (exact value).

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a byte, or if the value does not fit into a byte
        Since:
        2.0.0
      • computeShortExactIfPresent

        public JSONObject computeShortExactIfPresent​(String key,
                                                     BiFunction<String,​Short,​Object> remappingFunction)
        Replaces the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a short (exact value).

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a short, or if the value does not fit into a short
        Since:
        2.0.0
      • computeIntExactIfPresent

        public JSONObject computeIntExactIfPresent​(String key,
                                                   BiFunction<String,​Integer,​Object> remappingFunction)
        Replaces the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as an int (exact value).

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not an int, or if the value does not fit into an int
        Since:
        2.0.0
      • computeLongExactIfPresent

        public JSONObject computeLongExactIfPresent​(String key,
                                                    BiFunction<String,​Long,​Object> remappingFunction)
        Replaces the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a long (exact value).

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a long, or if the value does not fit into a long
        Since:
        2.0.0
      • computeFloatExactIfPresent

        public JSONObject computeFloatExactIfPresent​(String key,
                                                     BiFunction<String,​Float,​Object> remappingFunction)
        Replaces the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a float (exact value).

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a float, or if the value does not fit into a float
        Since:
        2.0.0
      • computeDoubleExactIfPresent

        public JSONObject computeDoubleExactIfPresent​(String key,
                                                      BiFunction<String,​Double,​Object> remappingFunction)
        Replaces the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a double (exact value).

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a double, or if the value does not fit into a double
        Since:
        2.0.0
      • computeObjectIfPresent

        public JSONObject computeObjectIfPresent​(String key,
                                                 BiFunction<String,​JSONObject,​Object> remappingFunction)
        Replaces the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a JSONObject.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a JSONObject
        Since:
        2.0.0
      • computeArrayIfPresent

        public JSONObject computeArrayIfPresent​(String key,
                                                BiFunction<String,​JSONArray,​Object> remappingFunction)
        Replaces the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as a JSONArray.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not a JSONArray
        Since:
        2.0.0
      • computeInstantIfPresent

        public JSONObject computeInstantIfPresent​(String key,
                                                  BiFunction<String,​Instant,​Object> remappingFunction)
        Replaces the value associated with the given key with the value returned by the remapping function.

        The remapping function receives the key and its associated value as an instant.

        Parameters:
        key - the key
        remappingFunction - the remapping function
        Returns:
        this JSONObject
        Throws:
        JSONException - if the value is not an instant
        Since:
        2.0.0
      • computeIfAbsent

        public JSONObject computeIfAbsent​(String key,
                                          Function<String,​Object> mappingFunction)
        Associates the value returned by the mapping function with the given key if the key is not already associated with a value.

        The mapping function receives the key.

        Parameters:
        key - the key
        mappingFunction - the mapping function
        Returns:
        this JSONObject
        Since:
        2.0.0
      • toString

        public String toString​(int indentFactor)
        Converts the JSONObject into its string representation. The indentation factor enables pretty-printing and defines how many spaces (' ') should be placed before each key/value pair. A factor of < 1 disables pretty-printing and discards any optional whitespace characters.

        indentFactor = 2:

         {
           "key0": "value0",
           "key1": {
             "nested": 123
           },
           "key2": false
         }
         

        indentFactor = 0:

         {"key0":"value0","key1":{"nested":123},"key2":false}
         
        Parameters:
        indentFactor - the indentation factor
        Returns:
        the string representation
        See Also:
        JSONStringify.toString(JSONObject, int)
      • toString

        public String toString()
        Converts the JSONObject into its compact string representation.
        Overrides:
        toString in class Object
        Returns:
        the compact string representation