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

Method Render

render/json.go:117–147  ·  view source on GitHub ↗

Render (JsonpJSON) marshals the given interface object and writes it and its callback with custom ContentType.

(w http.ResponseWriter)

Source from the content-addressed store, hash-verified

115
116// Render (JsonpJSON) marshals the given interface object and writes it and its callback with custom ContentType.
117func (r JsonpJSON) Render(w http.ResponseWriter) (err error) {
118 r.WriteContentType(w)
119 ret, err := json.API.Marshal(r.Data)
120 if err != nil {
121 return err
122 }
123
124 if r.Callback == "" {
125 _, err = w.Write(ret)
126 return err
127 }
128
129 callback := template.JSEscapeString(r.Callback)
130 if _, err = w.Write(bytesconv.StringToBytes(callback)); err != nil {
131 return err
132 }
133
134 if _, err = w.Write(bytesconv.StringToBytes("(")); err != nil {
135 return err
136 }
137
138 if _, err = w.Write(ret); err != nil {
139 return err
140 }
141
142 if _, err = w.Write(bytesconv.StringToBytes(");")); err != nil {
143 return err
144 }
145
146 return nil
147}
148
149// WriteContentType (JsonpJSON) writes Javascript ContentType.
150func (r JsonpJSON) WriteContentType(w http.ResponseWriter) {

Callers 1

TestRenderJsonpJSONErrorFunction · 0.95

Calls 4

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

Tested by 1

TestRenderJsonpJSONErrorFunction · 0.76