Class 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 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 TranscodingException
      Description copied from interface: FragmentTranscoder
      Decode content in a ByteBuf **without releasing it**. Suitable for populating a DocumentFragment's content.
      Specified by:
      decodeWithMessage in interface FragmentTranscoder
      Overrides:
      decodeWithMessage in class AbstractByteArrayFragmentTranscoder
      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. 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.
      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: 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.
      Specified by:
      byteArrayToGenericObject in class AbstractByteArrayFragmentTranscoder
      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 IOException
      Description 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 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.
      Specified by:
      byteArrayToClass in class AbstractByteArrayFragmentTranscoder
      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 - the Class to deserialize to.
      Returns:
      an instance of a suitable generic Object representation of the value.
      Throws:
      IOException
    • writeValueAsBytes

      protected <T> byte[] writeValueAsBytes​(T value) throws IOException
      Description copied from class: 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.
      Specified by:
      writeValueAsBytes in class AbstractByteArrayFragmentTranscoder
      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: 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.
      Specified by:
      writeValueIntoStream in class AbstractByteArrayFragmentTranscoder
      Parameters:
      out - the OutputStream of bytes representing a JSON sequence of serialized values.
      o - the value among the sequence that is currently serialized.
      Throws:
      IOException