Adds indented JSON render

This commit is contained in:
Manu Mtz-Almeida 2015-03-31 18:02:38 +02:00
parent d4413b6e91
commit abcc6d9dec

View File

@ -19,6 +19,8 @@ type (
jsonRender struct{} jsonRender struct{}
indentedJSON struct{}
xmlRender struct{} xmlRender struct{}
plainTextRender struct{} plainTextRender struct{}
@ -34,6 +36,7 @@ type (
var ( var (
JSON = jsonRender{} JSON = jsonRender{}
IndentedJSON = indentedJSON{}
XML = xmlRender{} XML = xmlRender{}
HTMLPlain = htmlPlainRender{} HTMLPlain = htmlPlainRender{}
Plain = plainTextRender{} Plain = plainTextRender{}
@ -51,6 +54,16 @@ func (_ jsonRender) Render(w http.ResponseWriter, code int, data ...interface{})
return json.NewEncoder(w).Encode(data[0]) return json.NewEncoder(w).Encode(data[0])
} }
func (_ indentedJSON) Render(w http.ResponseWriter, code int, data ...interface{}) error {
WriteHeader(w, code, "application/json")
jsonData, err := json.MarshalIndent(data, "", " ")
if err != nil {
return err
}
_, err = w.Write(jsonData)
return err
}
func (_ xmlRender) Render(w http.ResponseWriter, code int, data ...interface{}) error { func (_ xmlRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
WriteHeader(w, code, "application/xml") WriteHeader(w, code, "application/xml")
return xml.NewEncoder(w).Encode(data[0]) return xml.NewEncoder(w).Encode(data[0])