FAQ
I think you've hit an unfortunate gotcha with encoding/json. Decode and
Encode aren't exact mirrors of each other.

Encode writes a trailing newline, but decode doesn't read this newline:

https://github.com/golang/go/blob/f78a4c84ac8ed44aaf331989aa32e40081fd8f13/src/encoding/json/stream.go#L193


I became aware of this via a nasty docker bug:

https://github.com/docker/docker/issues/14203


I can't find any other discussion about the problem on this list, hopefully
others can chime in with references to why this decision was made.

As far as I can see, your solution to use ioutil.Discard is correct and
reasonable.

A strict reading of the documentation supports the current behaviour:

https://golang.org/pkg/encoding/json/#Encoder.Encode
Encode writes the JSON encoding of v to the stream, followed by a newline
character.
https://golang.org/pkg/encoding/json/#Decoder.Decode
Decode reads the next JSON-encoded value from its input and stores it in
the value pointed to by v.

Note that decode makes no promise to consume the extra newline.

On 4 May 2016 at 10:41, Jérôme LAFORGE wrote:

Normally I just read the response with
json.NewDecoder(response.Body).Decode(&something)
My current comprehension, that is json decoder doesn't read the last 0
that indicates the end of chunked response.
or do I miss something?
>

Do I have to put defer io.Copy(ioutil.Discard, r.Body)?
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 3 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 4, '16 at 9:41a
activeMay 17, '16 at 6:38a
posts3
users2
websitegolang.org

2 users in discussion

Jérôme LAFORGE: 2 posts Peter Waller: 1 post

People

Translate

site design / logo © 2022 Grokbase