Skip to content
Myr

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

8 files+133 added−4 removed

modified:json/json.go+1 added
10 unmodified lines
11var (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.45// +build go1.767package render89import (10 "net/http"1112 "github.com/gin-gonic/gin/json"13)1415type PureJSON struct {16 Data interface{}17}1819func (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}2526func (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.45// +build go1.767package render89import (10 "net/http/httptest"11 "testing"1213 "github.com/stretchr/testify/assert"14)1516func 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 lines
5152func TestRenderJSON(t *testing.T) {53 w := httptest.NewRecorder()54 data := map[string]interface{}{55 "foo": "bar",55 "foo": "bar",56 "html": "<b>",57 }5859 (JSON{data}).WriteContentType(w)60 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))6162 err := (JSON{data}).Render(w)6364 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}6869func TestRenderJSONPanics(t *testing.T) {
417 unmodified lines
added: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.45// +build go1.767package gin89import (10 "net/http"11 "net/http/httptest"12 "testing"1314 "github.com/stretchr/testify/assert"15)1617// Tests that the response is serialized as JSON18// and Content-Type is set to application/json19// and special HTML characters are preserved20func 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.45// +build go1.767package gin89import (10 "github.com/gin-gonic/gin/render"11)1213// 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.15func (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 lines
618}619620// Tests that the response is serialized as JSON621// and Content-Type is set to application/json622// and special HTML characters are escaped623func TestContextRenderJSON(t *testing.T) {624 w := httptest.NewRecorder()625 c, _ := CreateTestContext(w)626626 c.JSON(http.StatusCreated, H{"foo": "bar"})627 c.JSON(http.StatusCreated, H{"foo": "bar", "html": "<b>"})628629 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}633634// Tests that the response is serialized as JSONP
1054 unmodified lines
modified:README.md+28 added
989 unmodified lines
990 r.Run(":8080")991}992```993994#### PureJSON995996Normally, 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.997This feature is unavailable in Go 1.6 and lower.998999```go1000func 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```10211022### Serving static files10231024```go1025func main() {
861 unmodified lines