Package kong.unirest.core.json
Class JSONObject
- java.lang.Object
-
- kong.unirest.core.json.JSONElement
-
- kong.unirest.core.json.JSONObject
-
public class JSONObject extends JSONElement
https://json.org/ https://tools.ietf.org/html/rfc7159#section-4 represents a JSON Object
-
-
Field Summary
Fields Modifier and Type Field Description static Object
NULL
-
Fields inherited from class kong.unirest.core.json.JSONElement
MAPPER
-
-
Constructor Summary
Constructors Constructor Description JSONObject()
an empty JSON objectJSONObject(Object object)
construct using an object.JSONObject(String string)
https://tools.ietf.org/html/rfc7159#section-4JSONObject(Map<String,Object> map)
construct using a map
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JSONObject
accumulate(String key, Object additionalValue)
Add a element to a JSONArray in a element.JSONObject
append(String key, Object value)
appends to an existing arraystatic String
doubleToString(double d)
convert a primitive number to a string if the double can be converted to a whole number the decimal will be droppedboolean
equals(Object other)
Object
get(String key)
get and element by key as its native objectBigDecimal
getBigDecimal(String key)
get an element property as a BigDecimalBigInteger
getBigInteger(String key)
get an element property as a BigIntegerboolean
getBoolean(String key)
gets a boolean value at a particular keydouble
getDouble(String key)
get the value as a double<T extends Enum<T>>
TgetEnum(Class<T> enumClass, String key)
get element as a enum valuefloat
getFloat(String key)
get the value as a floatint
getInt(String key)
get an element property as a intJSONArray
getJSONArray(String key)
get the element as a JSONArrayJSONObject
getJSONObject(String key)
get the element as a JSONObjectlong
getLong(String key)
get the value as a longstatic String[]
getNames(Object o)
get all of the keys of a JSONObject or a empty array if not an JSONObjectstatic String[]
getNames(JSONObject jsonObject)
get all of the keys of a JSONObjectNumber
getNumber(String key)
get an element property as a NumberString
getString(String key)
get a element property as a stringboolean
has(String key)
int
hashCode()
JSONObject
increment(String key)
increments a numeric value by 1, or creates it with a value of 1 if it does not exist.boolean
isEmpty()
boolean
isNull(String key)
indicate if the key does not exist or its value is nullIterator<String>
keys()
get a iterator for the keysetSet<String>
keySet()
get all the keys as a setint
length()
JSONArray
names()
get the key names as a JSONArraystatic String
numberToString(Number number)
Convert a number to a stringObject
opt(String key)
optionally return the object or null if it doesn't existBigDecimal
optBigDecimal(String key, BigDecimal defaultValue)
get the value as a BigDecimal or default valueBigInteger
optBigInteger(String key, BigInteger defaultValue)
get the value as a BigInteger or default valueboolean
optBoolean(String key)
gets a boolean value at a particular key or false as defaultboolean
optBoolean(String key, boolean defaultValue)
gets a boolean value at a particular key or a default valuedouble
optDouble(String key)
the value as double or NaNdouble
optDouble(String key, double defaultValue)
get the value as a double or default value<T extends Enum<T>>
ToptEnum(Class<T> enumClass, String key)
get element as a enum value or null if the value cannot be mapped<T extends Enum<T>>
ToptEnum(Class<T> enumClass, String key, T defaultValue)
get element as a enum value or a default value if the value cannot be mappedfloat
optFloat(String key)
the value as double or NaNfloat
optFloat(String key, float defaultValue)
get the value as a float or default valueint
optInt(String key)
the value as int or NaNint
optInt(String key, int defaultValue)
get the value as a int or default valueJSONArray
optJSONArray(String key)
optionally get the element as a JSONArrayJSONObject
optJSONObject(String key)
get the element as a JSONObjectlong
optLong(String key)
the value as long or NaNlong
optLong(String key, long defaultValue)
get the value as a long or default valueNumber
optNumber(String key)
the value as int or 0Number
optNumber(String key, Number defaultValue)
get the value as a Number or default valueString
optString(String key)
get a element property as a stringString
optString(String key, String defaultValue)
get a element property as a stringJSONObject
put(String key, boolean value)
put a boolean at a particular keyJSONObject
put(String key, double value)
put a double at a particular keyJSONObject
put(String key, float value)
put a float at a particular keyJSONObject
put(String key, int value)
put a int at a particular keyJSONObject
put(String key, long value)
put a long at a particular keyJSONObject
put(String key, Number value)
put a Number at a particular keyJSONObject
put(String key, Object value)
put an object to a key.JSONObject
put(String key, String value)
put a String at a particular keyJSONObject
put(String key, Collection value)
put a Collection as a JSONArray at a particular keyJSONObject
put(String key, Map value)
put a Collection as a JSONArray at a particular keyJSONObject
put(String key, JSONArray array)
put a JSONArray at a particular keyJSONObject
put(String key, JSONObject object)
put a JSONObject at a particular key<T extends Enum<T>>
JSONObjectput(String key, T enumvalue)
put a enum at a particular key.JSONObject
putOnce(String key, Object value)
put a value to a key only if it does not existJSONObject
putOpt(String key, Object value)
optional put a value at a key as long as both they key and value are not null otherwise it does nothingstatic String
quote(String s)
quite escape a stringstatic Writer
quote(String s, Writer writer)
quite escape a stringObject
remove(String key)
remove a element by key nameboolean
similar(Object o)
indicates if a JSONObject has the same elements as another JSONObjectstatic Object
stringToValue(String str)
convert a primitive JSON type in a string (bool, number, null) to its primitive type all decimal types will become doublesJSONArray
toJSONArray(JSONArray names)
creates an array of the values for they keys you provideMap<String,Object>
toMap()
converts this object to a mapString
toString()
String
toString(int i)
render the object as a JSON Stringstatic String
valueToString(Object o)
Converts an object to a JSON Stringstatic Object
wrap(Object obj)
Convert an object to a object that can be added to a JSONElement If the object is null return the NULL object If the object is primitive return the original object If the object is a map convert it to a JSONObject If the object is a Collection or array return a JSONArray If the object is anything else return a empty JSON Object-
Methods inherited from class kong.unirest.core.json.JSONElement
getElement, optQuery, optQuery, query, query, toMap, toPrettyJson, write, write
-
-
-
-
Field Detail
-
NULL
public static final Object NULL
-
-
Constructor Detail
-
JSONObject
public JSONObject(String string)
https://tools.ietf.org/html/rfc7159#section-4- Parameters:
string
- a json object string
-
JSONObject
public JSONObject(Map<String,Object> map)
construct using a map- Parameters:
map
- a map representing the elements of a JSON Object
-
JSONObject
public JSONObject(Object object)
construct using an object. The Properties of the JSONObject will be taken from getters and properties of the object- Parameters:
object
- the object to turn into a JSONObject
-
JSONObject
public JSONObject()
an empty JSON object
-
-
Method Detail
-
quote
public static String quote(String s)
quite escape a string- Parameters:
s
- a string- Returns:
- a quoted string
-
quote
public static Writer quote(String s, Writer writer) throws IOException
quite escape a string- Parameters:
s
- a stringwriter
- a writer to write the string to- Returns:
- the same writer
- Throws:
IOException
- if some IO thing goes wrong
-
stringToValue
public static Object stringToValue(String str)
convert a primitive JSON type in a string (bool, number, null) to its primitive type all decimal types will become doubles- Parameters:
str
- a string- Returns:
- a object
-
wrap
public static Object wrap(Object obj)
Convert an object to a object that can be added to a JSONElement If the object is null return the NULL object If the object is primitive return the original object If the object is a map convert it to a JSONObject If the object is a Collection or array return a JSONArray If the object is anything else return a empty JSON Object- Parameters:
obj
- the object- Returns:
- another object suitable for use as JSON
-
doubleToString
public static String doubleToString(double d)
convert a primitive number to a string if the double can be converted to a whole number the decimal will be dropped- Parameters:
d
- a double- Returns:
- a string representation of the double
-
numberToString
public static String numberToString(Number number) throws JSONException
Convert a number to a string- Parameters:
number
- the number to convert- Returns:
- a string representation of that number
- Throws:
JSONException
- if something goes wrong
-
valueToString
public static String valueToString(Object o) throws JSONException
Converts an object to a JSON String- Parameters:
o
- any object- Returns:
- a json string
- Throws:
JSONException
- if something goes wrong
-
getNames
public static String[] getNames(JSONObject jsonObject)
get all of the keys of a JSONObject- Parameters:
jsonObject
- a JSONObject- Returns:
- a String[] of the objects keys
-
getNames
public static String[] getNames(Object o)
get all of the keys of a JSONObject or a empty array if not an JSONObject- Parameters:
o
- a Object- Returns:
- a String[] of the objects keys
-
toString
public String toString()
-
toString
public String toString(int i) throws JSONException
render the object as a JSON String- Parameters:
i
- (ignored due to limitations in gson which uses a hardcoded indentation)- Returns:
- a JSON String
- Throws:
JSONException
-
similar
public boolean similar(Object o)
indicates if a JSONObject has the same elements as another JSONObject- Parameters:
o
- another object- Returns:
- a bool
-
has
public boolean has(String key)
- Parameters:
key
- the key element to operate on- Returns:
- indicates that the structure has this key
-
length
public int length()
- Returns:
- number of keys in the structure
-
get
public Object get(String key) throws JSONException
get and element by key as its native object- Parameters:
key
- the key element to operate on- Returns:
- the object, this could be an object, array or primitive
- Throws:
JSONException
- if the key does not exist
-
getJSONObject
public JSONObject getJSONObject(String key) throws JSONException
get the element as a JSONObject- Parameters:
key
- the key element to operate on- Returns:
- the element as a JSONObject
- Throws:
JSONException
- if it is not a object or the key does not exist
-
optJSONObject
public JSONObject optJSONObject(String key)
get the element as a JSONObject- Parameters:
key
- the key element to operate on- Returns:
- an object or null if it is not an object or the key does not exist
-
getJSONArray
public JSONArray getJSONArray(String key) throws JSONException
get the element as a JSONArray- Parameters:
key
- the key element to operate on- Returns:
- the element as a JSONArray
- Throws:
JSONException
- if it is not an array or the key does not exist
-
optJSONArray
public JSONArray optJSONArray(String key)
optionally get the element as a JSONArray- Parameters:
key
- the key element to operate on- Returns:
- the element as a JSONArray or null if it doesn't exist or is not an array
-
getString
public String getString(String key) throws JSONException
get a element property as a string- Parameters:
key
- the key element to operate on- Returns:
- a string representation of the value
- Throws:
JSONException
- if the key does not exist
-
optString
public String optString(String key)
get a element property as a string- Parameters:
key
- the key element to operate on- Returns:
- a string representation of the value or null of it doesn't exist
-
optString
public String optString(String key, String defaultValue)
get a element property as a string- Parameters:
key
- the key element to operate ondefaultValue
- default value if the key does not exist or cannot be converted to a string- Returns:
- a string representation of the value or default value
-
getDouble
public double getDouble(String key) throws JSONException
get the value as a double- Parameters:
key
- the key element to operate on- Returns:
- the value
- Throws:
JSONException
- if the object is not a number or does not exist
-
optDouble
public double optDouble(String key)
the value as double or NaN- Parameters:
key
- the key element to operate on- Returns:
- the value as a double or NaN if the key doesn't exist or the value is not a number
-
optDouble
public double optDouble(String key, double defaultValue)
get the value as a double or default value- Parameters:
key
- the key element to operate ondefaultValue
- the default value to return if the index or value type are not valid- Returns:
- return value as double or a default value if value is not viable
-
getFloat
public float getFloat(String key) throws JSONException
get the value as a float- Parameters:
key
- the key element to operate on- Returns:
- the value
- Throws:
JSONException
- if the object is not a number or does not exist
-
optFloat
public float optFloat(String key)
the value as double or NaN- Parameters:
key
- the key element to operate on- Returns:
- the value as a float or NaN if the key doesn't exist or the value is not a number
-
optFloat
public float optFloat(String key, float defaultValue)
get the value as a float or default value- Parameters:
key
- the key element to operate ondefaultValue
- the default value to return if the index or value type are not valid- Returns:
- return value as double or a default value if value is not viable
-
getLong
public long getLong(String key) throws JSONException
get the value as a long- Parameters:
key
- the key element to operate on- Returns:
- the value
- Throws:
JSONException
- if the object is not a number or does not exist
-
optLong
public long optLong(String key)
the value as long or NaN- Parameters:
key
- the key element to operate on- Returns:
- the value as a long or NaN if the key doesn't exist or the value is not a number
-
optLong
public long optLong(String key, long defaultValue)
get the value as a long or default value- Parameters:
key
- the key element to operate ondefaultValue
- the default value to return if the index or value type are not valid- Returns:
- return value as long or a default value if value is not viable
-
getNumber
public Number getNumber(String key) throws JSONException
get an element property as a Number- Parameters:
key
- the key element to operate on- Returns:
- the element as a Number if it can be cast to one.
- Throws:
JSONException
- if it is not a number or the key does not exist
-
optNumber
public Number optNumber(String key)
the value as int or 0- Parameters:
key
- the key element to operate on- Returns:
- the value as a int or 0 if the key doesn't exist or the value is not a number
-
optNumber
public Number optNumber(String key, Number defaultValue)
get the value as a Number or default value- Parameters:
key
- the key element to operate ondefaultValue
- the default value to return if the index or value type are not valid- Returns:
- return value as long or a default value if value is not viable
-
getInt
public int getInt(String key) throws JSONException
get an element property as a int- Parameters:
key
- the key element to operate on- Returns:
- the element as a int if it can be cast to one.
- Throws:
JSONException
- if it is not a number or the key does not exist
-
optInt
public int optInt(String key)
the value as int or NaN- Parameters:
key
- the key element to operate on- Returns:
- the value as a int or 0 if the key doesn't exist or the value is not a number
-
optInt
public int optInt(String key, int defaultValue)
get the value as a int or default value- Parameters:
key
- the key element to operate ondefaultValue
- the default value to return if the index or value type are not valid- Returns:
- return value as long or a default value if value is not viable
-
getBigInteger
public BigInteger getBigInteger(String key) throws JSONException
get an element property as a BigInteger- Parameters:
key
- the key element to operate on- Returns:
- the element as a BigInteger if it can be cast to one.
- Throws:
JSONException
- if it is not a number or the key does not exist
-
optBigInteger
public BigInteger optBigInteger(String key, BigInteger defaultValue)
get the value as a BigInteger or default value- Parameters:
key
- the key element to operate ondefaultValue
- the default value to return if the index or value type are not valid- Returns:
- return value as BigInteger or a default value if value is not viable
-
getBigDecimal
public BigDecimal getBigDecimal(String key) throws JSONException
get an element property as a BigDecimal- Parameters:
key
- the key element to operate on- Returns:
- the element as a BigInteger if it can be cast to one.
- Throws:
JSONException
- if it is not a number or the key does not exist
-
optBigDecimal
public BigDecimal optBigDecimal(String key, BigDecimal defaultValue)
get the value as a BigDecimal or default value- Parameters:
key
- the key element to operate ondefaultValue
- the default value to return if the index or value type are not valid- Returns:
- return value as BigDecimal or a default value if value is not viable
-
getBoolean
public boolean getBoolean(String key) throws JSONException
gets a boolean value at a particular key- Parameters:
key
- the key- Returns:
- a boolean
- Throws:
JSONException
- if the element does not exist or is not a boolean
-
optBoolean
public boolean optBoolean(String key)
gets a boolean value at a particular key or false as default- Parameters:
key
- the key- Returns:
- a boolean
-
optBoolean
public boolean optBoolean(String key, boolean defaultValue)
gets a boolean value at a particular key or a default value- Parameters:
key
- the keydefaultValue
- a default value if the key does not exist or value is not a boolean- Returns:
- a boolean
-
getEnum
public <T extends Enum<T>> T getEnum(Class<T> enumClass, String key) throws JSONException
get element as a enum value- Type Parameters:
T
- the type of enum you want- Parameters:
enumClass
- a enum classkey
- the key element to operate on- Returns:
- the value as a enum of T
- Throws:
JSONException
- if it does not map to a enum of T or the key does not exist
-
optEnum
public <T extends Enum<T>> T optEnum(Class<T> enumClass, String key)
get element as a enum value or null if the value cannot be mapped- Type Parameters:
T
- the type of enum you want- Parameters:
enumClass
- a enum classkey
- the key element to operate on- Returns:
- the value as a enum of T
-
optEnum
public <T extends Enum<T>> T optEnum(Class<T> enumClass, String key, T defaultValue)
get element as a enum value or a default value if the value cannot be mapped- Type Parameters:
T
- the type of enum you want- Parameters:
enumClass
- a enum classkey
- the key element to operate ondefaultValue
- the default value to return if the index or value type are not valid- Returns:
- the value as a enum of T
-
put
public JSONObject put(String key, JSONObject object) throws JSONException
put a JSONObject at a particular key- Parameters:
key
- the key element to operate onobject
- JSONObject- Returns:
- this JSONObject
- Throws:
JSONException
-
put
public JSONObject put(String key, JSONArray array) throws JSONException
put a JSONArray at a particular key- Parameters:
key
- the key element to operate onarray
- JSONArray- Returns:
- this JSONObject
- Throws:
JSONException
-
put
public JSONObject put(String key, boolean value) throws JSONException
put a boolean at a particular key- Parameters:
key
- the key element to operate onvalue
- the boolean value to put- Returns:
- this JSONObject
- Throws:
JSONException
- if something goes wrong
-
put
public JSONObject put(String key, Number value) throws JSONException
put a Number at a particular key- Parameters:
key
- the key element to operate onvalue
- Number- Returns:
- this JSONObject
- Throws:
JSONException
-
put
public JSONObject put(String key, double value) throws JSONException
put a double at a particular key- Parameters:
key
- the key element to operate onvalue
- double- Returns:
- this JSONObject
- Throws:
JSONException
- if something goes wrong
-
put
public JSONObject put(String key, float value) throws JSONException
put a float at a particular key- Parameters:
key
- the key element to operate onvalue
- float- Returns:
- this JSONObject
- Throws:
JSONException
- if something goes wrong
-
put
public JSONObject put(String key, long value) throws JSONException
put a long at a particular key- Parameters:
key
- the key element to operate onvalue
- long- Returns:
- this JSONObject
- Throws:
JSONException
- if something goes wrong
-
put
public JSONObject put(String key, int value) throws JSONException
put a int at a particular key- Parameters:
key
- the key element to operate onvalue
- int- Returns:
- this JSONObject
- Throws:
JSONException
- if something goes wrong
-
put
public JSONObject put(String key, String value) throws JSONException
put a String at a particular key- Parameters:
key
- the key element to operate onvalue
- Number- Returns:
- this JSONObject
- Throws:
JSONException
-
put
public JSONObject put(String key, Collection value) throws JSONException
put a Collection as a JSONArray at a particular key- Parameters:
key
- the key element to operate onvalue
- Collection- Returns:
- this JSONObject
- Throws:
JSONException
-
put
public JSONObject put(String key, Map value) throws JSONException
put a Collection as a JSONArray at a particular key- Parameters:
key
- the key element to operate onvalue
- Collection- Returns:
- this JSONObject
- Throws:
JSONException
-
put
public <T extends Enum<T>> JSONObject put(String key, T enumvalue) throws JSONException
put a enum at a particular key. The enum will be stored as a string by name- Type Parameters:
T
- a type of enum- Parameters:
key
- the key element to operate onenumvalue
- a enum- Returns:
- this JSONObject
- Throws:
JSONException
- if something goes wrong
-
remove
public Object remove(String key)
remove a element by key name- Parameters:
key
- the key element to operate on- Returns:
- the object value that was removed
-
accumulate
public JSONObject accumulate(String key, Object additionalValue) throws JSONException
Add a element to a JSONArray in a element. If the value is not already an array it will be made one with the original value as the first element- Parameters:
key
- the key element to operate onadditionalValue
- value to append to the array- Returns:
- this JSONObject
- Throws:
JSONException
-
append
public JSONObject append(String key, Object value) throws JSONException
appends to an existing array- Parameters:
key
- the key element to operate onvalue
- the object to put- Returns:
- this JSONObject
- Throws:
JSONException
- if the value exists and is not an array
-
increment
public JSONObject increment(String key) throws JSONException
increments a numeric value by 1, or creates it with a value of 1 if it does not exist.- Parameters:
key
- the key element to operate on- Returns:
- this JSONObject
- Throws:
JSONException
- if something goes wrong
-
putOnce
public JSONObject putOnce(String key, Object value) throws JSONException
put a value to a key only if it does not exist- Parameters:
key
- the key element to operate onvalue
- the object to put- Returns:
- this JSONObject
- Throws:
JSONException
- if the key exists.
-
put
public JSONObject put(String key, Object value) throws JSONException
put an object to a key. the value must be a JSON type- Parameters:
key
- the key element to operate onvalue
- the object to put- Returns:
- this JSONObject
- Throws:
JSONException
- if something goes wrong
-
putOpt
public JSONObject putOpt(String key, Object value) throws JSONException
optional put a value at a key as long as both they key and value are not null otherwise it does nothing- Parameters:
key
- the key element to operate onvalue
- the object to put- Returns:
- this JSONObject
- Throws:
JSONException
- if something goes wrong
-
toMap
public Map<String,Object> toMap()
converts this object to a map- Returns:
- this object as a map
-
names
public JSONArray names()
get the key names as a JSONArray- Returns:
- a JSONArray of keys
-
toJSONArray
public JSONArray toJSONArray(JSONArray names) throws JSONException
creates an array of the values for they keys you provide- Parameters:
names
- a list of keys you want an array for- Returns:
- a JSONArray of values or null of the array is null or empty
- Throws:
JSONException
- if something goes wrong
-
opt
public Object opt(String key)
optionally return the object or null if it doesn't exist- Parameters:
key
- the key- Returns:
- the object at the key or null
-
isEmpty
public boolean isEmpty()
- Returns:
- boolean if the object is empty
-
isNull
public boolean isNull(String key)
indicate if the key does not exist or its value is null- Parameters:
key
- the key- Returns:
- a boolean indicating null
-
-