summaryrefslogtreecommitdiff
path: root/slicer/json/serializer.cpp
blob: 48b6b365853da83984d3b8e1cbe391606e6af9ad (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include "serializer.h"
#include <jsonpp.h>
#include <boost/lexical_cast.hpp>
#include <boost/bind.hpp>
#include <stdexcept>
#include <fstream>
#include <glibmm/ustring.h>

namespace Slicer {
	class JsonValueSource : public ValueSource {
		public:
			JsonValueSource(const json::Value & s) :
				value(s)
			{
			}

			void set(bool & v) const override
			{
				v = boost::get<bool>(value);
			}

			void set(Ice::Byte & v) const override
			{
				v = boost::numeric_cast<Ice::Byte>(boost::get<json::Number>(value));
			}

			void set(Ice::Short & v) const override
			{
				v = boost::numeric_cast<Ice::Short>(boost::get<json::Number>(value));
			}

			void set(Ice::Int & v) const override
			{
				v = boost::numeric_cast<Ice::Int>(boost::get<json::Number>(value));
			}

			void set(Ice::Long & v) const override
			{
				v = boost::numeric_cast<Ice::Long>(boost::get<json::Number>(value));
			}

			void set(Ice::Float & v) const override
			{
				v = boost::numeric_cast<Ice::Float>(boost::get<json::Number>(value));
			}

			void set(Ice::Double & v) const override
			{
				v = boost::numeric_cast<Ice::Double>(boost::get<json::Number>(value));
			}

			void set(std::string & v) const override
			{
				v = boost::get<json::String>(value);
			}

		protected:
			const json::Value & value;
	};

	class JsonValueTarget : public ValueTarget {
		public:
			JsonValueTarget(json::Value & t) :
				target(t)
			{
				target = json::Null();
			}

			virtual void get(const bool & value) const
			{
				target = value;
			}

			virtual void get(const Ice::Byte & value) const
			{
				target = boost::numeric_cast<json::Number>(value);
			}

			virtual void get(const Ice::Short & value) const
			{
				target = boost::numeric_cast<json::Number>(value);
			}

			virtual void get(const Ice::Int & value) const
			{
				target = boost::numeric_cast<json::Number>(value);
			}

			virtual void get(const Ice::Long & value) const
			{
				target = boost::numeric_cast<json::Number>(value);
			}

			virtual void get(const Ice::Float & value) const
			{
				target = boost::numeric_cast<json::Number>(value);
			}

			virtual void get(const Ice::Double & value) const
			{
				target = boost::numeric_cast<json::Number>(value);
			}

			virtual void get(const std::string & value) const
			{
				target = value;
			}

		private:
			json::Value & target;
	};

	class DocumentTreeIterate : public boost::static_visitor<> {
		public:
			DocumentTreeIterate(ModelPartPtr & mp) : modelPart(mp)
			{
			}
			template<typename SimpleT>
			void operator()(const SimpleT & v) const
			{
				modelPart->Create();
				modelPart->SetValue(new JsonValueSource(v));
				modelPart->Complete();
			}
			void operator()(const json::Null &) const
			{
				modelPart->Complete();
			}
			void operator()(const json::Object & o) const
			{
				auto typeAttrItr = o.find("slicer-typeid");
				if (typeAttrItr != o.end() && boost::get<json::String>(typeAttrItr->second.get())) {
					modelPart = modelPart->GetSubclassModelPart(boost::get<json::String>(*typeAttrItr->second));
				}
				modelPart->Create();
				BOOST_FOREACH(const auto & element, o) {
					auto emp = modelPart->GetChild(element.first);
					if (emp) {
						emp->Create();
						boost::apply_visitor(DocumentTreeIterate(emp), *element.second);
						emp->Complete();
					}
				}
				modelPart->Complete();
			}
			void operator()(const json::Array & a) const
			{
				modelPart->Create();
				BOOST_FOREACH(const auto & element, a) {
					auto emp = modelPart->GetChild(std::string());
					if (emp) {
						emp->Create();
						boost::apply_visitor(DocumentTreeIterate(emp), *element);
						emp->Complete();
					}
				}
				modelPart->Complete();
			}
		private:
			ModelPartPtr & modelPart;
	};

	void
	Json::ModelTreeIterateSeq(json::Value * n, ModelPartPtr mp)
	{
		auto & arr = boost::get<json::Array &>(*n);
		arr.push_back(json::ValuePtr(new json::Value()));
		ModelTreeIterateRoot(arr.back().get(), mp);
	}

	void
	Json::ModelTreeIterate(json::Value * n, const std::string & name, ModelPartPtr mp)
	{
		if (name.empty() || !n) {
			return;
		}
		if (mp) {
			switch (mp->GetType()) {
				case mpt_Null:
					boost::get<json::Object>(*n).insert({name, json::ValuePtr(new json::Value())});
					return;
				case mpt_Simple:
					mp->GetValue(new JsonValueTarget(*boost::get<json::Object>(*n).insert({name, json::ValuePtr(new json::Value())}).first->second));
					break;
				case mpt_Complex:
					{
						auto nn = json::ValuePtr(new json::Value(json::Object()));
						if (auto typeId = mp->GetTypeId()) {
							boost::get<json::Object>(*nn).insert({"slicer-typeid", json::ValuePtr(new json::Value(*typeId))});
							mp = mp->GetSubclassModelPart(*typeId);
						}
						mp->OnEachChild(boost::bind(&Json::ModelTreeIterate, boost::get<json::Object>(*n).insert({name, nn}).first->second.get(), _1, _2));
						break;
					}
				case mpt_Sequence:
				case mpt_Dictionary:
					mp->OnEachChild(boost::bind(&Json::ModelTreeIterateSeq, boost::get<json::Object>(*n).insert({name, json::ValuePtr(new json::Value(json::Array()))}).first->second.get(), _2));
					break;
			}
		}
	}

	void
	Json::ModelTreeIterateRoot(json::Value * n, ModelPartPtr mp)
	{
		if (mp) {
			switch (mp->GetType()) {
				case mpt_Null:
					*n = json::Null();
					return;
				case mpt_Simple:
					mp->GetValue(new JsonValueTarget(*n));
					break;
				case mpt_Complex:
					*n = json::Object();
					if (auto typeId = mp->GetTypeId()) {
						boost::get<json::Object>(*n).insert({"slicer-typeid", json::ValuePtr(new json::Value(*typeId))});
						mp = mp->GetSubclassModelPart(*typeId);
					}
					mp->OnEachChild(boost::bind(&Json::ModelTreeIterate, n, _1, _2));
					break;
				case mpt_Sequence:
					*n = json::Array();
					mp->OnEachChild(boost::bind(&Json::ModelTreeIterate, n, _1, _2));
					break;
				case mpt_Dictionary:
					*n = json::Array();
					mp->OnEachChild(boost::bind(&Json::ModelTreeIterate, n, _1, _2));
					break;
			}
		}
	}

	JsonFile::JsonFile(const boost::filesystem::path & p) :
		path(p)
	{
	}

	void
	JsonFile::Deserialize(ModelPartPtr modelRoot)
	{
		std::ifstream inFile(path.string());
		std::stringstream buffer;
		buffer << inFile.rdbuf();
		Glib::ustring doc(buffer.str());
		Glib::ustring::const_iterator itr = doc.begin();
		json::Value obj = json::parseValue(itr);
		auto mp = modelRoot->GetChild(std::string());
		boost::apply_visitor(DocumentTreeIterate(mp), obj);
	}

	void
	JsonFile::Serialize(ModelPartPtr modelRoot)
	{
		json::Value doc;
		modelRoot->OnEachChild(boost::bind(&Json::ModelTreeIterateRoot, &doc, _2));
		std::ofstream outFile(path.string());
		json::serializeValue(doc, outFile, "utf-8");
	}

	JsonValue::JsonValue(json::Value & v) :
		value(v)
	{
	}

	void
	JsonValue::Deserialize(ModelPartPtr modelRoot)
	{
		auto mp = modelRoot->GetChild(std::string());
		boost::apply_visitor(DocumentTreeIterate(mp), value);
	}

	void
	JsonValue::Serialize(ModelPartPtr modelRoot)
	{
		modelRoot->OnEachChild(boost::bind(&Json::ModelTreeIterateRoot, &value, _2));
	}
}