Class JsonObject
- All Implemented Interfaces:
Serializable
public class JsonObject extends JsonValue implements Serializable
NullPointerException
s.
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.- Since:
- 2.0
- See Also:
- Serialized Form
-
Field Summary
-
Method Summary
Modifier and Type Method Description boolean
containsKey(String name)
Checks if theJsonObject
contains the field name.boolean
containsValue(Object value)
Checks if theJsonObject
contains the value.static JsonObject
create()
Creates a emptyJsonObject
.static JsonObject
create(int initialCapacity)
Creates a emptyJsonObject
.JsonObjectCrypto
crypto(CryptoManager cryptoManager)
Returns a view of the object for reading and writing encrypted fields.JsonObjectCrypto
crypto(Collection collection)
Returns a view of the object for reading and writing encrypted fields.JsonObjectCrypto
crypto(ClusterEnvironment env)
Returns a view of the object for reading and writing encrypted fields.boolean
equals(Object o)
static JsonObject
from(Map<String,?> mapData)
Constructs aJsonObject
from aMap<String, ?>
.static JsonObject
fromJson(byte[] s)
static JsonObject
fromJson(String s)
Static method to create aJsonObject
from a JSONString
.Object
get(String name)
Retrieves the (potential null) content and not casting its type.JsonArray
getArray(String name)
Retrieves the value from the field name and casts it toJsonArray
.BigDecimal
getBigDecimal(String name)
Retrieves the value from the field name and casts it toBigDecimal
.BigInteger
getBigInteger(String name)
Retrieves the value from the field name and casts it toBigInteger
.Boolean
getBoolean(String name)
Retrieves the value from the field name and casts it toBoolean
.Double
getDouble(String name)
Retrieves the value from the field name and casts it toDouble
.Integer
getInt(String name)
Retrieves the value from the field name and casts it toInteger
.Long
getLong(String name)
Retrieves the value from the field name and casts it toLong
.Set<String>
getNames()
Returns a set of field names on theJsonObject
.Number
getNumber(String name)
Retrieves the value from the field name and casts it toNumber
.JsonObject
getObject(String name)
Retrieves the value from the field name and casts it toJsonObject
.String
getString(String name)
Retrieves the value from the field name and casts it toString
.int
hashCode()
boolean
isEmpty()
Returns true if theJsonObject
is empty, false otherwise.JsonObject
put(String name, boolean value)
Stores aBoolean
value identified by the field name.JsonObject
put(String name, double value)
Stores aDouble
value identified by the field name.JsonObject
put(String name, int value)
Stores aInteger
value identified by the field name.JsonObject
put(String name, long value)
Stores aLong
value identified by the field name.JsonObject
put(String name, JsonArray value)
Stores aJsonArray
value identified by the field name.JsonObject
put(String name, JsonObject value)
Stores aJsonObject
value identified by the field name.JsonObject
put(String name, Number value)
Stores aNumber
value identified by the field name.JsonObject
put(String name, Object value)
Stores aObject
value identified by the field name.JsonObject
put(String name, String value)
Stores aString
value identified by the field name.JsonObject
put(String name, List<?> value)
Stores aJsonArray
value identified by the field name.JsonObject
put(String name, Map<String,?> value)
Attempt to convert aMap
to aJsonObject
value and store it, identified by the field name.JsonObject
putNull(String name)
Store a null value identified by the field's name.JsonObject
removeKey(String name)
Removes an entry from theJsonObject
.int
size()
The size of theJsonObject
.byte[]
toBytes()
Similar totoString()
but turns this object directly into an encoded byte array.Map<String,Object>
toMap()
Transforms theJsonObject
into aMap
.String
toString()
Converts theJsonObject
into its JSON string representation.
-
Method Details
-
create
Creates a emptyJsonObject
.- Returns:
- a empty
JsonObject
.
-
create
Creates a emptyJsonObject
.- Parameters:
initialCapacity
- the initial capacity for the object.- Returns:
- a empty
JsonObject
.
-
from
Constructs aJsonObject
from 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 aNullPointerException
being thrown. If any unsupported value is present in the Map, anInvalidArgumentException
will 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 aClassCastException
. 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:
InvalidArgumentException
- 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 aJsonObject
from a JSONString
. The string is expected to be a valid JSON object representation (eg. starting with a '{').- Parameters:
s
- the JSON String to convert to aJsonObject
.- Returns:
- the corresponding
JsonObject
. - Throws:
InvalidArgumentException
- if the conversion cannot be done.
-
fromJson
-
crypto
Returns a view of the object for reading and writing encrypted fields.The returned
JsonObjectCrypto
uses the default encrypter. To use a different encrypter, chain this call withJsonObjectCrypto.withEncrypter(String)
.Note: Use of the Field-Level Encryption functionality is subject to the Couchbase Inc. Enterprise Subscription License Agreement v7
- Parameters:
cryptoManager
- the manager to use for encryption and decryption
-
crypto
Returns a view of the object for reading and writing encrypted fields.The returned
JsonObjectCrypto
uses the default encrypter. To use a different encrypter, chain this call withJsonObjectCrypto.withEncrypter(String)
.Note: Use of the Field-Level Encryption functionality is subject to the Couchbase Inc. Enterprise Subscription License Agreement v7
- Throws:
IllegalStateException
- if no CryptoManager was configured for the collection's cluster environment
-
crypto
Returns a view of the object for reading and writing encrypted fields.The returned
JsonObjectCrypto
uses the default encrypter. To use a different encrypter, chain this call withJsonObjectCrypto.withEncrypter(String)
.Note: Use of the Field-Level Encryption functionality is subject to the Couchbase Inc. Enterprise Subscription License Agreement v7
- Throws:
IllegalStateException
- if no CryptoManager was configured for this cluster environment
-
put
Stores aObject
value identified by the field name. Note that the value is checked and aInvalidArgumentException
is thrown if not supported.- Parameters:
name
- the name of the JSON field.value
- the value of the JSON field.- Returns:
- the
JsonObject
.
-
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 aString
value 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 aInteger
value 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 aLong
value 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 aDouble
value 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 aBoolean
value 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 aJsonObject
value 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 aMap
to aJsonObject
value 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:
from(Map)
-
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 aJsonArray
value 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 aNumber
value 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 aJsonArray
value 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 callingput(String, Object)
with eitherJsonValue.NULL
or 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 theJsonObject
is empty, false otherwise.- Returns:
- true if empty, false otherwise.
-
toMap
Transforms theJsonObject
into 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 theJsonObject
contains the field name.- Parameters:
name
- the name of the field.- Returns:
- true if its contained, false otherwise.
-
containsValue
Checks if theJsonObject
contains 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 theJsonObject
into its JSON string representation.- Overrides:
toString
in 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()
-