Add sealed classes support to serialization
See original GitHub issueCould you add support of “sealed” class to the FirebaseDecoder.structureDecoder and FirebaseEncoder.structureEncoder functions?
I would try it with some think like this:
fun FirebaseEncoder.structureEncoder(descriptor: SerialDescriptor, @Suppress("UNUSED_PARAMETER") vararg typeParams: KSerializer<*>): CompositeEncoder = when(descriptor.kind) {
StructureKind.LIST, PolymorphicKind.SEALED -> mutableListOf<Any?>()
.also { value = it }
.let { FirebaseCompositeEncoder(shouldEncodeElementDefault, positiveInfinity) { _, index, value -> it.add(index, value) } }
StructureKind.MAP -> mutableListOf<Any?>()
.let { FirebaseCompositeEncoder(shouldEncodeElementDefault, positiveInfinity, { value = it.chunked(2).associate { (k, v) -> k to v } }) { _, _, value -> it.add(value) } }
StructureKind.CLASS, StructureKind.OBJECT -> mutableMapOf<Any?, Any?>()
.also { value = it }
.let { FirebaseCompositeEncoder(shouldEncodeElementDefault, positiveInfinity) { _, index, value -> it[descriptor.getElementName(index)] = value } }
else -> TODO("The firebase-kotlin-sdk does not support $descriptor for serialization yet")
}
This case is optimized for plain Kotlin.
Unfortunately, I also have some building problems too, so I cannot implement them myself
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:21 (14 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 >SealedClassSerializer - Kotlin
This class provides support for multiplatform polymorphic serialization of sealed classes. In contrary to PolymorphicSerializer, all known subclasses with ...
Read more >How to: serializing / deserializing sealed classes #103 - GitHub
Sealed classes don't have any kind of special support now, but usual PolymorphicSerializer should work fine for them. ... val type = readString...
Read more >Third-party sealed classes serialization with `kotlinx ... - Medium
Develop a surrogate class per subclass and an appropriate serializer. This option works, but requires a lot of boilerplate code. Have a single ......
Read more >Serialization for sealed classes gives different results when ...
Sealed classes are stringified to differently when using kotlinx.serialization through Ktor content negotiation. To Reproduce
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 Free
Top 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
Hey y’all. Is this a fix we can get back into in the pipeline? I just started using this library (which has worked pretty great so far) but there are couple of hitches such as the inability to deserialize to a sealed class (which I might expect is a fairly common expectation today).
closing as support has been added