Paulius Ruminas
03/26/2020, 6:35 PM@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:
install(ContentNegotiation) {
json(json)
}
we respond with:
call.respond(PaymentResult.Authorised)
currently it returns: {}
is it possible to use polymorphic serializer for responses?Sergey Akhapkin
03/26/2020, 9:31 PM@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.