https://kotlinlang.org logo
#serialization
Title
# serialization
u

ursus

11/04/2023, 12:14 PM
Does Serialization have streaming parsing?
j

jw

11/04/2023, 12:14 PM
Only for JSON
Not arbitrary `BinaryFormat`s or `TextFormat`s
u

ursus

11/04/2023, 12:15 PM
Yes JSON is what I'm after. Do I need to enable something if it's plugged into retrofit/ktor?
j

jw

11/04/2023, 12:19 PM
I don't know about ktor. For Retrofit it's not supported by my converter but you could write your own fairly easily (after all, that's the whole point of Retrofits extendable design). https://github.com/JakeWharton/retrofit2-kotlinx-serialization-converter/issues/43
u

ursus

11/04/2023, 12:22 PM
Right thanks! Btw, what do you mean about other textformats, like xml etc?
j

jw

11/04/2023, 12:23 PM
Yep. My converter relies on the
TextFormat
type which is in serialization core. It doesn't actually know anything about whether you're using JSON, XML, HTML, TOML, etc. The same is true for
BinaryFormat
and protobuf, CBOR, etc.
u

ursus

11/04/2023, 12:25 PM
Gotcha Btw, is the assumption of staying in Square stack (okhttp+moshi+retrofit) as to use less memory because of sharing okio instances, still valid?
j

jw

11/04/2023, 12:27 PM
It helps a bit, sure. I don't think it's absolutely essential. For example, I only use kotlinx.serialization nowadays with Kotlin types. I think Moshi is great for Java, but its design for Kotlin leaves a bit to be desired. Plenty of people are still happy with it, though.
u

ursus

11/04/2023, 12:28 PM
Interesting, even if you dont need multiplatform? Btw what exactly,
sealed
stuff?
h

hfhbd

11/04/2023, 12:28 PM
BTW it is called StringFormat.
👍 1
👍🏾 1
j

jw

11/04/2023, 12:30 PM
Ah yeah I think TextFormat was an early 0.x name
Maybe not. Proto has a text format. I deal with a lot of serialization so names get mixed occasionally
For Moshi, I don't like that you're forced to use kotlin-reflect or use a reflection-based generated code lookup. Plus there's all the kapt/ksp/ir stuff for the generator. Moshi has a superior design for custom adapters, but I think the rest is better over in kotlinx land
h

hfhbd

11/04/2023, 12:35 PM
Yes, this is one advantage of kotlinx serialization: the format agnostic compiler plugin, so you don’t need reflection to get the scheme of the class.
u

ursus

11/04/2023, 12:35 PM
There's still reflection even if codegen in moshi? That's news, thanks About retrofit though, there is not much hope for it going pure kotlin (as to be multiplatform viable) because of the java Proxy thing, right?
j

jw

11/04/2023, 12:38 PM
Maybe in the distant future it'll be a compiler plugin that generates the backing implementation rather than doing it with proxy/reflection. But as it stands, there's little reason to migrate to Kotlin.
I think I have a plan for it which is to get some releases going again and stabilize, then convert to Kotlin and update OkHttp to latest while remaining binary compatible, then very long term to explore multiplatform and some other long-standing design things for a breaking change.
u

ursus

11/04/2023, 12:42 PM
So if you wanted to experiment with multiplatform say next year, in a existing square stack android codebase, you'd use Serialization and Ktor backed with OkHttp on android? Wondering how to translate all the Interceptors to multiplatform as its a OkHttp type. and thats supposed to be android detail
j

jw

11/04/2023, 12:54 PM
You can write a simple interface for an HTTP client and then implement it natively on each platform. That's what we do today, basically a single suspending function that takes a request and returns a response.
u

ursus

11/04/2023, 12:56 PM
but what about the "middleware" stuff like auth, interceptors? rewrite those in their respective libraries as a detail? or have that be part of the http client interface?
also, isn't okhttp 5 multiplatform?
j

jw

11/04/2023, 1:01 PM
Its HTTP model is, not the actual transport engine
Since we started from an existing set of apps on each platform, we already had everything in place for each one. So really we're just abstracting over them.
u

ursus

11/04/2023, 1:02 PM
which means its api?
Authenticator
and
Intereceptors
now magically become multiplatform? And I'll only need to swap the engine's engine? 😀
j

jw

11/04/2023, 1:12 PM
That's the goal. I'm not sure how far they are. Jesse and I are working on other crazy things that occupy the majority of our time so Retrofit and OkHttp haven't gotten a lot of attention
u

ursus

11/04/2023, 1:13 PM
Oh, back from the parental leave already? 😀
j

jw

11/04/2023, 1:14 PM
Honestly I wasn't thinking of that but I guess kids are crazy things that occupy the majority of ones time, too
u

ursus

11/04/2023, 1:15 PM
😀 nn, I though Jesse was still on the leave
You can write a simple interface for an HTTP client and then implement it natively on each platform. That's what we do today, basically a single suspending function that takes a request and returns a response.
Isn't that reimplementing ktor?
j

jw

11/04/2023, 1:19 PM
I'm about to take the rest of mine in December. He took a bunch in the summer. Not sure if he's done completely or not. Plus parental leave means we get to hack on open source more since there's no real work to do.
ktor is a bit more than just a one function interface
Ktor client tries to both be an abstraction and a high-level API for building requests and handling serialization and features like web sockets
u

ursus

11/04/2023, 1:21 PM
so you don't build the serialization into the interface? So then I presume you have some sort of pure kotlin ApiClient type which combines the HttpClient (implemented per platform) and Serialization, for feature people to consume (basically retrofit reimplemented?)
j

jw

11/04/2023, 2:03 PM
We do serialization in the common code, since kotlinx is multiplatform
u

ursus

11/04/2023, 2:05 PM
yes, but in essence you rebuilt retrofit, no?
j

jw

11/04/2023, 2:26 PM
It's an imperative API not a declarative one like Retrofit. Here's a very, very rough example of what I mean: https://github.com/cashapp/redwood/blob/trunk/samples/emoji-search/presenter-treehouse/src/commonMain/kotlin/com/example/redwood/emojisearch/treehouse/HostApi.kt#L24. This one is tailored to the single API call we have to make in the sample, but it's not far off of a general-purpose client either.
u

ursus

11/04/2023, 2:33 PM
right, but what it does is supplants retrofit, i.e. its implementation on android is directly via okhttp, not throught retrofit again, right?
j

jw

11/04/2023, 3:51 PM
Yes
u

ursus

11/04/2023, 7:48 PM
great, thanks for patience!
6 Views