fix(render): Stream JSON instead of buffering the message

Currently, not only the object but also the serialized representation is
kept in memory in full. For small objects that is usually fine, but when
exporting large data structures this makes the memory explode. This gets
even worse when exporting it indented.

The byte-output has changed since the Encoder.Encode functions add an
extra new-line, the marshaled output outside of that remains the same.
This commit is contained in:
Moritz Poldrack 2026-10-21 21:33:58 +02:00
parent 0f09c3a9b4
commit 71807f3414
No known key found for this signature in database
5 changed files with 18 additions and 9 deletions

View File

@ -37,6 +37,13 @@ type Encoder interface {
Encode(v any) error
}
// EncoderIndent is an interface for json marshallers that support setting an
// indentation character.
type EncoderIndent interface {
Encoder
SetIndent(prefix, indent string)
}
// Decoder an interface reads and decodes JSON values from an input stream.
type Decoder interface {
// UseNumber causes the Decoder to unmarshal a number into an any as a

View File

@ -1064,7 +1064,7 @@ func TestCustomUnmarshalStruct(t *testing.T) {
w := httptest.NewRecorder()
route.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
assert.Equal(t, `"2000/01/01"`, w.Body.String())
assert.Equal(t, "\"2000/01/01\"\n", w.Body.String())
}
// Test the fix for https://github.com/gin-gonic/gin/issues/4002

View File

@ -397,7 +397,7 @@ func TestErrorLogger(t *testing.T) {
w = PerformRequest(router, http.MethodGet, "/print")
assert.Equal(t, http.StatusInternalServerError, w.Code)
assert.Equal(t, "hola!{\"error\":\"this is an error\"}", w.Body.String())
assert.Equal(t, "hola!{\"error\":\"this is an error\"}\n", w.Body.String())
}
func TestLoggerWithWriterSkippingPaths(t *testing.T) {

View File

@ -249,5 +249,5 @@ func TestMiddlewareWrite(t *testing.T) {
w := PerformRequest(router, http.MethodGet, "/")
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Equal(t, strings.ReplaceAll("hola\n<map><foo>bar</foo></map>{\"foo\":\"bar\"}{\"foo\":\"bar\"}event:test\ndata:message\n\n", " ", ""), strings.ReplaceAll(w.Body.String(), " ", ""))
assert.Equal(t, strings.ReplaceAll("hola\n<map><foo>bar</foo></map>{\"foo\":\"bar\"}\n{\"foo\":\"bar\"}\nevent:test\ndata:message\n\n", " ", ""), strings.ReplaceAll(w.Body.String(), " ", ""))
}

View File

@ -66,17 +66,19 @@ func (r JSON) WriteContentType(w http.ResponseWriter) {
// WriteJSON marshals the given interface object and writes it with custom ContentType.
func WriteJSON(w http.ResponseWriter, obj any) error {
writeContentType(w, jsonContentType)
jsonBytes, err := json.API.Marshal(obj)
if err != nil {
return err
}
_, err = w.Write(jsonBytes)
return err
return json.API.NewEncoder(w).Encode(obj)
}
// Render (IndentedJSON) marshals the given interface object and writes it with custom ContentType.
func (r IndentedJSON) Render(w http.ResponseWriter) error {
r.WriteContentType(w)
enc := json.API.NewEncoder(w)
if indentEnc, ok := enc.(json.EncoderIndent); ok {
indentEnc.SetIndent("", " ")
return indentEnc.Encode(r.Data)
}
jsonBytes, err := json.API.MarshalIndent(r.Data, "", " ")
if err != nil {
return err