2015-05-07 10:44:52 +00:00
|
|
|
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, "", " ")
|
2015-05-07 10:44:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = w.Write(jsonData)
|
|
|
|
return err
|
|
|
|
}
|