Serialization works, deserialization doesn't

I have the following code (sorry for the dump):

@Test
    fun timestamp() {
        val timestampGenerator = DistributedTimestampGenerator(32)
        val timestamps = listOf(
            timestampGenerator.generate(),
            timestampGenerator.generate(),
            timestampGenerator.generate(),
        )

        val serialized = serializer.encodeToString(timestamps)
        val deserialized = serializer.decodeFromString<List<DistributedTimestamp>>(serialized)
        assertEquals(timestamps, deserialized)
    }

    @Test
    fun uuid() {
        val uuids = listOf(
            UUID.random(),
            UUID.random(),
            UUID.random(),
        )

        val serialized = serializer.encodeToString(uuids)
        val deserialized = serializer.decodeFromString<List<UUID>>(serialized)
        assertEquals(uuids, deserialized)
    }

@Test
    fun events() {
        val timestampGenerator = DistributedTimestampGenerator(32)
        val events: List<CRDT.Event<UUID, DistributedTimestamp, Component>> = listOf(
            CRDT.Event.Create(timestampGenerator.generate(), UUID.random()),
            CRDT.Event.Destroy(timestampGenerator.generate(), UUID.random()),
            CRDT.Event.Set(timestampGenerator.generate(), UUID.random(), Component.Name("Foo")),
            CRDT.Event.Set(timestampGenerator.generate(), UUID.random(), Component.Parent(UUID.random())),
            CRDT.Event.Set(timestampGenerator.generate(), UUID.random(), Component.OrderedChildren(listOf(UUID.random(), UUID.random()))),
            CRDT.Event.Set(timestampGenerator.generate(), UUID.random(), Component.EntityType.Department),
            CRDT.Event.Set(timestampGenerator.generate(), UUID.random(), Component.EntityType.Function),
            CRDT.Event.Unset(timestampGenerator.generate(), UUID.random(), nl.rjcoding.domain.Tag.EntityType)
        )

        val serialized = serializer.encodeToString(events)
        val deserialized = serializer.decodeFromString<List<CRDT.Event<UUID, DistributedTimestamp, Component>>>(serialized)
    }

The UUIDs and timestamp tests pass, so they get serialized and deserialized correctly.
However, for the events() test the serialization goes correct, but deserializing causes the following error:

Serializer for class 'Any' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
kotlinx.serialization.SerializationException: Serializer for class 'Any' is not found.

This is strange to me, why does the serializatoin work, but the deserialization doesn’t?

I have quite a complicated class (with a lot of type parameters), maybe I should convert it to a simple DTO first.

For completeness sake:

@Serializable
    sealed class Event<Id, Ts, C : Tagged<Id, C>>  {
        abstract val timestamp: Ts

        @Serializable @SerialName("create")
        data class Create<Id, Ts, C : Tagged<Id, C>>(@Contextual override val timestamp: Ts, @Contextual val id: Id) : Event<Id, Ts, C>()

        @Serializable @SerialName("destroy")
        data class Destroy<Id, Ts, C : Tagged<Id, C>>(@Contextual  override val timestamp: Ts, @Contextual  val id: Id): Event<Id, Ts, C>()

        @Serializable @SerialName("set")
        data class Set<Id, Ts, C : Tagged<Id, C>>(@Contextual  override val timestamp: Ts, @Contextual  val id: Id, val component: C): Event<Id, Ts, C>()

        @Serializable @SerialName("unset")
        data class Unset<Id, Ts, C : Tagged<Id, C>>(@Contextual  override val timestamp: Ts, @Contextual  val id: Id, val tag: Tag<Id, C>): Event<Id, Ts, C>()

    
    }

EDIT: I modified the testcode as follows:

val serialized = serializer.encodeToString(events)
        val deserializationStrategy = ListSerializer(CRDT.Event.serializer(UUIDAsStringSerializer, DistributedTimestampAsStringSerializer, Component.serializer()))
        val deserialized = serializer.decodeFromString(deserializationStrategy, serialized)

However, I still get the same error… I am missing someting.

To test this you want to dig deeper into which particular class doesn’t deserialize correctly. Start with: CRDT.Event, then look at each Component subtype, as well as the component parent. Also remember that for polymorphic (de)serialization to work, you must provide the mappings in a SerializersModule.