Skip to content

Commit

Permalink
Add PureJSON for rendering unescaped json data
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip Figiel committed Aug 31, 2016
1 parent 2079ce6 commit bcf1052
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
6 changes: 6 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,12 @@ func (c *Context) JSON(code int, obj interface{}) {
}
}

// PureJSON serializes the given struct as JSON into the response body.
// PureJSON, unlike JSON, does not replace special html characters with their unicode entities.
func (c *Context) PureJSON(code int, obj interface{}) {
c.Render(code, render.PureJSON{Data: obj})
}

// XML serializes the given struct as XML into the response body.
// It also sets the Content-Type as "application/xml".
func (c *Context) XML(code int, obj interface{}) {
Expand Down
15 changes: 15 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,21 @@ func TestContextRenderJSON(t *testing.T) {
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8")
}

// Tests that the response is serialized as JSON
// and Content-Type is set to application/json
// and special HTML characters are preserved
func TestContextRenderPureJSON(t *testing.T) {
c, w, _ := CreateTestContext()
c.PureJSON(201, H{"foo": "bar", "html": "<b>"})

assert.Equal(t, w.Code, 201)
assert.Equal(
t,
w.Body.String(),
"{\"foo\":\"bar\",\"html\":\"<b>\"}\n")
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8")
}

// Tests that the response is serialized as JSON
// we change the content-type before
func TestContextRenderAPIJSON(t *testing.T) {
Expand Down
11 changes: 11 additions & 0 deletions render/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ type (
Data interface{}
}

PureJSON struct {
Data interface{}
}

IndentedJSON struct {
Data interface{}
}
Expand All @@ -25,6 +29,13 @@ func (r JSON) Render(w http.ResponseWriter) error {
return WriteJSON(w, r.Data)
}

func (r PureJSON) Render(w http.ResponseWriter) error {
writeContentType(w, jsonContentType)
encoder := json.NewEncoder(w)
encoder.SetEscapeHTML(false)
return encoder.Encode(r.Data)
}

func (r IndentedJSON) Render(w http.ResponseWriter) error {
writeContentType(w, jsonContentType)
jsonBytes, err := json.MarshalIndent(r.Data, "", " ")
Expand Down
17 changes: 17 additions & 0 deletions render/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ func TestRenderJSON(t *testing.T) {
assert.Equal(t, w.Header().Get("Content-Type"), "application/json; charset=utf-8")
}

func TestRenderPureJSON(t *testing.T) {
w := httptest.NewRecorder()
data := map[string]interface{}{
"foo": "bar",
"html": "<b>",
}

err := (PureJSON{data}).Render(w)

assert.NoError(t, err)
assert.Equal(
t,
w.Body.String(),
"{\"foo\":\"bar\",\"html\":\"<b>\"}\n")
assert.Equal(t, w.Header().Get("Content-Type"), "application/json; charset=utf-8")
}

func TestRenderIndentedJSON(t *testing.T) {
w := httptest.NewRecorder()
data := map[string]interface{}{
Expand Down

0 comments on commit bcf1052

Please sign in to comment.