-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjson_encoder.go
More file actions
41 lines (34 loc) · 812 Bytes
/
json_encoder.go
File metadata and controls
41 lines (34 loc) · 812 Bytes
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
package ehpg
import (
"bytes"
"encoding/json"
eh "github.com/looplab/eventhorizon"
)
type Encoder interface {
Marshal(eh.EventData) ([]byte, error)
Unmarshal(eh.EventType, []byte) (eh.EventData, error)
String() string
}
type jsonEncoder struct{}
func (jsonEncoder) Marshal(data eh.EventData) ([]byte, error) {
if data != nil {
return json.Marshal(data)
}
return nil, nil
}
func (jsonEncoder) Unmarshal(eventType eh.EventType, raw []byte) (data eh.EventData, err error) {
if len(raw) == 0 {
return nil, nil
} else if bytes.Compare(raw, []byte("null")) == 0 {
return nil, nil
}
if data, err = eh.CreateEventData(eventType); err == nil {
if err = json.Unmarshal(raw, data); err == nil {
return data, nil
}
}
return nil, err
}
func (jsonEncoder) String() string {
return "json"
}