Loading...
Searching...
No Matches
TreeNodeSerialization.hpp
1#pragma once
2#include <score/model/tree/TreeNode.hpp>
4#include <score/serialization/JSONVisitor.hpp>
5
6template <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
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
39template <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
void readFrom(const score::Entity< T > &obj)
Called by code that wants to serialize.
Definition DataStreamVisitor.hpp:53
void insertDelimiter()
insertDelimiter
Definition DataStreamVisitor.hpp:156
Definition DataStreamVisitor.hpp:202
Definition VisitorInterface.hpp:61
Definition JSONVisitor.hpp:52
void readFrom(const score::Entity< T > &obj)
Definition EntitySerialization.hpp:7
Definition JSONVisitor.hpp:423
Definition TreeNode.hpp:52
Definition VisitorInterface.hpp:13