https://kotlinlang.org logo
#ktor
Title
# ktor
m

Mateu

02/27/2024, 12:09 PM
Hi all, I'm using Ktor with Json content serializer. Everything is perfect but now I want to be able to return Eithers form Arrow. The serializers are given by Arrow team but I don't know how to configure them (the either is directly the return of the service). Anyone knows how to add a custom serializer for a return type? Thanks,
a

Aleksei Tirman [JB]

02/27/2024, 12:38 PM
Can you please illustrate your question with some code?
m

Mateu

02/27/2024, 1:14 PM
I've simplified it to:
Copy code
get<Either<String, String>>{
    val result : Either<String, String> = "a".right()
    call.respond(result)
}
p

phldavies

02/27/2024, 1:28 PM
Generally you wouldn't want to serialize/respond with the Left of an either the same way you would the Right (as you'd likely want a different status code or body for different domain errors)
An example way of handling an
Either
from a route would be to have an extension on
PipelineContext<Unit, ApplicationCall>
that would handle calling the appropriate
call.respond
based on if the either is Left or Right
m

Mateu

02/27/2024, 1:50 PM
I know is kind of "dirty". The thing is that I'm building the client and server. If I return and recieve an Either I don't need to do the transform and untransform.
2 Views