kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

Cannot serializer flow of Objects

Open inemtsev opened this issue 1 year ago • 0 comments

Hi team,

I would like to start serializing a Flow of objects as they come in from a data source. But, when I try to use in this way, I get an error like this:

kotlinx.serialization.SerializationException: Class 'SafeFlow' is not registered for polymorphic serialization in the scope of 'Flow'.
To be registered automatically, class 'SafeFlow' has to be '@Serializable', and the base class 'Flow' has to be sealed and '@Serializable'.
Alternatively, register the serializer for 'SafeFlow' explicitly in a corresponding SerializersModule.
	at kotlinx.serialization.internal.AbstractPolymorphicSerializerKt.throwSubtypeNotRegistered(AbstractPolymorphicSerializer.kt:102)
	at kotlinx.serialization.internal.AbstractPolymorphicSerializerKt.throwSubtypeNotRegistered(AbstractPolymorphicSerializer.kt:114)
	at kotlinx.serialization.PolymorphicSerializerKt.findPolymorphicSerializer(PolymorphicSerializer.kt:109)
	at kotlinx.serialization.json.internal.StreamingJsonEncoder.encodeSerializableValue(StreamingJsonEncoder.kt:233)
	at kotlinx.serialization.json.internal.JsonStreamsKt.encodeByWriter(JsonStreams.kt:28)
	at kotlinx.serialization.json.Json.encodeToString(Json.kt:81)

Usage in Ktor endpoint:


get("/person") {
            val pplFlow: Flow<Person> = getDataFlow()
            call.respondText(Json.encodeToString(pplFlow), ContentType.Application.Json )
        }

Is this already achievable somehow?

inemtsev avatar Jun 30 '24 11:06 inemtsev