-
Notifications
You must be signed in to change notification settings - Fork 4
/
es11n.hpp
332 lines (299 loc) · 10.1 KB
/
es11n.hpp
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
/*
es11n -- An easy serialization library for C++..
Copyright (c) 2010-2017 <http://ez8.co> <[email protected]>
This library is released under the MIT License.
Please see LICENSE file or visit https://github.com/ez8-co/es11n for details.
*/
#include "xpjson.hpp"
#include <vector>
#include <list>
#include <deque>
#include <set>
#include <map>
#include <hash_set>
#include <hash_map>
#ifdef __XPJSON_SUPPORT_MOVE__
#include <forward_list>
#include <unordered_set>
#endif
#ifndef _WIN32
#define stdext __gnu_cxx
#endif
#define _AS_ |
#define ES11N_CUSTOM_CTOR_BASE ES11N::custom_ctor_base
#define ES11N_CUSTOM_CTOR_EX(statement) public:\
template<class T>\
static T s11n_custom_ctor() { statement }
#define ES11N_CUSTOM_CTOR(...) ES11N_CUSTOM_CTOR_EX(return T(__VA_ARGS__);)
#define ES11N(members)\
template<class char_t, class T> friend JSON::ValueT<char_t>& ES11N::operator>>(JSON::ValueT<char_t>&, T&);\
template<class char_t, class T> friend JSON::ValueT<char_t>& ES11N::operator<<(JSON::ValueT<char_t>&, T&);\
template<class char_t, class T> friend JSON::ValueT<char_t>& ES11N::operator>>(T&, JSON::ValueT<char_t>&);\
template<class char_t, class T> friend JSON::ValueT<char_t>& ES11N::operator<<(T&, JSON::ValueT<char_t>&);\
template<class char_t> ES11N::Archive<char_t>& serialize(ES11N::Archive<char_t>& json_s11n) { static ES11N::Schema s(#members); json_s11n.schema(s); return json_s11n | members;}\
namespace ES11N
{
using namespace JSON;
struct custom_ctor_base{};
template <typename B, typename D>
struct is_base_of
{
template <typename T>
static char helper(D*, T);
static int helper(B*, int);
struct conv {
operator D*();
operator B*() const;
};
static const bool value = sizeof(helper(conv(), 0)) == 1;
};
template<class T, bool custom = false>
struct s11n_ctor { static T ctor() { return T(); } };
template<class T>
struct s11n_ctor<T, true> { static T ctor() { return T::template s11n_custom_ctor<T>(); } };
struct Schema
{
Schema(const std::string& schema) {
std::string::size_type start = 0, end = 0;
do {
end = schema.find('|', start);
std::string::size_type quote = schema.find('\"', start = schema.find_first_not_of(" \t\r\n*&", start));
_schemas.push_back(quote >= end ?
schema.substr(start, schema.find_last_not_of(" \t\r\n", end - 1) - start + 1) :
schema.substr(quote + 1, schema.rfind('\"', end - 1) - quote - 1));
} while ((start = end + 1));
}
const std::string& index(int i) const { return _schemas[i]; }
private:
std::vector<std::string> _schemas;
};
template<class char_t>
struct Archive
{
Archive(ValueT<char_t>& v, bool s) : _s(s), _index(0), _schema(0), _v(v) {}
void schema(const Schema& s) { _schema = &s; }
const std::string& next() { return _schema->index(_index++); }
ValueT<char_t>& sub() { return _v[next()]; }
ValueT<char_t>& v() { return _v; }
bool store() const { return _s; }
private:
bool _s;
int _index;
const Schema* _schema;
ValueT<char_t>& _v;
};
template<class char_t, size_t M>
Archive<char_t>& operator _AS_(Archive<char_t>& s, const char (&)[M]) { return s; }
template<class char_t, class T>
ValueT<char_t>& operator>>(ValueT<char_t>& in, T& v)
{
Archive<char_t> s(in, true);
v.serialize(s);
return in;
}
template<class char_t, class T>
ValueT<char_t>& operator<<(ValueT<char_t>& out, T& v)
{
Archive<char_t> d(out, false);
v.serialize(d);
return out;
}
template<class char_t, class T>
ValueT<char_t>& operator>>(T& v, ValueT<char_t>& out) { return out << v; }
template<class char_t, class T>
ValueT<char_t>& operator<<(T& v, ValueT<char_t>& in) { return in >> v; }
template<class char_t, class T>
Archive<char_t>& operator|(Archive<char_t>& s, T& v)
{
Archive<char_t> sub(s.sub(), s.store());
v.serialize(sub);
return s;
}
#define S11N_HELPER(spec_type, default_value) \
template<class char_t>\
Archive<char_t>& operator|(Archive<char_t>& s, spec_type& v)\
{\
if(s.store()) {\
v = s.v().template get<spec_type>(s.next(), default_value);\
}\
else {\
s.sub() = v;\
}\
return s;\
}
#define S11N_HELPER_ARITHMETIC(type) S11N_HELPER(type, 0)
S11N_HELPER_ARITHMETIC(unsigned char)
S11N_HELPER_ARITHMETIC(signed char)
#ifdef _NATIVE_WCHAR_T_DEFINED
S11N_HELPER_ARITHMETIC(wchar_t)
#endif /* _NATIVE_WCHAR_T_DEFINED */
S11N_HELPER_ARITHMETIC(unsigned short)
S11N_HELPER_ARITHMETIC(signed short)
S11N_HELPER_ARITHMETIC(unsigned int)
S11N_HELPER_ARITHMETIC(signed int)
#if (defined (__GNUC__) && !defined(__x86_64__) && !defined(__aarch64__)) || (defined(_WIN32) && !defined(_WIN64))
S11N_HELPER_ARITHMETIC(unsigned long)
S11N_HELPER_ARITHMETIC(signed long)
#endif
S11N_HELPER_ARITHMETIC(uint64_t)
S11N_HELPER_ARITHMETIC(int64_t)
S11N_HELPER_ARITHMETIC(float)
S11N_HELPER_ARITHMETIC(double)
S11N_HELPER_ARITHMETIC(long double)
S11N_HELPER_ARITHMETIC(bool)
S11N_HELPER(JSON_TSTRING(char_t), "")
template<class char_t, class T>
void to_s11n(Archive<char_t>& s, ValueT<char_t>& out, T& v)
{
Archive<char_t> sub(out, s.store());
v.serialize(sub);
}
template<class char_t, class T>
void from_s11n(Archive<char_t>& s, ValueT<char_t>& in, T& v)
{
Archive<char_t> sub(in, s.store());
v.serialize(sub);
}
#define S11N_VALUE(type, default_value) \
template<class char_t> \
void to_s11n(Archive<char_t>& s, ValueT<char_t>& out, type& v) { out = v; }\
template<class char_t>\
void from_s11n(Archive<char_t>& s, ValueT<char_t>& in, type& v) { v = in.template get<type>(default_value); }
S11N_VALUE(unsigned char, 0)
S11N_VALUE(signed char, 0)
#ifdef _NATIVE_WCHAR_T_DEFINED
S11N_VALUE(wchar_t, 0)
#endif /* _NATIVE_WCHAR_T_DEFINED */
S11N_VALUE(unsigned short, 0)
S11N_VALUE(signed short, 0)
S11N_VALUE(unsigned int, 0)
S11N_VALUE(signed int, 0)
#if (defined (__GNUC__) && !defined(__x86_64__) && !defined(__aarch64__)) || (defined(_WIN32) && !defined(_WIN64))
S11N_VALUE(unsigned long, 0)
S11N_VALUE(signed long, 0)
#endif
S11N_VALUE(uint64_t, 0)
S11N_VALUE(int64_t, 0)
S11N_VALUE(float, 0)
S11N_VALUE(double, 0)
S11N_VALUE(long double, 0)
S11N_VALUE(bool, 0)
S11N_VALUE(JSON_TSTRING(char_t), "")
#define S11N_VALUE_ARRAY_CONT(spec_type, insert_method) \
template<class char_t, class T, class A>\
void to_s11n(Archive<char_t>& s, ValueT<char_t>& out, spec_type<T, A>& cont)\
{\
for(typename spec_type<T, A>::iterator it = cont.begin(); it != cont.end(); ++it) {\
out.a().push_back(NIL);\
to_s11n(s, out.a().back(), *it);\
}\
}\
template<class char_t, class T, class A>\
void from_s11n(Archive<char_t>& s, ValueT<char_t>& in, spec_type<T, A>& cont)\
{\
if(in.type() == ARRAY) {\
for(int i = 0; i < in.a().size(); ++i) {\
T v = s11n_ctor<T, is_base_of<custom_ctor_base, T>::value>::ctor();\
from_s11n(s, in.a()[i], v);\
cont.insert_method(v);\
}\
}\
}\
template<class char_t, class T, class A>\
Archive<char_t>& operator|(Archive<char_t>& s, spec_type<T, A>& cont)\
{\
if(s.store()) { from_s11n(s, s.sub(), cont); } else { to_s11n(s, s.sub(), cont); }\
return s;\
}
S11N_VALUE_ARRAY_CONT(std::vector, push_back)
S11N_VALUE_ARRAY_CONT(std::deque, push_back)
S11N_VALUE_ARRAY_CONT(std::list, push_back)
S11N_VALUE_ARRAY_CONT(std::set, insert)
S11N_VALUE_ARRAY_CONT(stdext::hash_set, insert)
S11N_VALUE_ARRAY_CONT(std::multiset, insert)
#ifdef __XPJSON_SUPPORT_MOVE__
S11N_VALUE_ARRAY_CONT(std::forward_list, push_back)
S11N_VALUE_ARRAY_CONT(std::unordered_set, insert)
S11N_VALUE_ARRAY_CONT(std::unordered_multiset, insert)
#endif
template<class char_t, class T, size_t M>
void to_s11n(Archive<char_t>& s, ValueT<char_t>& out, T (&arr)[M])
{
for (int i = 0; i < M; ++i) {
out.a().push_back(NIL);
to_s11n(s, out.a().back(), arr[i]);
}
}
template<class char_t, class T, size_t M>
void from_s11n(Archive<char_t>& s, ValueT<char_t>& in, T (&arr)[M])
{
if(in.type() == ARRAY) {
for(int i = 0; i < in.a().size(); ++i) {
from_s11n(s, in.a()[i], arr[i]);
}
}
}
template<class char_t, class T, size_t M>
Archive<char_t>& operator|(Archive<char_t>& s, T (&arr)[M])
{
if(s.store()) { from_s11n(s, s.sub(), arr); } else { to_s11n(s, s.sub(), arr); }
return s;
}
template<class char_t, class T>
void to_s11n(Archive<char_t>& s, ValueT<char_t>& out, T*& ptr)
{
if(!ptr) return; to_s11n(s, out, *ptr);
}
template<class char_t, class T>
void from_s11n(Archive<char_t>& s, ValueT<char_t>& in, T*& ptr)
{
if(!ptr) return; from_s11n(s, in, *ptr);
}
template<class char_t, class T>
Archive<char_t>& operator|(Archive<char_t>& s, T*& ptr)
{
if(!ptr) return s;
if(s.store()) { from_s11n(s, s.sub(), *ptr); } else { to_s11n(s, s.sub(), *ptr); }
return s;
}
#define S11N_VALUE_KV_CONT(spec_type) \
template<class char_t, class K, class V>\
void to_s11n(Archive<char_t>& s, ValueT<char_t>& out, spec_type<K, V> &cont)\
{\
for(typename spec_type<K, V>::iterator it = cont.begin(); it != cont.end(); ++it) {\
ValueT<char_t> k_json;\
to_s11n(s, k_json, const_cast<K&>(it->first));\
JSON_TSTRING(char_t) key;\
k_json.write(key);\
to_s11n(s, out[key], it->second);\
}\
}\
template<class char_t, class K, class V>\
void from_s11n(Archive<char_t>& s, ValueT<char_t>& in, spec_type<K, V> &cont)\
{\
if(in.type() == OBJECT) {\
for(typename ObjectT<char_t>::iterator it = in.o().begin(); it != in.o().end(); ++it) {\
ValueT<char_t> k_json;\
k_json.read(it->first.c_str(), it->first.length());\
K k = s11n_ctor<K, is_base_of<custom_ctor_base, K>::value>::ctor();\
from_s11n(s, k_json, k);\
V v = s11n_ctor<V, is_base_of<custom_ctor_base, V>::value>::ctor();\
from_s11n(s, it->second, v);\
cont.insert(JSON_MOVE(make_pair(k, v)));\
}\
}\
}\
template<class char_t, class K, class V>\
Archive<char_t>& operator|(Archive<char_t>& s, spec_type<K, V> &cont)\
{\
if(s.store()) { from_s11n(s, s.sub(), cont); } else { to_s11n(s, s.sub(), cont); }\
return s;\
}
S11N_VALUE_KV_CONT(std::map)
S11N_VALUE_KV_CONT(std::multimap)
S11N_VALUE_KV_CONT(stdext::hash_map)
#ifdef __XPJSON_SUPPORT_MOVE__
S11N_VALUE_KV_CONT(std::unordered_map)
S11N_VALUE_KV_CONT(std::unordered_multimap)
#endif
}