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.

Inherited serializable fields lead to incorrect serialization order

See original GitHub issue

This Issue is producable with kotlin >=1.3.20.

Consider the following scenario with a base class:

import kotlinx.serialization.Serializable

@Serializable
abstract class TestBase {
    val b = "val b"
    val a = "val a"
}

…and one (or two) implementations

@Serializable
class Test : TestBase() {
    val c = "val c"
}

@Serializable
class Test2 : TestBase() {
    val c2 = "val c2"
}

The serialized output of a “Test” object with Json(indented = true) is not consistent: Sometimes it will be as expected, but sometimes it will be this:

{
    "a": "val b",
    "b": "val a",
    "c": "val c"
}

However, I was not able to get an absolutely clear order of file manipulation to yield the above output, but In order to reproduce the behavior, I would usually have to change something in the implementation class(es) Test or Test2, like remove or add the second implementation. It also seemed to only work, when base- and implementation classes were in separate Files.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
sandwwraithcommented, Jul 23, 2020

I can confirm that most of the bugs related to multi-module/project setups are fixed in Kotlin 1.4 (starting from 1.4-M3, to be precise). So this long and painful story is finally over.

2reactions
altavircommented, Aug 17, 2019

The only workaround I found is to mark ancestor as non-serializeable and override all properties in the child. It does the trick, but requires a lot of duplicated code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Object Serialization with Inheritance in Java - GeeksforGeeks
Case 2: If a superclass is not serializable, then subclass can still be serialized. Even though the superclass doesn't implement a Serializable ...
Read more >
Order of fields when serializing the derived class in JSON.NET
Is it possible to have the base class properties come first, when serializing the derived class (without using [JsonProperty(Order=)] for each ...
Read more >
Will An Exception Be Thrown If The Base Class Isn't Marked ...
Ask questionsInherited serializable fields lead to incorrect serialization order. This Issue is producable with kotlin >1.3.20.
Read more >
Serialization and deserialization in Java | Snyk Blog
By altering the serialized objects, we can create invalid objects, alter the data's integrity, or worse. Arbitrary code execution, gadgets, and ...
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 >

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