diff --git a/context.go b/context.go index 23f734c0..7fca00e5 100644 --- a/context.go +++ b/context.go @@ -156,19 +156,19 @@ func (c *Context) String(code int, format string, a ...interface{}) (err error) // JSON sends a JSON response with status code. func (c *Context) JSON(code int, i interface{}) (err error) { - return c.JSONPrettyPrint(code, i, false) + b, err := json.Marshal(i) + if err != nil { + return err + } + c.response.Header().Set(ContentType, ApplicationJSONCharsetUTF8) + c.response.WriteHeader(code) + c.response.Write(b) + return } -// JSON sends a pretty printed JSON response with status code. -func (c *Context) JSONPrettyPrint(code int, i interface{}, prettyPrint bool) (err error) { - var b []byte - - if prettyPrint { - b, err = json.MarshalIndent(i, " ", " ") - } else { - b, err = json.Marshal(i) - } - +// JSON sends a JSON response with status code, but it applies prefix and indent to format the output. +func (c *Context) JSONIndent(code int, i interface{}, prefix string, indent string) (err error) { + b, err := json.MarshalIndent(i, prefix, indent) if err != nil { return err } @@ -206,6 +206,19 @@ func (c *Context) XML(code int, i interface{}) (err error) { return } +// XML sends an XML response with status code, but it applies prefix and indent to format the output. +func (c *Context) XMLIndent(code int, i interface{}, prefix string, indent string) (err error) { + b, err := xml.MarshalIndent(i, prefix, indent) + if err != nil { + return err + } + c.response.Header().Set(ContentType, ApplicationXMLCharsetUTF8) + c.response.WriteHeader(code) + c.response.Write([]byte(xml.Header)) + c.response.Write(b) + return +} + // File sends a response with the content of the file. If `attachment` is set // to true, the client is prompted to save the file with provided `name`, // name can be empty, in that case name of the file is used. diff --git a/context_test.go b/context_test.go index e2cdc738..47ea3e7b 100644 --- a/context_test.go +++ b/context_test.go @@ -10,9 +10,9 @@ import ( "strings" - "encoding/xml" "net/url" + "encoding/xml" "github.com/stretchr/testify/assert" ) @@ -28,8 +28,9 @@ func (t *Template) Render(w io.Writer, name string, data interface{}) error { func TestContext(t *testing.T) { userJSON := `{"id":"1","name":"Joe"}` - userJSONPrettyPrint := "{\n \"id\": \"1\",\n \"name\": \"Joe\"\n }" + userJSONIndent := "{\n_?\"id\": \"1\",\n_?\"name\": \"Joe\"\n_}" userXML := `1Joe` + userXMLIndent := "_\n_?1\n_?Joe\n_" req, _ := http.NewRequest(POST, "/", strings.NewReader(userJSON)) rec := httptest.NewRecorder() @@ -98,24 +99,14 @@ func TestContext(t *testing.T) { assert.Equal(t, userJSON, rec.Body.String()) } - // JSONPrettyPrint (pretty) + // JSONIndent rec = httptest.NewRecorder() c = NewContext(req, NewResponse(rec), New()) - err = c.JSONPrettyPrint(http.StatusOK, user{"1", "Joe"}, true) + err = c.JSONIndent(http.StatusOK, user{"1", "Joe"}, "_", "?") if assert.NoError(t, err) { assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(t, ApplicationJSONCharsetUTF8, rec.Header().Get(ContentType)) - assert.Equal(t, userJSONPrettyPrint, rec.Body.String()) - } - - // JSONPrettyPrint (not pretty) - rec = httptest.NewRecorder() - c = NewContext(req, NewResponse(rec), New()) - err = c.JSONPrettyPrint(http.StatusOK, user{"1", "Joe"}, false) - if assert.NoError(t, err) { - assert.Equal(t, http.StatusOK, rec.Code) - assert.Equal(t, ApplicationJSONCharsetUTF8, rec.Header().Get(ContentType)) - assert.Equal(t, userJSON, rec.Body.String()) + assert.Equal(t, userJSONIndent, rec.Body.String()) } // JSONP @@ -136,7 +127,17 @@ func TestContext(t *testing.T) { if assert.NoError(t, err) { assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(t, ApplicationXMLCharsetUTF8, rec.Header().Get(ContentType)) - assert.Equal(t, xml.Header, xml.Header, rec.Body.String()) + assert.Equal(t, xml.Header+userXML, rec.Body.String()) + } + + // XMLIndent + rec = httptest.NewRecorder() + c = NewContext(req, NewResponse(rec), New()) + err = c.XMLIndent(http.StatusOK, user{"1", "Joe"}, "_", "?") + if assert.NoError(t, err) { + assert.Equal(t, http.StatusOK, rec.Code) + assert.Equal(t, ApplicationXMLCharsetUTF8, rec.Header().Get(ContentType)) + assert.Equal(t, xml.Header+userXMLIndent, rec.Body.String()) } // String