summaryrefslogtreecommitdiff
path: root/slicer/json/serializer.cpp
blob: e6ddf819d5256cf309ed01e3ed06661b95856146 (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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "serializer.h"
#include <slicer/metadata.h>
#include <jsonpp.h>
#include <boost/lexical_cast.hpp>
#include <boost/bind.hpp>
#include <stdexcept>
#include <fstream>
#include <glibmm/ustring.h>

NAMEDFACTORY(".js", Slicer::JsonFileSerializer, Slicer::FileSerializerFactory);
NAMEDFACTORY(".js", Slicer::JsonFileDeserializer, Slicer::FileDeserializerFactory);
NAMEDFACTORY(".json", Slicer::JsonFileSerializer, Slicer::FileSerializerFactory);
NAMEDFACTORY(".json", Slicer::JsonFileDeserializer, Slicer::FileDeserializerFactory);
NAMEDFACTORY("application/javascript", Slicer::JsonStreamSerializer, Slicer::StreamSerializerFactory);
NAMEDFACTORY("application/javascript", Slicer::JsonStreamDeserializer, Slicer::StreamDeserializerFactory);
NAMEDFACTORY("application/json", Slicer::JsonStreamSerializer, Slicer::StreamSerializerFactory);
NAMEDFACTORY("application/json", Slicer::JsonStreamDeserializer, Slicer::StreamDeserializerFactory);

namespace Slicer {
	const std::string md_object = "json:object";
	const std::string keyName = "key";
	const std::string valueName = "value";

	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
			{
				if (auto typeIdName = modelPart->GetTypeIdProperty()) {
					auto typeAttrItr = o.find(*typeIdName);
					if (typeAttrItr != o.end() && boost::get<json::String>(typeAttrItr->second.get())) {
						modelPart = modelPart->GetSubclassModelPart(boost::get<json::String>(*typeAttrItr->second));
					}
				}
				modelPart->Create();
				if (metaDataFlagSet(modelPart->GetMetadata(), md_object)) {
					for (const auto & element : o) {
						auto emp = modelPart->GetAnonChild();
						emp->Create();
						auto key = emp->GetChild(keyName);
						auto value = emp->GetChild(valueName);
						key->Create();
						key->SetValue(new JsonValueSource(element.first));
						key->Complete();
						boost::apply_visitor(DocumentTreeIterate(value), *element.second);
						emp->Complete();
					}
				}
				else {
					for (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();
				for (const auto & element : a) {
					auto emp = modelPart->GetAnonChild();
					if (emp) {
						emp->Create();
						boost::apply_visitor(DocumentTreeIterate(emp), *element);
						emp->Complete();
					}
				}
				modelPart->Complete();
			}
		private:
			ModelPartPtr & modelPart;
	};

	void
	JsonSerializer::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
	JsonSerializer::ModelTreeIterateDictObj(json::Value * n, ModelPartPtr mp)
	{
		auto obj = boost::get<json::Object>(n);
		json::Object::key_type k;
		auto v = json::ValuePtr(new json::Value());
		json::Value kv;
		mp->GetChild(keyName)->GetValue(new JsonValueTarget(kv));
		JsonValueSource s(kv);
		s.set(k);
		ModelTreeIterateRoot(v.get(), mp->GetChild(valueName));
		obj->insert({ k, v });
	}

	void
	JsonSerializer::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 typeIdName = mp->GetTypeIdProperty()) {
							if (auto typeId = mp->GetTypeId()) {
								boost::get<json::Object>(*nn).insert({*typeIdName, json::ValuePtr(new json::Value(*typeId))});
								mp = mp->GetSubclassModelPart(*typeId);
							}
						}
						mp->OnEachChild(boost::bind(&JsonSerializer::ModelTreeIterate, boost::get<json::Object>(*n).insert({name, nn}).first->second.get(), _1, _2));
						break;
					}
				case mpt_Sequence:
					mp->OnEachChild(boost::bind(&JsonSerializer::ModelTreeIterateSeq, boost::get<json::Object>(*n).insert({name, json::ValuePtr(new json::Value(json::Array()))}).first->second.get(), _2));
					break;
				case mpt_Dictionary:
					if (metaDataFlagSet(mp->GetMetadata(), md_object)) {
						mp->OnEachChild(boost::bind(&JsonSerializer::ModelTreeIterateDictObj, boost::get<json::Object>(*n).insert({name, json::ValuePtr(new json::Value(json::Object()))}).first->second.get(), _2));
					}
					else {
						mp->OnEachChild(boost::bind(&JsonSerializer::ModelTreeIterateSeq, boost::get<json::Object>(*n).insert({name, json::ValuePtr(new json::Value(json::Array()))}).first->second.get(), _2));
					}
					break;
			}
		}
	}

	void
	JsonSerializer::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 typeIdName = mp->GetTypeIdProperty()) {
						if (auto typeId = mp->GetTypeId()) {
							boost::get<json::Object>(*n).insert({*typeIdName, json::ValuePtr(new json::Value(*typeId))});
							mp = mp->GetSubclassModelPart(*typeId);
						}
					}
					mp->OnEachChild(boost::bind(&JsonSerializer::ModelTreeIterate, n, _1, _2));
					break;
				case mpt_Sequence:
					*n = json::Array();
					mp->OnEachChild(boost::bind(&JsonSerializer::ModelTreeIterateSeq, n, _2));
					break;
				case mpt_Dictionary:
					if (metaDataFlagSet(mp->GetMetadata(), md_object)) {
						*n = json::Object();
						mp->OnEachChild(boost::bind(&JsonSerializer::ModelTreeIterateDictObj, n, _2));
					}
					else {
						*n = json::Array();
						mp->OnEachChild(boost::bind(&JsonSerializer::ModelTreeIterate, n, _1, _2));
					}
					break;
			}
		}
	}

	JsonStreamSerializer::JsonStreamSerializer(std::ostream & s) :
		strm(s)
	{
	}

	JsonStreamDeserializer::JsonStreamDeserializer(std::istream & s) :
		strm(s)
	{
	}

	void
	JsonStreamDeserializer::Deserialize(ModelPartPtr modelRoot)
	{
		json::Value obj = json::parseValue(strm);
		auto mp = modelRoot->GetAnonChild();
		boost::apply_visitor(DocumentTreeIterate(mp), obj);
	}

	void
	JsonStreamSerializer::Serialize(ModelPartPtr modelRoot)
	{
		json::Value doc;
		modelRoot->OnEachChild(boost::bind(&JsonSerializer::ModelTreeIterateRoot, &doc, _2));
		json::serializeValue(doc, strm, "utf-8");
	}

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

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

	void
	JsonFileDeserializer::Deserialize(ModelPartPtr modelRoot)
	{
		std::ifstream inFile(path.string());
		json::Value obj = json::parseValue(inFile);
		auto mp = modelRoot->GetAnonChild();
		boost::apply_visitor(DocumentTreeIterate(mp), obj);
	}

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

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

	JsonValueDeserializer::JsonValueDeserializer(const json::Value & v) :
		value(v)
	{
	}

	void
	JsonValueDeserializer::Deserialize(ModelPartPtr modelRoot)
	{
		auto mp = modelRoot->GetAnonChild();
		boost::apply_visitor(DocumentTreeIterate(mp), value);
	}

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