-
Notifications
You must be signed in to change notification settings - Fork 8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add PureJSON renderer #694
Changes from 14 commits
6613cdb
79478c0
ddb175b
5676689
5c70490
f019519
2000a13
fa2e05a
147b479
786a213
a682226
df00f24
ac48b84
4152da4
a84483b
fbe37a5
ee988b5
b1cf3bf
a847321
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build go1.7 | ||
|
||
package gin | ||
|
||
import ( | ||
"github.com/gin-gonic/gin/render" | ||
) | ||
|
||
// 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}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// +build go1.7 | ||
|
||
package gin | ||
|
||
import ( | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// 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) { | ||
w := httptest.NewRecorder() | ||
c, _ := CreateTestContext(w) | ||
c.PureJSON(201, H{"foo": "bar", "html": "<b>"}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please change |
||
assert.Equal(t, 201, w.Code) | ||
assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"<b>\"}\n", w.Body.String()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. too |
||
assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type")) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -570,14 +570,18 @@ func TestContextRenderPanicIfErr(t *testing.T) { | |
|
||
// Tests that the response is serialized as JSON | ||
// and Content-Type is set to application/json | ||
// and special HTML characters are escaped | ||
func TestContextRenderJSON(t *testing.T) { | ||
w := httptest.NewRecorder() | ||
c, _ := CreateTestContext(w) | ||
|
||
c.JSON(201, H{"foo": "bar"}) | ||
c.JSON(201, H{"foo": "bar", "html": "<b>"}) | ||
|
||
assert.Equal(t, 201, w.Code) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please change |
||
assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String()) | ||
assert.Equal( | ||
t, | ||
"{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}", | ||
w.Body.String()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggest to use one line |
||
assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type")) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please change to // Copyright 2018 Gin Core Team. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
// +build go1.7 | ||
|
||
package render | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin/json" | ||
) | ||
|
||
type PureJSON struct { | ||
Data interface{} | ||
} | ||
|
||
func (r PureJSON) Render(w http.ResponseWriter) error { | ||
r.WriteContentType(w) | ||
encoder := json.NewEncoder(w) | ||
encoder.SetEscapeHTML(false) | ||
return encoder.Encode(r.Data) | ||
} | ||
|
||
func (r PureJSON) WriteContentType(w http.ResponseWriter) { | ||
writeContentType(w, jsonContentType) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please change to // Copyright 2018 Gin Core Team. All rights reserved. |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// +build go1.7 | ||
|
||
package render | ||
|
||
import ( | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
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, "{\"foo\":\"bar\",\"html\":\"<b>\"}\n", w.Body.String()) | ||
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type")) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,13 +42,18 @@ func TestRenderMsgPack(t *testing.T) { | |
func TestRenderJSON(t *testing.T) { | ||
w := httptest.NewRecorder() | ||
data := map[string]interface{}{ | ||
"foo": "bar", | ||
"foo": "bar", | ||
"html": "<b>", | ||
} | ||
|
||
err := (JSON{data}).Render(w) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String()) | ||
assert.Equal( | ||
t, | ||
"{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}\n", | ||
w.Body.String()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggest to use one line |
||
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type")) | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.