Add PureJSON renderer (#694)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Closes #693
authored by Filip Figiel · committed by Bo-Yi Wu · parent b7bb9ba · Browse files · 8 files·+133 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+133 added−4 removed
- other:+133 added−4 removed
Provenance
- Commit
c6110f9- Tree
b440acf- Author
Filip Figiel <figiel.filip@gmail.com>- Committer
Bo-Yi Wu <appleboy.tw@gmail.com>- Parent
b7bb9ba
8 files changed+133 added−4 removed
modified:json/json.go+1 added
10 unmodified lines11 var (12 Marshal = json.Marshal13 MarshalIndent = json.MarshalIndent14 NewDecoder = json.NewDecoder15+ NewEncoder = json.NewEncoder16 )added:render/json_17.go+28 added
1+// Copyright 2018 Gin Core Team. All rights reserved.2+// Use of this source code is governed by a MIT style3+// license that can be found in the LICENSE file.4+5+// +build go1.76+7+package render8+9+import (10+ "net/http"11+12+ "github.com/gin-gonic/gin/json"13+)14+15+type PureJSON struct {16+ Data interface{}17+}18+19+func (r PureJSON) Render(w http.ResponseWriter) error {20+ r.WriteContentType(w)21+ encoder := json.NewEncoder(w)22+ encoder.SetEscapeHTML(false)23+ return encoder.Encode(r.Data)24+}25+26+func (r PureJSON) WriteContentType(w http.ResponseWriter) {27+ writeContentType(w, jsonContentType)28+}added:render/render_17_test.go+26 added
1+// Copyright 2018 Gin Core Team. All rights reserved.2+// Use of this source code is governed by a MIT style3+// license that can be found in the LICENSE file.4+5+// +build go1.76+7+package render8+9+import (10+ "net/http/httptest"11+ "testing"12+13+ "github.com/stretchr/testify/assert"14+)15+16+func TestRenderPureJSON(t *testing.T) {17+ w := httptest.NewRecorder()18+ data := map[string]interface{}{19+ "foo": "bar",20+ "html": "<b>",21+ }22+ err := (PureJSON{data}).Render(w)23+ assert.NoError(t, err)24+ assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"<b>\"}\n", w.Body.String())25+ assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))26+}modified:render/render_test.go+3 added-2 removed
50 unmodified lines51 52 func TestRenderJSON(t *testing.T) {53 w := httptest.NewRecorder()54 data := map[string]interface{}{55- "foo": "bar",55+ "foo": "bar",56+ "html": "<b>",57 }58 59 (JSON{data}).WriteContentType(w)60 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))61 62 err := (JSON{data}).Render(w)63 64 assert.NoError(t, err)64- assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String())65+ assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}", w.Body.String())66 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))67 }68 69 func TestRenderJSONPanics(t *testing.T) {417 unmodified linesadded:context_17_test.go+27 added
1+// Copyright 2018 Gin Core Team. All rights reserved.2+// Use of this source code is governed by a MIT style3+// license that can be found in the LICENSE file.4+5+// +build go1.76+7+package gin8+9+import (10+ "net/http"11+ "net/http/httptest"12+ "testing"13+14+ "github.com/stretchr/testify/assert"15+)16+17+// Tests that the response is serialized as JSON18+// and Content-Type is set to application/json19+// and special HTML characters are preserved20+func TestContextRenderPureJSON(t *testing.T) {21+ w := httptest.NewRecorder()22+ c, _ := CreateTestContext(w)23+ c.PureJSON(http.StatusCreated, H{"foo": "bar", "html": "<b>"})24+ assert.Equal(t, http.StatusCreated, w.Code)25+ assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"<b>\"}\n", w.Body.String())26+ assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type"))27+}added:context_17.go+17 added
1+// Copyright 2018 Gin Core Team. All rights reserved.2+// Use of this source code is governed by a MIT style3+// license that can be found in the LICENSE file.4+5+// +build go1.76+7+package gin8+9+import (10+ "github.com/gin-gonic/gin/render"11+)12+13+// PureJSON serializes the given struct as JSON into the response body.14+// PureJSON, unlike JSON, does not replace special html characters with their unicode entities.15+func (c *Context) PureJSON(code int, obj interface{}) {16+ c.Render(code, render.PureJSON{Data: obj})17+}modified:context_test.go+3 added-2 removed
617 unmodified lines618 }619 620 // Tests that the response is serialized as JSON621 // and Content-Type is set to application/json622+// and special HTML characters are escaped623 func TestContextRenderJSON(t *testing.T) {624 w := httptest.NewRecorder()625 c, _ := CreateTestContext(w)626 626- c.JSON(http.StatusCreated, H{"foo": "bar"})627+ c.JSON(http.StatusCreated, H{"foo": "bar", "html": "<b>"})628 629 assert.Equal(t, http.StatusCreated, w.Code)629- assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String())630+ assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}", w.Body.String())631 assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type"))632 }633 634 // Tests that the response is serialized as JSONP1054 unmodified linesmodified:README.md+28 added
989 unmodified lines990 r.Run(":8080")991 }992 ```993 994+#### PureJSON995+996+Normally, JSON replaces special HTML characters with their unicode entities, e.g. `<` becomes `\u003c`. If you want to encode such characters literally, you can use PureJSON instead.997+This feature is unavailable in Go 1.6 and lower.998+999+```go1000+func main() {1001+ r := gin.Default()1002+ 1003+ // Serves unicode entities1004+ r.GET("/json", func(c *gin.Context) {1005+ c.JSON(200, gin.H{1006+ "html": "<b>Hello, world!</b>",1007+ })1008+ })1009+ 1010+ // Serves literal characters1011+ r.GET("/purejson", func(c *gin.Context) {1012+ c.PureJSON(200, gin.H{1013+ "html": "<b>Hello, world!</b>",1014+ })1015+ })1016+ 1017+ // listen and serve on 0.0.0.0:80801018+ r.Run(":8080)1019+}1020+```1021+1022 ### Serving static files1023 1024 ```go1025 func main() {861 unmodified lines