1
1
package geojson
2
2
3
3
import (
4
- "encoding/json"
5
4
"errors"
5
+
6
6
"github.com/paulmach/orb"
7
7
)
8
8
@@ -85,14 +85,15 @@ func (g Geometry) MarshalJSON() ([]byte, error) {
85
85
ng .Geometries = g .Geometries
86
86
ng .Type = orb.Collection {}.GeoJSONType ()
87
87
}
88
- return json .Marshal (ng )
88
+
89
+ return marshalJSON (ng )
89
90
}
90
91
91
92
// UnmarshalGeometry decodes the data into a GeoJSON feature.
92
93
// Alternately one can call json.Unmarshal(g) directly for the same result.
93
94
func UnmarshalGeometry (data []byte ) (* Geometry , error ) {
94
95
g := & Geometry {}
95
- err := json . Unmarshal (data , g )
96
+ err := unmarshalJSON (data , g )
96
97
if err != nil {
97
98
return nil , err
98
99
}
@@ -103,35 +104,35 @@ func UnmarshalGeometry(data []byte) (*Geometry, error) {
103
104
// UnmarshalJSON will unmarshal the correct geometry from the json structure.
104
105
func (g * Geometry ) UnmarshalJSON (data []byte ) error {
105
106
jg := & jsonGeometry {}
106
- err := json . Unmarshal (data , jg )
107
+ err := unmarshalJSON (data , jg )
107
108
if err != nil {
108
109
return err
109
110
}
110
111
111
112
switch jg .Type {
112
113
case "Point" :
113
114
p := orb.Point {}
114
- err = json . Unmarshal (jg .Coordinates , & p )
115
+ err = unmarshalJSON (jg .Coordinates , & p )
115
116
g .Coordinates = p
116
117
case "MultiPoint" :
117
118
mp := orb.MultiPoint {}
118
- err = json . Unmarshal (jg .Coordinates , & mp )
119
+ err = unmarshalJSON (jg .Coordinates , & mp )
119
120
g .Coordinates = mp
120
121
case "LineString" :
121
122
ls := orb.LineString {}
122
- err = json . Unmarshal (jg .Coordinates , & ls )
123
+ err = unmarshalJSON (jg .Coordinates , & ls )
123
124
g .Coordinates = ls
124
125
case "MultiLineString" :
125
126
mls := orb.MultiLineString {}
126
- err = json . Unmarshal (jg .Coordinates , & mls )
127
+ err = unmarshalJSON (jg .Coordinates , & mls )
127
128
g .Coordinates = mls
128
129
case "Polygon" :
129
130
p := orb.Polygon {}
130
- err = json . Unmarshal (jg .Coordinates , & p )
131
+ err = unmarshalJSON (jg .Coordinates , & p )
131
132
g .Coordinates = p
132
133
case "MultiPolygon" :
133
134
mp := orb.MultiPolygon {}
134
- err = json . Unmarshal (jg .Coordinates , & mp )
135
+ err = unmarshalJSON (jg .Coordinates , & mp )
135
136
g .Coordinates = mp
136
137
case "GeometryCollection" :
137
138
g .Geometries = jg .Geometries
@@ -154,13 +155,13 @@ func (p Point) Geometry() orb.Geometry {
154
155
155
156
// MarshalJSON will convert the Point into a GeoJSON Point geometry.
156
157
func (p Point ) MarshalJSON () ([]byte , error ) {
157
- return json . Marshal (Geometry {Coordinates : orb .Point (p )})
158
+ return marshalJSON (Geometry {Coordinates : orb .Point (p )})
158
159
}
159
160
160
161
// UnmarshalJSON will unmarshal the GeoJSON Point geometry.
161
162
func (p * Point ) UnmarshalJSON (data []byte ) error {
162
163
g := & Geometry {}
163
- err := json . Unmarshal (data , & g )
164
+ err := unmarshalJSON (data , & g )
164
165
if err != nil {
165
166
return err
166
167
}
@@ -184,13 +185,13 @@ func (mp MultiPoint) Geometry() orb.Geometry {
184
185
185
186
// MarshalJSON will convert the MultiPoint into a GeoJSON MultiPoint geometry.
186
187
func (mp MultiPoint ) MarshalJSON () ([]byte , error ) {
187
- return json . Marshal (Geometry {Coordinates : orb .MultiPoint (mp )})
188
+ return marshalJSON (Geometry {Coordinates : orb .MultiPoint (mp )})
188
189
}
189
190
190
191
// UnmarshalJSON will unmarshal the GeoJSON MultiPoint geometry.
191
192
func (mp * MultiPoint ) UnmarshalJSON (data []byte ) error {
192
193
g := & Geometry {}
193
- err := json . Unmarshal (data , & g )
194
+ err := unmarshalJSON (data , & g )
194
195
if err != nil {
195
196
return err
196
197
}
@@ -214,13 +215,13 @@ func (ls LineString) Geometry() orb.Geometry {
214
215
215
216
// MarshalJSON will convert the LineString into a GeoJSON LineString geometry.
216
217
func (ls LineString ) MarshalJSON () ([]byte , error ) {
217
- return json . Marshal (Geometry {Coordinates : orb .LineString (ls )})
218
+ return marshalJSON (Geometry {Coordinates : orb .LineString (ls )})
218
219
}
219
220
220
221
// UnmarshalJSON will unmarshal the GeoJSON MultiPoint geometry.
221
222
func (ls * LineString ) UnmarshalJSON (data []byte ) error {
222
223
g := & Geometry {}
223
- err := json . Unmarshal (data , & g )
224
+ err := unmarshalJSON (data , & g )
224
225
if err != nil {
225
226
return err
226
227
}
@@ -244,13 +245,13 @@ func (mls MultiLineString) Geometry() orb.Geometry {
244
245
245
246
// MarshalJSON will convert the MultiLineString into a GeoJSON MultiLineString geometry.
246
247
func (mls MultiLineString ) MarshalJSON () ([]byte , error ) {
247
- return json . Marshal (Geometry {Coordinates : orb .MultiLineString (mls )})
248
+ return marshalJSON (Geometry {Coordinates : orb .MultiLineString (mls )})
248
249
}
249
250
250
251
// UnmarshalJSON will unmarshal the GeoJSON MultiPoint geometry.
251
252
func (mls * MultiLineString ) UnmarshalJSON (data []byte ) error {
252
253
g := & Geometry {}
253
- err := json . Unmarshal (data , & g )
254
+ err := unmarshalJSON (data , & g )
254
255
if err != nil {
255
256
return err
256
257
}
@@ -274,13 +275,13 @@ func (p Polygon) Geometry() orb.Geometry {
274
275
275
276
// MarshalJSON will convert the Polygon into a GeoJSON Polygon geometry.
276
277
func (p Polygon ) MarshalJSON () ([]byte , error ) {
277
- return json . Marshal (Geometry {Coordinates : orb .Polygon (p )})
278
+ return marshalJSON (Geometry {Coordinates : orb .Polygon (p )})
278
279
}
279
280
280
281
// UnmarshalJSON will unmarshal the GeoJSON Polygon geometry.
281
282
func (p * Polygon ) UnmarshalJSON (data []byte ) error {
282
283
g := & Geometry {}
283
- err := json . Unmarshal (data , & g )
284
+ err := unmarshalJSON (data , & g )
284
285
if err != nil {
285
286
return err
286
287
}
@@ -304,13 +305,13 @@ func (mp MultiPolygon) Geometry() orb.Geometry {
304
305
305
306
// MarshalJSON will convert the MultiPolygon into a GeoJSON MultiPolygon geometry.
306
307
func (mp MultiPolygon ) MarshalJSON () ([]byte , error ) {
307
- return json . Marshal (Geometry {Coordinates : orb .MultiPolygon (mp )})
308
+ return marshalJSON (Geometry {Coordinates : orb .MultiPolygon (mp )})
308
309
}
309
310
310
311
// UnmarshalJSON will unmarshal the GeoJSON MultiPolygon geometry.
311
312
func (mp * MultiPolygon ) UnmarshalJSON (data []byte ) error {
312
313
g := & Geometry {}
313
- err := json . Unmarshal (data , & g )
314
+ err := unmarshalJSON (data , & g )
314
315
if err != nil {
315
316
return err
316
317
}
@@ -335,10 +336,3 @@ type jsonGeometryMarshall struct {
335
336
Coordinates orb.Geometry `json:"coordinates,omitempty"`
336
337
Geometries []* Geometry `json:"geometries,omitempty"`
337
338
}
338
-
339
- type nocopyRawMessage []byte
340
-
341
- func (m * nocopyRawMessage ) UnmarshalJSON (data []byte ) error {
342
- * m = data
343
- return nil
344
- }
0 commit comments