This repository has been archived by the owner on Jun 28, 2021. It is now read-only.
forked from paulmach/go.geo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
114 lines (89 loc) · 2.59 KB
/
json.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package geo
import (
"encoding/json"
"errors"
)
// MarshalJSON enables lines to be encoded as JSON using the encoding/json package.
func (l *Line) MarshalJSON() ([]byte, error) {
return json.Marshal([2]Point{l.a, l.b})
}
// UnmarshalJSON enables lines to be decoded as JSON using the encoding/json package.
func (l *Line) UnmarshalJSON(data []byte) error {
var points [][2]float64
err := json.Unmarshal(data, &points)
if err != nil {
return err
}
if len(points) > 2 {
return errors.New("geo: too many points to unmarshal into line")
}
if len(points) < 2 {
return errors.New("geo: not enough points to unmarshal into line")
}
l.a = Point{points[0][0], points[0][1]}
l.b = Point{points[1][0], points[1][1]}
return nil
}
// MarshalJSON enables paths to be encoded as JSON using the encoding/json package.
func (p *Path) MarshalJSON() ([]byte, error) {
return json.Marshal(p.PointSet)
}
// UnmarshalJSON enables paths to be decoded as JSON using the encoding/json package.
func (p *Path) UnmarshalJSON(data []byte) error {
var pointSet PointSet
err := json.Unmarshal(data, &pointSet)
if err != nil {
return err
}
p.PointSet = pointSet
return nil
}
// MarshalJSON enables bounds to be encoded as JSON using the encoding/json package.
func (b *Bound) MarshalJSON() ([]byte, error) {
return json.Marshal([2]*Point{b.sw, b.ne})
}
// UnmarshalJSON enables bounds to be decoded as JSON using the encoding/json package.
func (b *Bound) UnmarshalJSON(data []byte) error {
var points []*Point
err := json.Unmarshal(data, &points)
if err != nil {
return err
}
if len(points) > 2 {
return errors.New("geo: too many points to unmarshal into bound")
}
if len(points) < 2 {
return errors.New("geo: not enough points to unmarshal into bound")
}
b.sw = points[0]
b.ne = points[0].Clone()
b.Extend(points[1])
return nil
}
// MarshalJSON enables surfaces to be encoded as JSON using the encoding/json package.
func (s *Surface) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"bound": s.bound,
"values": s.Grid,
})
}
// UnmarshalJSON enables surfaces to be decoded as JSON using the encoding/json package.
func (s *Surface) UnmarshalJSON(data []byte) error {
surface := struct {
Bound *Bound
Values [][]float64
}{}
err := json.Unmarshal(data, &surface)
if err != nil {
return err
}
// if len(points) > 2 {
// return errors.New("geo: too many points to unmarshal into bound")
// }
// if len(points) < 2 {
// return errors.New("geo: not enough points to unmarshal into bound")
// }
s.bound = surface.Bound
s.Grid = surface.Values
return nil
}