Replace json.Marshal with json.Encoder
mroobert opened this issue · comments
On the foundation -> web -> response.go we have:
// Convert the response value to JSON.
jsonData, err := json.Marshal(data)
if err != nil {
return err
}
Wouldn't be this version better? Could be considered an over-optimization?
// Convert the response value to JSON.
e := json.NewEncoder(w)
err := e.Encode(data)
if err != nil {
return err
}
I don't know why I didn't use this form, thought it would be all in one line
if err := json.NewEncoder(w).Encode(data); err != nil {
I think I like what I have because it breaks up the marshalling from the write. In some projects I like to use MarshalIndent to make the output more easy to read. If you wanted to do this, I would do it after the call to w.WriteHeader
I ran a test and I lost some control over the response. I don't know enough to understand the API again and I really don't have time. I think I will keep those two parts separated.