Interface DataConverter

All Known Implementing Classes:
CodecDataConverter, DefaultDataConverter

public interface DataConverter
Used by the framework to serialize/deserialize method parameters that need to be sent over the wire.

Most users should never implement this interface until absolutely needed.

Instead, users should implement
  • PayloadConverter to customize Object <-> Payload (bytes) conversion
  • PayloadCodec to perform Payload (bytes) <-> Payload (bytes) encoding (like encryption or compression)
A custom PayloadConverter can be registered on DefaultDataConverter instance. For that: A DataConverter created on previous step may be bundled with PayloadCodecs using CodecDataConverter or used directly if no custom PayloadCodecs are needed.

DataConverter is expected to pass the RawValue payload through without conversion. Though it should still apply the PayloadCodec to the RawValue payloads.

  • Method Details

    • getDefaultInstance

      @Deprecated static DataConverter getDefaultInstance()
      Deprecated.
    • toPayload

      <T> Optional<io.temporal.api.common.v1.Payload> toPayload(T value) throws DataConverterException
      Parameters:
      value - value to convert
      Returns:
      a Payload which is a protobuf message containing byte-array serialized representation of value. Optional here is for legacy and backward compatibility reasons. This Optional is expected to always be filled.
      Throws:
      DataConverterException - if conversion fails
    • fromPayload

      <T> T fromPayload(io.temporal.api.common.v1.Payload payload, Class<T> valueClass, Type valueType) throws DataConverterException
      Throws:
      DataConverterException
    • toPayloads

      Optional<io.temporal.api.common.v1.Payloads> toPayloads(Object... values) throws DataConverterException
      Implements conversion of a list of values.
      Parameters:
      values - Java values to convert to String.
      Returns:
      converted value. Return empty Optional if values are empty.
      Throws:
      DataConverterException - if conversion of the value passed as parameter failed for any reason.
    • fromPayloads

      <T> T fromPayloads(int index, Optional<io.temporal.api.common.v1.Payloads> content, Class<T> valueType, Type valueGenericType) throws DataConverterException
      Implements conversion of a single Payload from the serialized Payloads.
      Parameters:
      index - index of the value in the payloads
      content - serialized value to convert to Java objects.
      valueType - type of the value stored in the content
      valueGenericType - generic type of the value stored in the content
      Returns:
      converted Java object
      Throws:
      DataConverterException - if conversion of the data passed as parameter failed for any reason.
    • fromPayloads

      default Object[] fromPayloads(Optional<io.temporal.api.common.v1.Payloads> content, Class<?>[] parameterTypes, Type[] genericParameterTypes) throws DataConverterException
      Implements conversion of the whole content Payloads into an array of values of different types.

      Implementation note
      This method is expected to return an array of the same length as parameterTypes. If content has not enough Payload elements, this method provides default instances.

      Parameters:
      content - serialized value to convert to Java objects.
      parameterTypes - types of the values stored in the @code content}
      genericParameterTypes - generic types of the values stored in the content
      Returns:
      array if converted Java objects
      Throws:
      DataConverterException - if conversion of the data passed as parameter failed for any reason.
    • failureToException

      @Nonnull default RuntimeException failureToException(@Nonnull io.temporal.api.failure.v1.Failure failure)
      Instantiate an appropriate Java Exception from a serialized Failure object. The default implementation delegates the conversion process to an instance of FailureConverter, using this data converter for payload decoding.
      Parameters:
      failure - Failure protobuf object to deserialize into an exception
      Throws:
      NullPointerException - if failure is null
    • exceptionToFailure

      @Nonnull default io.temporal.api.failure.v1.Failure exceptionToFailure(@Nonnull Throwable throwable)
      Serialize an existing Throwable object into a Failure object. The default implementation delegates the conversion process to an instance of FailureConverter, using this data converter for payload encoding.
      Parameters:
      throwable - a Throwable object to serialize into a Failure protobuf object
      Throws:
      NullPointerException - if throwable is null
    • withContext

      @Experimental @Nonnull default DataConverter withContext(@Nonnull SerializationContext context)
      A correct implementation of this interface should have a fully functional "contextless" implementation. Temporal SDK will call this method when a knowledge of the context exists, but DataConverter can be used directly by user code and sometimes SDK itself without any context.

      Note: this method is expected to be cheap and fast. Temporal SDK doesn't always cache the instances and may be calling this method very often. Users are responsible to make sure that this method doesn't recreate expensive objects like Jackson's ObjectMapper on every call.

      Parameters:
      context - provides information to the data converter about the abstraction the data belongs to
      Returns:
      an instance of DataConverter that may use the provided context for serialization
      See Also:
    • arrayFromPayloads

      @Deprecated static Object[] arrayFromPayloads(DataConverter converter, Optional<io.temporal.api.common.v1.Payloads> content, Class<?>[] parameterTypes, Type[] genericParameterTypes) throws DataConverterException
      Deprecated.
      use fromPayloads(int, Optional, Class, Type). This is an SDK implementation detail and never was expected to be exposed to users.
      Throws:
      DataConverterException