- java.lang.Object
-
- at.syntaxerror.json5.JSONObject
-
-
Constructor Summary
Constructors Constructor Description JSONObject()Constructs a new JSONObjectJSONObject(JSONParser parser)Constructs a new JSONObject from a JSONParserJSONObject(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.Objectget(String key)Returns the value for a given keyObjectget(String key, Object defaults)Returns the value for a given key, or the default value if the operation is not possibleJSONArraygetArray(String key)Returns the value as a JSONArray for a given keyJSONArraygetArray(String key, JSONArray defaults)Returns the exact value as a JSONArray for a given key, or the default value if the operation is not possiblebooleangetBoolean(String key)Returns the value as a boolean for a given keybooleangetBoolean(String key, boolean defaults)Returns the value as a boolean for a given key, or the default value if the operation is not possiblebytegetByte(String key)Returns the value as a byte for a given keybytegetByte(String key, byte defaults)Returns the value as a byte for a given key, or the default value if the operation is not possiblebytegetByteExact(String key)Returns the exact value as a byte for a given key.bytegetByteExact(String key, byte defaults)Returns the exact value as a byte for a given key, or the default value if the operation is not possibledoublegetDouble(String key)Returns the value as a double for a given keydoublegetDouble(String key, double defaults)Returns the value as a double for a given key, or the default value if the operation is not possibledoublegetDoubleExact(String key)Returns the exact value as a double for a given key.doublegetDoubleExact(String key, double defaults)Returns the exact value as a double for a given key, or the default value if the operation is not possiblefloatgetFloat(String key)Returns the value as a float for a given keyfloatgetFloat(String key, float defaults)Returns the value as a float for a given key, or the default value if the operation is not possiblefloatgetFloatExact(String key)Returns the exact value as a float for a given key.floatgetFloatExact(String key, float defaults)Returns the exact value as a float for a given key, or the default value if the operation is not possibleintgetInt(String key)Returns the value as an int for a given keyintgetInt(String key, int defaults)Returns the value as an int for a given key, or the default value if the operation is not possibleintgetIntExact(String key)Returns the exact value as an int for a given key.intgetIntExact(String key, int defaults)Returns the exact value as an int for a given key, or the default value if the operation is not possiblelonggetLong(String key)Returns the value as a long for a given keylonggetLong(String key, long defaults)Returns the value as a long for a given key, or the default value if the operation is not possiblelonggetLongExact(String key)Returns the exact value as a long for a given key.longgetLongExact(String key, long defaults)Returns the exact value as a long for a given key, or the default value if the operation is not possibleNumbergetNumber(String key)Returns the value as a number for a given keyNumbergetNumber(String key, Number defaults)Returns the value as a number for a given key, or the default value if the operation is not possibleJSONObjectgetObject(String key)Returns the value as a JSONObject for a given keyJSONObjectgetObject(String key, JSONObject defaults)Returns the exact value as a JSONObject for a given key, or the default value if the operation is not possibleshortgetShort(String key)Returns the value as a short for a given keyshortgetShort(String key, short defaults)Returns the value as a short for a given key, or the default value if the operation is not possibleshortgetShortExact(String key)Returns the exact value as a short for a given key.shortgetShortExact(String key, short defaults)Returns the exact value as a short for a given key, or the default value if the operation is not possibleStringgetString(String key)Returns the value as a string for a given keyStringgetString(String key, String defaults)Returns the value as a string for a given key, or the default value if the operation is not possiblebooleanhas(String key)Checks if a key exists within the JSONObjectbooleanisArray(String key)Checks if the value with the specified key is a JSONArraybooleanisBoolean(String key)Checks if the value with the specified key is a booleanbooleanisNull(String key)Checks if the value with the specified key isnullbooleanisNumber(String key)Checks if the value with the specified key is a numberbooleanisObject(String key)Checks if the value with the specified key is a JSONObjectbooleanisString(String key)Checks if the value with the specified key is a stringIterator<Map.Entry<String,Object>>iterator()Set<String>keySet()Returns a set of keys of the JSONObjectintlength()Returns the number of entries in the JSONObjectvoidset(String key, boolean value)Sets the value at a given keyvoidset(String key, byte value)Sets the value at a given keyvoidset(String key, double value)Sets the value at a given keyvoidset(String key, float value)Sets the value at a given keyvoidset(String key, int value)Sets the value at a given keyvoidset(String key, long value)Sets the value at a given keyvoidset(String key, short value)Sets the value at a given keyvoidset(String key, JSONArray value)Sets the value at a given keyvoidset(String key, JSONObject value)Sets the value at a given keyvoidset(String key, Number value)Sets the value at a given keyvoidset(String key, Object value)Sets the value at a given keyvoidset(String key, String value)Sets the value at a given keyMap<String,Object>toMap()Converts the JSONObject into a map.StringtoString()Converts the JSONObject into its compact string representation.StringtoString(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(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 isnull- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- the default value- Returns:
- the JSONArray
-
set
public void set(String key, Object value)
Sets the value at a given key- Parameters:
key- the keyvalue- the new value
-
set
public void set(String key, boolean value)
Sets the value at a given key- Parameters:
key- the keyvalue- the new value
-
set
public void set(String key, String value)
Sets the value at a given key- Parameters:
key- the keyvalue- the new value
-
set
public void set(String key, Number value)
Sets the value at a given key- Parameters:
key- the keyvalue- the new value
-
set
public void set(String key, byte value)
Sets the value at a given key- Parameters:
key- the keyvalue- the new value
-
set
public void set(String key, short value)
Sets the value at a given key- Parameters:
key- the keyvalue- the new value
-
set
public void set(String key, int value)
Sets the value at a given key- Parameters:
key- the keyvalue- the new value
-
set
public void set(String key, long value)
Sets the value at a given key- Parameters:
key- the keyvalue- the new value
-
set
public void set(String key, float value)
Sets the value at a given key- Parameters:
key- the keyvalue- the new value
-
set
public void set(String key, double value)
Sets the value at a given key- Parameters:
key- the keyvalue- the new value
-
set
public void set(String key, JSONObject value)
Sets the value at a given key- Parameters:
key- the keyvalue- the new value
-
set
public void set(String key, JSONArray value)
Sets the value at a given key- Parameters:
key- the keyvalue- 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< 1disables 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)
-
-