Kotlin / kotlinx.serialization

Kotlin multiplatform / multi-format serialization
Apache License 2.0
5.44k stars 623 forks source link

CBOR serialization into an `OutputStream` or `WritableByteChannel` or externally allocated `ByteBuffer` #2618

Open 0x6675636b796f75676974687562 opened 8 months ago

0x6675636b796f75676974687562 commented 8 months ago

What is your use-case and why do you need this feature?

When encoding data as JSON, it's possible to write serialized data directly into an OutputStream, via Json.encodeToStream(SerializationStrategy<T>, T, OutputStream).

Yet, with CBOR, only writing to or reading from a ByteArray (byte[] JVM type) seems possible (Cbor.encodeToByteArray(SerializationStrategy<T>, T): ByteArray), and the byte array returned gets allocated by the library itself, can't be supplied externally, nor can it be reused.

This leads to extra allocations and frequent OutOfMemoryError's when encoding large data hierarchies as CBOR, which never happens with JSON.

Describe the solution you'd like

We'd appreciate if any of the following extensions, with the corresponding decodeXyz() counterparts, are eventually implemented:

xiaozhikang0916 commented 7 months ago

This feature may be commonized as Supporting streaming decoding and encoding with multiplatform.

Personally I like this idea. My team has also faced OOM issue using FastJSON and got solved by using streaming decoding. With this feature in serialization lib, we can have more flexiable control.

We can introduce a new stream-ext lib to support it with help of kotlinx.io, but it would need some refactoring in the core lib and all the formats.

corlaez commented 1 month ago

I don't really care for multiplatform. Would love to at least see the Stream encode/decode in JVM. Or at least more info in the readme of what are my options if I really need stream support, instead of trial and error of each library