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 ObjectNULL-
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 JSONObjectaccumulate(String key, Object additionalValue)Add a element to a JSONArray in a element.JSONObjectappend(String key, Object value)appends to an existing arraystatic StringdoubleToString(double d)convert a primitive number to a string if the double can be converted to a whole number the decimal will be droppedbooleanequals(Object other)Objectget(String key)get and element by key as its native objectBigDecimalgetBigDecimal(String key)get an element property as a BigDecimalBigIntegergetBigInteger(String key)get an element property as a BigIntegerbooleangetBoolean(String key)gets a boolean value at a particular keydoublegetDouble(String key)get the value as a double<T extends Enum<T>>
TgetEnum(Class<T> enumClass, String key)get element as a enum valuefloatgetFloat(String key)get the value as a floatintgetInt(String key)get an element property as a intJSONArraygetJSONArray(String key)get the element as a JSONArrayJSONObjectgetJSONObject(String key)get the element as a JSONObjectlonggetLong(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 JSONObjectNumbergetNumber(String key)get an element property as a NumberStringgetString(String key)get a element property as a stringbooleanhas(String key)inthashCode()JSONObjectincrement(String key)increments a numeric value by 1, or creates it with a value of 1 if it does not exist.booleanisEmpty()booleanisNull(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 setintlength()JSONArraynames()get the key names as a JSONArraystatic StringnumberToString(Number number)Convert a number to a stringObjectopt(String key)optionally return the object or null if it doesn't existBigDecimaloptBigDecimal(String key, BigDecimal defaultValue)get the value as a BigDecimal or default valueBigIntegeroptBigInteger(String key, BigInteger defaultValue)get the value as a BigInteger or default valuebooleanoptBoolean(String key)gets a boolean value at a particular key or false as defaultbooleanoptBoolean(String key, boolean defaultValue)gets a boolean value at a particular key or a default valuedoubleoptDouble(String key)the value as double or NaNdoubleoptDouble(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 mappedfloatoptFloat(String key)the value as double or NaNfloatoptFloat(String key, float defaultValue)get the value as a float or default valueintoptInt(String key)the value as int or NaNintoptInt(String key, int defaultValue)get the value as a int or default valueJSONArrayoptJSONArray(String key)optionally get the element as a JSONArrayJSONObjectoptJSONObject(String key)get the element as a JSONObjectlongoptLong(String key)the value as long or NaNlongoptLong(String key, long defaultValue)get the value as a long or default valueNumberoptNumber(String key)the value as int or 0NumberoptNumber(String key, Number defaultValue)get the value as a Number or default valueStringoptString(String key)get a element property as a stringStringoptString(String key, String defaultValue)get a element property as a stringJSONObjectput(String key, boolean value)put a boolean at a particular keyJSONObjectput(String key, double value)put a double at a particular keyJSONObjectput(String key, float value)put a float at a particular keyJSONObjectput(String key, int value)put a int at a particular keyJSONObjectput(String key, long value)put a long at a particular keyJSONObjectput(String key, Number value)put a Number at a particular keyJSONObjectput(String key, Object value)put an object to a key.JSONObjectput(String key, String value)put a String at a particular keyJSONObjectput(String key, Collection value)put a Collection as a JSONArray at a particular keyJSONObjectput(String key, Map value)put a Collection as a JSONArray at a particular keyJSONObjectput(String key, JSONArray array)put a JSONArray at a particular keyJSONObjectput(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.JSONObjectputOnce(String key, Object value)put a value to a key only if it does not existJSONObjectputOpt(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 Stringquote(String s)quite escape a stringstatic Writerquote(String s, Writer writer)quite escape a stringObjectremove(String key)remove a element by key namebooleansimilar(Object o)indicates if a JSONObject has the same elements as another JSONObjectstatic ObjectstringToValue(String str)convert a primitive JSON type in a string (bool, number, null) to its primitive type all decimal types will become doublesJSONArraytoJSONArray(JSONArray names)creates an array of the values for they keys you provideMap<String,Object>toMap()converts this object to a mapStringtoString()StringtoString(int i)render the object as a JSON Stringstatic StringvalueToString(Object o)Converts an object to a JSON Stringstatic Objectwrap(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
-
-