MCPcopy
hub / github.com/gin-gonic/gin / Render

Method Render

render/json.go:155–176  ·  view source on GitHub ↗

Render (AsciiJSON) marshals the given interface object and writes it with custom ContentType.

(w http.ResponseWriter)

Source from the content-addressed store, hash-verified

153
154// Render (AsciiJSON) marshals the given interface object and writes it with custom ContentType.
155func (r AsciiJSON) Render(w http.ResponseWriter) error {
156 r.WriteContentType(w)
157 ret, err := json.API.Marshal(r.Data)
158 if err != nil {
159 return err
160 }
161
162 var buffer bytes.Buffer
163 escapeBuf := make([]byte, 0, 6) // Preallocate 6 bytes for Unicode escape sequences
164
165 for _, r := range bytesconv.BytesToString(ret) {
166 if r > unicode.MaxASCII {
167 escapeBuf = fmt.Appendf(escapeBuf[:0], "\\u%04x", r) // Reuse escapeBuf
168 buffer.Write(escapeBuf)
169 } else {
170 buffer.WriteByte(byte(r))
171 }
172 }
173
174 _, err = w.Write(buffer.Bytes())
175 return err
176}
177
178// WriteContentType (AsciiJSON) writes JSON ContentType.
179func (r AsciiJSON) WriteContentType(w http.ResponseWriter) {

Callers

nothing calls this directly

Calls 4

WriteContentTypeMethod · 0.95
BytesToStringFunction · 0.92
MarshalMethod · 0.65
WriteMethod · 0.45

Tested by

no test coverage detected