github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/render/json.go (about) 1 package render 2 3 import ( 4 "encoding/json" 5 "io" 6 ) 7 8 type jsonRenderer struct { 9 value interface{} 10 } 11 12 func (s jsonRenderer) ContentType() string { 13 return "application/json; charset=utf-8" 14 } 15 16 func (s jsonRenderer) Render(w io.Writer, data Data) error { 17 return json.NewEncoder(w).Encode(s.value) 18 } 19 20 // JSON renders the value using the "application/json" 21 // content type. 22 func JSON(v interface{}) Renderer { 23 return jsonRenderer{value: v} 24 } 25 26 // JSON renders the value using the "application/json" 27 // content type. 28 func (e *Engine) JSON(v interface{}) Renderer { 29 return JSON(v) 30 }