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

carlmjohnson opened this issue Oct 16, 2020 · 2 comments


Copy link

@carlmjohnson carlmjohnson commented Oct 16, 2020

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.

Copy link

@mvdan 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.

Copy link
Contributor Author

@carlmjohnson carlmjohnson commented Oct 16, 2020

Closing in favor of #36225.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.