Peanuuutz / tomlkt

Multiplatform TOML library with builtin support for kotlinx.serialization
Apache License 2.0
117 stars 5 forks source link

Please add `decodeFromStream` for symmetry with `encodeToStream` #62

Closed sschuberth closed 1 year ago

sschuberth commented 1 year ago

That would ease reading from files on the JVM a bit 😉

Peanuuutz commented 1 year ago

Short answer: Won't fix. Please use inputStream.reader() with decodeFromNativeReader. By the way, we plan to deprecate encodeToStream in favor of encodeToNativeWriter in the next release.

Long answer: Actually that was a thing before, but was abandoned. Note that, the parser is based on character stream, not byte stream, and InputStream is a byte stream. While it is possible to achieve this with inputStream.bufferedReader(), we don't want to break the symmetry with other implementations either, which simply use x.buffered(). We've noticed that the offical Json format does copy InputStreamReader to support Json.decodeFromStream, which is not ideal in respect to no repetition. We don't want to pollute user's disk further with another copy of InputStreamReader when it is easy enough to use decodeFromNativeReader, so we don't plan to support InputStream directly. encodeToStream, on the other hand, just happens to be doable without a lot of paste work, but still hurts performance because it allocates a byte array each time even a single character goes into the file, so we don't recommend it either.

sschuberth commented 1 year ago

We've noticed that the offical Json format does copy InputStreamReader to support Json.decodeFromStream

Yeah, that's where I'm coming from, and what I'm used to.

Thanks for the elaborate answer, makes sense now!