Skip to content

Commit

Permalink
JSON and XML work correctly on the invalid json or xml data
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Dec 29, 2021
1 parent 1172d77 commit a3b846a
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,18 +772,26 @@ func (c *Context) BlobXML(code int, b []byte) (err error) {
}

// XML sends an XML response with the status code.
func (c *Context) XML(code int, v interface{}) error {
c.setContentTypeAndCode(code, MIMEApplicationXMLCharsetUTF8)
if _, err := c.res.WriteString(xml.Header); err != nil {
return err
}
return xml.NewEncoder(c.res).Encode(v)
func (c *Context) XML(code int, v interface{}) (err error) {
buf := c.AcquireBuffer()
buf.WriteString(xml.Header)
if err = xml.NewEncoder(buf).Encode(v); err == nil {
c.setContentTypeAndCode(code, MIMEApplicationXMLCharsetUTF8)
_, err = c.res.Write(buf.Bytes())
}
c.ReleaseBuffer(buf)
return
}

// JSON sends a JSON response with the status code.
func (c *Context) JSON(code int, v interface{}) error {
c.setContentTypeAndCode(code, MIMEApplicationJSONCharsetUTF8)
return json.NewEncoder(c.res).Encode(v)
func (c *Context) JSON(code int, v interface{}) (err error) {
buf := c.AcquireBuffer()
if err = json.NewEncoder(buf).Encode(v); err == nil {
c.setContentTypeAndCode(code, MIMEApplicationJSONCharsetUTF8)
_, err = c.res.Write(buf.Bytes())
}
c.ReleaseBuffer(buf)
return
}

// HTML sends an HTML response with the status code.
Expand Down

0 comments on commit a3b846a

Please sign in to comment.