-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtopojson.go
57 lines (44 loc) · 1.18 KB
/
topojson.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
package xonacatl
import (
"encoding/json"
"io"
)
type topoObject struct {
data json.RawMessage
}
func (t *topoObject) MarshalJSON() ([]byte, error) {
return t.data.MarshalJSON()
}
func (t *topoObject) UnmarshalJSON(data []byte) error {
return t.data.UnmarshalJSON(data)
}
type topoJSON struct {
Type string `json:"type"`
Transform *json.RawMessage `json:"transform,omitempty"`
Objects map[string]*topoObject `json:"objects"`
// arcs currently stored as RawMessage to avoid precision issues when Unmarshalling and Marshalling them. see long comment in json.go.
Arcs *json.RawMessage `json:"arcs"`
}
type topoJSONCopier struct {
layers map[string]bool
}
func NewCopyTopoJSONLayers(layers map[string]bool) *topoJSONCopier {
return &topoJSONCopier{layers: layers}
}
func (c *topoJSONCopier) CopyLayers(rd io.Reader, wr io.Writer) error {
var t topoJSON
dec := json.NewDecoder(rd)
err := dec.Decode(&t)
if err != nil {
return err
}
for k := range t.Objects {
if !c.layers[k] {
delete(t.Objects, k)
}
}
// TODO: collect arcs and reset unused ones to empty
enc := json.NewEncoder(wr)
enc.SetIndent("", "")
return enc.Encode(&t)
}