Module json5

Class JSONObject

    • Constructor Summary

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

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

      • 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
        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.
        Returns:
        a set of entries
        See Also:
        Map.entrySet()
      • length

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

        public boolean has​(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​(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​(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​(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​(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​(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​(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
      • 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
      • 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 exact 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 exact 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
      • set

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

        public void set​(String key,
                        boolean value)
        Sets the value at a given key
        Parameters:
        key - the key
        value - the new value
      • set

        public void set​(String key,
                        String value)
        Sets the value at a given key
        Parameters:
        key - the key
        value - the new value
      • set

        public void set​(String key,
                        Number value)
        Sets the value at a given key
        Parameters:
        key - the key
        value - the new value
      • set

        public void set​(String key,
                        byte value)
        Sets the value at a given key
        Parameters:
        key - the key
        value - the new value
      • set

        public void set​(String key,
                        short value)
        Sets the value at a given key
        Parameters:
        key - the key
        value - the new value
      • set

        public void set​(String key,
                        int value)
        Sets the value at a given key
        Parameters:
        key - the key
        value - the new value
      • set

        public void set​(String key,
                        long value)
        Sets the value at a given key
        Parameters:
        key - the key
        value - the new value
      • set

        public void set​(String key,
                        float value)
        Sets the value at a given key
        Parameters:
        key - the key
        value - the new value
      • set

        public void set​(String key,
                        double value)
        Sets the value at a given key
        Parameters:
        key - the key
        value - the new value
      • set

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

        public void set​(String key,
                        JSONArray value)
        Sets the value at a given key
        Parameters:
        key - the key
        value - the new value
      • 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