/
json.go
38 lines (30 loc) · 942 Bytes
/
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
package codec
import (
"io"
"reflect"
"github.com/ugorji/go/codec"
)
var defaultJsonHandle = codec.JsonHandle{MapKeyAsString: true}
// DecJSON decode byte json into struct or map.
func DecJSON(s []byte, v interface{}) error {
dec := codec.NewDecoderBytes(s, &defaultJsonHandle)
return dec.Decode(v)
}
// DecJSONReader decode Reader json into struct or map.
func DecJSONReader(s io.Reader, v interface{}) error {
dec := codec.NewDecoder(s, &defaultJsonHandle)
return dec.Decode(v)
}
// EncJSON encode struct or map data into byte json.
func EncJSON(v interface{}, s *[]byte) error {
enc := codec.NewEncoderBytes(s, &defaultJsonHandle)
return enc.Encode(v)
}
// EncJSONWriter encode Writer data into byte json.
func EncJSONWriter(v interface{}, s io.Writer) error {
enc := codec.NewEncoder(s, &defaultJsonHandle)
return enc.Encode(v)
}
func init() {
defaultJsonHandle.MapType = reflect.TypeOf(map[string]interface{}(nil))
}