Skip to content

Allow users to pass an io.Writer to protojson #1673

Open
@denysvitali

Description

@denysvitali

What language does this apply to?

Golang (protocolbuffers/protobuf-go)

Describe the problem you are trying to solve.
As part of open-telemetry/opentelemetry-collector#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:

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.

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 protocolbuffers/protobuf#20049

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions