You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
I'm not sure why you are trying to put Flow into the response. Flow is intended to be a (potentially infinite) asynchronous stream of objects, not a collection. Depending on your Flow usage, you may want to do one of the following:
Convert it to list and encode List<Person>: Json.encodeToString(pplFlow.toList()).
Send items one-by-one into an open response socket with pplFlow.collect { item -> ...}. I'm not sure if this is possible to do with Ktor's call.respond..., consult their documentation.
I was hoping to achieve some extra efficiency, by starting to serialize a flow of results as they are read from several database queries, instead of waiting for all of them to come back first.
A method that yields each query result is what getDataFlow() was hoping to achieve.
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:
Usage in Ktor endpoint:
Is this already achievable somehow?
The text was updated successfully, but these errors were encountered: