> For example, the error returned may show : "*invalid character 'G'
looking for beginning of value*". But you can't see the original message.

I believe https://golang.org/pkg/encoding/json/#Decoder.Buffered was added
for this purpose. For example, https://play.golang.org/p/yAn2fypIELc

> 1- I'm not sure the buffer is always complete at the time I call the
ReadAll

ReadAll (https://golang.org/pkg/io/ioutil/#ReadAll) reads until the io.EOF.
If you're using long lived TCP connections on which you expect to have
multiple request/response cycles then you likely don't want to use ReadAll
since the stream won't encounter an EOF until some later point in time. The
Decoder is made for the streaming case is very likely the right choice over
reading things in with other tools.

> 2- It can be time-consuming

I don't know that you're necessarily going to avoid this problem using the
json.Decoder. As it was pointed out, the current implementation of Decoder
continues to read and buffer bytes from the stream until it has enough to
represent a whole object before it decodes it. There may be some early exit
cases from errors in the "decoderState" that is used to manage the internal
buffer but I don't expect they should be relied on for performance.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to