public class JsonObject extends JsonValue implements Serializable
Represents a JSON object that can be stored and loaded from Couchbase Server.
If boxed return values are unboxed, the calling code needs to make sure to handle potential 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.
Modifier and Type | Method and Description |
---|---|
boolean |
containsKey(String name)
Checks if the
JsonObject contains the field name. |
boolean |
containsValue(Object value)
Checks if the
JsonObject contains the value. |
static JsonObject |
create()
Creates a empty
JsonObject . |
static JsonObject |
empty()
Creates a empty
JsonObject . |
boolean |
equals(Object o) |
static JsonObject |
from(Map<String,?> mapData)
Constructs a
JsonObject from a Map<String, ?> . |
static JsonObject |
fromJson(String s)
Static method to create a
JsonObject from a JSON String . |
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 to
JsonArray . |
Boolean |
getBoolean(String name)
Retrieves the value from the field name and casts it to
Boolean . |
Double |
getDouble(String name)
Retrieves the value from the field name and casts it to
Double . |
Integer |
getInt(String name)
Retrieves the value from the field name and casts it to
Integer . |
Long |
getLong(String name)
Retrieves the value from the field name and casts it to
Long . |
Set<String> |
getNames()
Returns a set of field names on the
JsonObject . |
JsonObject |
getObject(String name)
Retrieves the value from the field name and casts it to
JsonObject . |
String |
getString(String name)
Retrieves the value from the field name and casts it to
String . |
int |
hashCode() |
boolean |
isEmpty()
Returns true if the
JsonObject is empty, false otherwise. |
JsonObject |
put(String name,
boolean value)
Stores a
Boolean value identified by the field name. |
JsonObject |
put(String name,
double value)
Stores a
Double value identified by the field name. |
JsonObject |
put(String name,
int value)
Stores a
Integer value identified by the field name. |
JsonObject |
put(String name,
JsonArray value)
Stores a
JsonArray value identified by the field name. |
JsonObject |
put(String name,
JsonObject value)
Stores a
JsonObject value identified by the field name. |
JsonObject |
put(String name,
List<?> value)
Stores a
JsonArray value identified by the field name. |
JsonObject |
put(String name,
long value)
Stores a
Long value identified by the field name. |
JsonObject |
put(String name,
Map<String,?> value)
Attempt to convert a
Map to a JsonObject value and store it, identified by the field name. |
JsonObject |
put(String name,
Object value)
Stores a
Object value identified by the field name. |
JsonObject |
put(String name,
String value)
Stores a
String value 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 the
JsonObject . |
int |
size()
The size of the
JsonObject . |
Map<String,Object> |
toMap()
Transforms the
JsonObject into a Map . |
String |
toString()
Converts the
JsonObject into its JSON string representation. |
public static JsonObject empty()
Creates a empty JsonObject
.
JsonObject
.public static JsonObject create()
Creates a empty JsonObject
.
JsonObject
.public static JsonObject from(Map<String,?> mapData)
Constructs a JsonObject
from a Map<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 NullPointerException
being thrown. If any unsupported value is present in the Map, an IllegalArgumentException
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 a ClassCastException
. If the sub-conversion raises an exception (like an IllegalArgumentException) then it is put as cause for the ClassCastException.
mapData
- the Map to convert to a JsonObjectIllegalArgumentException
- 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)public static JsonObject fromJson(String s)
Static method to create a JsonObject
from a JSON String
.
The string is expected to be a valid JSON object representation (eg. starting with a ‘{’).
s
- the JSON String to convert to a JsonObject
.JsonObject
.IllegalArgumentException
- if the conversion cannot be done.public JsonObject put(String name, Object value)
Stores a Object
value identified by the field name.
Note that the value is checked and a IllegalArgumentException
is thrown if not supported.
name
- the name of the JSON field.value
- the value of the JSON field.JsonObject
.public Object get(String name)
Retrieves the (potential null) content and not casting its type.
name
- the key of the field.public JsonObject put(String name, String value)
Stores a String
value identified by the field name.
name
- the name of the JSON field.value
- the value of the JSON field.JsonObject
.public String getString(String name)
Retrieves the value from the field name and casts it to String
.
name
- the name of the field.public JsonObject put(String name, int value)
Stores a Integer
value identified by the field name.
name
- the name of the JSON field.value
- the value of the JSON field.JsonObject
.public Integer getInt(String name)
Retrieves the value from the field name and casts it to Integer
.
Note that if value was stored as another numerical type, some truncation or rounding may occur.
name
- the name of the field.public JsonObject put(String name, long value)
Stores a Long
value identified by the field name.
name
- the name of the JSON field.value
- the value of the JSON field.JsonObject
.public Long getLong(String name)
Retrieves the value from the field name and casts it to Long
.
Note that if value was stored as another numerical type, some truncation or rounding may occur.
name
- the name of the field.public JsonObject put(String name, double value)
Stores a Double
value identified by the field name.
name
- the name of the JSON field.value
- the value of the JSON field.JsonObject
.public Double getDouble(String name)
Retrieves the value from the field name and casts it to Double
.
Note that if value was stored as another numerical type, some truncation or rounding may occur.
name
- the name of the field.public JsonObject put(String name, boolean value)
Stores a Boolean
value identified by the field name.
name
- the name of the JSON field.value
- the value of the JSON field.JsonObject
.public Boolean getBoolean(String name)
Retrieves the value from the field name and casts it to Boolean
.
name
- the name of the field.public JsonObject put(String name, JsonObject value)
Stores a JsonObject
value identified by the field name.
name
- the name of the JSON field.value
- the value of the JSON field.JsonObject
.public JsonObject put(String name, Map<String,?> value)
Attempt to convert a Map
to a JsonObject
value and store it, identified by the field name.
name
- the name of the JSON field.value
- the value of the JSON field.JsonObject
.from(Map)
public JsonObject getObject(String name)
Retrieves the value from the field name and casts it to JsonObject
.
name
- the name of the field.public JsonObject put(String name, JsonArray value)
Stores a JsonArray
value identified by the field name.
name
- the name of the JSON field.value
- the value of the JSON field.JsonObject
.public JsonObject put(String name, List<?> value)
Stores a JsonArray
value identified by the field name.
name
- the name of the JSON field.value
- the value of the JSON field.JsonObject
.public JsonArray getArray(String name)
Retrieves the value from the field name and casts it to JsonArray
.
name
- the name of the field.public JsonObject putNull(String name)
Store a null value identified by the field’s name.
This method is equivalent to calling put(String, Object)
with either JsonValue.NULL
or a null value explicitly cast to Object.
name
- The null field’s name.JsonObject
public JsonObject removeKey(String name)
Removes an entry from the JsonObject
.
name
- the name of the field to removeJsonObject
public Set<String> getNames()
Returns a set of field names on the JsonObject
.
public boolean isEmpty()
Returns true if the JsonObject
is empty, false otherwise.
public Map<String,Object> toMap()
Transforms the JsonObject
into a Map
. The resulting map is not backed by this JsonObject
, and all sub-objects or sub-arrays (JsonArray
) are also recursively converted to maps and lists, respectively.
Map
.public boolean containsKey(String name)
Checks if the JsonObject
contains the field name.
name
- the name of the field.public boolean containsValue(Object value)
Checks if the JsonObject
contains the value.
value
- the actual value.public int size()
The size of the JsonObject
.
public String toString()
Converts the JsonObject
into its JSON string representation.
toString
in class Object
JsonObject
.Copyright © 2015 Couchbase, Inc.