Data Serialization
The library uses a string encoding for the message payload that is published or received from a queue. The proper string encoding/decoding is performed by the underlying SDK, allowing you to only focus on the string serialization part.
Data Serializer
A Serializer is defined as a SAM interface that is basically a T => String. Defining a new one can be done easily by providing an implicit conversion function from the type T to serialize to a String.
For instance, adding a serializer for Ints can be done as follows.
import com.commercetools.queue.Serializer
implicit val serializer: Serializer[Int] = _.toString
// serializer: Serializer[Int] = repl.MdocSession$MdocApp$$anonfun$1@78562067
The library provides natively a no-op serializer for Strings.
Data Deserializer
A Deserializer is defined as a SAM interface that is basically a String => Either[Throwable, T]. Defining a new one can be done easily by providing an implicit conversion function from a String to serialize to either a value of the type T or an exception.
For instance, adding a deserializer for Ints can be done as follows.
import cats.syntax.either._
import com.commercetools.queue.Deserializer
implicit val deserializer: Deserializer[Int] = s => Either.catchNonFatal(s.toInt)
// deserializer: Deserializer[Int] = repl.MdocSession$MdocApp$$anonfun$2@24f47d
The library provides natively a no-op deserializer for Strings.
Library integration
We provide integration with some well-established serialization libraries (e.g. to perform JSON serialization). Have a look at the Library Integration section for your favorite library.