@InterfaceStability.Uncommitted @InterfaceAudience.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.
Constructor and Description |
---|
JacksonFragmentTranscoder(com.couchbase.client.deps.com.fasterxml.jackson.databind.ObjectMapper mapper) |
Modifier and Type | Method and 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 a
ByteBuf 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’s
OutputStream . |
doEncodeMulti, doEncodeSingle
decode, encode, encodeWithMessage
public JacksonFragmentTranscoder(com.couchbase.client.deps.com.fasterxml.jackson.databind.ObjectMapper mapper)
public <T> T decodeWithMessage(com.couchbase.client.deps.io.netty.buffer.ByteBuf encoded, Class<? extends T> clazz, String transcodingErrorMessage) throws TranscodingException
FragmentTranscoder
Decode content in a ByteBuf
without releasing it. Suitable for populating a DocumentFragment
’s content.
decodeWithMessage
in interface FragmentTranscoder
decodeWithMessage
in class AbstractByteArrayFragmentTranscoder
T
- the type of the decoded fragment.encoded
- the encoded fragment value (will not be released).clazz
- the target class for decoded value. Using Object.class
implies a generic decode, where dictionaries are represented by JsonObject
and arrays by JsonArray
.transcodingErrorMessage
- the error message to be used in the thrown TranscodingException
if the decoding couldn’t happen.TranscodingException
- if the decoding couldn’t happen.protected Object byteArrayToGenericObject(byte[] byteArray, int offset, int length) throws IOException
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 original ByteBuf
, which will get released from the heap.
If the array represents a non-scalar value, implementations may choose different classes like a JsonObject
or a Map
to instantiate it.
This method is called by AbstractByteArrayFragmentTranscoder.decodeWithMessage(ByteBuf, Class, String)
when the clazz parameter is Object.class
.
byteArrayToGenericObject
in class AbstractByteArrayFragmentTranscoder
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.IOException
protected <T> T byteArrayToClass(byte[] byteArray, int offset, int length, Class<? extends T> clazz) throws IOException
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 original ByteBuf
, which will get released from the heap.
This method is called by AbstractByteArrayFragmentTranscoder.decodeWithMessage(ByteBuf, Class, String)
when the clazz parameter isn’t Object.class
.
byteArrayToClass
in class AbstractByteArrayFragmentTranscoder
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
- the Class
to deserialize to.IOException
protected <T> byte[] writeValueAsBytes(T value) throws IOException
AbstractByteArrayFragmentTranscoder
Serializes a single value object as an array of bytes. The array will be backing a ByteBuf
, so modifications to the array will be visible in the ByteBuf.
writeValueAsBytes
in class AbstractByteArrayFragmentTranscoder
value
- the value object to serialize.IOException
protected void writeValueIntoStream(OutputStream out, Object o) throws IOException
AbstractByteArrayFragmentTranscoder
Serializes a single object out of a sequence of multiple values, into the sequence’s OutputStream
. Implementation should simply write the bytes corresponding to the serialized value object into the stream.
writeValueIntoStream
in class AbstractByteArrayFragmentTranscoder
out
- the OutputStream
of bytes representing a JSON sequence of serialized values.o
- the value among the sequence that is currently serialized.IOException
Copyright © 2015 Couchbase, Inc.