Skip to content
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

How can I have serializer for a full hierarchy of classes without needing deserialization ? #1868

Closed
Ayfri opened this issue Feb 18, 2022 · 6 comments

Comments

@Ayfri
Copy link

Ayfri commented Feb 18, 2022

Hi, I have a project with a lot of simple classes, but they differ a lot and there are like 50 classes in total (but they might be a lot more in the future).
The root is a sealed interface without any properties, the second layer contains 3 sealed classes and the next layers contains a lot of different classes with different fields, so I can't really use JsonContentPolymorphicSerializer.

Also, some fields requires custom serializers because I'm using Compose Desktop to I have a custom serializer for MutableState, SnapshotListState and SnapshotMapState. There are other little custom serializer for lower casing value etc.

Using only @Serializable and only sealed classes, I still come up with an error when trying to serialize any of the bottom-layer classes, with every time the error

Exception in thread "AWT-EventQueue-0" kotlinx.serialization.SerializationException: Class 'MyBottomLayerClass' is not registered for polymorphic serialization in the scope of 'MyRootInterface'.
Mark the base class as 'sealed' or register the serializer explicitly.

Do you have any solution for this ? I might look for GSON if not ¯_(ツ)_/¯

@Ayfri Ayfri added the feature label Feb 18, 2022
@Ayfri Ayfri changed the title How can I have selector for a full hierarchy of classes without needing deserialization ? How can I have serializer for a full hierarchy of classes without needing deserialization ? Feb 18, 2022
@pdvrieze
Copy link
Contributor

Unfortunately sealed interface serialization isn't released yet (see #1576). It depends on the compiler plugin to support this, which will likely be in 1.6.20 (it may already be in -M1, it has been implemented).

@Ayfri
Copy link
Author

Ayfri commented Feb 18, 2022

But I have the same error when putting the root interface as sealed class. Should I try with 1.6.20-M1 then ?

@sandwwraith
Copy link
Member

Can you try with 1.6.20-M1, please?

However,

the same error when putting the root interface as sealed class

shouldn't happen on the current version. Are you sure about this and have a reproducer?

@Ayfri
Copy link
Author

Ayfri commented Feb 25, 2022

Hi again, sorry to reply only now but here's a repo in a branch where it doesn't work, the master branch works and is pretty much the same thing.

@pdvrieze
Copy link
Contributor

@Ayfri I noticed you're also using compose in the same project. There are known conflicts between compose and kotlinx.serialization. It may be worthwhile to try out the serialization code in a proof-of-concept (or separate module) that does not have the compose/UI code (that tends to be a good design approach as well).

@sandwwraith
Copy link
Member

Is it still actual?

@sandwwraith sandwwraith closed this as not planned Won't fix, can't repro, duplicate, stale Feb 6, 2023
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

3 participants