JSON marshals 'v' to JSON, automatically escaping HTML and setting the Content-Type as application/json.
(w http.ResponseWriter, r *http.Request, v interface{})
| 91 | // JSON marshals 'v' to JSON, automatically escaping HTML and setting the |
| 92 | // Content-Type as application/json. |
| 93 | func JSON(w http.ResponseWriter, r *http.Request, v interface{}) { |
| 94 | buf := &bytes.Buffer{} |
| 95 | enc := json.NewEncoder(buf) |
| 96 | enc.SetEscapeHTML(true) |
| 97 | if err := enc.Encode(v); err != nil { |
| 98 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 99 | return |
| 100 | } |
| 101 | |
| 102 | w.Header().Set("Content-Type", "application/json") |
| 103 | if status, ok := r.Context().Value(StatusCtxKey).(int); ok { |
| 104 | w.WriteHeader(status) |
| 105 | } |
| 106 | w.Write(buf.Bytes()) //nolint:errcheck |
| 107 | } |
| 108 | |
| 109 | // XML marshals 'v' to XML, setting the Content-Type as application/xml. It |
| 110 | // will automatically prepend a generic XML header (see encoding/xml.Header) if |
no outgoing calls
no test coverage detected
searching dependent graphs…