-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJson.go
62 lines (52 loc) · 1.38 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
package main
import (
"encoding/json"
"fmt"
)
type messageJSON struct {
Subnet string `json:"subnet"`
OriginAS int `json:"origin"`
Timestamp int `json:"timestamp"`
Aspath []int `json:"aspath"`
}
type ConflictJSON struct {
ReferenceAnnouncement messageJSON `json:"referenceAnnouncement"`
Conflicts []messageJSON `json:"conflicts"`
}
func convertMessageForJSON(m message) messageJSON {
mesJSON := messageJSON{
Subnet: m.subnet.String(),
OriginAS: int(m.origin),
Timestamp: int(m.timestamp),
Aspath: aspathtoIntSlice(m.aspath),
}
return mesJSON
}
func convertMessagesForJSON(m []message) []messageJSON {
messages := make([]messageJSON, len(m))
for i := 0; i < len(m); i++ {
messages[i] = convertMessageForJSON(m[i])
}
return messages
}
func prepareJSON(c conflicts) {
conflicts := convertMessagesForJSON(c.conflictingMessages)
conJSON := ConflictJSON{
ReferenceAnnouncement: convertMessageForJSON(c.referenceAnnouncement),
Conflicts: conflicts,
}
writeJson(conJSON)
}
func writeJson(c ConflictJSON) {
data, err := json.Marshal(c)
if err != nil {
fmt.Println(Red("Could not marshal the following conflict(s) as JSON: ", c))
}
_, err = conflictsFile.Write(data)
if err != nil {
fmt.Println(Red("Could not write to the JSON file for found conflicts: ", string(data)))
fmt.Println(Red(err))
fmt.Println()
} else {
}
}