Skip to content
Myr

use encode replace json marshal increase json encoder speed (#1546)

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

authored by itcloudy · committed by 田欧 · parent b1d607a · Browse files · 5 files+12 added−15 removed
This commit

State

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

Diff stats

5 files+12 added−15 removed

  • other:+12 added−15 removed

5 files+12 added−15 removed

modified:render/json.go+2 added-5 removed
66 unmodified lines
6768// WriteJSON marshals the given interface object and writes it with custom ContentType.69func WriteJSON(w http.ResponseWriter, obj interface{}) error {70 writeContentType(w, jsonContentType)71 jsonBytes, err := json.Marshal(obj)72 if err != nil {73 return err74 }75 _, err = w.Write(jsonBytes)71 encoder := json.NewEncoder(w)72 err := encoder.Encode(&obj)73 return err74}7576// Render (IndentedJSON) marshals the given interface object and writes it with custom ContentType.
115 unmodified lines
modified:render/render_test.go+1 added-1 removed
60 unmodified lines
6162 err := (JSON{data}).Render(w)6364 assert.NoError(t, err)65 assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}", w.Body.String())65 assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}\n", w.Body.String())66 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))67}6869func TestRenderJSONPanics(t *testing.T) {
431 unmodified lines
modified:context_test.go+5 added-5 removed
658 unmodified lines
659660 c.JSON(http.StatusCreated, H{"foo": "bar", "html": "<b>"})661662 assert.Equal(t, http.StatusCreated, w.Code)663 assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}", w.Body.String())663 assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}\n", w.Body.String())664 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))665}666667// Tests that the response is serialized as JSONP
19 unmodified lines
687688 c.JSONP(http.StatusCreated, H{"foo": "bar"})689690 assert.Equal(t, http.StatusCreated, w.Code)691 assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String())691 assert.Equal(t, "{\"foo\":\"bar\"}\n", w.Body.String())692 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))693}694695// Tests that no JSON is rendered if code is 204
17 unmodified lines
713 c.Header("Content-Type", "application/vnd.api+json")714 c.JSON(http.StatusCreated, H{"foo": "bar"})715716 assert.Equal(t, http.StatusCreated, w.Code)717 assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String())717 assert.Equal(t, "{\"foo\":\"bar\"}\n", w.Body.String())718 assert.Equal(t, "application/vnd.api+json", w.Header().Get("Content-Type"))719}720721// Tests that no Custom JSON is rendered if code is 204
394 unmodified lines
1116 Data: H{"foo": "bar"},1117 })11181119 assert.Equal(t, http.StatusOK, w.Code)1120 assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String())1120 assert.Equal(t, "{\"foo\":\"bar\"}\n", w.Body.String())1121 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))1122}11231124func TestContextNegotiationWithXML(t *testing.T) {
155 unmodified lines
1280 buf := new(bytes.Buffer)1281 _, err := buf.ReadFrom(w.Body)1282 assert.NoError(t, err)1283 jsonStringBody := buf.String()1284 assert.Equal(t, fmt.Sprint(`{"foo":"fooValue","bar":"barValue"}`), jsonStringBody)1284 assert.Equal(t, fmt.Sprint("{\"foo\":\"fooValue\",\"bar\":\"barValue\"}\n"), jsonStringBody)1285}12861287func TestContextError(t *testing.T) {1288 c, _ := CreateTestContext(httptest.NewRecorder())
535 unmodified lines
modified:logger_test.go+3 added-3 removed
367 unmodified lines
368 })369370 w := performRequest(router, "GET", "/error")371 assert.Equal(t, http.StatusOK, w.Code)372 assert.Equal(t, "{\"error\":\"this is an error\"}", w.Body.String())372 assert.Equal(t, "{\"error\":\"this is an error\"}\n", w.Body.String())373374 w = performRequest(router, "GET", "/abort")375 assert.Equal(t, http.StatusUnauthorized, w.Code)376 assert.Equal(t, "{\"error\":\"no authorized\"}", w.Body.String())376 assert.Equal(t, "{\"error\":\"no authorized\"}\n", w.Body.String())377378 w = performRequest(router, "GET", "/print")379 assert.Equal(t, http.StatusInternalServerError, w.Code)380 assert.Equal(t, "hola!{\"error\":\"this is an error\"}", w.Body.String())380 assert.Equal(t, "hola!{\"error\":\"this is an error\"}\n", w.Body.String())381}382383func TestLoggerWithWriterSkippingPaths(t *testing.T) {384 buffer := new(bytes.Buffer)
50 unmodified lines
modified:middleware_test.go+1 added-1 removed
244 unmodified lines
245246 w := performRequest(router, "GET", "/")247248 assert.Equal(t, http.StatusBadRequest, w.Code)249 assert.Equal(t, strings.Replace("hola\n<map><foo>bar</foo></map>{\"foo\":\"bar\"}{\"foo\":\"bar\"}event:test\ndata:message\n\n", " ", "", -1), strings.Replace(w.Body.String(), " ", "", -1))249 assert.Equal(t, strings.Replace("hola\n<map><foo>bar</foo></map>{\"foo\":\"bar\"}\n{\"foo\":\"bar\"}\nevent:test\ndata:message\n\n", " ", "", -1), strings.Replace(w.Body.String(), " ", "", -1))250}