-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.go
More file actions
46 lines (35 loc) · 820 Bytes
/
Copy pathjson.go
File metadata and controls
46 lines (35 loc) · 820 Bytes
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 encoding
import (
"bytes"
"encoding/json"
)
type JsonCodec struct {
codec
}
func (c *JsonCodec) Marshal(value interface{}) ([]byte, error) {
if primitive, err := c.marshalPrimitive(value); err != errNotPrimitive {
return primitive, err
}
var b bytes.Buffer
encoder := json.NewEncoder(&b)
if err := encoder.Encode(value); err != nil {
return nil, err
}
return c.compress(b.Bytes())
}
func (c *JsonCodec) Unmarshal(byt []byte, ptr interface{}) (err error) {
if err := c.unmarshalPrimitive(byt, ptr); err != errNotPrimitive {
return err
}
if byt, err = c.decompress(byt); err != nil {
return
}
b := bytes.NewBuffer(byt)
decoder := json.NewDecoder(b)
return decoder.Decode(ptr)
}
func NewJsonCodec(compressor Compressor) *JsonCodec {
return &JsonCodec{
codec{compressor},
}
}