Serialization
Restate sends data over the network for storing state, journaling actions, awakeables, etc. There are multiple ways to specify which (de)serializers should be used.
The SDK uses by default Jackson Databind for the Java API, and Kotlinx serialization for the Kotlin API.
Customizing serialization
There are different entrypoints in the SDK to customize (de)serialization, depending on your needs.
Custom Jackson ObjectMapper
You can customize the Jackson's ObjectMapper
by subclassing the JacksonSerdeFactory
class:
class MyJacksonSerdeFactory extends JacksonSerdeFactory {public MyJacksonSerdeFactory() {super(new ObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, true));}}
And annotating your services with @CustomSerdeFactory
:
@CustomSerdeFactory(MyJacksonSerdeFactory.class)@Serviceclass ServiceWithCustomJacksonObjectMapper {@Handlerpublic String greet(Context context) {return "Hello world";}}
Custom Kotlinx Serialization Json
You can customize the Kotlinx Serialization Json
by subclassing the KotlinSerializationSerdeFactory
class:
class MyJsonSerdeFactory : KotlinSerializationSerdeFactory(json = Json { prettyPrint = true })
And annotating your services with @CustomSerdeFactory
:
@CustomSerdeFactory(MyJsonSerdeFactory::class)@Serviceclass ServiceWithCustomSerdeFactory {@Handler suspend fun greet(ctx: Context) = "Hello world!"}
Use custom serializer for a specific operation
If you need for a specific operation (e.g. for Context.run
or Context.Awakeable
) a custom (de)serializer, you can implement one using the interface Serde
:
class MyPersonSerde implements Serde<Person> {@Overridepublic Slice serialize(Person person) {// convert value to a byte array, then wrap in a Slicereturn Slice.wrap(person.toBytes());}@Overridepublic Person deserialize(Slice slice) {// convert value to Personreturn Person.fromBytes(slice.toByteArray());}}
And then use it, for example, in combination with ctx.run
:
ctx.run(new MyPersonSerde(), () -> new Person());
Use another serialization library
If you want to use a different serialization library throughout your service, we suggest implementing SerdeFactory
and annotating the service class with @CustomSerdeFactory
. Refer to the Javadocs for more details.