-
Notifications
You must be signed in to change notification settings - Fork 1
/
featurecollection.go
49 lines (41 loc) · 1.1 KB
/
featurecollection.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
package joejson
import (
"encoding/json"
"fmt"
)
// TypeFeatureCollection is the value for a FeatureCollection's 'type' member.
const TypeFeatureCollection string = "FeatureCollection"
// FeatureCollection is a collection of Features.
type FeatureCollection struct {
Features []Feature
Bbox []Position
}
// MarshalJSON is a custom JSON marshaller.
func (f FeatureCollection) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Type string `json:"type"`
Features []Feature `json:"features"`
BBox []Position `json:"bbox,omitempty"`
}{
TypeFeatureCollection,
f.Features,
f.Bbox,
})
}
// UnmarshalJSON is a custom JSON unmarshaller.
func (f *FeatureCollection) UnmarshalJSON(b []byte) error {
var tmp struct {
Type string `json:"type"`
Features []Feature `json:"features"`
Bbox []Position `json:"bbox"`
}
if err := json.Unmarshal(b, &tmp); err != nil {
return err
}
if tmp.Type != TypeFeatureCollection {
return fmt.Errorf("invalid type %q, expected %q", tmp.Type, TypeFeatureCollection)
}
f.Features = tmp.Features
f.Bbox = tmp.Bbox
return nil
}