Module json5

Class JSONObject

  • All Implemented Interfaces:
    java.lang.Iterable<java.util.Map.Entry<java.lang.String,​java.lang.Object>>

    public class JSONObject
    extends java.lang.Object
    implements java.lang.Iterable<java.util.Map.Entry<java.lang.String,​java.lang.Object>>
    A JSONObject is a map (key-value) structure capable of holding multiple values, including other JSONArrays and JSONObjects
    Author:
    SyntaxError404
    • Constructor Summary

      Constructors 
      Constructor Description
      JSONObject()
      Constructs a new JSONObject
      JSONObject​(JSONParser parser)
      Constructs a new JSONObject from a JSONParser
      JSONObject​(java.lang.String source)
      Constructs a new JSONObject from a string
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.Set<java.util.Map.Entry<java.lang.String,​java.lang.Object>> entrySet()
      Returns a set of entries of the JSONObject.
      java.lang.Object get​(java.lang.String key)
      Returns the value for a given key
      java.lang.Object get​(java.lang.String key, java.lang.Object defaults)
      Returns the value for a given key, or the default value if the operation is not possible
      JSONArray getArray​(java.lang.String key)
      Returns the value as a JSONArray for a given key
      JSONArray getArray​(java.lang.String key, JSONArray defaults)
      Returns the value as a JSONArray for a given key, or the default value if the operation is not possible
      boolean getBoolean​(java.lang.String key)
      Returns the value as a boolean for a given key
      boolean getBoolean​(java.lang.String key, boolean defaults)
      Returns the value as a boolean for a given key, or the default value if the operation is not possible
      byte getByte​(java.lang.String key)
      Returns the value as a byte for a given key
      byte getByte​(java.lang.String key, byte defaults)
      Returns the value as a byte for a given key, or the default value if the operation is not possible
      byte getByteExact​(java.lang.String key)
      Returns the exact value as a byte for a given key.
      byte getByteExact​(java.lang.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
      double getDouble​(java.lang.String key)
      Returns the value as a double for a given key
      double getDouble​(java.lang.String key, double defaults)
      Returns the value as a double for a given key, or the default value if the operation is not possible
      double getDoubleExact​(java.lang.String key)
      Returns the exact value as a double for a given key.
      double getDoubleExact​(java.lang.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
      float getFloat​(java.lang.String key)
      Returns the value as a float for a given key
      float getFloat​(java.lang.String key, float defaults)
      Returns the value as a float for a given key, or the default value if the operation is not possible
      float getFloatExact​(java.lang.String key)
      Returns the exact value as a float for a given key.
      float getFloatExact​(java.lang.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
      java.time.Instant getInstant​(java.lang.String key)
      Returns the value as an Instant for a given key
      java.time.Instant getInstant​(java.lang.String key, java.time.Instant defaults)
      Returns the value as an Instant for a given key, or the default value if the operation is not possible
      int getInt​(java.lang.String key)
      Returns the value as an int for a given key
      int getInt​(java.lang.String key, int defaults)
      Returns the value as an int for a given key, or the default value if the operation is not possible
      int getIntExact​(java.lang.String key)
      Returns the exact value as an int for a given key.
      int getIntExact​(java.lang.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
      long getLong​(java.lang.String key)
      Returns the value as a long for a given key
      long getLong​(java.lang.String key, long defaults)
      Returns the value as a long for a given key, or the default value if the operation is not possible
      long getLongExact​(java.lang.String key)
      Returns the exact value as a long for a given key.
      long getLongExact​(java.lang.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
      java.lang.Number getNumber​(java.lang.String key)
      Returns the value as a number for a given key
      java.lang.Number getNumber​(java.lang.String key, java.lang.Number defaults)
      Returns the value as a number for a given key, or the default value if the operation is not possible
      JSONObject getObject​(java.lang.String key)
      Returns the value as a JSONObject for a given key
      JSONObject getObject​(java.lang.String key, JSONObject defaults)
      Returns the value as a JSONObject for a given key, or the default value if the operation is not possible
      short getShort​(java.lang.String key)
      Returns the value as a short for a given key
      short getShort​(java.lang.String key, short defaults)
      Returns the value as a short for a given key, or the default value if the operation is not possible
      short getShortExact​(java.lang.String key)
      Returns the exact value as a short for a given key.
      short getShortExact​(java.lang.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
      java.lang.String getString​(java.lang.String key)
      Returns the value as a string for a given key
      java.lang.String getString​(java.lang.String key, java.lang.String defaults)
      Returns the value as a string for a given key, or the default value if the operation is not possible
      boolean has​(java.lang.String key)
      Checks if a key exists within the JSONObject
      boolean isArray​(java.lang.String key)
      Checks if the value with the specified key is a JSONArray
      boolean isBoolean​(java.lang.String key)
      Checks if the value with the specified key is a boolean
      boolean isInstant​(java.lang.String key)
      Checks if the value with the specified key is an Instant
      boolean isNull​(java.lang.String key)
      Checks if the value with the specified key is null
      boolean isNumber​(java.lang.String key)
      Checks if the value with the specified key is a number
      boolean isObject​(java.lang.String key)
      Checks if the value with the specified key is a JSONObject
      boolean isString​(java.lang.String key)
      Checks if the value with the specified key is a string
      java.util.Iterator<java.util.Map.Entry<java.lang.String,​java.lang.Object>> iterator()  
      java.util.Set<java.lang.String> keySet()
      Returns a set of keys of the JSONObject
      int length()
      Returns the number of entries in the JSONObject
      JSONObject set​(java.lang.String key, java.lang.Object value)
      Sets the value at a given key
      java.util.Map<java.lang.String,​java.lang.Object> toMap()
      Converts the JSONObject into a map.
      java.lang.String toString()
      Converts the JSONObject into its compact string representation.
      java.lang.String toString​(int indentFactor)
      Converts the JSONObject into its string representation.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Constructor Detail

      • JSONObject

        public JSONObject()
        Constructs a new JSONObject
      • JSONObject

        public JSONObject​(java.lang.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

      • toMap

        public java.util.Map<java.lang.String,​java.lang.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 java.util.Set<java.lang.String> keySet()
        Returns a set of keys of the JSONObject
        Returns:
        a set of keys
        See Also:
        Map.keySet()
      • entrySet

        public java.util.Set<java.util.Map.Entry<java.lang.String,​java.lang.Object>> entrySet()
        Returns a set of entries of the JSONObject. Modifying the set or an entry will modify the JSONObject Use with caution.
        Returns:
        a set of entries
        See Also:
        Map.entrySet()
      • iterator

        public java.util.Iterator<java.util.Map.Entry<java.lang.String,​java.lang.Object>> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<java.util.Map.Entry<java.lang.String,​java.lang.Object>>
      • length

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

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

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

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

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

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

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

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

        public boolean isInstant​(java.lang.String key)
        Checks if the value with the specified key is an Instant
        Parameters:
        key - the key
        Returns:
        whether or not the value is an Instant
        Throws:
        JSONException - if the key does not exist
        Since:
        1.1.0
      • get

        public java.lang.Object get​(java.lang.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​(java.lang.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 java.lang.String getString​(java.lang.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 java.lang.Number getNumber​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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 java.time.Instant getInstant​(java.lang.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
      • get

        public java.lang.Object get​(java.lang.String key,
                                    java.lang.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​(java.lang.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 java.lang.String getString​(java.lang.String key,
                                          java.lang.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 java.lang.Number getNumber​(java.lang.String key,
                                          java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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 java.time.Instant getInstant​(java.lang.String key,
                                            java.time.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
      • set

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

        public java.lang.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 java.lang.String toString()
        Converts the JSONObject into its compact string representation.
        Overrides:
        toString in class java.lang.Object
        Returns:
        the compact string representation