Re: [go-nuts] json.NewDecoder()/Decode() versus Unmarshal() for single large JSON objects

2020-12-29 Thread Amit Saha
On Wed, Dec 30, 2020 at 4:07 AM Amnon wrote: > > How do you know, if you don't check? FTR, it's not just about sending > garbage, it's also about requests accidentally being truncated or just > generally garbled. > > json.NewDecoder(r.Body).Decode() will return an error if the request > is

Re: [go-nuts] json.NewDecoder()/Decode() versus Unmarshal() for single large JSON objects

2020-12-29 Thread Amit Saha
On Tue, Dec 29, 2020 at 9:50 PM Axel Wagner wrote: > > There is an important semantic difference between the two, which means you > almost never want to use a `Decoder`: `Unmarshal` is for parsing a single > json document, whereas a `Decoder` is for parsing a stream of concatenated >

Re: [go-nuts] json.NewDecoder()/Decode() versus Unmarshal() for single large JSON objects

2020-12-29 Thread 'Axel Wagner' via golang-nuts
On Tue, Dec 29, 2020 at 6:07 PM Amnon wrote: > *How do you know, if you don't check? FTR, it's not just about sending > garbage, it's also about requests accidentally being truncated or just > generally garbled.* > > json.NewDecoder(r.Body).Decode() will return an error if the > request is

Re: [go-nuts] json.NewDecoder()/Decode() versus Unmarshal() for single large JSON objects

2020-12-29 Thread Amnon
*How do you know, if you don't check? FTR, it's not just about sending garbage, it's also about requests accidentally being truncated or just generally garbled.* json.NewDecoder(r.Body).Decode() will return an error if the request is garbled or truncated. The other nice thing about this code

Re: [go-nuts] json.NewDecoder()/Decode() versus Unmarshal() for single large JSON objects

2020-12-29 Thread 'Axel Wagner' via golang-nuts
On Tue, Dec 29, 2020 at 5:17 PM Amnon wrote: > I always use `json.NewDecoder(r.Body).Decode()` > You shouldn't. The code is more succinct than reading the entire body into a buffer, and > then unmarshalling it. And there is only one error to check. > There is only one error to check, because

Re: [go-nuts] json.NewDecoder()/Decode() versus Unmarshal() for single large JSON objects

2020-12-29 Thread Amnon
I always use `json.NewDecoder(r.Body).Decode()` The code is more succinct than reading the entire body into a buffer, and then unmarshalling it. And there is only one error to check. If I was super concerned about people sending trailing gibberish to my server, I could call `dec.Buffered()` to

Re: [go-nuts] json.NewDecoder()/Decode() versus Unmarshal() for single large JSON objects

2020-12-29 Thread 'Axel Wagner' via golang-nuts
There is an important semantic difference between the two, which means you almost never want to use a `Decoder`: `Unmarshal` is for parsing a single json document, whereas a `Decoder` is for parsing a stream of concatenated documents, like so: https://play.golang.org/p/4uiNyJlNIKh. In particular,

Re: [go-nuts] json.NewDecoder()/Decode() versus Unmarshal() for single large JSON objects

2020-12-29 Thread Amit Saha
On Tue, Dec 29, 2020 at 11:35 AM burak serdar wrote: > > On Mon, Dec 28, 2020 at 5:22 PM Amit Saha wrote: > > > > Hi all, let's say I am a single large JSON object that I want to > > process in my HTTP server backend. > > > > I am trying to get my head around if there is any performance > >

Re: [go-nuts] json.NewDecoder()/Decode() versus Unmarshal() for single large JSON objects

2020-12-28 Thread burak serdar
On Mon, Dec 28, 2020 at 5:22 PM Amit Saha wrote: > > Hi all, let's say I am a single large JSON object that I want to > process in my HTTP server backend. > > I am trying to get my head around if there is any performance > advantage - memory or CPU to use the json.NewDecoder()/Decode() >

[go-nuts] json.NewDecoder()/Decode() versus Unmarshal() for single large JSON objects

2020-12-28 Thread Amit Saha
Hi all, let's say I am a single large JSON object that I want to process in my HTTP server backend. I am trying to get my head around if there is any performance advantage - memory or CPU to use the json.NewDecoder()/Decode() mechanism versus the Unmarshal() function? Thanks, Amit -- You