Exception is kotlinx.serialization.json.internal.JsonDecodingException: Polymorphic serializer was not found for missing class discriminator ('null')
See original GitHub issueI’m getting Exception is kotlinx.serialization.json.internal.JsonDecodingException: Polymorphic serializer was not found for missing class discriminator ('null')
.
@kotlinx.serialization.Serializable
abstract class BaseShape
@kotlinx.serialization.Serializable
class Rectangle(
val edges: Int
) : BaseShape()
@kotlinx.serialization.Serializable
internal data class Test(
val name: String,
val shape: BaseShape,
)
val testData = Json {
ignoreUnknownKeys = true
prettyPrint = true
}.decodeFromString<Test>(jsonString)
I’m trying to deserialize the json to get Test
object. And here it’s my json;
{
"name": "Lorem Ipsum",
"shape": {
"edges": 4
}
}
Environment
- Kotlin version: 1.4.21
- Library version: 1.0.1
- Kotlin platforms: JVM
- Gradle version: 6.5
- IDE version: Android Studio 4.1.1
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Polymorphic serializer was not found for missing class ...
Describe the bug Unable to deserialize json string. Upon performing decodeFromString() throws Exception: kotlinx.serialization.json.internal ...
Read more >Polymorphic serializer was not found for missing class ...
We can use JsonContentPolymorphicSerializer. According to documentation: Base class for custom serializers that allows selecting polymorphic ...
Read more >JsonClassDiscriminator - Kotlin
Specifies key for class discriminator value used during polymorphic serialization in Json. Provided key is used only for an annotated class and its ......
Read more >Polymorphic serializer was not found for missing class ...
Coding example for the question kotlinx-serialization: Polymorphic serializer was not found for missing class discriminator ('null')-kotlin.
Read more >HttpResponse.bodyAsChannel should not be converted by ...
Exception in thread "main" kotlinx.serialization.json.internal.JsonDecodingException: Polymorphic serializer was not found for missing class ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In your example
BaseShape
is abstract, it can’t be instantiated.Can you show the actual code for classes and deserialization?
If you using an abstract class for
shape
polymorphic serialization then expected a special discriminator column with information about the concrete type ofshape
property.e.g. for classes
deserialization will be looks like
and valid JSON is
see https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/polymorphism.md#open-polymorphism