Polymorphic json serialization with inherited fields is invalid
See original GitHub issueDescribe the bug
If I have an abstract class with a field, and other classes which extend this abstract class, the field from the abstract class will have the same property name as the first field from the concrete class.
To Reproduce
@Serializable
abstract class Snippet(
@SerialName("objectFieldName") val objectFieldName: String
)
@Serializable
data class TestSnippet(
@SerialName("experiments") val experiments: List<KrakenExperiment>
) : Snippet("test")
@Serializable
data class ScreenSnippet(
@SerialName("name") val name: String,
@SerialName("uuid") val uuid: String? = null,
@SerialName("source") val source: String? = null
) : Snippet("screen")
val snippetModule = SerializersModule {
polymorphic(Snippet::class) {
ScreenSnippet::class with ScreenSnippet.serializer()
TestSnippet::class with TestSnippet.serializer()
}
}
val json = Json(
configuration = JsonConfiguration.Stable,
context = snippetModule
)
val testSnippet = TestSnippet(emptyList())
val screenSnippet = ScreenSnippet("one", "two", "three")
println (json.stringify(TestSnippet.serializer(), testSnippet))
println (json.stringify(ScreenSnippet.serializer(), screenSnippet))
Outputs:
{"experiments":"test","experiments":[]}
{"name":"screen","name":"one","uuid":"two","source":"three"}
Expected behavior It should output:
{"objectFieldName":"test","experiments":[]}
{"objectFieldName":"screen","name":"one","uuid":"two","source":"three"}
Environment
- Kotlin version: 1.3.61
- Library version: 0.14.0
- Kotlin platforms: JVM, android; this code is in kotlin MP “common” source set
- Gradle version: 5.6.4
- IDE version: Android Studio 4 Canary 8
- Other relevant context: (AdoptOpenJDK)(build 1.8.0_232-b09)
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top Results From Across the Web
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 >Polymorphic Serialization with .NET System.Text.Json
So you've decided you needed to use inheritance within your object model but are struggling to serialize all the data present on the...
Read more >Json.Net Serialization of Type with Polymorphic Child Object
Having the subtype information in the container class is problematic for two reasons: The container class instance is not accessible when ...
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 >Will An Exception Be Thrown If The Base Class Isn't Marked ...
Polymorphic json serialization with inherited fields is invalid #652 Snippettest @Serializable data class ScreenSnippet @SerialNamename val name: String ...
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
It is good old #378. I also had the problems in the same source set. Guys, the issue is critical. Please fix it ASAP. Right now it tremendously hinders library development since I need to duplicate all fields in final classes. The simple proposal is to add serialization order to descriptor and generate those numbers automatically when the descriptor itself is being generated. Maybe there is a better solution.
https://github.com/Kotlin/kotlinx.serialization/issues/378#issuecomment-663134060