Patrick
05/26/2020, 8:33 AM{
"array": [
"foo",
1,
2
]
}
Now this is apparently not valid JSON, since I need to turn on isLenient = true
in the JSON configuration.
If I turn it on, calling json.stringify()
on an object of class List<Any>
is not a problem, I will get back the correct result.
When I try to parse this JSON though, I get back this error message:
Uncaught Kotlin exception: kotlinx.serialization.SerializationException: Can't locate argument-less serializer for class Any. For generic classes, such as lists, please provide serializer explicitly.
Now I tried to write a custom serializer but I got stuck. First question: Does this error message mean I need to only provide a serializer for class Any
or also for class List
? I interpreted this as only needing a serializer for Any
, so I created a custom serializer like this:
@Serializer(forClass = Any::class)
object AnySerializer {
override fun deserialize(decoder: Decoder): Any {
return decoder.decodeString()
}
}
and annotated the class with List<@Serializable(with = AnySerializer::class) Any>
Then I get this error message:
e: Compilation failed: Please provide implementation of .deserialize method for external class
What did I do wrong?