Often, we have a type-safe contract specifying what we accept and what we return in requests. Serializing this data can be challenging, especially when dealing with different formats or migrating to a new one. Even if it's not the case, defining your own wrappers or extensions from scratch takes time and, as mentioned before, can lead to potential problems in the future. That's why rsocket-kotlin-router
provides a ready-to-use pragmatic serialization system.
Warning
This feature is experimental, and migration steps might be required in the future.
First, add the necessary dependencies:
dependencies {
implementation("com.y9vad9.rsocket.router:router-serialization-core:$version")
// for JSON support
implementation("com.y9vad9.rsocket.router:router-serialization-json:$version")
// for ProtoBuf support
implementation("com.y9vad9.rsocket.router:router-serialization-protobuf:$version")
// for Cbor support
implementation("com.y9vad9.rsocket.router:router-serialization-cbor:$version")
}
To add serialization to your requests, install the required ContentSerializer in your router. For example, using JsonContentSerializer:
val router = router {
// ...
serialization { JsonContentSerializer() }
// ...
}
You can use the bundled extensions as follows:
routing {
route("authorization") {
requestResponse<Foo, Bar>("register") { foo: Foo ->
return@requestResponse Bar(/* ... */)
}
// other types of the requests have the same extensions
}
}
To add support for other existing formats, you can simply extend ContentSerializer
. You can take a look at
JsonContentSerializer
as an example.