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

Cannot serializer flow of Objects #2733

Closed
inemtsev opened this issue Jun 30, 2024 · 2 comments
Closed

Cannot serializer flow of Objects #2733

inemtsev opened this issue Jun 30, 2024 · 2 comments
Labels

Comments

@inemtsev
Copy link

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?

@sandwwraith
Copy link
Member

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:

  1. Convert it to list and encode List<Person>: Json.encodeToString(pplFlow.toList()).
  2. 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.

@inemtsev
Copy link
Author

inemtsev commented Jul 7, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants