I saw it’s possible to customize the value of the ...
# serialization
b
I saw it’s possible to customize the value of the
type
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?
b
thank you!
Hm, even with the experimental opt-in enabled, it doesn’t seem to be working for me.
e
Copy code
@Serializable
@JsonClassDiscriminator("kind")
sealed class Foo {
    @Serializable
    object One : Foo()
}
Json.encodeToString<Foo>(Foo.One) // => {"kind":"One"}
or
Copy code
@Serializable
sealed class Bar {
    @Serializable
    object Two : Bar()
}
val json = Json {
    classDiscriminator = "kind"
}
json.encodeToString<Bar>(Bar.Two) // => {"kind":"Two"}
b
d’oh, i’m an idiot…put the annotation on the wrong class. thanks @ephemient.