Bernhard
07/10/2026, 2:59 PMCasey Brooks
07/10/2026, 3:15 PMBernhard
07/10/2026, 3:16 PMCasey Brooks
07/10/2026, 3:26 PM@Serializable
data class User(val name: String, val age: Int)
val text = """{"name": "Alice", "age": 30}"""
val stream = text.byteInputStream()
val user = Json.decodeFromStream<User>(stream)
But again, this mostly just operates at the byte-decoding level, and doesn't really work for processing the entire source in smaller chunks, since it still collects the results in memory and returns the whole thing when its done. It just helps avoid doubling the memory by not holding both the input text AND the decoded objects in memory.
A real streaming approach would be finding some way to not need an entire JSON Array at once, but to process the small chunks individually. Again, the core Kotlinx.Serialization isn't really built to handle this itself, but it's pretty each to build your own streaming pipeline:
val jsonl = """
{"name": "Alice", "age": 30}
{"name": "Bob", "age": 25}
{"name": "Carol", "age": 35}
""".trimIndent()
jsonl.lineSequence() // could be any method of reading content line-by-line. Reading a file, receiving data from a socket, etc.
.map { Json.decodeFromString<User>(it) }
.forEach { user ->
println(user.name)
}hfhbd
07/10/2026, 3:30 PMhfhbd
07/10/2026, 3:33 PMCasey Brooks
07/10/2026, 3:34 PMhfhbd
07/10/2026, 3:36 PMBernhard
07/13/2026, 7:40 AMBernhard
07/13/2026, 7:41 AMJuan Hurtado
07/15/2026, 3:51 AMGhostJsonReader is public, you can do a manual token-by-token loop:
val reader = GhostJsonReader(bufferedSource)
reader.beginObject()
// ... read keys, then:
reader.beginArray()
while (reader.hasNext()) {
val user = userSerializer.deserialize(reader)
// O(1) memory
}
Ghost Android Guide — Flat vs Streaming Section
Ghost Architecture — Reader Pipeline
If you want to try it out 🙂pdvrieze
07/15/2026, 3:31 PMBernhard
07/16/2026, 7:23 AMBernhard
07/16/2026, 7:24 AMpdvrieze
07/29/2026, 11:16 AMBernhard
07/29/2026, 11:16 AMpdvrieze
07/29/2026, 11:22 AMBernhard
07/29/2026, 11:34 AMpdvrieze
07/29/2026, 3:53 PMJson format support being created on top of a general Json parser (and generator). This would need to be multiplatform but would be feasible. You should probably create an issue for it (with use case). But the gigabytes size makes clear that (de)serialization of parts of a json document would be needed somehow (the container using direct parsing, serialization for the elements).