forked from cinience/render
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshaller.go
46 lines (35 loc) · 928 Bytes
/
marshaller.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
36
37
38
39
40
41
42
43
44
45
46
package render
import (
"encoding/json"
"io"
)
var jsonMarshaller Marshaller = jsonDefaultMarshaller{}
type Marshaller interface {
Marshal(v interface{}) ([]byte, error)
Unmarshall(data []byte, v interface{}) error
NewEncoder(w io.Writer) Encoder
Decode(r io.Reader, v interface{}) error
}
type Encoder interface {
SetEscapeHTML(on bool)
Encode(v interface{}) error
}
type jsonDefaultMarshaller struct{}
func (j jsonDefaultMarshaller) NewEncoder(w io.Writer) Encoder {
return json.NewEncoder(w)
}
func (j jsonDefaultMarshaller) Decode(r io.Reader, v interface{}) error {
return json.NewDecoder(r).Decode(v)
}
func (j jsonDefaultMarshaller) Marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
func (j jsonDefaultMarshaller) Unmarshall(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
func SetJsonMarshaller(m Marshaller) {
if m == nil {
return
}
jsonMarshaller = m
}