Serializing a sealead class do not set the type field
See original GitHub issueDescribe the bug
When serializing a single object that extends a sealed class, the type
field will not be set, not allowing to deserialize it as a parent calss.
To Reproduce
sealed class Parent {
data class A(val foo: String) : Parent()
data class B(val bar: String) : Parent()
}
val json = Json()
val a = A("hello")
val s = json.encodeToString(a)
json.decodeFromString<Parent.A>(s) // will throw
Expected behavior
Field type
present in the serialized object when serializing a sealed class.
Environment
- Kotlin version: 1.4.21
- Library version: 1.0.1
- Kotlin platforms: JVM
- Gradle version: 6.7.1
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top Results From Across the Web
How to serialize/deserialize Kotlin sealed class?
I've tried to use Genson serializer/deserializer library - it can handle Kotlin data classes, it's also possible to support polymorphic types ( ...
Read more >Third-party sealed classes serialization with `kotlinx. ...
All subclasses of a sealed class must be explicitly marked as @Serializable. This is unfortunate because it is impossible to annotate 3rd party ......
Read more >SerializationBinder Class (System.Runtime.Serialization)
The binder gives you an opportunity to inspect what types are being loaded in your application domain, but this doesn't guarantee an exploit...
Read more >SealedClassSerializer
SealedClass Serializer ... This class provides support for multiplatform polymorphic serialization of sealed classes. In contrary to PolymorphicSerializer, all ...
Read more >Binary Serialization - Yazan Diranieh
By default types are not serializable. The easiest way to make a class serializable is to mark it with [Serializable] attribute. The [Serializable]...
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
The same problem here: #1194
That’s intended behavior. Framework uses static (compile-time) types when available