1
0
mirror of https://github.com/gluster/glusterd2.git synced 2026-02-06 15:46:00 +01:00
Files
glusterd2/utils/jsonutils.go
Kaushal M 021db4ac97 Fix golint issues
Reported in https://app.wercker.com/#buildstep/55cdc9467331453f08005062

Or just run `golint ./...` in the root of this repo

Change-Id: I6870b220e101e264967fbb9495cd0bc65da70d78
Reviewed-on: https://review.gerrithub.io/243208
Reviewed-by: Kaushal M <kshlmster@gmail.com>
Tested-by: Kaushal M <kshlmster@gmail.com>
2015-08-14 14:23:50 +02:00

35 lines
643 B
Go

package utils
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
)
func jsonFromBody(r io.Reader, v interface{}) error {
// Check body
body, err := ioutil.ReadAll(r)
if err != nil {
return err
}
if err := json.Unmarshal(body, v); err != nil {
return err
}
return nil
}
// GetJSONFromRequest unmarshals JSON in `r` into `v`
func GetJSONFromRequest(r *http.Request, v interface{}) error {
defer r.Body.Close()
return jsonFromBody(r.Body, v)
}
// GetJSONFromResponse unmarshals JSON in `r` into `v`
func GetJSONFromResponse(r *http.Response, v interface{}) error {
defer r.Body.Close()
return jsonFromBody(r.Body, v)
}