Package kong.unirest.core.json
Class JSONObject
java.lang.Object
kong.unirest.core.json.JSONElement
kong.unirest.core.json.JSONObject
- Direct Known Subclasses:
NullObject
https://json.org/
https://tools.ietf.org/html/rfc7159#section-4
represents a JSON Object
-
Field Summary
FieldsFields inherited from class kong.unirest.core.json.JSONElement
MAPPER
-
Constructor Summary
ConstructorsConstructorDescriptionan 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 mapJSONObject
(JsonEngine.Element jsonElement) -
Method Summary
Modifier and TypeMethodDescriptionaccumulate
(String key, Object additionalValue) Add a element to a JSONArray in a element.appends to an existing array(package private) JsonEngine.Element
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 droppedboolean
get and element by key as its native objectgetBigDecimal
(String key) get an element property as a BigDecimalgetBigInteger
(String key) get an element property as a BigIntegerboolean
getBoolean
(String key) gets a boolean value at a particular keydouble
get the value as a double<T extends Enum<T>>
Tget element as a enum valuefloat
get the value as a floatint
get an element property as a intgetJSONArray
(String key) get the element as a JSONArraygetJSONObject
(String key) get the element as a JSONObjectlong
get the value as a longstatic String[]
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 JSONObjectget an element property as a Numberprivate <T> T
getOrDefault
(Supplier<T> supplier, T defaultValue) private JsonEngine.Element
getProperty
(String key) get a element property as a stringboolean
int
hashCode()
increments a numeric value by 1, or creates it with a value of 1 if it does not exist.boolean
isEmpty()
boolean
indicate if the key does not exist or its value is nullprivate static boolean
keys()
get a iterator for the keysetkeySet()
get all the keys as a setint
length()
names()
get the key names as a JSONArraystatic String
numberToString
(Number number) Convert a number to a stringoptionally return the object or null if it doesn't existoptBigDecimal
(String key, BigDecimal defaultValue) get the value as a BigDecimal or default valueoptBigInteger
(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
the value as double or NaNdouble
get the value as a double or default value<T extends Enum<T>>
Tget element as a enum value or null if the value cannot be mapped<T extends Enum<T>>
Tget element as a enum value or a default value if the value cannot be mappedfloat
the value as double or NaNfloat
get the value as a float or default valueint
the value as int or NaNint
get the value as a int or default valueoptJSONArray
(String key) optionally get the element as a JSONArrayoptJSONObject
(String key) get the element as a JSONObjectlong
the value as long or NaNlong
get the value as a long or default valuethe value as int or 0get the value as a Number or default valueget a element property as a stringget a element property as a stringput a boolean at a particular keyput a double at a particular keyput a float at a particular keyput a int at a particular keyput a long at a particular keyput a Number at a particular keyput an object to a key.put a String at a particular keyput
(String key, Collection value) put a Collection as a JSONArray at a particular keyput a Collection as a JSONArray at a particular keyput a JSONArray at a particular keyput
(String key, JSONObject object) put a JSONObject at a particular key<T extends Enum<T>>
JSONObjectput a enum at a particular key.put a value to a key only if it does not existoptional put a value at a key as long as both they key and value are not null otherwise it does nothingstatic String
quite escape a stringstatic Writer
quite escape a stringremove a element by key nameboolean
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 doublestoJSONArray
(JSONArray names) creates an array of the values for they keys you providetoMap()
converts this object to a maptoString()
toString
(int i) render the object as a JSON Stringprivate <T extends Number>
Tstatic String
Converts an object to a JSON Stringstatic Object
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 Objectprivate static JSONArray
Methods inherited from class kong.unirest.core.json.JSONElement
getElement, optQuery, optQuery, query, query, toJson, toJsonObject, toMap, toPrettyJson, toTree, write, write, write
-
Field Details
-
NULL
-
obj
-
-
Constructor Details
-
JSONObject
https://tools.ietf.org/html/rfc7159#section-4- Parameters:
string
- a json object string
-
JSONObject
construct using a map- Parameters:
map
- a map representing the elements of a JSON Object
-
JSONObject
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 -
JSONObject
JSONObject(JsonEngine.Element jsonElement)
-
-
Method Details
-
quote
quite escape a string- Parameters:
s
- a string- Returns:
- a quoted string
-
quote
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
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
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
-
wrapArray
-
doubleToString
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
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
Converts an object to a JSON String- Parameters:
o
- any object- Returns:
- a json string
- Throws:
JSONException
- if something goes wrong
-
getNames
get all of the keys of a JSONObject- Parameters:
jsonObject
- a JSONObject- Returns:
- a String[] of the objects keys
-
getNames
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
-
asElement
JsonEngine.Element asElement() -
toString
-
toString
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
indicates if a JSONObject has the same elements as another JSONObject- Parameters:
o
- another object- Returns:
- a bool
-
has
- 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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
gets a boolean value at a particular key or false as default- Parameters:
key
- the key- Returns:
- a boolean
-
optBoolean
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
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
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
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
put a JSONObject at a particular key- Parameters:
key
- the key element to operate onobject
- JSONObject- Returns:
- this JSONObject
- Throws:
JSONException
-
put
put a JSONArray at a particular key- Parameters:
key
- the key element to operate onarray
- JSONArray- Returns:
- this JSONObject
- Throws:
JSONException
-
put
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
put a Number at a particular key- Parameters:
key
- the key element to operate onvalue
- Number- Returns:
- this JSONObject
- Throws:
JSONException
-
put
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
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
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
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
put a String at a particular key- Parameters:
key
- the key element to operate onvalue
- Number- Returns:
- this JSONObject
- Throws:
JSONException
-
put
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
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
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
remove a element by key name- Parameters:
key
- the key element to operate on- Returns:
- the object value that was removed
-
accumulate
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
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
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
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
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
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
-
keySet
get all the keys as a set- Returns:
- a set of keys
-
keys
get a iterator for the keyset- Returns:
- a Iterator of keys
-
toMap
converts this object to a map- Returns:
- this object as a map
-
names
get the key names as a JSONArray- Returns:
- a JSONArray of keys
-
toJSONArray
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
-
getProperty
-
tryNumber
-
getOrDefault
-
equals
-
hashCode
public int hashCode() -
opt
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
indicate if the key does not exist or its value is null- Parameters:
key
- the key- Returns:
- a boolean indicating null
-
isPrimitive
-