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 aFragmentTranscoder, based onAbstractByteArrayFragmentTranscoder. This implementation changes thedecodeWithMessagebehavior compared to the parent strategy by attempting to deserialize JSON arrays intoJsonArrayand JSON dictionaries intoJsonObject. 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected <T> TbyteArrayToClass(byte[] byteArray, int offset, int length, Class<? extends T> clazz)Deserializes a byte array into a specific class instance.protected ObjectbyteArrayToGenericObject(byte[] byteArray, int offset, int length)Deserializes a byte array into a generic Object.<T> TdecodeWithMessage(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 voidwriteValueIntoStream(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
-
-
-
-
Method Detail
-
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:FragmentTranscoderDecode content in aByteBuf**without releasing it**. Suitable for populating aDocumentFragment's content.- Specified by:
decodeWithMessagein interfaceFragmentTranscoder- Overrides:
decodeWithMessagein 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.classimplies a generic decode, where dictionaries are represented byJsonObjectand arrays byJsonArray.transcodingErrorMessage- the error message to be used in the thrownTranscodingExceptionif 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 IOException
Description copied from class:AbstractByteArrayFragmentTranscoderDeserializes 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 aJsonObjector aMapto instantiate it. This method is called byAbstractByteArrayFragmentTranscoder.decodeWithMessage(ByteBuf, Class, String)when the clazz parameter isObject.class.- Specified by:
byteArrayToGenericObjectin 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:AbstractByteArrayFragmentTranscoderDeserializes 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:
byteArrayToClassin 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- theClassto deserialize to.- Returns:
- an instance of a suitable generic Object representation of the value.
- Throws:
IOException
-
writeValueAsBytes
protected <T> byte[] writeValueAsBytes(T value) throws IOExceptionDescription copied from class:AbstractByteArrayFragmentTranscoderSerializes 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:
writeValueAsBytesin classAbstractByteArrayFragmentTranscoder- Parameters:
value- the value object to serialize.- Returns:
- the array of bytes representing the serialized value object.
- Throws:
IOException
-
writeValueIntoStream
protected void writeValueIntoStream(OutputStream out, Object o) throws IOException
Description copied from class:AbstractByteArrayFragmentTranscoderSerializes 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:
writeValueIntoStreamin classAbstractByteArrayFragmentTranscoder- Parameters:
out- theOutputStreamof bytes representing a JSON sequence of serialized values.o- the value among the sequence that is currently serialized.- Throws:
IOException
-
-