Inherited serializable fields lead to incorrect serialization order
See original GitHub issueThis 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:
- Created 5 years ago
- Reactions:3
- Comments:13 (4 by maintainers)
Top 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 >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
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.
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.