Description
Summary
Currently, there is no API which reads or writes directly from
stream
in protobuf format. Such API makes sense for frameworks, which helps them to avoid decoding streams to an array first or encoding result to an array and then passing it to the stream.
Frameworks
Spring actually needs such an API to complete integration with kotlinx.serialization
, see: Spring-KotlinProtobufEncoder.
Besides Spring, it is to my understanding that also other frameworks can benefit from this addition, for example ktor.
Proposal
Message
A prototype shape of API can be similar to ones json format has:
ProtoBuf.encodeToStream
ProtoBuf.decodeFromStream
Implementation
A draft implementation has already been implemented and reviewed. See #2082.
Delimited messages
Since we are taking about streams and protobuf messages, we cannot ignore delimited messages. It is a core technique where we can write multiple messages in the same stream.
It is important to note that, delimited messages are the main reason and goal of this proposal. This is because, frameworks will most likely use/need this technique to write multiple messages in the same steam.
Implementation
SerializedSize:
To support delimited messages there is one thing to address. How to compute the required size for encoding a message without reading all bytes to an array first.
To address this problem we need an API which will compute the required bytes to encode a message. A proof of concept implementation is in #2239.
An early shape of the API is:
ProtoBuf.getOrComputeSerializedSize(serializer: SerializationStrategy<T>, value: T): Int
Encode delimited messages:
A very early implementation is in #2082, but is not using the solution from above yet.
A draft shape of the APIs are:
ProtoBuf.encodeDelimitedMessagesToStream
ProtoBuf.decodeDelimitedMessagesFromStream