Skip to content
Myr

c.JSON performance improvement

Before adoption: this commit came in with the history the project adopted, so no landing records it.

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%

authored by Manu Mtz-Almeida · committed by Manu Mtz-Almeida · parent 56683d3 · Browse files · 2 files+16 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+16 added−5 removed

  • other:+16 added−5 removed

2 files+16 added−5 removed

modified:render/json.go+6 added-2 removed
20 unmodified lines
2122var jsonContentType = []string{"application/json; charset=utf-8"}2324func (r JSON) Render(w http.ResponseWriter) error {25 w.Header()["Content-Type"] = jsonContentType26 return json.NewEncoder(w).Encode(r.Data)25 return WriteJSON(w, r.Data)26}2728func (r IndentedJSON) Render(w http.ResponseWriter) error {29 w.Header()["Content-Type"] = jsonContentType
3 unmodified lines
33 }34 w.Write(jsonBytes)35 return nil36}3738func WriteJSON(w http.ResponseWriter, obj interface{}) error {39 w.Header()["Content-Type"] = jsonContentType40 return json.NewEncoder(w).Encode(obj)41}
modified:context.go+10 added-3 removed
283 unmodified lines
284285func (c *Context) Render(code int, r render.Render) {286 c.writermem.WriteHeader(code)287 if err := r.Render(c.Writer); err != nil {288 debugPrintError(err)289 c.AbortWithError(500, err).SetType(ErrorTypeRender)288 c.renderError(err)289 }290}291292func (c *Context) renderError(err error) {293 debugPrintError(err)294 c.AbortWithError(500, err).SetType(ErrorTypeRender)295}296297// Renders the HTTP template specified by its file name.298// It also updates the HTTP code and sets the Content-Type as "text/html".299// See http://golang.org/doc/articles/wiki/300func (c *Context) HTML(code int, name string, obj interface{}) {
11 unmodified lines
312313// Serializes the given struct as JSON into the response body.314// It also sets the Content-Type as "application/json".315func (c *Context) JSON(code int, obj interface{}) {312 c.Render(code, render.JSON{Data: obj})316 c.writermem.WriteHeader(code)317 if err := render.WriteJSON(c.Writer, obj); err != nil {318 c.renderError(err)319 }320}321322// Serializes the given struct as XML into the response body.323// It also sets the Content-Type as "application/xml".
138 unmodified lines