#1673·protobuf

Allow users to pass an `io.Writer` to `protojson`

Author: denysvitaliCreated Jan 21, 2025Updated Jul 20, 2026
Labelshelp-wantedperformance

What language does this apply to?

Golang (protocolbuffers/protobuf-go)

Describe the problem you are trying to solve. As part of https://github.com/open-telemetry/opentelemetry-collector/issues/7095 we would like to switch to the new Protobuf Opaque API to increase the performance of opentelemetry-collector (specifically when exporting telemetry as JSON).

We would like to use encoding/protojson for this, but the current implementation doesn't allow users to pass an io.Writer.

The only method available to encode JSON is:

go
func Marshal(m proto.Message) ([]byte, error)

Describe the solution you'd like

We would like to see a solution similar to encoding/json's Encoder, where a new encoder can be created by passing an io.Writer and we can call Encode(obj).
This will prevent the use of a buffer that will only impact the encoding performance.

go
func NewEncoder(w io.Writer) *Encoder
func (enc *Encoder) Encode(v any) error

Describe alternatives you've considered

None

Additional context


cc/ @stapelberg, @els0r

Moved from https://github.com/protocolbuffers/protobuf/issues/20049