-
Notifications
You must be signed in to change notification settings - Fork 1
/
BoardSerializer.pde
40 lines (30 loc) · 967 Bytes
/
BoardSerializer.pde
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
public class BoardSerializer {
protected Board _board;
public BoardSerializer(Board board) {
_board = board;
}
public JSONObject toJSONObject() {
JSONObject json = new JSONObject();
json.setString("name", _board.Name());
json.setJSONArray("elements", elementsToJSON());
return json;
}
private JSONArray elementsToJSON() {
JSONArray json = new JSONArray();
for (Node node : _board.GetNodes().values()) {
Element element = node.get("self");
JSONObject serializedElement = null;
if (element instanceof SwitchTrack) {
serializedElement = new SwitchTrackSerializer((SwitchTrack) element).toJSONObject();
} else if (element instanceof Track) {
serializedElement = new TrackSerializer((Track) element).toJSONObject();
} else {
// not implemented
}
if (serializedElement != null) {
json.append(serializedElement);
}
}
return json;
}
}