forked from gocraft/meta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.go
36 lines (28 loc) · 873 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
package meta
import (
"bytes"
"encoding/json"
)
var MetaJson JsonLoader
func init() {
MetaJson = &defaultJsonLoader{}
}
// Expose json loader interface that can be implemneted in client code for customization
type JsonLoader interface {
Marshal(v interface{}) ([]byte, error)
Unmarshal(data []byte, v interface{}) error
UnmarshalUsingNumber(data []byte, v interface{}) error
}
// Default implementation of Json loader just uses Go's standard json lib
type defaultJsonLoader struct{}
func (j *defaultJsonLoader) Marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
func (j *defaultJsonLoader) Unmarshal(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
func (j *defaultJsonLoader) UnmarshalUsingNumber(data []byte, v interface{}) error {
dec := json.NewDecoder(bytes.NewReader(data))
dec.UseNumber()
return dec.Decode(v)
}