-
Notifications
You must be signed in to change notification settings - Fork 178
/
codec.go
52 lines (38 loc) · 907 Bytes
/
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
package json
import (
"encoding/json"
"io"
"github.com/onflow/flow-go/model/encoding"
)
var _ encoding.Marshaler = (*Marshaler)(nil)
type Marshaler struct{}
func NewMarshaler() *Marshaler {
return &Marshaler{}
}
func (m *Marshaler) Marshal(val interface{}) ([]byte, error) {
return json.Marshal(val)
}
func (m *Marshaler) Unmarshal(b []byte, val interface{}) error {
return json.Unmarshal(b, val)
}
func (m *Marshaler) MustMarshal(val interface{}) []byte {
b, err := m.Marshal(val)
if err != nil {
panic(err)
}
return b
}
func (m *Marshaler) MustUnmarshal(b []byte, val interface{}) {
err := m.Unmarshal(b, val)
if err != nil {
panic(err)
}
}
var _ encoding.Codec = (*Codec)(nil)
type Codec struct{}
func (c *Codec) NewEncoder(w io.Writer) encoding.Encoder {
return json.NewEncoder(w)
}
func (c *Codec) NewDecoder(r io.Reader) encoding.Decoder {
return json.NewDecoder(r)
}