principlegoCritical
Decoding JSON using json.Unmarshal vs json.NewDecoder.Decode
Viewed 0 times
decodingnewdecoderdecodeusingunmarshaljson
Problem
I'm developing an API client where I need to encode a JSON payload on request and decode a JSON body from the response.
I've read the source code from several libraries and from what I have seen, I have basically two possibilities for encoding and decoding a JSON string.
Use
or using
In my case, when dealing with HTTP responses that implements
Moreover, the accepted answer from this question says
Please use
but it didn't mention the reason. Should I really avoid using
I've read the source code from several libraries and from what I have seen, I have basically two possibilities for encoding and decoding a JSON string.
Use
json.Unmarshal passing the entire response stringdata, err := ioutil.ReadAll(resp.Body)
if err == nil && data != nil {
err = json.Unmarshal(data, value)
}or using
json.NewDecoder.Decodeerr = json.NewDecoder(resp.Body).Decode(value)In my case, when dealing with HTTP responses that implements
io.Reader, the second version seems to be require less code, but since I've seen both I wonder if there is any preference whether I should use a solution rather than the other.Moreover, the accepted answer from this question says
Please use
json.Decoder instead of json.Unmarshal.but it didn't mention the reason. Should I really avoid using
json.Unmarshal?Solution
It really depends on what your input is. If you look at the implementation of the
So a better rule of thumb is this:
For the case of reading from an HTTP request, I'd pick
Decode method of json.Decoder, it buffers the entire JSON value in memory before unmarshalling it into a Go value. So in most cases it won't be any more memory efficient (although this could easily change in a future version of the language).So a better rule of thumb is this:
- Use
json.Decoderif your data is coming from anio.Readerstream, or you need to decode multiple values from a stream of data.
- Use
json.Unmarshalif you already have the JSON data in memory.
For the case of reading from an HTTP request, I'd pick
json.Decoder since you're obviously reading from a stream.Context
Stack Overflow Q#21197239, score: 397
Revisions (0)
No revisions yet.