bbaldino
05/02/2022, 11:55 PMtype
field when serializing polymorphic classes…I’m curious if it’s possible to customize the name of that field (type
) via an annotation? Or would that require a custom serializer?ephemient
05/03/2022, 12:43 AMbbaldino
05/03/2022, 3:48 PMbbaldino
05/03/2022, 4:28 PMephemient
05/03/2022, 4:54 PM@Serializable
@JsonClassDiscriminator("kind")
sealed class Foo {
@Serializable
object One : Foo()
}
Json.encodeToString<Foo>(Foo.One) // => {"kind":"One"}
or
@Serializable
sealed class Bar {
@Serializable
object Two : Bar()
}
val json = Json {
classDiscriminator = "kind"
}
json.encodeToString<Bar>(Bar.Two) // => {"kind":"Two"}
bbaldino
05/03/2022, 5:07 PM