hivemind 1.0.0
Loading...
Searching...
No Matches
serializer.cpp
Go to the documentation of this file.
1#define RAPIDJSON_HAS_STDSTRING 1
2
3#include "core/serializer.h"
4#include "rapidjson/document.h"
5#include "rapidjson/istreamwrapper.h"
6#include "rapidjson/prettywriter.h" // for stringify JSON
7#include <fstream>
8#include <iostream>
9#include <memory>
10#include <string>
11
12bool debug = false;
13
14namespace Json
15{
16
17 rapidjson::Value
18 ISInt::ToDom(rapidjson::Document&)
19 {
20 rapidjson::Value v;
21 v.SetInt(value);
22 return v;
23 }
24
25 void
26 ISInt::FromDom(rapidjson::Value& v, rapidjson::Document&)
27 {
28 assert(v.IsInt());
29 value = v.GetInt();
30 }
31
32 rapidjson::Value
33 ISFloat::ToDom(rapidjson::Document&)
34 {
35 rapidjson::Value v;
36 v.SetFloat(value);
37 return v;
38 }
39
40 void
41 ISFloat::FromDom(rapidjson::Value& v, rapidjson::Document&)
42 {
43 assert(v.IsFloat());
44 value = v.GetFloat();
45 }
46
47 rapidjson::Value
48 ISDouble::ToDom(rapidjson::Document&)
49 {
50 rapidjson::Value v;
51 if (debug)
52 std::cout << value << std::endl;
53 v.SetDouble(value);
54 return v;
55 }
56
57 void
58 ISDouble::FromDom(rapidjson::Value& v, rapidjson::Document&)
59 {
60 assert(v.IsDouble());
61 value = v.GetFloat();
62 }
63
64 rapidjson::Value
65 ISBool::ToDom(rapidjson::Document&)
66 {
67 rapidjson::Value v;
68 v.SetBool(value);
69 return v;
70 }
71
72 void
73 ISBool::FromDom(rapidjson::Value& v, rapidjson::Document&)
74 {
75 assert(v.IsBool());
76 value = v.GetBool();
77 }
78
79 rapidjson::Value
80 ISString::ToDom(rapidjson::Document& d)
81 {
82 rapidjson::Value v;
83 v.SetString(value.c_str(), d.GetAllocator());
84 return v;
85 }
86
87 void
88 ISString::FromDom(rapidjson::Value& v, rapidjson::Document&)
89 {
90 assert(v.IsString());
91 value = v.GetString();
92 }
93
94 rapidjson::Value
95 ISValue::ToDom(rapidjson::Document& d)
96 {
98
99 rapidjson::Value v;
100 v.SetObject();
101 v.AddMember("TypeId", GetName(d), d.GetAllocator());
102
103 for (auto& element : p) {
104 rapidjson::Value n;
105 n.SetString(element.name, d.GetAllocator());
106 if (element.value != nullptr)
107 v.AddMember(n, element.value->ToDom(d), d.GetAllocator());
108 }
109 return v;
110 }
111
112 void
113 ISValue::FromDom(rapidjson::Value& v, rapidjson::Document& d)
114 {
116 if (v.IsObject()) {
117 for (auto& element : p) {
118 element.value->FromDom(v[element.name], d);
119 }
120 }
121 }
122
123 rapidjson::Value
124 ISIntVector::ToDom(rapidjson::Document& d)
125 {
126 rapidjson::Value a;
127 a.SetArray();
128 for (auto& element : value) {
129 rapidjson::Value v;
130 v.SetInt(element);
131 a.PushBack(v, d.GetAllocator());
132 }
133 return a;
134 }
135
136 void
137 ISIntVector::FromDom(rapidjson::Value& v, rapidjson::Document&)
138 {
139 for (rapidjson::SizeType i = 0; i < v.Size();
140 i++) { // rapidjson uses SizeType instead of size_t.
141 value.push_back(v[i].GetInt());
142 }
143 }
144
145 rapidjson::Value
146 ISFloatVector::ToDom(rapidjson::Document& d)
147 {
148 rapidjson::Value a;
149 a.SetArray();
150 for (auto& element : value) {
151 rapidjson::Value v;
152 v.SetFloat(element);
153 a.PushBack(v, d.GetAllocator());
154 }
155 return a;
156 }
157
158 void
159 ISFloatVector::FromDom(rapidjson::Value& v, rapidjson::Document&)
160 {
161 for (rapidjson::SizeType i = 0; i < v.Size();
162 i++) { // rapidjson uses SizeType instead of size_t.
163 value.push_back(v[i].GetFloat());
164 }
165 }
166
167 rapidjson::Value
168 ISDoubleVector::ToDom(rapidjson::Document& d)
169 {
170 rapidjson::Value a;
171 a.SetArray();
172 for (auto& element : value) {
173 rapidjson::Value v;
174 v.SetDouble(element);
175 a.PushBack(v, d.GetAllocator());
176 }
177 return a;
178 }
179
180 void
181 ISDoubleVector::FromDom(rapidjson::Value& v, rapidjson::Document&)
182 {
183 for (rapidjson::SizeType i = 0; i < v.Size();
184 i++) { // rapidjson uses SizeType instead of size_t.
185 value.push_back(v[i].GetDouble());
186 }
187 }
188
189 int
190 ISConstructors::AddConstructor(std::string name, ISValuePtr (*creator)())
191 {
192 m_TheRegistry[name] = creator;
193
194 return 0;
195 }
196
199 {
200 Json::ISValuePtr (*cnsctr)() = m_TheRegistry[name];
201 Json::ISValuePtr no = cnsctr();
202 return no;
203 }
204
205 void
206 serialize(std::string filename, ISValue* top)
207 {
208 rapidjson::Document document;
209 document.SetObject();
210
211 ISProperties p = top->GetProperty();
212
213 document.AddMember("TypeId", top->GetName(document),
214 document.GetAllocator());
215
216 for (auto& element : p) {
217 rapidjson::Value n;
218 n.SetString(element.name, document.GetAllocator());
219 if (element.value != nullptr)
220 document.AddMember(n, element.value->ToDom(document),
221 document.GetAllocator());
222 }
223
224 rapidjson::StringBuffer sb;
225
226 rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
227 document.Accept(
228 writer); // Accept() traverses the DOM and generates Handler events.
229 std::fstream jsonout(filename, std::ios_base::out);
230 jsonout << sb.GetString() << std::endl;
231 jsonout.close();
232 }
233
234 void
235 deserialize(std::string filename, ISValue* top)
236 {
237 rapidjson::Document document;
238
239 std::ifstream ifs(filename);
240 rapidjson::IStreamWrapper isw(ifs);
241
242 document.ParseStream(isw);
243 ISProperties p = top->GetProperty();
244 if (document.IsObject()) {
245 for (auto& element : p) {
246 element.value->FromDom(document[element.name], document);
247 }
248 }
249 }
250
251} // namespace Json
bool & value
Definition: serializer.h:123
virtual rapidjson::Value ToDom(rapidjson::Document &d)
ToDom is the function that enables the serializer to take data from the application to the JSON file.
Definition: serializer.cpp:65
virtual void FromDom(rapidjson::Value &v, rapidjson::Document &d)
FromDom is the function that enables the serializer to get data out of the JSON file and put it in th...
Definition: serializer.cpp:73
int AddConstructor(std::string name, ISValuePtr(*creator)())
Definition: serializer.cpp:190
ISValuePtr GetObject(std::string name)
Definition: serializer.cpp:198
std::map< std::string, Json::ISValuePtr(*)()> m_TheRegistry
Definition: serializer.h:490
virtual rapidjson::Value ToDom(rapidjson::Document &d)
ToDom is the function that enables the serializer to take data from the application to the JSON file.
Definition: serializer.cpp:168
std::vector< double > & value
Definition: serializer.h:456
virtual void FromDom(rapidjson::Value &v, rapidjson::Document &d)
FromDom is the function that enables the serializer to get data out of the JSON file and put it in th...
Definition: serializer.cpp:181
virtual void FromDom(rapidjson::Value &v, rapidjson::Document &d)
FromDom is the function that enables the serializer to get data out of the JSON file and put it in th...
Definition: serializer.cpp:58
virtual rapidjson::Value ToDom(rapidjson::Document &d)
ToDom is the function that enables the serializer to take data from the application to the JSON file.
Definition: serializer.cpp:48
double & value
Definition: serializer.h:110
virtual void FromDom(rapidjson::Value &v, rapidjson::Document &d)
FromDom is the function that enables the serializer to get data out of the JSON file and put it in th...
Definition: serializer.cpp:159
std::vector< float > & value
Definition: serializer.h:441
virtual rapidjson::Value ToDom(rapidjson::Document &d)
ToDom is the function that enables the serializer to take data from the application to the JSON file.
Definition: serializer.cpp:146
virtual void FromDom(rapidjson::Value &v, rapidjson::Document &d)
FromDom is the function that enables the serializer to get data out of the JSON file and put it in th...
Definition: serializer.cpp:41
virtual rapidjson::Value ToDom(rapidjson::Document &d)
ToDom is the function that enables the serializer to take data from the application to the JSON file.
Definition: serializer.cpp:33
float & value
Definition: serializer.h:97
virtual rapidjson::Value ToDom(rapidjson::Document &d)
ToDom is the function that enables the serializer to take data from the application to the JSON file.
Definition: serializer.cpp:124
virtual void FromDom(rapidjson::Value &v, rapidjson::Document &d)
FromDom is the function that enables the serializer to get data out of the JSON file and put it in th...
Definition: serializer.cpp:137
std::vector< int > & value
Definition: serializer.h:426
int & value
Definition: serializer.h:84
virtual void FromDom(rapidjson::Value &v, rapidjson::Document &d)
FromDom is the function that enables the serializer to get data out of the JSON file and put it in th...
Definition: serializer.cpp:26
virtual rapidjson::Value ToDom(rapidjson::Document &d)
ToDom is the function that enables the serializer to take data from the application to the JSON file.
Definition: serializer.cpp:18
virtual void FromDom(rapidjson::Value &v, rapidjson::Document &d)
FromDom is the function that enables the serializer to get data out of the JSON file and put it in th...
Definition: serializer.cpp:88
virtual rapidjson::Value ToDom(rapidjson::Document &d)
ToDom is the function that enables the serializer to take data from the application to the JSON file.
Definition: serializer.cpp:80
std::string & value
Definition: serializer.h:136
Rflection is made possible by the help of the ISValue class and the type classes.
Definition: serializer.h:42
virtual rapidjson::Value ToDom(rapidjson::Document &d)
ToDom is the function that enables the serializer to take data from the application to the JSON file.
Definition: serializer.cpp:95
virtual ISProperties GetProperty()
GetProperty enables the serializer to deal with composite type like objects and members.
Definition: serializer.h:48
virtual void FromDom(rapidjson::Value &v, rapidjson::Document &d)
FromDom is the function that enables the serializer to get data out of the JSON file and put it in th...
Definition: serializer.cpp:113
virtual rapidjson::Value GetName(rapidjson::Document &d)
For future expansion.
Definition: serializer.h:60
void deserialize(std::string filename, ISValue *p)
Function to start deserializing a file.
Definition: serializer.cpp:235
void serialize(std::string filename, ISValue *p)
Function to start serializing an onbject.
Definition: serializer.cpp:206
std::vector< ISProperty > ISProperties
ISProperties is a vector with ISProperty.
Definition: serializer.h:34
std::shared_ptr< ISValue > ISValuePtr
Definition: serializer.h:17
bool debug
Definition: serializer.cpp:12
bool debug
Definition: serializer.cpp:12