Patrick
05/25/2020, 8:42 AMinterface ExampleInterface
@Serializable
enum class Example: ExampleInterface {
TEST,
TEST2
}
@Serializable
enum class Example2: ExampleInterface {
WOW,
WOW2
}
@Serializable
data class ExampleWrapper(val m: ExampleInterface)
fun main() {
val module = SerializersModule {
polymorphic(ExampleInterface::class) {
Example::class with Example.serializer()
Example2::class with Example2.serializer()
}
}
val json = Json(JsonConfiguration.Stable, context = module)
val jsonData = json.stringify(ExampleWrapper.serializer(), ExampleWrapper(Example.TEST))
println(jsonData)
}
When I run this, the following error message is displayed:
Uncaught Kotlin exception: kotlin.IllegalStateException: Enums cannot be serialized polymorphically with 'type' parameter. You can use 'JsonConfiguration.useArrayPolymorphism' instead
Now if I do what the error message says, I will get back something like this:
{
"m": [
"barcodeParser.Example",
"TEST"
]
}
But I would rather have it look like this:
{
"m": "TEST"
}
How can I achieve this?Kroppeb
05/25/2020, 11:26 AMjson
{
"m":"TEST"
}
Without array polymorphism you could maybe have the following if it weren't for the fact that it seems that Enum's can't be used in polymorphism
json
{
"m":{"type":"Example", "value":"TEST"}
}
The only way I see to fix this is to write a custom serializer.Patrick
05/25/2020, 11:45 AMPatrick
05/25/2020, 11:47 AMKroppeb
05/25/2020, 11:59 AMPatrick
05/25/2020, 1:43 PM