-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathencode.go
76 lines (64 loc) · 2.3 KB
/
encode.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2015 Alex Browne. All rights reserved.
// Use of this source code is governed by the MIT
// license, which can be found in the LICENSE file.
package locstor
import (
"bytes"
"encoding/gob"
"encoding/json"
)
var (
// BinaryEncoding is a ready-to-use implementation of EncoderDecoder which
// encodes data structures in a binary format using the gob package.
BinaryEncoding = &binaryEncoderDecoder{}
// JSONEncoding is a ready-to-use implementation of EncoderDecoder which
// encodes data structures as json.
JSONEncoding = &jsonEncoderDecoder{}
)
// Encoder is an interface implemented by objects which can encode an arbitrary
// go object into a slice of bytes.
type Encoder interface {
Encode(interface{}) ([]byte, error)
}
// Decoder is an interface implemented by objects which can decode a slice
// of bytes into an arbitrary go object.
type Decoder interface {
Decode([]byte, interface{}) error
}
// EncoderDecoder is an interface implemented by objects which can both encode
// an arbitrary go object into a slice of bytes and decode that slice of bytes
// into an arbitrary go object. EncoderDecoders should have the property that
// Encode(Decode(x)) == x for all objects x which are encodable.
type EncoderDecoder interface {
Encoder
Decoder
}
// jsonEncoderDecoder is an implementation of EncoderDecoder which uses json
// encoding.
type jsonEncoderDecoder struct{}
// Encode implements the Encode method of Encoder
func (jsonEncoderDecoder) Encode(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
// Decode implements the Decode method of Decoder
func (jsonEncoderDecoder) Decode(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
// binaryEncoderDecoder is an implementation of EncoderDecoder which uses binary
// encoding via the gob package in the standard library.
type binaryEncoderDecoder struct{}
// Encode implements the Encode method of Encoder
func (b binaryEncoderDecoder) Encode(v interface{}) ([]byte, error) {
buf := bytes.NewBuffer([]byte{})
enc := gob.NewEncoder(buf)
if err := enc.Encode(v); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// Decode implements the Decode method of Decoder
func (b binaryEncoderDecoder) Decode(data []byte, v interface{}) error {
buf := bytes.NewBuffer(data)
dec := gob.NewDecoder(buf)
return dec.Decode(v)
}