-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencoding.go
46 lines (39 loc) · 1011 Bytes
/
encoding.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
package httpc
import (
"bytes"
"encoding/gob"
"encoding/json"
"io"
)
type (
// EncodeFn is an encoder func.
EncodeFn func(interface{}) (io.Reader, error)
// DecodeFn is a decoder func.
DecodeFn func(r io.Reader) error
)
// JSONEncode sets the client's encodeFn to a json encoder.
func JSONEncode() EncodeFn {
return func(v interface{}) (io.Reader, error) {
var buf bytes.Buffer
return &buf, json.NewEncoder(&buf).Encode(v)
}
}
// JSONDecode sets the client's decodeFn to a json decoder.
func JSONDecode(v interface{}) DecodeFn {
return func(r io.Reader) error {
return json.NewDecoder(r).Decode(v)
}
}
// GobEncode sets the client's encodeFn to a gob encoder.
func GobEncode() EncodeFn {
return func(v interface{}) (io.Reader, error) {
var buf bytes.Buffer
return &buf, gob.NewEncoder(&buf).Encode(v)
}
}
// GobDecode sets the client's decodeFn to a gob decoder.
func GobDecode(v interface{}) DecodeFn {
return func(r io.Reader) error {
return gob.NewDecoder(r).Decode(v)
}
}