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.

Interface polymorphic deserialization with nested subclasses

See original GitHub issue

Describe the bug Can´t deserialize a JSON using polymorphic deserialization and interfaces, if the structure coin nested implementation of the interface.

To Reproduce Given the following class hierarchies:

    interface Deliverable
 
    @Serializable
    @SerialName("box")
    data class Box(val address: String) : Deliverable

    @Serializable 
    @SerialName("parcel")
    data class Parcel(val senderNumber: Int, val box: Box) : Deliverable

And the corresponding SerializersModule to register the implementations:

     private val module = SerializersModule {
        polymorphic(Deliverable::class) {
            subclass(Box::class)
            subclass(Parcel::class)
        }
    }

Whenever I try to deserialize a matching JSON string. i.e :

  @Test
    fun `deserialize parcel`() {

        val deliverable = """
        {
            "type":"parcel",
            "senderNumber": "5",
            "box": {
                "type": "box",
                "address": "new york"
            }
        }
        """.trimIndent()

        val box = Box("new york")
        val parcel = Parcel(5, box)

        val result = Json { serializersModule = module}.decodeFromString<Deliverable>(deliverable)

        Truth.assertThat(result).isEqualTo(parcel)
    }

I get the following error:

kotlinx.serialization.json.internal.JsonDecodingException: Encountered unknown key 'type'. Use 'ignoreUnknownKeys = true' in 'Json {}' builder to ignore unknown keys

My assumption is that since Parcel has an explicit implementation declared rather than a Deliverable then the polymorphic strategy for interfaces its not applied and cant account for the type field?.

But if that’s the case how’s the correct way of handling these situations?

Disclaimer: Adding 'ignoreUnknownKeys = true' like the error suggest fixes the issues but it doesn’t feel right to enable this for this use case, or maybe I´m missing a key part here

Environment

  • Kotlin version: [1.4.10]
  • Library version: [1.0.1]
  • Kotlin platforms: [JVM]
  • Gradle version: [6.5]
  • IDE version [Android Studio 4.1]

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Dominaezzzcommented, May 29, 2021

Can this be closed now?

1reaction
testowercommented, Nov 12, 2020

Can confirm I’m having the same problem.

kotlin_version=1.4.10 kotlinx_serialization_version=1.0.1

Read more comments on GitHub >

github_iconTop Results From Across the Web

Polymorphic deserialization of nested list object in Kotlin
So I have a polymorphic deserializer built in kotlinx, the issue is the polymorphic class is deeply nested within the JSON. I can...
Read more >
Interface polymorphic deserialization with nested subclasses -
Describe the bug. Can´t deserialize a JSON using polymorphic deserialization and interfaces, if the structure coin nested implementation of the interface.
Read more >
How to serialize properties of derived classes with System ...
In this article. Serialize properties of derived classes; Polymorphic type discriminators; Configure polymorphism with the contract model ...
Read more >
Deserializing JSON into polymorphic classes with System.Text ...
In this post, I'll explain how to write a custom JsonConverter for System.Text.Json to help with deserialization for such cases.
Read more >
Polymorphism and Inheritance with Jackson - OctoPerf
Learn how to serialize and deserialize polymorphic object trees with Jackson Json Databind. Using practical code examples to make it easy to ...
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