How can I have serializer for a full hierarchy of classes without needing deserialization ?
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 ¯_(ツ)_/¯
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).
But I have the same error when putting the root interface as sealed class. Should I try with 1.6.20-M1 then ?
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?
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.
@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).
Is it still actual?