Class JacksonFragmentTranscoder
java.lang.Object
com.couchbase.client.java.transcoder.subdoc.AbstractFragmentTranscoder
com.couchbase.client.java.transcoder.subdoc.AbstractByteArrayFragmentTranscoder
com.couchbase.client.java.transcoder.subdoc.JacksonFragmentTranscoder
- All Implemented Interfaces:
FragmentTranscoder
@Uncommitted @Private public class JacksonFragmentTranscoder extends AbstractByteArrayFragmentTranscoder
A Jackson-based implementation of a
FragmentTranscoder
, based on AbstractByteArrayFragmentTranscoder
.
This implementation changes the decodeWithMessage
behavior
compared to the parent strategy by attempting to deserialize JSON arrays into JsonArray
and JSON
dictionaries into JsonObject
.
Care should be taken to not use Jackson specific annotations if you want to be able to
easily swap this for another SubdocumentTranscoder implementation at a later time.- Since:
- 2.2
- Author:
- Simon Baslé
-
Constructor Summary
Constructors Constructor Description JacksonFragmentTranscoder(com.couchbase.client.deps.com.fasterxml.jackson.databind.ObjectMapper mapper)
-
Method Summary
Modifier and Type Method Description protected <T> T
byteArrayToClass(byte[] byteArray, int offset, int length, Class<? extends T> clazz)
Deserializes a byte array into a specific class instance.protected Object
byteArrayToGenericObject(byte[] byteArray, int offset, int length)
Deserializes a byte array into a generic Object.<T> T
decodeWithMessage(com.couchbase.client.deps.io.netty.buffer.ByteBuf encoded, Class<? extends T> clazz, String transcodingErrorMessage)
Decode content in aByteBuf
**without releasing it**.protected <T> byte[]
writeValueAsBytes(T value)
Serializes a single value object as an array of bytes.protected void
writeValueIntoStream(OutputStream out, Object o)
Serializes a single object out of a sequence of multiple values, into the sequence'sOutputStream
.Methods inherited from class com.couchbase.client.java.transcoder.subdoc.AbstractByteArrayFragmentTranscoder
doEncodeMulti, doEncodeSingle
Methods inherited from class com.couchbase.client.java.transcoder.subdoc.AbstractFragmentTranscoder
decode, encode, encodeWithMessage
-
Constructor Details
-
JacksonFragmentTranscoder
public JacksonFragmentTranscoder(com.couchbase.client.deps.com.fasterxml.jackson.databind.ObjectMapper mapper)
-
-
Method Details
-
decodeWithMessage
public <T> T decodeWithMessage(com.couchbase.client.deps.io.netty.buffer.ByteBuf encoded, Class<? extends T> clazz, String transcodingErrorMessage) throws TranscodingExceptionDescription copied from interface:FragmentTranscoder
Decode content in aByteBuf
**without releasing it**. Suitable for populating aDocumentFragment
's content.- Specified by:
decodeWithMessage
in interfaceFragmentTranscoder
- Overrides:
decodeWithMessage
in classAbstractByteArrayFragmentTranscoder
- Type Parameters:
T
- the type of the decoded fragment.- Parameters:
encoded
- the encoded fragment value (will not be released).clazz
- the target class for decoded value. UsingObject.class
implies a generic decode, where dictionaries are represented byJsonObject
and arrays byJsonArray
.transcodingErrorMessage
- the error message to be used in the thrownTranscodingException
if the decoding couldn't happen.- Returns:
- a decoded fragment.
- Throws:
TranscodingException
- if the decoding couldn't happen.
-
byteArrayToGenericObject
protected Object byteArrayToGenericObject(byte[] byteArray, int offset, int length) throws IOExceptionDescription copied from class:AbstractByteArrayFragmentTranscoder
Deserializes a byte array into a generic Object. The provided offset and length must be considered when processing the array, which may hold more data that just the value to deserialize. Note that he byte array should not be considered reliable for long-term usage (eg. backing a String) as it might be tied to the originalByteBuf
, which will get released from the heap. If the array represents a non-scalar value, implementations may choose different classes like aJsonObject
or aMap
to instantiate it. This method is called byAbstractByteArrayFragmentTranscoder.decodeWithMessage(ByteBuf, Class, String)
when the clazz parameter isObject.class
.- Specified by:
byteArrayToGenericObject
in classAbstractByteArrayFragmentTranscoder
- Parameters:
byteArray
- the array of bytes containing the value to deserialize (you'll need to copy it if long term usage is needed).offset
- the offset in the array at which the value starts.length
- the number of bytes after the offset that represents the value.- Returns:
- an instance of a suitable generic Object representation of the value.
- Throws:
IOException
-
byteArrayToClass
protected <T> T byteArrayToClass(byte[] byteArray, int offset, int length, Class<? extends T> clazz) throws IOExceptionDescription copied from class:AbstractByteArrayFragmentTranscoder
Deserializes a byte array into a specific class instance. The provided offset and length must be considered when processing the array, which may hold more data that just the value to deserialize. Note that he byte array should not be considered reliable for long-term usage (eg. backing a String) as it might be tied to the originalByteBuf
, which will get released from the heap. This method is called byAbstractByteArrayFragmentTranscoder.decodeWithMessage(ByteBuf, Class, String)
when the clazz parameter isn'tObject.class
.- Specified by:
byteArrayToClass
in classAbstractByteArrayFragmentTranscoder
- Parameters:
byteArray
- the array of bytes containing the value to deserialize (you'll need to copy it if long term usage is needed).offset
- the offset in the array at which the value starts.length
- the number of bytes after the offset that represents the value.clazz
- theClass
to deserialize to.- Returns:
- an instance of a suitable generic Object representation of the value.
- Throws:
IOException
-
writeValueAsBytes
Description copied from class:AbstractByteArrayFragmentTranscoder
Serializes a single value object as an array of bytes. The array will be backing aByteBuf
, so modifications to the array will be visible in the ByteBuf.- Specified by:
writeValueAsBytes
in classAbstractByteArrayFragmentTranscoder
- Parameters:
value
- the value object to serialize.- Returns:
- the array of bytes representing the serialized value object.
- Throws:
IOException
-
writeValueIntoStream
Description copied from class:AbstractByteArrayFragmentTranscoder
Serializes a single object out of a sequence of multiple values, into the sequence'sOutputStream
. Implementation should simply write the bytes corresponding to the serialized value object into the stream.- Specified by:
writeValueIntoStream
in classAbstractByteArrayFragmentTranscoder
- Parameters:
out
- theOutputStream
of bytes representing a JSON sequence of serialized values.o
- the value among the sequence that is currently serialized.- Throws:
IOException
-