mirror of https://github.com/gin-gonic/gin.git
c.JSON performance improvement
benchmark old ns/op new ns/op delta BenchmarkOneRouteJSON 1143 1053 -7.87% benchmark old allocs new allocs delta BenchmarkOneRouteJSON 4 3 -25.00% benchmark old bytes new bytes delta BenchmarkOneRouteJSON 72 56 -22.22%
This commit is contained in:
parent
56683d33b1
commit
822b995687
11
context.go
11
context.go
|
@ -285,10 +285,14 @@ func (c *Context) Header(key, value string) {
|
||||||
func (c *Context) Render(code int, r render.Render) {
|
func (c *Context) Render(code int, r render.Render) {
|
||||||
c.writermem.WriteHeader(code)
|
c.writermem.WriteHeader(code)
|
||||||
if err := r.Render(c.Writer); err != nil {
|
if err := r.Render(c.Writer); err != nil {
|
||||||
|
c.renderError(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Context) renderError(err error) {
|
||||||
debugPrintError(err)
|
debugPrintError(err)
|
||||||
c.AbortWithError(500, err).SetType(ErrorTypeRender)
|
c.AbortWithError(500, err).SetType(ErrorTypeRender)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Renders the HTTP template specified by its file name.
|
// Renders the HTTP template specified by its file name.
|
||||||
// It also updates the HTTP code and sets the Content-Type as "text/html".
|
// It also updates the HTTP code and sets the Content-Type as "text/html".
|
||||||
|
@ -309,7 +313,10 @@ func (c *Context) IndentedJSON(code int, obj interface{}) {
|
||||||
// Serializes the given struct as JSON into the response body.
|
// Serializes the given struct as JSON into the response body.
|
||||||
// It also sets the Content-Type as "application/json".
|
// It also sets the Content-Type as "application/json".
|
||||||
func (c *Context) JSON(code int, obj interface{}) {
|
func (c *Context) JSON(code int, obj interface{}) {
|
||||||
c.Render(code, render.JSON{Data: obj})
|
c.writermem.WriteHeader(code)
|
||||||
|
if err := render.WriteJSON(c.Writer, obj); err != nil {
|
||||||
|
c.renderError(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serializes the given struct as XML into the response body.
|
// Serializes the given struct as XML into the response body.
|
||||||
|
|
|
@ -22,8 +22,7 @@ type (
|
||||||
var jsonContentType = []string{"application/json; charset=utf-8"}
|
var jsonContentType = []string{"application/json; charset=utf-8"}
|
||||||
|
|
||||||
func (r JSON) Render(w http.ResponseWriter) error {
|
func (r JSON) Render(w http.ResponseWriter) error {
|
||||||
w.Header()["Content-Type"] = jsonContentType
|
return WriteJSON(w, r.Data)
|
||||||
return json.NewEncoder(w).Encode(r.Data)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r IndentedJSON) Render(w http.ResponseWriter) error {
|
func (r IndentedJSON) Render(w http.ResponseWriter) error {
|
||||||
|
@ -35,3 +34,8 @@ func (r IndentedJSON) Render(w http.ResponseWriter) error {
|
||||||
w.Write(jsonBytes)
|
w.Write(jsonBytes)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WriteJSON(w http.ResponseWriter, obj interface{}) error {
|
||||||
|
w.Header()["Content-Type"] = jsonContentType
|
||||||
|
return json.NewEncoder(w).Encode(obj)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue