-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow users to pass an io.Writer
to protojson
#1673
Comments
I suspect it would be more forward thinking to match the prospective "encoding/json/v2" API, and thus provide: func MarshalWrite(io.Writer, proto.Message) error
func MarshalEncode(*jsontext.Encoder, proto.Message) error
func UnmarshalRead(io.Reader, proto.Message) error
func UnmarshalDecode(*jsontext.Decoder, proto.Message) error and similar methods hung off the func (MarshalOptions) MarshalWrite(io.Writer, proto.Message) error
func (MarshalOptions) MarshalEncode(*jsontext.Encoder, proto.Message) error
func (UnmarshalOptions) UnmarshalRead(io.Reader, proto.Message) error
func (UnmarshalOptions) UnmarshalDecode(*jsontext.Decoder, proto.Message) error The entirety of the This has other benefits such as making it easier to bridge "encoding/json/v2" with "protojson" for users that are trying to serialize Go values that happen to have a import "encoding/json/v2"
json.Marshal(v,
json.WithMarshalers(json.MarshalToFunc(protojson.MarshalEncode)),
) where |
@denysvitali Would you be willing to send a (Gerrit) change that implements your suggestion (and also @dsnet’s comments)? See https://github.com/protocolbuffers/protobuf-go/blob/master/CONTRIBUTING.md |
Sure thing! |
I'm remembering now that https://github.com/protocolbuffers/protobuf-go/blob/master/internal/encoding/json is currently implemented only in terms of Option 1: Buffer entirely
Option 2: Modify "./internal/encoding/json" to be streaming
Option 2a: Depend on "github.com/go-json-experiment/json/jsontext"
Option 2b: Vendor "github.com/go-json-experiment/json/jsontext"
Option 2c: Wait for "encoding/json/jsontext"
Option 1.5: Use streaming features in v1 "encoding/json"
|
Also #1203 for context of the last time we discussed this matter… an issue that seems to even still be open. Though, it focused a lot more on streaming decoding vs singleton decoding, but still covered the topic. Raising the primary important factor from that message here: |
Thanks for the analysis, @dsnet! Just a brief note: between 2a (dependency), 2b (vendor), 2c (wait), I have a preference for 2c. I don’t think this issue is important enough to warrant adding dependencies to Go Protobuf, and I think vendoring is cumbersome enough from a compliance perspective (even if the licenses match up) that I would want to avoid it, too. |
BTW, a formal proposal for "encoding/json/v2" along with "encoding/json/jsontext" has be filed at golang/go#71497. |
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:
Describe the solution you'd like
We would like to see a solution similar to
encoding/json
'sEncoder
, where a new encoder can be created by passing anio.Writer
and we can callEncode(obj)
.This will prevent the use of a buffer that will only impact the encoding performance.
Describe alternatives you've considered
None
Additional context
cc/ @stapelberg, @els0r
Moved from protocolbuffers/protobuf#20049
The text was updated successfully, but these errors were encountered: