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.

Serializing a sealead class do not set the type field

See original GitHub issue

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

github_iconTop GitHub Comments

1reaction
sandwwraithcommented, Jan 18, 2021

The same problem here: #1194

1reaction
sandwwraithcommented, Jan 18, 2021

That’s intended behavior. Framework uses static (compile-time) types when available

Read more comments on GitHub >

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

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