-
Notifications
You must be signed in to change notification settings - Fork 25
/
json.go
52 lines (42 loc) · 1.32 KB
/
json.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
package documentdb
import (
"bytes"
"encoding/json"
"io"
)
// JSONEncoder describes json encoder
type JSONEncoder interface {
Encode(val interface{}) error
}
// JSONDecoder describes json decoder
type JSONDecoder interface {
Decode(obj interface{}) error
}
// Marshal function type
type Marshal func(v interface{}) ([]byte, error)
// Unmarshal function type
type Unmarshal func(data []byte, v interface{}) error
// EncoderFactory describes function that creates json encoder
type EncoderFactory func(*bytes.Buffer) JSONEncoder
// DecoderFactory describes function that creates json decoder
type DecoderFactory func(io.Reader) JSONDecoder
// SerializationDriver struct holds serialization / deserilization providers
type SerializationDriver struct {
EncoderFactory EncoderFactory
DecoderFactory DecoderFactory
Marshal Marshal
Unmarshal Unmarshal
}
// DefaultSerialization holds default stdlib json driver
var DefaultSerialization = SerializationDriver{
EncoderFactory: func(b *bytes.Buffer) JSONEncoder {
return json.NewEncoder(b)
},
DecoderFactory: func(r io.Reader) JSONDecoder {
return json.NewDecoder(r)
},
Marshal: json.Marshal,
Unmarshal: json.Unmarshal,
}
// Serialization holds driver that is actually used
var Serialization = DefaultSerialization