Class JsonObject
If boxed return values are unboxed, the calling code needs to make sure to handle potential
NullPointerExceptions.
The JsonObject is backed by a Map and is intended to work similar to it API wise, but to only
allow to store such objects which can be represented by JSON.
-
Method Summary
Modifier and TypeMethodDescriptionbooleancontainsKey(String name) Checks if theJsonObjectcontains the field name.booleancontainsValue(Object value) Checks if theJsonObjectcontains the value.static JsonObjectcreate()Creates an emptyJsonObject.static JsonObjectcreate(int initialCapacity) Creates an emptyJsonObject.booleanstatic JsonObjectConstructs aJsonObjectfrom aMap<String, ?>.static JsonObjectfromJson(byte[] jsonObject) static JsonObjectStatic method to create aJsonObjectfrom a JSONString.@Nullable ObjectRetrieves the (potential null) content and not casting its type.@Nullable JsonArrayRetrieves the value from the field name and casts it toJsonArray.@Nullable BigDecimalgetBigDecimal(String name) Retrieves the value from the field name and casts it toBigDecimal.@Nullable BigIntegergetBigInteger(String name) Retrieves the value from the field name and casts it toBigInteger.@Nullable BooleangetBoolean(String name) Retrieves the value from the field name and casts it toBoolean.@Nullable DoubleRetrieves the value from the field name and casts it toDouble.@Nullable IntegerRetrieves the value from the field name and casts it toInteger.@Nullable LongRetrieves the value from the field name and casts it toLong.getNames()Returns a set of field names on theJsonObject.@Nullable NumberRetrieves the value from the field name and casts it toNumber.@Nullable JsonObjectRetrieves the value from the field name and casts it toJsonObject.@Nullable StringRetrieves the value from the field name and casts it toString.inthashCode()booleanisEmpty()Returns true if theJsonObjectis empty, false otherwise.Stores aBooleanvalue identified by the field name.Stores aDoublevalue identified by the field name.Stores aIntegervalue identified by the field name.Stores aLongvalue identified by the field name.Stores aJsonArrayvalue identified by the field name.put(String name, @Nullable JsonObject value) Stores aJsonObjectvalue identified by the field name.Stores aNumbervalue identified by the field name.Stores aObjectvalue identified by the field name.Stores aStringvalue identified by the field name.Stores aJsonArrayvalue identified by the field name.Attempt to convert aMapto aJsonObjectvalue and store it, identified by the field name.Store a null value identified by the field's name.Removes an entry from theJsonObject.intsize()The size of theJsonObject.byte[]toBytes()Similar totoString()but turns this object directly into an encoded byte array.toMap()Transforms theJsonObjectinto aMap.toString()Converts theJsonObjectinto its JSON string representation.
-
Method Details
-
create
Creates an emptyJsonObject.- Returns:
- an empty
JsonObject.
-
create
Creates an emptyJsonObject.- Parameters:
initialCapacity- the initial capacity for the object.- Returns:
- an empty
JsonObject.
-
from
Constructs aJsonObjectfrom aMap<String, ?>.This is only possible if the given Map is well-formed, that is it contains non-null keys, and all values are of a supported type.
A null input Map or null key will lead to a
NullPointerExceptionbeing thrown. If any unsupported value is present in the Map, anIllegalArgumentExceptionwill be thrown.*Sub Maps and Lists* If possible, Maps and Lists contained in mapData will be converted to JsonObject and JsonArray respectively. However, same restrictions apply. Any non-convertible collection will raise a
ClassCastException. If the sub-conversion raises an exception (like an InvalidArgumentException) then it is put as cause for the ClassCastException.- Parameters:
mapData- the Map to convert to a JsonObject- Returns:
- the resulting JsonObject
- Throws:
IllegalArgumentException- in case one or more unsupported values are presentNullPointerException- in case a null map is provided or if it contains a null keyClassCastException- if map contains a sub-Map or sub-List not supported (see above)
-
fromJson
Static method to create aJsonObjectfrom a JSONString.The string is expected to be a valid JSON object representation (eg. starting with a '{').
- Parameters:
jsonObject- the JSON String to convert to aJsonObject.- Returns:
- the corresponding
JsonObject. - Throws:
IllegalArgumentException- if the conversion cannot be done.
-
fromJson
-
put
Stores aObjectvalue identified by the field name.- Parameters:
name- the name of the JSON field.value- the value of the JSON field.- Returns:
- the
JsonObject. - Throws:
IllegalArgumentException- if the value is not a supported JSON type
-
get
Retrieves the (potential null) content and not casting its type.- Parameters:
name- the key of the field.- Returns:
- the value of the field, or null if it does not exist.
-
put
Stores aStringvalue identified by the field name.- Parameters:
name- the name of the JSON field.value- the value of the JSON field.- Returns:
- the
JsonObject.
-
getString
Retrieves the value from the field name and casts it toString.- Parameters:
name- the name of the field.- Returns:
- the result or null if it does not exist.
-
put
Stores aIntegervalue identified by the field name.- Parameters:
name- the name of the JSON field.value- the value of the JSON field.- Returns:
- the
JsonObject.
-
getInt
Retrieves the value from the field name and casts it toInteger.Note that if value was stored as another numerical type, some truncation or rounding may occur.
- Parameters:
name- the name of the field.- Returns:
- the result or null if it does not exist.
-
put
Stores aLongvalue identified by the field name.- Parameters:
name- the name of the JSON field.value- the value of the JSON field.- Returns:
- the
JsonObject.
-
getLong
Retrieves the value from the field name and casts it toLong.Note that if value was stored as another numerical type, some truncation or rounding may occur.
- Parameters:
name- the name of the field.- Returns:
- the result or null if it does not exist.
-
put
Stores aDoublevalue identified by the field name.- Parameters:
name- the name of the JSON field.value- the value of the JSON field.- Returns:
- the
JsonObject.
-
getDouble
Retrieves the value from the field name and casts it toDouble.Note that if value was stored as another numerical type, some truncation or rounding may occur.
- Parameters:
name- the name of the field.- Returns:
- the result or null if it does not exist.
-
put
Stores aBooleanvalue identified by the field name.- Parameters:
name- the name of the JSON field.value- the value of the JSON field.- Returns:
- the
JsonObject.
-
getBoolean
Retrieves the value from the field name and casts it toBoolean.- Parameters:
name- the name of the field.- Returns:
- the result or null if it does not exist.
-
put
Stores aJsonObjectvalue identified by the field name.- Parameters:
name- the name of the JSON field.value- the value of the JSON field.- Returns:
- the
JsonObject.
-
put
Attempt to convert aMapto aJsonObjectvalue and store it, identified by the field name.- Parameters:
name- the name of the JSON field.value- the value of the JSON field.- Returns:
- the
JsonObject. - See Also:
-
getObject
Retrieves the value from the field name and casts it toJsonObject.- Parameters:
name- the name of the field.- Returns:
- the result or null if it does not exist.
-
put
Stores aJsonArrayvalue identified by the field name.- Parameters:
name- the name of the JSON field.value- the value of the JSON field.- Returns:
- the
JsonObject.
-
put
Stores aNumbervalue identified by the field name.- Parameters:
name- the name of the JSON field.value- the value of the JSON field.- Returns:
- the
JsonObject.
-
put
Stores aJsonArrayvalue identified by the field name.- Parameters:
name- the name of the JSON field.value- the value of the JSON field.- Returns:
- the
JsonObject.
-
getArray
Retrieves the value from the field name and casts it toJsonArray.- Parameters:
name- the name of the field.- Returns:
- the result or null if it does not exist.
-
getBigInteger
Retrieves the value from the field name and casts it toBigInteger.- Parameters:
name- the name of the field.- Returns:
- the result or null if it does not exist.
-
getBigDecimal
Retrieves the value from the field name and casts it toBigDecimal.- Parameters:
name- the name of the field.- Returns:
- the result or null if it does not exist.
-
getNumber
Retrieves the value from the field name and casts it toNumber.- Parameters:
name- the name of the field.- Returns:
- the result or null if it does not exist.
-
putNull
Store a null value identified by the field's name.This method is equivalent to calling
put(String, Object)with a null value explicitly cast to Object.- Parameters:
name- The null field's name.- Returns:
- the
JsonObject
-
removeKey
Removes an entry from theJsonObject.- Parameters:
name- the name of the field to remove- Returns:
- the
JsonObject
-
getNames
Returns a set of field names on theJsonObject.- Returns:
- the set of names on the object.
-
isEmpty
public boolean isEmpty()Returns true if theJsonObjectis empty, false otherwise.- Returns:
- true if empty, false otherwise.
-
toMap
Transforms theJsonObjectinto aMap. The resulting map is not backed by thisJsonObject, and all sub-objects or sub-arrays (JsonArray) are also recursively converted to maps and lists, respectively.- Returns:
- the content copied as a
Map.
-
containsKey
Checks if theJsonObjectcontains the field name.- Parameters:
name- the name of the field.- Returns:
- true if its contained, false otherwise.
-
containsValue
Checks if theJsonObjectcontains the value.- Parameters:
value- the actual value.- Returns:
- true if its contained, false otherwise.
-
size
public int size()The size of theJsonObject.- Returns:
- the size.
-
toString
Converts theJsonObjectinto its JSON string representation.- Overrides:
toStringin classObject- Returns:
- the JSON string representing this
JsonObject.
-
toBytes
public byte[] toBytes()Similar totoString()but turns this object directly into an encoded byte array.- Returns:
- the byte array representing this
JsonObject.
-
equals
-
hashCode
public int hashCode()
-