Skip to content
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/json: add doc note that Decoder is not more efficient for single values #42016

Closed
carlmjohnson opened this issue Oct 16, 2020 · 2 comments

Comments

@carlmjohnson
Copy link
Contributor

I frequently see code where an http.Response.Body is decoded with json.Decoder, under the presumption that json.Decoder performs memory saving streaming. In fact, it is not more efficient and it is arguably incorrect for http.Responses where only one value should be present.

Here is the current docstring:

A Decoder reads and decodes JSON values from an input stream.

Here is a proposed docstring:

A Decoder reads and decodes JSON values from an input stream. For a stream that should only have a single JSON value, use Unmarshal.

@mvdan
Copy link
Member

mvdan commented Oct 16, 2020

I think the better long term fix here would be #36225; Unmarshal is not a particularly better option right now. The decoder buffering an entire value can be surprising, but I don't think it's a behavior that we want to document and teach people. I especially don't want people to memorize that Unmarshal is supposedly faster than Decoder.Decode for a single value, because that might not be true in the future.

@carlmjohnson
Copy link
Contributor Author

Closing in favor of #36225.

@golang golang locked and limited conversation to collaborators Oct 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants