- java.lang.Object
-
- at.syntaxerror.json5.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 otherJSONArraysand JSONObjects- Author:
- SyntaxError404
-
-
Constructor Summary
Constructors Constructor Description JSONObject()Constructs a new JSONObjectJSONObject(JSONParser parser)Constructs a new JSONObject from a JSONParserJSONObject(java.lang.String source)Constructs a new JSONObject from a string
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Removes all values from this JSONObjectjava.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>>entrySet()Returns a set of entries of the JSONObject.java.lang.Objectget(java.lang.String key)Returns the value for a given keyjava.lang.Objectget(java.lang.String key, java.lang.Object defaults)Returns the value for a given key, or the default value if the operation is not possibleJSONArraygetArray(java.lang.String key)Returns the value as a JSONArray for a given keyJSONArraygetArray(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 possiblebooleangetBoolean(java.lang.String key)Returns the value as a boolean for a given keybooleangetBoolean(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 possiblebytegetByte(java.lang.String key)Returns the value as a byte for a given keybytegetByte(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 possiblebytegetByteExact(java.lang.String key)Returns the exact value as a byte for a given key.bytegetByteExact(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 possibledoublegetDouble(java.lang.String key)Returns the value as a double for a given keydoublegetDouble(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 possibledoublegetDoubleExact(java.lang.String key)Returns the exact value as a double for a given key.doublegetDoubleExact(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 possiblefloatgetFloat(java.lang.String key)Returns the value as a float for a given keyfloatgetFloat(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 possiblefloatgetFloatExact(java.lang.String key)Returns the exact value as a float for a given key.floatgetFloatExact(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 possiblejava.time.InstantgetInstant(java.lang.String key)Returns the value as an Instant for a given keyjava.time.InstantgetInstant(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 possibleintgetInt(java.lang.String key)Returns the value as an int for a given keyintgetInt(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 possibleintgetIntExact(java.lang.String key)Returns the exact value as an int for a given key.intgetIntExact(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 possiblelonggetLong(java.lang.String key)Returns the value as a long for a given keylonggetLong(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 possiblelonggetLongExact(java.lang.String key)Returns the exact value as a long for a given key.longgetLongExact(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 possiblejava.lang.NumbergetNumber(java.lang.String key)Returns the value as a number for a given keyjava.lang.NumbergetNumber(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 possibleJSONObjectgetObject(java.lang.String key)Returns the value as a JSONObject for a given keyJSONObjectgetObject(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 possibleshortgetShort(java.lang.String key)Returns the value as a short for a given keyshortgetShort(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 possibleshortgetShortExact(java.lang.String key)Returns the exact value as a short for a given key.shortgetShortExact(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 possiblejava.lang.StringgetString(java.lang.String key)Returns the value as a string for a given keyjava.lang.StringgetString(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 possiblebooleanhas(java.lang.String key)Checks if a key exists within the JSONObjectbooleanisArray(java.lang.String key)Checks if the value with the specified key is a JSONArraybooleanisBoolean(java.lang.String key)Checks if the value with the specified key is a booleanbooleanisInstant(java.lang.String key)Checks if the value with the specified key is an InstantbooleanisNull(java.lang.String key)Checks if the value with the specified key isnullbooleanisNumber(java.lang.String key)Checks if the value with the specified key is a numberbooleanisObject(java.lang.String key)Checks if the value with the specified key is a JSONObjectbooleanisString(java.lang.String key)Checks if the value with the specified key is a stringjava.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 JSONObjectintlength()Returns the number of entries in the JSONObjectvoidremove(java.lang.String key)Removes a key from a JSONObjectJSONObjectset(java.lang.String key, java.lang.Object value)Sets the value at a given keyjava.util.Map<java.lang.String,java.lang.Object>toMap()Converts the JSONObject into a map.java.lang.StringtoString()Converts the JSONObject into its compact string representation.java.lang.StringtoString(int indentFactor)Converts the JSONObject into its string representation.
-
-
-
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:
iteratorin interfacejava.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
-
clear
public void clear()
Removes all values from this JSONObject- Since:
- 1.2.0
-
remove
public void remove(java.lang.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(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 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(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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keydefaults- 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 keyvalue- 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< 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)
-
toString
public java.lang.String toString()
Converts the JSONObject into its compact string representation.- Overrides:
toStringin classjava.lang.Object- Returns:
- the compact string representation
-
-