Skip to content

Support kotlinx.serialization #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
NorbertSandor opened this issue Oct 30, 2019 · 11 comments
Open

Support kotlinx.serialization #63

NorbertSandor opened this issue Oct 30, 2019 · 11 comments

Comments

@NorbertSandor
Copy link

My main blocking issue is that https://github.com/Kotlin/kotlinx.serialization is not supported :(

@fvasco
Copy link

fvasco commented Oct 30, 2019

You can consider to use custom serializers and pack them in a serial module, as a temporary workaround.

@jfontsaballs
Copy link

I'd also like immutable collections to be serializable using https://github.com/Kotlin/kotlinx.serialization.

My use case is that I'm passing information between a Ktor server and a KotlinJS client. I have a shared data model in a multiplatform module and I send and receive data between server and client by serializing classes to JSON and sending it via HTTP calls. I'm using kotlinx.serialization for serialization and deserialization. The classes in the shared module are designed to be immutable and I thought the using immutable collections would just fit nicely until I discovered that they can't be serialized.

Right now I'm using regular Kotlin collections but modifications are more difficult.

@jfontsaballs
Copy link

jfontsaballs commented Dec 27, 2022

@NorbertSandor could you please update the issue title so that it clearly reflects that you are requesting serialization with https://github.com/Kotlin/kotlinx.serialization.

This is not to be confused with Java serialization which seems to be discarded (see #17).

@NorbertSandor NorbertSandor changed the title Support serialization Support kotlinx.serialization Dec 27, 2022
@har-nick
Copy link

har-nick commented May 7, 2023

Sorry to necro this, but is this planned at all, or do we need to create custom serializers instead?

@StylianosGakis
Copy link

I've personally had the best experience by doing this

typealias SerializableImmutableList<T> = @Serializable(ImmutableListSerializer::class) ImmutableList<T>

@Serializer(forClass = ImmutableList::class)
class ImmutableListSerializer<T>(private val dataSerializer: KSerializer<T>) : KSerializer<ImmutableList<T>> {
  private class PersistentListDescriptor : SerialDescriptor by serialDescriptor<List<String>>() {
    override val serialName: String = "kotlinx.serialization.immutable.ImmutableList"
  }

  override val descriptor: SerialDescriptor = PersistentListDescriptor()
  override fun serialize(encoder: Encoder, value: ImmutableList<T>) {
    return ListSerializer(dataSerializer).serialize(encoder, value.toList())
  }

  override fun deserialize(decoder: Decoder): ImmutableList<T> {
    return ListSerializer(dataSerializer).deserialize(decoder).toPersistentList()
  }
}

And using this type in all the serializable models, while still downcasting them to ImmutableList for the UI or wherever else where you don't care for this extra information in the type.

But still quite frustrating to have to do this everywhere, without any error at compile time that having an ImmutableList inside a Serializable data class isn't gonna work by default.
Also no way to make this work on the SerializersModule level, where it could be done once and never again on each usage of the type.

@shaqaruden
Copy link

This really should have been completed by now, being 4 years after initial request. This is a fairly important thing to have

@qurbonzoda
Copy link
Contributor

This issue is one of our top priorities.
Unfortunately, we can't provide you with a timeline at the moment as our resources are limited.
You can implement custom serializers as hinted by some participants.
Also, we would be very happy to accept PR from contributors.

@lisonge
Copy link

lisonge commented Feb 9, 2024

Thank you for your work first

I use kotlinx.immutable in androidx.compose, https://developer.android.com/jetpack/compose/performance/stability#summary

In most cases, my data needs to be transmitted serialized between pages, and It also needs to be stored as file content

this greatly limits kotlinx.immutable scope of use

Although I can use androidx.compose.runtime.Immutable, but I hope want to use kotlinx.immutable

@mgroth0
Copy link

mgroth0 commented May 13, 2024

@StylianosGakis your workaround is helpful, thank you. I had no idea that an annotation could be used in a typealias like that!

Just noticed when I copied the code that I got this warning:

[EXTERNAL_SERIALIZER_USELESS] @Serializer annotation has no effect on class 'ImmutableListSerializer<T>', because all members of KSerializer are already overridden

So might want to edit your snippet to remove the @Serializer annotation.

@ZacSweers
Copy link

ZacSweers commented Sep 2, 2024

It seems the above solution no longer works in 2.0.20. I see the following error when trying to encode with it.

Exception in thread "AWT-EventQueue-0" kotlinx.serialization.SerializationException: Serializer for subclass 'SmallPersistentVector' is not found in the polymorphic scope of 'ImmutableList'.
Check if class with serial name 'SmallPersistentVector' exists and serializer is registered in a corresponding SerializersModule.
To be registered automatically, class 'SmallPersistentVector' has to be '@Serializable', and the base class 'ImmutableList' has to be sealed and '@Serializable'.

@jershell
Copy link

Does anyone have a workaround?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests