Skip to content

Commit

Permalink
refactor(response/json): encode directly to response body writer
Browse files Browse the repository at this point in the history
  • Loading branch information
savsgio committed Jun 7, 2023
1 parent 51e2fe5 commit f81fcc6
Showing 1 changed file with 2 additions and 14 deletions.
16 changes: 2 additions & 14 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,9 @@ func (ctx *RequestCtx) JSONResponse(body interface{}, statusCode ...int) (err er
ctx.Response.Header.SetStatusCode(statusCode[0])
}

var data []byte
w := ctx.Response.BodyWriter()

if jm, ok := body.(json.Marshaler); ok {
data, err = jm.MarshalJSON()
} else {
data, err = json.Marshal(body)
}

if err != nil {
return wrapError(err, "failed to marshal response body")
}

ctx.Response.SetBody(data)

return nil
return json.NewEncoder(w).Encode(body) // nolint:wrapcheck
}

// HTTPResponse return response with body in html format.
Expand Down

0 comments on commit f81fcc6

Please sign in to comment.