-
Notifications
You must be signed in to change notification settings - Fork 4
/
codec.go
45 lines (37 loc) · 1010 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
package amqprpc
import (
"bytes"
"encoding/gob"
"encoding/json"
)
//EncodingCodec implements marshaling and unmarshaling of seralized data.
type EncodingCodec interface {
Marshal(interface{}) ([]byte, error)
Unmarshal([]byte, interface{}) error
}
//JSONCodec is an EncodingCodec implementation to send/receieve JSON data
//over AMQP.
type JSONCodec struct{}
func (JSONCodec) Marshal(v interface{}) ([]byte, error) {
b, err := json.Marshal(v)
return b, err
}
func (JSONCodec) Unmarshal(data []byte, v interface{}) error {
err := json.Unmarshal(data, v)
return err
}
//GobCodec is an EncodingCodec implementation to send/recieve Gob data
//over AMQP.
type GobCodec struct{}
func (GobCodec) Marshal(v interface{}) ([]byte, error) {
body := new(bytes.Buffer)
enc := gob.NewEncoder(body)
err := enc.Encode(v)
return body.Bytes(), err
}
func (GobCodec) Unmarshal(data []byte, v interface{}) error {
body := bytes.NewBuffer(data)
dec := gob.NewDecoder(body)
err := dec.Decode(v)
return err
}