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.

Add sealed classes support to serialization

See original GitHub issue

Could 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:21 (14 by maintainers)

github_iconTop GitHub Comments

2reactions
walkingbradcommented, Nov 9, 2021

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).

1reaction
nbransbycommented, Apr 6, 2022

closing as support has been added

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

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