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

kenkyee

11/14/2023, 7:12 PM
Has anyone had problems with json.encodeToStream truncating the JSON result? It worked fine w/ kotlinx.serialization 1.3.3, but 1.4.1/1.5.1/1.6.0 truncates the JSON (not even that big...300KB). Workaround is to use json.encodeToString() and write it out to a file.
j

jw

11/14/2023, 7:17 PM
Are you sure you're not leaving data in an intermediate buffer without a proper flush/close?
k

kenkyee

11/14/2023, 7:49 PM
I did a writer.flush() to be sure...also tried adding delays in case it took a while to write, but fairly sure. Only thing changed was flipping kotlinx.serialization from 1.3.3 to 1.4+. I'll have to see if I can create a repro...
Also, this code can emit YAML or JSON...using encodeToStream w/ YAML worked fine...
j

Joshua Hansen

11/14/2023, 10:12 PM
I'm using it for some larger json structures ~5MB in size and I don't have issues. This is what I'm doing:
Copy code
Path(
    System.getProperty("user.home"),
    "Desktop",
    "filename.json",
).outputStream().use {
    Json.encodeToStream(mySerializableClass, it)
}
Im using 1.6 Might be worth seeing if using
use
and letting the stream close itself helps?
j

jw

11/14/2023, 10:21 PM
There is no 1.9. 1.6 is the latest.
1.4 is where the abstraction between JVM streams and Okio streams was added. It's possible it had bugs. Is there a reason you're on a such an old version rather than going to 1.6?
oh nevermind you said you tried it
j

Joshua Hansen

11/14/2023, 10:33 PM
Looked at the wrong version number in my project. Yeah I'm on 1.6. Fixed it
k

kenkyee

11/14/2023, 11:22 PM
1.4 is where the abstraction between JVM streams and Okio streams was added. It's possible it had bugs. Is there a reason you're on a such an old version rather than going to 1.6?
Good to know that some major change did go in with 1.4. Yep, only noticed because someone bumped our kotlinx.serialization libs to the latest 1.6 and broke this bit of code and I had to backtrack a bit to find it. I should have mentioned the current and original code used writer.use {} to let it autoclose as well. I tried adding a flush in that but it made no difference. Will try to create a repro since this would be ugly for other folks to hit...