-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
serializer.go
52 lines (40 loc) · 987 Bytes
/
serializer.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 serializer
import (
"encoding/json"
pb "github.com/antonputra/tutorials/lessons/149/app/event"
"google.golang.org/protobuf/proto"
)
// SerializeJSON serializes Event struct to bytes
func SerializeJSON(ev *pb.Event) ([]byte, error) {
b, err := json.Marshal(ev)
if err != nil {
return nil, err
}
return b, nil
}
// SerializeProtoBuf serializes Event struct to bytes
func SerializeProtoBuf(ev *pb.Event) ([]byte, error) {
b, err := proto.Marshal(ev)
if err != nil {
return nil, err
}
return b, nil
}
// DeserializeJSON deserializes encoded string to struct
func DeserializeJSON(b []byte) (*pb.Event, error) {
var ev pb.Event
err := json.Unmarshal(b, &ev)
if err != nil {
return &pb.Event{}, err
}
return &ev, nil
}
// DeserializeProtoBuf deserializes encoded protobuf to struct
func DeserializeProtoBuf(b []byte) (*pb.Event, error) {
var ev pb.Event
err := proto.Unmarshal(b, &ev)
if err != nil {
return &pb.Event{}, err
}
return &ev, nil
}