MCPcopy Create free account
hub / github.com/go-chi/render / JSON

Function JSON

responder.go:93–107  ·  view source on GitHub ↗

JSON marshals 'v' to JSON, automatically escaping HTML and setting the Content-Type as application/json.

(w http.ResponseWriter, r *http.Request, v interface{})

Source from the content-addressed store, hash-verified

91// JSON marshals 'v' to JSON, automatically escaping HTML and setting the
92// Content-Type as application/json.
93func 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

Callers 1

DefaultResponderFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…