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
Provenance
- Commit
0cbf290- Tree
f22e913- Author
itcloudy <272685110@qq.com>- Committer
田欧 <thinkerou@gmail.com>- Parent
b1d607a
5 files changed+12 added−15 removed
modified:render/json.go+2 added-5 removed
66 unmodified lines67 68 // WriteJSON marshals the given interface object and writes it with custom ContentType.69 func 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 }75 76 // Render (IndentedJSON) marshals the given interface object and writes it with custom ContentType.115 unmodified linesmodified:render/render_test.go+1 added-1 removed
60 unmodified lines61 62 err := (JSON{data}).Render(w)63 64 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 }68 69 func TestRenderJSONPanics(t *testing.T) {431 unmodified linesmodified:context_test.go+5 added-5 removed
658 unmodified lines659 660 c.JSON(http.StatusCreated, H{"foo": "bar", "html": "<b>"})661 662 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 }666 667 // Tests that the response is serialized as JSONP19 unmodified lines687 688 c.JSONP(http.StatusCreated, H{"foo": "bar"})689 690 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 }694 695 // Tests that no JSON is rendered if code is 20417 unmodified lines713 c.Header("Content-Type", "application/vnd.api+json")714 c.JSON(http.StatusCreated, H{"foo": "bar"})715 716 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 }720 721 // Tests that no Custom JSON is rendered if code is 204394 unmodified lines1116 Data: H{"foo": "bar"},1117 })1118 1119 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 }1123 1124 func TestContextNegotiationWithXML(t *testing.T) {155 unmodified lines1280 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 }1286 1287 func TestContextError(t *testing.T) {1288 c, _ := CreateTestContext(httptest.NewRecorder())535 unmodified linesmodified:logger_test.go+3 added-3 removed
367 unmodified lines368 })369 370 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())373 374 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())377 378 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 }382 383 func TestLoggerWithWriterSkippingPaths(t *testing.T) {384 buffer := new(bytes.Buffer)50 unmodified linesmodified:middleware_test.go+1 added-1 removed
244 unmodified lines245 246 w := performRequest(router, "GET", "/")247 248 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 }