-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpoint.go
47 lines (38 loc) · 982 Bytes
/
point.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
package joejson
import (
"encoding/json"
"fmt"
)
// GeometryTypePoint is the value for a Point's 'type' member.
const GeometryTypePoint = "Point"
// Point is a single position geometry.
type Point Position
// Raw exposes the data for this geometry as primitive types.
func (p Point) Raw() []float64 {
return p
}
// MarshalJSON is a custom JSON marshaller.
func (p Point) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Coordinates []float64 `json:"coordinates"`
Type string `json:"type"`
}{
p,
GeometryTypePoint,
})
}
// UnmarshalJSON is a custom JSON unmarshaller.
func (p *Point) UnmarshalJSON(b []byte) error {
var tmp struct {
Position Position `json:"coordinates"`
Type string `json:"type"`
}
if err := json.Unmarshal(b, &tmp); err != nil {
return err
}
if tmp.Type != GeometryTypePoint {
return fmt.Errorf("invalid type %q, expected %q", tmp.Type, GeometryTypePoint)
}
*p = Point(tmp.Position)
return nil
}