forked from paulmach/osm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
json.go
149 lines (122 loc) · 3.72 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package osm
import (
"encoding/json"
"encoding/xml"
)
// 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/onXmaps/osm"
// )
//
// var c = jsoniter.Config{
// EscapeHTML: true,
// SortMapKeys: false,
// MarshalFloatWith6Digits: true,
// }.Froze()
//
// osm.CustomJSONMarshaler = c
// osm.CustomJSONUnmarshaler = c
//
// Note that any errors encountered during marshaling will be different.
var CustomJSONMarshaler interface {
Marshal(v interface{}) ([]byte, error)
}
// 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/onXmaps/osm"
// )
//
// var c = jsoniter.Config{
// EscapeHTML: true,
// SortMapKeys: false,
// MarshalFloatWith6Digits: true,
// }.Froze()
//
// osm.CustomJSONMarshaler = c
// osm.CustomJSONUnmarshaler = c
//
// Note that any errors encountered during unmarshaling will be different.
var CustomJSONUnmarshaler interface {
Unmarshal(data []byte, v interface{}) error
}
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
}
// xmlNameJSONTypeNode is kind of a hack to encode the proper json
// object type attribute for this struct type.
type xmlNameJSONTypeNode xml.Name
func (x xmlNameJSONTypeNode) MarshalJSON() ([]byte, error) {
return []byte(`"node"`), nil
}
func (x xmlNameJSONTypeNode) UnmarshalJSON(data []byte) error {
return nil
}
// xmlNameJSONTypeWay is kind of a hack to encode the proper json
// object type attribute for this struct type.
type xmlNameJSONTypeWay xml.Name
func (x xmlNameJSONTypeWay) MarshalJSON() ([]byte, error) {
return []byte(`"way"`), nil
}
func (x xmlNameJSONTypeWay) UnmarshalJSON(data []byte) error {
return nil
}
// xmlNameJSONTypeRel is kind of a hack to encode the proper json
// object type attribute for this struct type.
type xmlNameJSONTypeRel xml.Name
func (x xmlNameJSONTypeRel) MarshalJSON() ([]byte, error) {
return []byte(`"relation"`), nil
}
func (x xmlNameJSONTypeRel) UnmarshalJSON(data []byte) error {
return nil
}
// xmlNameJSONTypeCS is kind of a hack to encode the proper json
// object type attribute for this struct type.
type xmlNameJSONTypeCS xml.Name
func (x xmlNameJSONTypeCS) MarshalJSON() ([]byte, error) {
return []byte(`"changeset"`), nil
}
func (x xmlNameJSONTypeCS) UnmarshalJSON(data []byte) error {
return nil
}
// xmlNameJSONTypeUser is kind of a hack to encode the proper json
// object type attribute for this struct type.
type xmlNameJSONTypeUser xml.Name
func (x xmlNameJSONTypeUser) MarshalJSON() ([]byte, error) {
return []byte(`"user"`), nil
}
func (x xmlNameJSONTypeUser) UnmarshalJSON(data []byte) error {
return nil
}
// xmlNameJSONTypeNote is kind of a hack to encode the proper json
// object type attribute for this struct type.
type xmlNameJSONTypeNote xml.Name
func (x xmlNameJSONTypeNote) MarshalJSON() ([]byte, error) {
return []byte(`"note"`), nil
}
func (x xmlNameJSONTypeNote) UnmarshalJSON(data []byte) error {
return nil
}