-
Notifications
You must be signed in to change notification settings - Fork 10
/
encoder.go
35 lines (28 loc) · 1.07 KB
/
encoder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package respond
import (
"encoding/json"
"net/http"
)
// Encoder descirbes an object capable of encoding
// a response.
type Encoder interface {
// Encode writes a serialization of v to w, optionally using additional
// information from the http.Request to do so.
Encode(w http.ResponseWriter, r *http.Request, v interface{}) error
// ContentType gets a string that will become the Content-Type header
// when responding through w to the specified http.Request.
// Most of the time the argument will be ignored, but occasionally
// details in the request, or even in the headers in the ResponseWriter may
// change the content type.
ContentType(w http.ResponseWriter, r *http.Request) string
}
type jsonEncoder struct{}
var _ Encoder = (*jsonEncoder)(nil)
// JSON is an Encoder for JSON.
var JSON Encoder = (*jsonEncoder)(nil)
func (*jsonEncoder) Encode(w http.ResponseWriter, r *http.Request, v interface{}) error {
return json.NewEncoder(w).Encode(v)
}
func (*jsonEncoder) ContentType(w http.ResponseWriter, r *http.Request) string {
return "application/json; charset=utf-8"
}