gin/render/json.go

36 lines
821 B
Go
Raw Normal View History

package render
import (
"encoding/json"
"net/http"
)
type (
jsonRender struct{}
indentedJSON struct{}
)
func (_ jsonRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
return WriteJSON(w, code, data[0])
}
func (_ indentedJSON) Render(w http.ResponseWriter, code int, data ...interface{}) error {
2015-05-10 23:02:17 +00:00
return WriteIndentedJSON(w, code, data[0])
}
func WriteJSON(w http.ResponseWriter, code int, data interface{}) error {
writeHeader(w, code, "application/json; charset=utf-8")
return json.NewEncoder(w).Encode(data)
}
func WriteIndentedJSON(w http.ResponseWriter, code int, data interface{}) error {
writeHeader(w, code, "application/json; charset=utf-8")
jsonData, err := json.MarshalIndent(data, "", " ")
if err != nil {
return err
}
_, err = w.Write(jsonData)
return err
}