gin/render/json.go
Manu Mtz-Almeida 2d8f0a4801 Performance improvements when rendering
- Fast path for JSON, XML and plain text rendering
2015-05-07 12:44:52 +02:00

32 lines
665 B
Go

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 {
WriteHeader(w, code, "application/json")
jsonData, err := json.MarshalIndent(data[0], "", " ")
if err != nil {
return err
}
_, err = w.Write(jsonData)
return err
}
func WriteJSON(w http.ResponseWriter, code int, data interface{}) error {
WriteHeader(w, code, "application/json")
return json.NewEncoder(w).Encode(data)
}