question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Exception is kotlinx.serialization.json.internal.JsonDecodingException: Polymorphic serializer was not found for missing class discriminator ('null')

See original GitHub issue

I’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:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
shanshincommented, Jan 11, 2021

com.yekta.example.data.BaseShape@f18051d

In your example BaseShape is abstract, it can’t be instantiated.

Can you show the actual code for classes and deserialization?

1reaction
shanshincommented, Jan 11, 2021

If you using an abstract class for shape polymorphic serialization then expected a special discriminator column with information about the concrete type of shape property.

e.g. for classes

    @kotlinx.serialization.Serializable
    abstract class BaseShape

    @kotlinx.serialization.Serializable
    @SerialName("Rectangle")
    class Rectangle(
        val edges: Int
    ) : BaseShape()

    @kotlinx.serialization.Serializable
     internal data class Test(
        val name: String,
        val shape: BaseShape,
    )

deserialization will be looks like

    val module = SerializersModule {
        polymorphic(BaseShape::class) {
            subclass(Rectangle::class, Rectangle.serializer())
        }
    }

val testData = Json {
            serializersModule = module
            ignoreUnknownKeys = true
            prettyPrint = true
        }.decodeFromString<Test>(jsonString)

and valid JSON is

{        
    "name": "Lorem Ipsum", 
    "shape": { 
        "type": "Rectangle", 
        "edges": 4  
    }
}

see https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/polymorphism.md#open-polymorphism

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found