From abcc6d9dec17b1fd66f07aa2a0202eda8954dd39 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Tue, 31 Mar 2015 18:02:38 +0200 Subject: [PATCH] Adds indented JSON render --- render/render.go | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/render/render.go b/render/render.go index ff2fdfc4..6058483d 100644 --- a/render/render.go +++ b/render/render.go @@ -19,6 +19,8 @@ type ( jsonRender struct{} + indentedJSON struct{} + xmlRender struct{} plainTextRender struct{} @@ -33,11 +35,12 @@ type ( ) var ( - JSON = jsonRender{} - XML = xmlRender{} - HTMLPlain = htmlPlainRender{} - Plain = plainTextRender{} - Redirect = redirectRender{} + JSON = jsonRender{} + IndentedJSON = indentedJSON{} + XML = xmlRender{} + HTMLPlain = htmlPlainRender{} + Plain = plainTextRender{} + Redirect = redirectRender{} ) func (_ redirectRender) Render(w http.ResponseWriter, code int, data ...interface{}) error { @@ -51,6 +54,16 @@ func (_ jsonRender) Render(w http.ResponseWriter, code int, data ...interface{}) 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 { WriteHeader(w, code, "application/xml") return xml.NewEncoder(w).Encode(data[0])