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.

Polymorphic json serialization with inherited fields is invalid

See original GitHub issue

Describe 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
altavircommented, Jan 14, 2020

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.

Read more comments on GitHub >

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

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