public class Jsr353JsonCodec extends TypeCodec<JsonStructure>
More specifically, this codec maps an arbitrary JsonStructure
to
a CQL varchar
column.
Note that this codec handles the Java type JsonStructure
.
It is therefore required that values are set and retrieved using that exact Java type;
users should manually downcast to either JsonObject
or JsonArray
,
as in the example below:
// setting values
JsonObject myObject = ...
PreparedStatement ps = ...
// set values using JsonStructure
BoundStatement bs = ps.bind().set(1, myObject, JsonStructure.class);
// retrieving values
Row row = session.execute(bs).one();
// use JsonStructure to retrieve values
JsonStructure json = row.get(0, JsonStructure.class);
if (json instanceof JsonObject) {
myObject = (JsonObject) json;
...
}
TypeCodec.AbstractCollectionCodec<E,C extends Collection<E>>, TypeCodec.AbstractMapCodec<K,V>, TypeCodec.AbstractTupleCodec<T>, TypeCodec.AbstractUDTCodec<T>, TypeCodec.PrimitiveBooleanCodec, TypeCodec.PrimitiveByteCodec, TypeCodec.PrimitiveDoubleCodec, TypeCodec.PrimitiveFloatCodec, TypeCodec.PrimitiveIntCodec, TypeCodec.PrimitiveLongCodec, TypeCodec.PrimitiveShortCodec
Constructor and Description |
---|
Jsr353JsonCodec() |
Jsr353JsonCodec(Map<String,?> config) |
Modifier and Type | Method and Description |
---|---|
JsonStructure |
deserialize(ByteBuffer bytes,
ProtocolVersion protocolVersion)
Deserialize the given
ByteBuffer instance according to the CQL type
handled by this codec. |
String |
format(JsonStructure value)
Format the given value as a valid CQL literal according
to the CQL type handled by this codec.
|
JsonStructure |
parse(String value)
Parse the given CQL literal into an instance of the Java type
handled by this codec.
|
ByteBuffer |
serialize(JsonStructure value,
ProtocolVersion protocolVersion)
Serialize the given value according to the CQL type
handled by this codec.
|
accepts, accepts, accepts, ascii, bigint, blob, cboolean, cdouble, cfloat, cint, counter, custom, date, decimal, getCqlType, getJavaType, inet, list, map, set, smallInt, time, timestamp, timeUUID, tinyInt, toString, tuple, userType, uuid, varchar, varint
public ByteBuffer serialize(JsonStructure value, ProtocolVersion protocolVersion) throws InvalidTypeException
TypeCodec
Implementation notes:
null
input as
the equivalent of an empty collection.serialize
in class TypeCodec<JsonStructure>
value
- An instance of T; may be null
.protocolVersion
- the protocol version to use when serializing
bytes
. In most cases, the proper value to provide for this argument
is the value returned by ProtocolOptions.getProtocolVersion()
(which
is the protocol version in use by the driver).ByteBuffer
instance containing the serialized form of TInvalidTypeException
- if the given value does not have the expected typepublic JsonStructure deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException
TypeCodec
ByteBuffer
instance according to the CQL type
handled by this codec.
Implementation notes:
null
or a default value for the corresponding Java type, if applicable;null
;
they should return empty collections instead (the driver's default collection codecs all comply with this rule).ByteBuffer
should never be consumed by read operations that
modify its current position; if necessary,
ByteBuffer.duplicate()
duplicate} it before consuming.deserialize
in class TypeCodec<JsonStructure>
bytes
- A ByteBuffer
instance containing the serialized form of T;
may be null
or empty.protocolVersion
- the protocol version to use when serializing
bytes
. In most cases, the proper value to provide for this argument
is the value returned by ProtocolOptions.getProtocolVersion()
(which
is the protocol version in use by the driver).InvalidTypeException
- if the given ByteBuffer
instance cannot be deserializedpublic String format(JsonStructure value) throws InvalidTypeException
TypeCodec
"NULL"
for null
inputs.format
in class TypeCodec<JsonStructure>
value
- An instance of T; may be null
.InvalidTypeException
- if the given value does not have the expected typepublic JsonStructure parse(String value) throws InvalidTypeException
TypeCodec
"NULL"
;
in most cases, implementations should interpret these inputs has equivalent to a null
reference.parse
in class TypeCodec<JsonStructure>
value
- The CQL string to parse, may be null
or empty.null
on a null input
.InvalidTypeException
- if the given value cannot be parsed into the expected typeCopyright © 2012–2015. All rights reserved.