Unable deserialize into a polymorphic sealed data class without identifier fields
See original GitHub issueI 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:
- Created 6 years ago
- Reactions:8
- Comments:5 (3 by maintainers)
Top 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 >
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 Free
Top 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
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.
How would it map to the correct class, by parameter names only? What about ambiguous classes with the same parameter list?