Class JsonObject

java.lang.Object
com.couchbase.client.java.json.JsonValue
com.couchbase.client.java.json.JsonObject
All Implemented Interfaces:
Serializable

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 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.
Since:
2.0
See Also:
  • Method Details

    • create

      public static JsonObject create()
      Creates a empty JsonObject.
      Returns:
      a empty JsonObject.
    • create

      public static JsonObject create(int initialCapacity)
      Creates a empty JsonObject.
      Parameters:
      initialCapacity - the initial capacity for the object.
      Returns:
      a empty JsonObject.
    • from

      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 InvalidArgumentException 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 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 present
      NullPointerException - in case a null map is provided or if it contains a null key
      ClassCastException - if map contains a sub-Map or sub-List not supported (see above)
    • fromJson

      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 '{').
      Parameters:
      s - the JSON String to convert to a JsonObject.
      Returns:
      the corresponding JsonObject.
      Throws:
      InvalidArgumentException - if the conversion cannot be done.
    • fromJson

      public static JsonObject fromJson(byte[] s)
    • crypto

      @Volatile public JsonObjectCrypto crypto(CryptoManager cryptoManager)
      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 with JsonObjectCrypto.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

      @Volatile public JsonObjectCrypto crypto(Collection collection)
      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 with JsonObjectCrypto.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 with JsonObjectCrypto.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

      public JsonObject put(String name, Object value)
      Stores a Object value identified by the field name. Note that the value is checked and a InvalidArgumentException is thrown if not supported.
      Parameters:
      name - the name of the JSON field.
      value - the value of the JSON field.
      Returns:
      the JsonObject.
    • get

      public Object get(String name)
      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

      public JsonObject put(String name, String value)
      Stores a String 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

      public String getString(String name)
      Retrieves the value from the field name and casts it to String.
      Parameters:
      name - the name of the field.
      Returns:
      the result or null if it does not exist.
    • put

      public JsonObject put(String name, int value)
      Stores a Integer 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

      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.
      Parameters:
      name - the name of the field.
      Returns:
      the result or null if it does not exist.
    • put

      public JsonObject put(String name, long value)
      Stores a Long 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

      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.
      Parameters:
      name - the name of the field.
      Returns:
      the result or null if it does not exist.
    • put

      public JsonObject put(String name, double value)
      Stores a Double 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

      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.
      Parameters:
      name - the name of the field.
      Returns:
      the result or null if it does not exist.
    • put

      public JsonObject put(String name, boolean value)
      Stores a Boolean 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

      public Boolean getBoolean(String name)
      Retrieves the value from the field name and casts it to Boolean.
      Parameters:
      name - the name of the field.
      Returns:
      the result or null if it does not exist.
    • put

      public JsonObject put(String name, JsonObject value)
      Stores a JsonObject 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

      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.
      Parameters:
      name - the name of the JSON field.
      value - the value of the JSON field.
      Returns:
      the JsonObject.
      See Also:
    • getObject

      public JsonObject getObject(String name)
      Retrieves the value from the field name and casts it to JsonObject.
      Parameters:
      name - the name of the field.
      Returns:
      the result or null if it does not exist.
    • put

      public JsonObject put(String name, JsonArray value)
      Stores a JsonArray 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

      public JsonObject put(String name, Number value)
      Stores a Number 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

      public JsonObject put(String name, List<?> value)
      Stores a JsonArray 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

      public JsonArray getArray(String name)
      Retrieves the value from the field name and casts it to JsonArray.
      Parameters:
      name - the name of the field.
      Returns:
      the result or null if it does not exist.
    • getBigInteger

      public BigInteger getBigInteger(String name)
      Retrieves the value from the field name and casts it to BigInteger.
      Parameters:
      name - the name of the field.
      Returns:
      the result or null if it does not exist.
    • getBigDecimal

      public BigDecimal getBigDecimal(String name)
      Retrieves the value from the field name and casts it to BigDecimal.
      Parameters:
      name - the name of the field.
      Returns:
      the result or null if it does not exist.
    • getNumber

      public Number getNumber(String name)
      Retrieves the value from the field name and casts it to Number.
      Parameters:
      name - the name of the field.
      Returns:
      the result or null if it does not exist.
    • putNull

      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.
      Parameters:
      name - The null field's name.
      Returns:
      the JsonObject
    • removeKey

      public JsonObject removeKey(String name)
      Removes an entry from the JsonObject.
      Parameters:
      name - the name of the field to remove
      Returns:
      the JsonObject
    • getNames

      public Set<String> getNames()
      Returns a set of field names on the JsonObject.
      Returns:
      the set of names on the object.
    • isEmpty

      public boolean isEmpty()
      Returns true if the JsonObject is empty, false otherwise.
      Returns:
      true if empty, false otherwise.
    • toMap

      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.
      Returns:
      the content copied as a Map.
    • containsKey

      public boolean containsKey(String name)
      Checks if the JsonObject contains the field name.
      Parameters:
      name - the name of the field.
      Returns:
      true if its contained, false otherwise.
    • containsValue

      public boolean containsValue(Object value)
      Checks if the JsonObject contains the value.
      Parameters:
      value - the actual value.
      Returns:
      true if its contained, false otherwise.
    • size

      public int size()
      The size of the JsonObject.
      Returns:
      the size.
    • toString

      public String toString()
      Converts the JsonObject into its JSON string representation.
      Overrides:
      toString in class Object
      Returns:
      the JSON string representing this JsonObject.
    • toBytes

      public byte[] toBytes()
      Similar to toString() but turns this object directly into an encoded byte array.
      Returns:
      the byte array representing this JsonObject.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object