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.

Unable deserialize into a polymorphic sealed data class without identifier fields

See original GitHub issue

I have been trying to mimic dealing with polymorphic responses using sealed data classes, similar to how Json can be handled in Scala using libraries such as Circe; however, I cannot get it to work and there are no tests in this module that show the module can handle it.

For example, this simple test will fail as Jackson cannot create a TestClass instance. It is also not possible in this instance to use the Polymorphic type annotations as there is no field that can be used to identify the type:

import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import kotlin.test.assertTrue


sealed class TestClass {
    data class TestMessageA(val name: String) : TestClass()
    data class TestMessageB(val age: Int) : TestClass()
}

class JacksonTest {

    @Nested
    inner class `Given JSON for a sealed class member` {
        @Test
        fun `it should map the Json to a representation`() {
            val json = """{"name": "test"}"""
            val mapper = com.fasterxml.jackson.module.kotlin.jacksonObjectMapper()

            val testData = mapper.readValue<TestClass>(json)

            assertTrue {
                when (testData) {
                    is TestClass.TestMessageA -> true
                    is TestClass.TestMessageB -> false
                }
            }
        }
    }
}```

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:8
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
apatridacommented, Jul 18, 2018

This is out of scope of what this module can do with Jackson. We follow the rules and limits Jackson data binding sets out and work from there with what is possible. Everything you show is already supported minus the deserialization without some type identifier in the JSON to an abstract base type.

1reaction
apatridacommented, Jul 14, 2018

How would it map to the correct class, by parameter names only? What about ambiguous classes with the same parameter list?

Read more comments on GitHub >

github_iconTop Results From Across the Web

json - Missing identity field with polymorphic (de)serialisation ...
I found a solution in manually adding an already initialised field to the body of subclasses with the name of the subclass. Eg....
Read more >
Kotlix.serialization polymorphism using property presense
I want to implement for my model a polymorphism without a presense of discriminator field. I managed to do this relatively easily in...
Read more >
Ad-hoc polymorphism in JSON with Kotlin - Adit Lal
We want to use Kotlin data classes for concise code, non-nullable types for ... when the mapping fails completely (required field missing).
Read more >
How to serialize properties of derived classes with System ...
Learn how to serialize polymorphic objects while serializing to and deserializing from JSON in .NET.
Read more >
Serialization with Jackson - Documentation - Akka
To enable Jackson serialization for a class you need to configure it or one of ... not nested classes that it references in...
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