Firstly, congratulations for the awesome project. I started using it two days ago and it is very fast and simple.
I would like to use kotlinx.serialization instead of Jackson. How can I do it?
Firstly, congratulations for the awesome project. I started using it two days ago and it is very fast and simple.
Thanks!
I would like to use kotlinx.serialization instead of Jackson. How can I do it?
You can configure the JavalinJson plugin:
JavalinJson.toJsonMapper = object : ToJsonMapper {
override fun map(obj: Any) = { /* Your implementation here */ }
}
JavalinJson.fromJsonMapper = object : FromJsonMapper {
override fun <T> map(json: String, targetClass: Class<T>) { /* Your implementation here */ }
}
These will then be used when calling ctx.json() or ctx.body<MyClass>().
Ok. Thanks for the quick response. I'll try it.
@wellingtoncosta Have you finally achieved kotlinx serializer replacement ?
I'm stuck here:
val kotlinx = Json { coerceInputValues = true }
JavalinJson.toJsonMapper = object : ToJsonMapper {
override fun map(obj: Any): String = kotlinx.encodeToString(obj)
}
JavalinJson.fromJsonMapper = object : FromJsonMapper {
override fun <T> map(json: String, targetClass: Class<T>): T = kotlinx.decodeFromString(json)
}
The decodeFromString part is not working. I also tried:
JavalinJson.fromJsonMapper = object : FromJsonMapper {
override inline fun <reified T> map(json: String, targetClass: Class<T>): T = kotlinx.decodeFromString(json)
}
but I get a warning: Override by an inline function and an error: Override by a function with reified type parameter.
I can't figure out how to implement this override.
@Freezystem I am trying too as well! Have you/someone found a solution yet?
Hi @Welcius, actually no. As of today I couldn't figure it out yet.
There was a proposal for using extension function but I didn't succeed implementing it.
Hence I resort using explicit serialization/deserialization in my endpoints like so:
// first I'm declaring a serializer/deserializer config on top of my file
val kotlinx = Json {
ignoreUnknownKeys = true
coerceInputValues = true
encodeDefaults = false
}
// then later in the handler I do
fun someHandler(ctx: Context): Context {
kotlinx.decodeFromString<MyDataClass>(ctx.body())
// do some stuff..
// and finally
return ctx.status(200)
.result(kotlinx.encodeToString(res))
.contentType("application/json")
}
It's cumbersome but it works as expected.
Once I get a little bit more time I'll eventually try to make it works properly but I'm too busy for the moment. :disappointed:
If you ever find something interesting feel free to share here or on the other issue, I'd be glad to review it with you.
Most helpful comment
Thanks!
You can configure the
JavalinJsonplugin:These will then be used when calling
ctx.json()orctx.body<MyClass>().