-
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
encoding/{protojson,prototext}: add MarshalWriter/UnmarshalReader functionality #1203
Comments
Yeah, the subtle buggy behavior has come up a couple times in some of the code I‘ve maintained. One test even required the buggy behavior in order to pass, because of a typoed extra So, it would be better if we didn‘t repeat this behavior, that is we would want to avoid using a parallel 🤔 I think the best choice might to stay away from the pattern after all, so that we neither reinforce unexpected behavior, or the unexpectedness of the behavior.? |
It seems that part of this is figuring out what we want to do with
😲 |
How is it suggested to use protojson with streaming data? Use var data interface{}
d := json.NewDecoder(os.Stdin)
_ = d.Decode(&data)
b := json.Marshal(data)
protojson.Umarshal(b, &m) |
It would be simpler as: b, err := io.ReadAll(os.Stdin)
... // handle err
err = protojson.Unmarshal(b, &m)
... // handle err |
The problem with
Where, previously I could decode from a stream, or even messages like:
|
If you need to decode a JSON stream, then something similar to what you suggested is the right way to go. You can actually do it more efficiently with var b json.RawMessage
dec := json.NewDecoder(os.Stdin)
err := dec.Decode(&b)
... // check err
err = protojson.Umarshal(b, &m)
... // check err BTW, parsing a JSON stream is actually different from what this issue about, which is regarding adding for parsing JSON from an IO stream. |
JSON serialization is very commonly used in HTTP servers where use of
net/http
means that the user typically starts with aio.Reader
andio.Writer
on hand. As such, it is slightly inconvenient usingprotojson
since it operates primarily on[]byte
.Perhaps we should add
MarshalWriter
that takes in aio.Writer
as input and aUnmarshalReader
that takes in aio.Reader
as input.Consideration needs to be given to:
proto
package to support streaming read/write and/or scatter/gather functionality.json.NewDecoder(r).Decode(...)
is subtly buggy since it permits trailing trash data to go undetected.(We would probably do the equivalent change to
prototext
since the two packages have nearly identical API surface despite having obviously different semantic behavior).The text was updated successfully, but these errors were encountered: