-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodec.go
81 lines (61 loc) · 1.46 KB
/
codec.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package json
import (
"encoding/json"
"io"
"github.com/mirango/framework"
)
type JSON struct{}
func New() *JSON {
return &JSON{}
}
func (JSON) Encode(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
func (JSON) EncodeTo(w io.Writer, v interface{}) error {
enc := json.NewEncoder(w)
return enc.Encode(v)
}
func (JSON) EncodingType() string {
return framework.MIME_JSON
}
func (JSON) Decode(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
func (JSON) DecodeFrom(r io.Reader, v interface{}) error {
dec := json.NewDecoder(r)
return dec.Decode(v)
}
func (JSON) DecodingType() string {
return framework.MIME_JSON
}
func (JSON) MIMEType() string {
return framework.MIME_JSON
}
type JSONIndented struct{}
func NewIndented() *JSONIndented {
return &JSONIndented{}
}
func (JSONIndented) Encode(v interface{}) ([]byte, error) {
return json.MarshalIndent(v, "", " ")
}
func (JSONIndented) EncodeTo(w io.Writer, v interface{}) error {
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
return enc.Encode(v)
}
func (JSONIndented) EncodingType() string {
return framework.MIME_JSON
}
func (JSONIndented) Decode(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
func (JSONIndented) DecodeFrom(r io.Reader, v interface{}) error {
dec := json.NewDecoder(r)
return dec.Decode(v)
}
func (JSONIndented) DecodingType() string {
return framework.MIME_JSON
}
func (JSONIndented) MIMEType() string {
return framework.MIME_JSON
}