Skip to content

Commit

Permalink
Add serialization documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
satabin committed Apr 29, 2024
1 parent 9e4866c commit 853b21f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
14 changes: 10 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import laika.config.PrettyURLs
import laika.config.LinkConfig
import laika.config.ApiLinks
import laika.config.SourceLinks

ThisBuild / tlBaseVersion := "0.0"

Expand Down Expand Up @@ -103,10 +104,15 @@ lazy val docs = project
.settings(
tlSiteApiPackage := Some("com.commercetools.queue"),
tlSiteHelium := CTTheme(tlSiteHelium.value),
laikaConfig := tlSiteApiUrl.value.fold(laikaConfig.value) { apiUrl =>
laikaConfig.value.withConfigValue(LinkConfig.empty
.addApiLinks(ApiLinks(baseUri = apiUrl.toString().dropRight("index.html".size))))
},
laikaConfig := tlSiteApiUrl.value
.fold(laikaConfig.value) { apiUrl =>
laikaConfig.value.withConfigValue(
LinkConfig.empty
.addApiLinks(ApiLinks(baseUri = apiUrl.toString().dropRight("index.html".size)))
.addSourceLinks(
SourceLinks(baseUri = "https://github.com/commercetools/fs2-queues", suffix = "scala")
))
},
laikaExtensions += PrettyURLs,
tlFatalWarnings := false,
libraryDependencies ++= List(
Expand Down
33 changes: 33 additions & 0 deletions docs/getting-started/serialization.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
{% nav = true %}
# 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 @:api(com.commercetools.queue.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 `Int`s can be done as follows.

```scala mdoc
import com.commercetools.queue.Serializer

implicit val serializer: Serializer[Int] = _.toString
```

The library provides natively a _no-op_ serializer for `String`s.

## Data `Deserializer`

A @:api(com.commercetools.queue.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 `Int`s can be done as follows.

```scala mdoc
import cats.syntax.either._

import com.commercetools.queue.Deserializer

implicit val deserializer: Deserializer[Int] = s => Either.catchNonFatal(s.toInt)
```

The library provides natively a _no-op_ deserializer for `String`s.

## 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.

0 comments on commit 853b21f

Please sign in to comment.