TreeNodeSerialization.hpp
1 #pragma once
2 #include <score/model/tree/TreeNode.hpp>
4 #include <score/serialization/JSONVisitor.hpp>
5 
6 template <typename T>
8 {
9  static void readFrom(DataStream::Serializer& s, const TreeNode<T>& n)
10  {
11  s.readFrom(static_cast<const T&>(n));
12 
13  s.stream() << n.childCount();
14  for(const auto& child : n)
15  {
16  s.readFrom(child);
17  }
18 
19  s.insertDelimiter();
20  }
21 
22  static void writeTo(DataStream::Deserializer& s, TreeNode<T>& n)
23  {
24  s.writeTo(static_cast<T&>(n));
25 
26  int childCount;
27  s.stream() >> childCount;
28  for(int i = 0; i < childCount; ++i)
29  {
30  TreeNode<T> child;
31  s.writeTo(child);
32  n.push_back(std::move(child));
33  }
34 
35  s.checkDelimiter();
36  }
37 };
38 
39 template <typename T>
41 {
42  static void readFrom(JSONObject::Serializer& s, const TreeNode<T>& n)
43  {
44  s.stream.StartObject();
45 
46  s.readFrom(static_cast<const T&>(n));
47 
48  if(n.childCount() > 0)
49  s.obj[s.strings.Children] = n.children();
50 
51  s.stream.EndObject();
52  }
53 
54  static void writeTo(JSONObject::Deserializer& s, TreeNode<T>& n)
55  {
56  s.writeTo(static_cast<T&>(n));
57  auto it = s.obj.constFind(s.strings.Children);
58  if(it != s.obj.constEnd())
59  {
60  const auto& children = it->toArray();
61  for(const auto& val : children)
62  {
63  TreeNode<T> child;
64  JSONObject::Deserializer nodeWriter(val);
65 
66  nodeWriter.writeTo(child);
67  n.push_back(std::move(child));
68  }
69  }
70  }
71 };
Definition: VisitorInterface.hpp:53
Definition: DataStreamVisitor.hpp:27
Definition: DataStreamVisitor.hpp:202
Definition: VisitorInterface.hpp:61
Definition: JSONVisitor.hpp:52
Definition: JSONVisitor.hpp:423
Definition: TreeNode.hpp:52
Definition: VisitorInterface.hpp:13