This post by Dan Abramov <https://overreacted.io/p...
# squarelibraries
c
This post by Dan Abramov https://overreacted.io/progressive-json/ is making me rethink how json is parsed/deserialized via okhttp. From what Dan says you can't deserialize json until its completely downloaded, but i always thought with okhttp + moshi (or whatever) that the deserialization happens while streaming the resopnse. i vaugley remember a droidcon talk where jake/jesse talked about amoritized cost of network response vs deserialization. and even the fact that json serializer benchmarks that use strings aren't really benchmarking the right thing. Can someone confirm if okhttp allows streaming of responses + streamed deserialization?
j
It does, yes. JS does not.
🚀 4
today i learned 1
c
aha. figured it was maybe a JS specific thing, but i guess i thought that an http client is a http client everywhere and it would meet the specs of being an http client (i guess theres probably some rfc of being a sepc compliant http client)
m
Looks like the JS community is also working on tools to do JSON streaming like https://github.com/formkit/jsonreader
But yea, probably 99% are using the HTTP client + JSON implementation from the browser that buffers everything
j
This post is suggesting something much more galaxy brain . . .
The bad approach is completely sequential: 1. download the entire JSON 2. parse the downloaded JSON
The Retrofit approach is parallel 1. download the JSON, parsing as bytes arrive
But this is suggesting something weirder: 1. download the JSON, parse as bytes arrive, and also update the UI mid-document
I think the right solution for this is to decompose your JSON into multiple documents.
âž• 2
(And only in the situation where doing so would yield a meaningful user benefit. If your JSON contains the entire application state, you might decompose it into a JSON-per-screen)
c
"The Retrofit approach is parallel" what about a ktor (via okhttp) with kotlinx.serialization (i guess maybe out of scope for this channel)? or does the parallelism come from retrofit since it has pluggable deserializers?
j
I don’t know about Ktor
c
what about okhttp + kotlinx.serialization? or is it something instrinsic to retrofit that gives us this parallel approach?
m
kotlinx.serialization has BufferedSource APIs. I didn't check the implementation but I would expect that they also amortize the parsing during I/O.
kodee happy 1