-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserializers.go
35 lines (29 loc) · 927 Bytes
/
serializers.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
package miller
import (
"bytes"
"encoding/json"
)
// Serializer represents the ability to encode and decode data.
type Serializer interface {
// Encode serializes v into a byte slice.
Encode(v interface{}) ([]byte, error)
// Decode deserializes b and stores the result in the value pointed to by v.
Decode(b []byte, v interface{}) error
}
// DefaultSerializer is the default token serializer.
// This serializer leverages encoding/json for fast serialization.
type DefaultSerializer struct{}
// Encode implements the Serializer interface.
func (s DefaultSerializer) Encode(v interface{}) ([]byte, error) {
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(v)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// Decode implements the Serializer interface.
func (s DefaultSerializer) Decode(b []byte, v interface{}) error {
buf := bytes.NewBuffer(b)
return json.NewDecoder(buf).Decode(v)
}