-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
158 lines (132 loc) · 3.22 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package api
import (
"encoding/json"
"github.com/MaxHalford/eaopt"
"github.com/rickyfitts/distributed-evolution/go/util"
)
/**
* Since Shapes contains pointers, custom json marshalling is needed.
* This file contains the JSON-encodable versions of Shapes and eaopt.Individual,
* and handles the logic associated with them.
*/
type JSONShapes struct {
Bounds util.Vector `json:"bounds"`
Type string `json:"type"`
Circles []Circle `json:"circles"`
Polygons []Polygon `json:"polygon"`
Triangles []Triangle `json:"triangle"`
}
type JSONIndividual struct {
Genome JSONShapes `json:"genome"`
Fitness float64 `json:"fitness"`
ID string `json:"ID"`
}
// convert a Shapes instance to a json encodable version
func (s Shapes) ToJSON() JSONShapes {
j := JSONShapes{
Bounds: s.Bounds,
Type: s.Type,
}
nMembers := len(s.Members)
switch s.Type {
case "circles":
j.Circles = make([]Circle, nMembers)
for i, m := range s.Members {
j.Circles[i] = m.(Circle)
}
case "polygons":
j.Polygons = make([]Polygon, nMembers)
for i, m := range s.Members {
j.Polygons[i] = m.(Polygon)
}
case "triangles":
j.Triangles = make([]Triangle, nMembers)
for i, m := range s.Members {
j.Triangles[i] = m.(Triangle)
}
}
return j
}
// convert a decoded json shapes intance to a Shapes instance
func (j JSONShapes) ToShapes() Shapes {
s := Shapes{
Bounds: j.Bounds,
Type: j.Type,
}
switch s.Type {
case "circles":
s.Members = make([]Shape, len(j.Circles))
for i, m := range j.Circles {
s.Members[i] = m
}
case "polygons":
s.Members = make([]Shape, len(j.Polygons))
for i, m := range j.Polygons {
s.Members[i] = m
}
case "triangles":
s.Members = make([]Shape, len(j.Triangles))
for i, m := range j.Triangles {
s.Members[i] = m
}
}
return s
}
func getJSONIndividual(i eaopt.Individual) JSONIndividual {
j := JSONIndividual{
Fitness: i.Fitness,
ID: i.ID,
}
if i.Genome == nil {
j.Genome = JSONShapes{}
} else {
j.Genome = i.Genome.(Shapes).ToJSON()
}
return j
}
func getJSONIndividuals(pop []eaopt.Individual) []JSONIndividual {
population := make([]JSONIndividual, len(pop))
for i, m := range pop {
population[i] = getJSONIndividual(m)
}
return population
}
func individualFromJSON(j JSONIndividual) eaopt.Individual {
return eaopt.Individual{
Genome: j.Genome.ToShapes(),
Fitness: j.Fitness,
ID: j.ID,
}
}
func (t *Task) MarshalJSON() ([]byte, error) {
type Alias Task
return json.Marshal(&struct {
BestFit JSONIndividual `json:"bestFit"`
Population []JSONIndividual `json:"population"`
*Alias
}{
BestFit: getJSONIndividual(t.BestFit),
Population: getJSONIndividuals(t.Population),
Alias: (*Alias)(t),
})
}
func (t *Task) UnmarshalJSON(data []byte) error {
type Alias Task
aux := &struct {
BestFit JSONIndividual `json:"bestFit"`
Population []JSONIndividual `json:"population"`
*Alias
}{
Alias: (*Alias)(t),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
population := make([]eaopt.Individual, len(aux.Population))
for i, m := range aux.Population {
population[i] = individualFromJSON(m)
}
t.BestFit = individualFromJSON(aux.BestFit)
t.Population = population
return nil
}