11package geojson
22
33import (
4- "encoding/json"
54 "errors"
5+
66 "github.com/paulmach/orb"
77)
88
@@ -85,14 +85,15 @@ func (g Geometry) MarshalJSON() ([]byte, error) {
8585 ng .Geometries = g .Geometries
8686 ng .Type = orb.Collection {}.GeoJSONType ()
8787 }
88- return json .Marshal (ng )
88+
89+ return marshalJSON (ng )
8990}
9091
9192// UnmarshalGeometry decodes the data into a GeoJSON feature.
9293// Alternately one can call json.Unmarshal(g) directly for the same result.
9394func UnmarshalGeometry (data []byte ) (* Geometry , error ) {
9495 g := & Geometry {}
95- err := json . Unmarshal (data , g )
96+ err := unmarshalJSON (data , g )
9697 if err != nil {
9798 return nil , err
9899 }
@@ -103,35 +104,35 @@ func UnmarshalGeometry(data []byte) (*Geometry, error) {
103104// UnmarshalJSON will unmarshal the correct geometry from the json structure.
104105func (g * Geometry ) UnmarshalJSON (data []byte ) error {
105106 jg := & jsonGeometry {}
106- err := json . Unmarshal (data , jg )
107+ err := unmarshalJSON (data , jg )
107108 if err != nil {
108109 return err
109110 }
110111
111112 switch jg .Type {
112113 case "Point" :
113114 p := orb.Point {}
114- err = json . Unmarshal (jg .Coordinates , & p )
115+ err = unmarshalJSON (jg .Coordinates , & p )
115116 g .Coordinates = p
116117 case "MultiPoint" :
117118 mp := orb.MultiPoint {}
118- err = json . Unmarshal (jg .Coordinates , & mp )
119+ err = unmarshalJSON (jg .Coordinates , & mp )
119120 g .Coordinates = mp
120121 case "LineString" :
121122 ls := orb.LineString {}
122- err = json . Unmarshal (jg .Coordinates , & ls )
123+ err = unmarshalJSON (jg .Coordinates , & ls )
123124 g .Coordinates = ls
124125 case "MultiLineString" :
125126 mls := orb.MultiLineString {}
126- err = json . Unmarshal (jg .Coordinates , & mls )
127+ err = unmarshalJSON (jg .Coordinates , & mls )
127128 g .Coordinates = mls
128129 case "Polygon" :
129130 p := orb.Polygon {}
130- err = json . Unmarshal (jg .Coordinates , & p )
131+ err = unmarshalJSON (jg .Coordinates , & p )
131132 g .Coordinates = p
132133 case "MultiPolygon" :
133134 mp := orb.MultiPolygon {}
134- err = json . Unmarshal (jg .Coordinates , & mp )
135+ err = unmarshalJSON (jg .Coordinates , & mp )
135136 g .Coordinates = mp
136137 case "GeometryCollection" :
137138 g .Geometries = jg .Geometries
@@ -154,13 +155,13 @@ func (p Point) Geometry() orb.Geometry {
154155
155156// MarshalJSON will convert the Point into a GeoJSON Point geometry.
156157func (p Point ) MarshalJSON () ([]byte , error ) {
157- return json . Marshal (Geometry {Coordinates : orb .Point (p )})
158+ return marshalJSON (Geometry {Coordinates : orb .Point (p )})
158159}
159160
160161// UnmarshalJSON will unmarshal the GeoJSON Point geometry.
161162func (p * Point ) UnmarshalJSON (data []byte ) error {
162163 g := & Geometry {}
163- err := json . Unmarshal (data , & g )
164+ err := unmarshalJSON (data , & g )
164165 if err != nil {
165166 return err
166167 }
@@ -184,13 +185,13 @@ func (mp MultiPoint) Geometry() orb.Geometry {
184185
185186// MarshalJSON will convert the MultiPoint into a GeoJSON MultiPoint geometry.
186187func (mp MultiPoint ) MarshalJSON () ([]byte , error ) {
187- return json . Marshal (Geometry {Coordinates : orb .MultiPoint (mp )})
188+ return marshalJSON (Geometry {Coordinates : orb .MultiPoint (mp )})
188189}
189190
190191// UnmarshalJSON will unmarshal the GeoJSON MultiPoint geometry.
191192func (mp * MultiPoint ) UnmarshalJSON (data []byte ) error {
192193 g := & Geometry {}
193- err := json . Unmarshal (data , & g )
194+ err := unmarshalJSON (data , & g )
194195 if err != nil {
195196 return err
196197 }
@@ -214,13 +215,13 @@ func (ls LineString) Geometry() orb.Geometry {
214215
215216// MarshalJSON will convert the LineString into a GeoJSON LineString geometry.
216217func (ls LineString ) MarshalJSON () ([]byte , error ) {
217- return json . Marshal (Geometry {Coordinates : orb .LineString (ls )})
218+ return marshalJSON (Geometry {Coordinates : orb .LineString (ls )})
218219}
219220
220221// UnmarshalJSON will unmarshal the GeoJSON MultiPoint geometry.
221222func (ls * LineString ) UnmarshalJSON (data []byte ) error {
222223 g := & Geometry {}
223- err := json . Unmarshal (data , & g )
224+ err := unmarshalJSON (data , & g )
224225 if err != nil {
225226 return err
226227 }
@@ -244,13 +245,13 @@ func (mls MultiLineString) Geometry() orb.Geometry {
244245
245246// MarshalJSON will convert the MultiLineString into a GeoJSON MultiLineString geometry.
246247func (mls MultiLineString ) MarshalJSON () ([]byte , error ) {
247- return json . Marshal (Geometry {Coordinates : orb .MultiLineString (mls )})
248+ return marshalJSON (Geometry {Coordinates : orb .MultiLineString (mls )})
248249}
249250
250251// UnmarshalJSON will unmarshal the GeoJSON MultiPoint geometry.
251252func (mls * MultiLineString ) UnmarshalJSON (data []byte ) error {
252253 g := & Geometry {}
253- err := json . Unmarshal (data , & g )
254+ err := unmarshalJSON (data , & g )
254255 if err != nil {
255256 return err
256257 }
@@ -274,13 +275,13 @@ func (p Polygon) Geometry() orb.Geometry {
274275
275276// MarshalJSON will convert the Polygon into a GeoJSON Polygon geometry.
276277func (p Polygon ) MarshalJSON () ([]byte , error ) {
277- return json . Marshal (Geometry {Coordinates : orb .Polygon (p )})
278+ return marshalJSON (Geometry {Coordinates : orb .Polygon (p )})
278279}
279280
280281// UnmarshalJSON will unmarshal the GeoJSON Polygon geometry.
281282func (p * Polygon ) UnmarshalJSON (data []byte ) error {
282283 g := & Geometry {}
283- err := json . Unmarshal (data , & g )
284+ err := unmarshalJSON (data , & g )
284285 if err != nil {
285286 return err
286287 }
@@ -304,13 +305,13 @@ func (mp MultiPolygon) Geometry() orb.Geometry {
304305
305306// MarshalJSON will convert the MultiPolygon into a GeoJSON MultiPolygon geometry.
306307func (mp MultiPolygon ) MarshalJSON () ([]byte , error ) {
307- return json . Marshal (Geometry {Coordinates : orb .MultiPolygon (mp )})
308+ return marshalJSON (Geometry {Coordinates : orb .MultiPolygon (mp )})
308309}
309310
310311// UnmarshalJSON will unmarshal the GeoJSON MultiPolygon geometry.
311312func (mp * MultiPolygon ) UnmarshalJSON (data []byte ) error {
312313 g := & Geometry {}
313- err := json . Unmarshal (data , & g )
314+ err := unmarshalJSON (data , & g )
314315 if err != nil {
315316 return err
316317 }
@@ -335,10 +336,3 @@ type jsonGeometryMarshall struct {
335336 Coordinates orb.Geometry `json:"coordinates,omitempty"`
336337 Geometries []* Geometry `json:"geometries,omitempty"`
337338}
338-
339- type nocopyRawMessage []byte
340-
341- func (m * nocopyRawMessage ) UnmarshalJSON (data []byte ) error {
342- * m = data
343- return nil
344- }
0 commit comments