Hi, we have this sealed class: ```@Serializable se...
# ktor
p
Hi, we have this sealed class:
Copy code
@Serializable
sealed class PaymentResult {
    @Serializable
    object Authorised : PaymentResult()
    @Serializable
    data class Error(val refusalReason: String) : PaymentResult()
    @Serializable
    data class Refused(val refusalReason: String, val refusalReasonCode: String) : PaymentResult()
}
we have installed json content negotiation:
Copy code
install(ContentNegotiation) {
    json(json)
}
we respond with:
Copy code
call.respond(PaymentResult.Authorised)
currently it returns:
{}
is it possible to use polymorphic serializer for responses?
s
I cannot help you with kotlinx.serialization, but you can use polymorphic serializers with Jackson, like this:
Copy code
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes(
    JsonSubTypes.Type(value = Base.A::class, name = "A"),
    JsonSubTypes.Type(value = Base.B::class, name = "B")
)
sealed class Base {
    object A : Base()
    data class B(val x:Int) : Base()
}

....

    install(ContentNegotiation) {
        jackson(ContentType.Application.Json, configureObjectMapper)
    }
....
val configureObjectMapper: ObjectMapper.() -> Unit = {
    registerModule(Jdk8Module())
    .registerModule(JavaTimeModule())
....
}
A -> { "type" : "A" } B -> { "type" : "B", "x": 42 } It works in both directions.