Skip to content
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

Dev json engine #4095

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,18 @@ type Context struct {
// or PUT body parameters.
formCache url.Values

// SameSite allows a server to define a cookie attribute making it impossible for
// sameSite allows a server to define a cookie attribute making it impossible for
// the browser to send this cookie along with cross-site requests.
sameSite http.SameSite

// jsonRender the JSON renderer currently used. The default value is render.JSON,
// which is used to get the JSON rendering type when executing the c.JSON function.
jsonRender func(data any) render.Render
}

// SetJSONRender set the current JSON render
func (c *Context) SetJSONRender(jsonRender func(data any) render.Render) {
c.jsonRender = jsonRender
}

/************************************/
Expand All @@ -109,6 +118,8 @@ func (c *Context) reset() {
c.queryCache = nil
c.formCache = nil
c.sameSite = 0
// registering a default JSON renderer
c.jsonRender = render.NewJSON
*c.params = (*c.params)[:0]
*c.skippedNodes = (*c.skippedNodes)[:0]
}
Expand Down Expand Up @@ -1095,7 +1106,7 @@ func (c *Context) JSONP(code int, obj any) {
// JSON serializes the given struct as JSON into the response body.
// It also sets the Content-Type as "application/json".
func (c *Context) JSON(code int, obj any) {
c.Render(code, render.JSON{Data: obj})
c.Render(code, c.jsonRender(obj))
}

// AsciiJSON serializes the given struct as JSON into the response body with unicode to ASCII string.
Expand Down
33 changes: 31 additions & 2 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"context"
"errors"
"fmt"
"github.com/gin-gonic/gin/render"
"html/template"
"io"
"io/fs"
Expand Down Expand Up @@ -166,7 +167,7 @@ func TestSaveUploadedFileWithPermission(t *testing.T) {
require.NoError(t, err)
mw.Close()
c, _ := CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest("POST", "/", buf)
c.Request, _ = http.NewRequest(http.MethodPost,, "/", buf)
c.Request.Header.Set("Content-Type", mw.FormDataContentType())
f, err := c.FormFile("file")
require.NoError(t, err)
Expand All @@ -187,7 +188,7 @@ func TestSaveUploadedFileWithPermissionFailed(t *testing.T) {
require.NoError(t, err)
mw.Close()
c, _ := CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest("POST", "/", buf)
c.Request, _ = http.NewRequest(http.MethodPost, "/", buf)
c.Request.Header.Set("Content-Type", mw.FormDataContentType())
f, err := c.FormFile("file")
require.NoError(t, err)
Expand Down Expand Up @@ -3123,3 +3124,31 @@ func TestContextNext(t *testing.T) {
assert.True(t, exists)
assert.Equal(t, "value3", value)
}

// MyJSON customizing JSON rendering
type MyJSON struct {
Data any
render.JSON
}

// Render rewrite the Render function
func (r MyJSON) Render(w http.ResponseWriter) error {
_, err := w.Write([]byte("test"))
return err
}

func NewMyJSON(data any) render.Render {
return &MyJSON{Data: data}
}

// TestCustomJSONRender the test uses a custom JSON render.
// The final result is that the user can customize the JSON render without affecting the original function.
func TestCustomJSONRender(t *testing.T) {
w := httptest.NewRecorder()
c, _ := CreateTestContext(w)
c.SetJSONRender(NewMyJSON)

c.JSON(http.StatusCreated, H{"foo": "bar", "html": "<b>"})

t.Log(w.Body.String())
}
4 changes: 4 additions & 0 deletions render/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ type JSON struct {
Data any
}

func NewJSON(data any) Render {
return &JSON{Data: data}
}

// IndentedJSON contains the given interface object.
type IndentedJSON struct {
Data any
Expand Down