-
Notifications
You must be signed in to change notification settings - Fork 57
/
cborserializer.go
60 lines (49 loc) · 1.61 KB
/
cborserializer.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
53
54
55
56
57
58
59
60
package serialize
import (
"errors"
"reflect"
"github.com/ugorji/go/codec"
"github.com/gammazero/nexus/v3/wamp"
)
var ch *codec.CborHandle
func init() {
ch = &codec.CborHandle{}
ch.MapType = reflect.TypeOf(map[string]interface{}(nil))
}
// CBORSerializer is an implementation of Serializer that handles
// serializing and deserializing cbor encoded payloads.
type CBORSerializer struct{}
// Serialize encodes a Message into a cbor payload.
func (s *CBORSerializer) Serialize(msg wamp.Message) ([]byte, error) {
var b []byte
err := codec.NewEncoderBytes(&b, ch).Encode(msgToList(msg))
return b, err
}
// Deserialize decodes a cbor payload into a Message.
func (s *CBORSerializer) Deserialize(data []byte) (wamp.Message, error) {
var v []interface{}
err := codec.NewDecoderBytes(data, ch).Decode(&v)
if err != nil {
return nil, err
}
if len(v) == 0 {
return nil, errors.New("invalid message")
}
// cbor deserializer gives us an uint64 instead of an int64, whyever it
// doesn't matter here, because valid values are only within an 8bit range.
typ, ok := v[0].(uint64)
if !ok {
return nil, errors.New("unsupported message format")
}
return listToMsg(wamp.MessageType(typ), v)
}
// SerializeDataItem encodes any object/structure into a cbor payload.
func (s *CBORSerializer) SerializeDataItem(item interface{}) ([]byte, error) {
var b []byte
err := codec.NewEncoderBytes(&b, ch).Encode(item)
return b, err
}
// DeserializeDataItem decodes a json payload into an object/structure.
func (s *CBORSerializer) DeserializeDataItem(data []byte, v interface{}) error {
return codec.NewDecoderBytes(data, ch).Decode(&v)
}