public class JacksonJsonCodec<T> extends TypeCodec<T>
This codec maps a single Java object to a single JSON structure at a time; mapping of arrays or collections to root-level JSON arrays is not supported, but such a codec can be easily crafted after this one.
Note that this codec requires the presence of Jackson library at runtime. If you use Maven, this can be done by declaring the following dependency in your project:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
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 |
---|
JacksonJsonCodec(Class<T> javaClass)
Creates a new instance for the provided
javaClass , using a default, newly-allocated
ObjectMapper . |
JacksonJsonCodec(Class<T> javaClass,
ObjectMapper objectMapper)
Creates a new instance for the provided
javaClass , and using the provided ObjectMapper . |
JacksonJsonCodec(TypeToken<T> javaType)
Creates a new instance for the provided
javaType , using a default, newly-allocated
ObjectMapper . |
JacksonJsonCodec(TypeToken<T> javaType,
ObjectMapper objectMapper)
Creates a new instance for the provided
javaType , and using the provided ObjectMapper . |
Modifier and Type | Method and Description |
---|---|
T |
deserialize(ByteBuffer bytes,
ProtocolVersion protocolVersion)
Deserialize the given
ByteBuffer instance according to the CQL type handled by this
codec. |
String |
format(T value)
Format the given value as a valid CQL literal according to the CQL type handled by this codec.
|
T |
parse(String value)
Parse the given CQL literal into an instance of the Java type handled by this codec.
|
ByteBuffer |
serialize(T value,
ProtocolVersion protocolVersion)
Serialize the given value according to the CQL type handled by this codec.
|
protected JavaType |
toJacksonJavaType()
|
accepts, accepts, accepts, accepts, ascii, bigint, blob, cboolean, cdouble, cfloat, cint, counter, custom, date, decimal, duration, getCqlType, getJavaType, inet, list, map, set, smallInt, time, timestamp, timeUUID, tinyInt, toString, tuple, userType, uuid, varchar, varint
public JacksonJsonCodec(Class<T> javaClass)
javaClass
, using a default, newly-allocated
ObjectMapper
.javaClass
- the Java class this codec maps to.public JacksonJsonCodec(TypeToken<T> javaType)
javaType
, using a default, newly-allocated
ObjectMapper
.javaType
- the Java type this codec maps to.public JacksonJsonCodec(Class<T> javaClass, ObjectMapper objectMapper)
javaClass
, and using the provided ObjectMapper
.javaClass
- the Java class this codec maps to.public JacksonJsonCodec(TypeToken<T> javaType, ObjectMapper objectMapper)
javaType
, and using the provided ObjectMapper
.javaType
- the Java type this codec maps to.public ByteBuffer serialize(T value, ProtocolVersion protocolVersion) throws InvalidTypeException
TypeCodec
Implementation notes:
null
input as the equivalent of an
empty collection.
serialize
in class TypeCodec<T>
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 T 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<T>
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(T value) throws InvalidTypeException
TypeCodec
Implementors should take care of quoting and escaping the resulting CQL literal where
applicable. Null values should be accepted; in most cases, implementations should return the
CQL keyword "NULL"
for null
inputs.
Implementing this method is not strictly mandatory. It is used:
BuiltStatement
for a detailed explanation of when
this happens);
QueryLogger
, if parameter logging is enabled;
AggregateMetadata.asCQLQuery(boolean)
;
toString()
implementation of some objects (UDTValue
, TupleValue
, and the internal representation of a ROWS
response), which may
appear in driver logs.
format
in class TypeCodec<T>
value
- An instance of T; may be null
.InvalidTypeException
- if the given value does not have the expected typepublic T parse(String value) throws InvalidTypeException
TypeCodec
Implementors should take care of unquoting and unescaping the given CQL string where
applicable. Null values and empty Strings should be accepted, as well as the string "NULL"
; in most cases, implementations should interpret these inputs has equivalent to a
null
reference.
Implementing this method is not strictly mandatory: internally, the driver only uses it to parse the INITCOND when building the metadata of an aggregate function (and in most cases it will use a built-in codec, unless the INITCOND has a custom type).
parse
in class TypeCodec<T>
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–2020. All rights reserved.