-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
marshaller.go
48 lines (38 loc) · 996 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
47
48
package irt
import (
"encoding/json"
"fmt"
)
type Marshaller interface {
Marshal(data interface{}) ([]byte, error)
Unmarshal(data []byte, model interface{}) error
}
type JSONMarshaller struct {
pretty bool
}
func NewJSONMarshaller(pretty bool) *JSONMarshaller {
return &JSONMarshaller{
pretty: pretty,
}
}
func (m *JSONMarshaller) Marshal(data interface{}) ([]byte, error) {
if m.pretty {
return json.MarshalIndent(data, "", " ")
} else {
return json.Marshal(data)
}
}
func (m *JSONMarshaller) Unmarshal(data []byte, model interface{}) error {
return json.Unmarshal(data, model)
}
type BinaryMarshaller struct {
}
func NewBinaryMarshaller() *BinaryMarshaller {
return &BinaryMarshaller{}
}
func (m *BinaryMarshaller) Marshal(data interface{}) ([]byte, error) {
return nil, fmt.Errorf("binary marshaller is not implemented")
}
func (m *BinaryMarshaller) Unmarshal(data []byte, model interface{}) error {
return fmt.Errorf("binary marshaller is not implemented")
}