TL;DR: Is it possible to do streaming decoding of ...
# ktor
d
TL;DR: Is it possible to do streaming decoding of JSON in Ktor server and client? I need it to be similar to
javax.json.stream.JsonParser
where I get to handle the lexer tokens the way I want. I am developing a multi-platform library that is currently JVM-only since it uses
java.io.Reader
and
java.io.Writer
. It also uses
javax.json.stream.JsonParser
to parse JSON as it streams in. I currently cannot use
kotlinx.serialization
for decoding since I have a specific representation in JSON that is not suitable for
kotlinx.serialization
. I currently use my library in a Ktor-based server to convert JSON in POST requests as they stream in. I also need to use my library on the client side to convert JSON in responses as they stream in. So I want to make my library a multi-platform library so that I can use it on the server side (Kotlin/JVM) as well as the client side (Kotlin/JS). I went looking for
java.io.Reader
replacements that are multi-platform. I narrowed it down to Okio and Ktor’s
ByteReadChannel
. I would prefer to use Ktor since it is from JetBrains, and also because I can use it on both the client and the server (Okio is only client side). The next thing I went looking for is a multi-platform streaming JSON decoder. I found some classes in
kotlinx.serialization
and I see that it is supported for Okio. I could be wrong, but I did not find it supported for Ktor’s
ByteReadChannel
.
a
The problem is that
java.io.Reader
and classes from Okio provide blocking I/O but Ktor uses non-blocking one. We have the general-purpose asynchronous multiplatform I/O library in development but there is no ETA yet. You are right about integration between
kotlinx.serialization
and Okio libraries https://github.com/Kotlin/kotlinx.serialization/releases/tag/v1.4.0-RC.
j
Okio is not just for clients
d
Thank you @jw for responding! You’re right, Okio can be used on the server since it is multi-platform. I confused it with Okhttp when I was writing my message. Since I am already using Ktor on the server side, I was hoping there was a solution with Ktor itself.
j
okhttp also works great on servers, for what it's worth
d
@jw how do I use okhttp on the server? Do I replace ktor? Is okhttp client multiplatform (I need JS support)?
Thank you @Aleksei Tirman [JB] for responding! I did not realize that okio is blocking (just started looking into it yesterday afternoon).
@jw I probably misunderstood you when you said okhttp can be used on the server side. I thought you meant that I can use it as the server. I am now guessing you meant that it can be used on the server as an http client to talk to external dependencies of the server.
106 Views