Skip to content

Commit 74bdecd

Browse files
committed
geojson: more comments and examples about extra/foreign members
1 parent 7e5d621 commit 74bdecd

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

geojson/example_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,62 @@ func ExampleFeatureCollection_foreignMembers() {
4343

4444
fmt.Println(fc.Features[0].Geometry)
4545
fmt.Println(fc.ExtraMembers["title"])
46+
47+
data, _ := json.Marshal(fc)
48+
fmt.Println(string(data))
49+
4650
// Output:
4751
// [102 0.5]
4852
// Title as Foreign Member
53+
// {"features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[102,0.5]},"properties":{"prop0":"value0"}}],"title":"Title as Foreign Member","type":"FeatureCollection"}
54+
}
55+
56+
// MyFeatureCollection is a depricated/no longer supported way to extract
57+
// foreign/extra members from a feature collection. Now an UnmarshalJSON
58+
// method, like below, is required for it to work.
59+
type MyFeatureCollection struct {
60+
geojson.FeatureCollection
61+
Title string `json:"title"`
62+
}
63+
64+
// UnmarshalJSON implemented as below is now required for the extra members
65+
// to be decoded directly into the type.
66+
func (fc *MyFeatureCollection) UnmarshalJSON(data []byte) error {
67+
err := json.Unmarshal(data, &fc.FeatureCollection)
68+
if err != nil {
69+
return err
70+
}
71+
72+
fc.Title = fc.ExtraMembers.MustString("title", "")
73+
return nil
74+
}
75+
76+
func ExampleFeatureCollection_foreignMembersCustom() {
77+
// Note: this approach to handling foreign/extra members requires
78+
// implementing an `UnmarshalJSON` method on the new type.
79+
// See MyFeatureCollection type and its UnmarshalJSON function above.
80+
81+
rawJSON := []byte(`
82+
{ "type": "FeatureCollection",
83+
"features": [
84+
{ "type": "Feature",
85+
"geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
86+
"properties": {"prop0": "value0"}
87+
}
88+
],
89+
"title": "Title as Foreign Member"
90+
}`)
91+
92+
fc := &MyFeatureCollection{}
93+
json.Unmarshal(rawJSON, &fc)
94+
95+
fmt.Println(fc.FeatureCollection.Features[0].Geometry)
96+
fmt.Println(fc.Features[0].Geometry)
97+
fmt.Println(fc.Title)
98+
// Output:
99+
// [102 0.5]
100+
// [102 0.5]
101+
// Title as Foreign Member
49102
}
50103

51104
func ExampleUnmarshalFeatureCollection() {

geojson/feature_collection.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ type FeatureCollection struct {
2020
Features []*Feature `json:"features"`
2121

2222
// ExtraMembers can be used to encoded/decode extra key/members in
23-
// the base of the feature collection.
23+
// the base of the feature collection. Note that keys of "type", "bbox"
24+
// and "features" will not work as those are reserved by the GeoJSON spec.
2425
ExtraMembers Properties `json:"-"`
2526
}
2627

@@ -41,6 +42,8 @@ func (fc *FeatureCollection) Append(feature *Feature) *FeatureCollection {
4142
// MarshalJSON converts the feature collection object into the proper JSON.
4243
// It will handle the encoding of all the child features and geometries.
4344
// Alternately one can call json.Marshal(fc) directly for the same result.
45+
// Items in the ExtraMembers map will be included in the base of the
46+
// feature collection object.
4447
func (fc FeatureCollection) MarshalJSON() ([]byte, error) {
4548
var tmp map[string]interface{}
4649
if fc.ExtraMembers != nil {
@@ -50,6 +53,7 @@ func (fc FeatureCollection) MarshalJSON() ([]byte, error) {
5053
}
5154

5255
tmp["type"] = featureCollection
56+
delete(tmp, "bbox")
5357
if fc.BBox != nil {
5458
tmp["bbox"] = fc.BBox
5559
}
@@ -63,6 +67,7 @@ func (fc FeatureCollection) MarshalJSON() ([]byte, error) {
6367
}
6468

6569
// UnmarshalJSON decodes the data into a GeoJSON feature collection.
70+
// Extra/foreign members will be put into the `ExtraMembers` attribute.
6671
func (fc *FeatureCollection) UnmarshalJSON(data []byte) error {
6772
tmp := make(map[string]json.RawMessage, 4)
6873

0 commit comments

Comments
 (0)