Is it possible to use something like `decodeSource...
# serialization
s
Is it possible to use something like
decodeSourceToSequence
to receive a flow rather than blocking until the entire source is serialized? For context, I'm receiving a response with a ktor client using http streaming
Copy code
channelFlow<ExerciseList.Exercise> {
        client.prepareGet("<http://localhost:8080/list>").execute { httpResponse ->
            val channel: ByteReadChannel = httpResponse.body()

            while (!channel.isClosedForRead) {
                channel.readAvailable(10) {
                    trySend(
                        Serializer.json.decodeFromString(
                            it.readString().drop(1).dropLastWhile { it == ']' })
                    )
                    0
                }
            }
        }
    }.collect {
        println(it)
    }
Not sure if that's the ideal approach but this also works
j
I have created a very simple library to build a custom deserializer that does this. A usage example is:
Copy code
val deserializer: DeserializationStrategy<Unit> = buildDeserializer {
    field("description") { description: Text -> showText(description) }
    objectField("table") {
        arrayField("columnNames") { name: String -> addColumnName(name) }
        arrayField("rows") { row: Row -> processRow(row) }
    }
}
Json.decodeFromStream(deserializer, FileInputStream("huge-table.json"))
That deserializer could then be used to decode a Json object like this:
Copy code
{ "description":"text...",
  "table": {
     "columnNames":["a","b",c"],
     "rows": [ { row 1 }, { row 2 } ]
   }
}
Where
Row
is just a standard
@Serializable
class. You can find it on maven:
implementation("com.beetstra.blockbuster:blockbuster:0.1.0")
Is that what you are looking for?
s
looks like I could make this work too. in my case the json objects aren't that big, it's just that the array is streamed (sent from the ktor backend as a flow)
update, my version does not work. it make the page completely unresponsive