-
Notifications
You must be signed in to change notification settings - Fork 104
/
json.go
74 lines (65 loc) · 1.84 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package geojson
import "encoding/json"
// CustomJSONMarshaler can be set to have the code use a different
// json marshaler than the default in the standard library.
// One use case in enabling `github.com/json-iterator/go`
// with something like this:
//
// import (
// jsoniter "github.com/json-iterator/go"
// "github.com/paulmach/orb"
// )
//
// var c = jsoniter.Config{
// EscapeHTML: true,
// SortMapKeys: false,
// MarshalFloatWith6Digits: true,
// }.Froze()
//
// orb.CustomJSONMarshaler = c
// orb.CustomJSONUnmarshaler = c
//
// Note that any errors encountered during marshaling will be different.
var CustomJSONMarshaler interface {
Marshal(v interface{}) ([]byte, error)
} = nil
// CustomJSONUnmarshaler can be set to have the code use a different
// json unmarshaler than the default in the standard library.
// One use case in enabling `github.com/json-iterator/go`
// with something like this:
//
// import (
// jsoniter "github.com/json-iterator/go"
// "github.com/paulmach/orb"
// )
//
// var c = jsoniter.Config{
// EscapeHTML: true,
// SortMapKeys: false,
// MarshalFloatWith6Digits: true,
// }.Froze()
//
// orb.CustomJSONMarshaler = c
// orb.CustomJSONUnmarshaler = c
//
// Note that any errors encountered during unmarshaling will be different.
var CustomJSONUnmarshaler interface {
Unmarshal(data []byte, v interface{}) error
} = nil
func marshalJSON(v interface{}) ([]byte, error) {
if CustomJSONMarshaler == nil {
return json.Marshal(v)
}
return CustomJSONMarshaler.Marshal(v)
}
func unmarshalJSON(data []byte, v interface{}) error {
if CustomJSONUnmarshaler == nil {
return json.Unmarshal(data, v)
}
return CustomJSONUnmarshaler.Unmarshal(data, v)
}
type nocopyRawMessage []byte
func (m *nocopyRawMessage) UnmarshalJSON(data []byte) error {
*m = data
return nil
}