Module json5

Class JSONObject

java.lang.Object
at.syntaxerror.json5.JSONObject
All Implemented Interfaces:
Iterable<Map.Entry<String,Object>>

public class JSONObject extends Object implements Iterable<Map.Entry<String,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
    Constructs a new JSONObject
    Constructs a new JSONObject from a JSONParser
    Constructs a new JSONObject from a string
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Removes all values from this JSONObject
    Returns a set of entries of the JSONObject.
    get(String key)
    Returns the value for a given key
    get(String key, Object defaults)
    Returns the value for a given key, or the default value if the operation is not possible
    Returns the value as a JSONArray for a given key
    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
    boolean
    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
    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
    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
    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
    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
    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
    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
    Returns the value as an Instant for a given key
    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
    int
    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
    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
    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
    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
    Returns the value as a number for a given key
    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
    Returns the value as a JSONObject for a given key
    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
    short
    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
    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
    Returns the value as a string for a given key
    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
    Checks if the value with the specified key is a JSONArray
    boolean
    Checks if the value with the specified key is a boolean
    boolean
    Checks if the value with the specified key is an Instant
    boolean
    Checks if the value with the specified key is null
    boolean
    Checks if the value with the specified key is a number
    boolean
    Checks if the value with the specified key is a JSONObject
    boolean
    Checks if the value with the specified key is a string
     
    Returns a set of keys of the JSONObject
    int
    Returns the number of entries in the JSONObject
    void
    Removes a key from a JSONObject
    set(String key, Object value)
    Sets the value at a given key
    Converts the JSONObject into a map.
    Converts the JSONObject into its compact string representation.
    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 Details

    • 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 Details

    • 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:
    • 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:
    • iterator

      public Iterator<Map.Entry<String,Object>> iterator()
      Specified by:
      iterator in interface Iterable<Map.Entry<String,Object>>
    • 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
    • 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 is 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
    • 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
    • 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
    • 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
    • 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:
    • toString

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