/
lng_lat.json.go
60 lines (55 loc) · 1.48 KB
/
lng_lat.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
package geom
import (
jsoniter "github.com/json-iterator/go"
"unsafe"
)
func init() {
jsoniter.RegisterTypeDecoder("geom.LngLat", &LngLatCodec{})
}
type LngLatCodec struct {
}
func (codec *LngLatCodec) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
a := iter.ReadAny()
lngLat := (*LngLat)(ptr)
if a.ValueType() == jsoniter.ArrayValue {
var points []float64
a.ToVal(&points)
if len(points) < 2 {
iter.ReportError("LngLatCodec.Decode", "invalid format of the lngLat, at least two element")
return
}
lngLat.Longitude = points[0]
lngLat.Latitude = points[1]
if len(points) > 2 {
lngLat.Altitude = points[2]
}
} else if a.ValueType() == jsoniter.ObjectValue {
lng := a.Get("longitude")
if lng.ValueType() == jsoniter.NumberValue {
lngLat.Longitude = lng.ToFloat64()
} else {
lngLat.Longitude = a.Get("lng").ToFloat64()
}
lat := a.Get("latitude")
if lat.ValueType() == jsoniter.NumberValue {
lngLat.Latitude = lat.ToFloat64()
} else {
lngLat.Latitude = a.Get("lat").ToFloat64()
}
alt := a.Get("altitude")
if lng.ValueType() == jsoniter.NumberValue {
lngLat.Altitude = alt.ToFloat64()
} else {
lngLat.Altitude = a.Get("alt").ToFloat64()
}
} else if a.ValueType() == jsoniter.StringValue {
err := lngLat.Parse(a.ToString())
if err != nil {
iter.ReportError("LngLatCodec.Decode", err.Error())
}
}
}
func (codec *LngLatCodec) IsEmpty(ptr unsafe.Pointer) bool {
lngLat := (*LngLat)(ptr)
return lngLat == nil || lngLat.IsEmpty()
}