-
Notifications
You must be signed in to change notification settings - Fork 4
/
picojson_serializer.h
336 lines (279 loc) · 10.9 KB
/
picojson_serializer.h
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
/**
* picojson_serializer - a simple serialization solution based on picojson
* --------------------------------------------------------
* Copyright 2013, Dmitry Ledentsov ([email protected])
* Copyright 2014, project contributors
*
* This software is distributed under the MIT License. See notice at the end
* of this file.
*
* This work requires picojson: https://github.com/kazuho/picojson
*/
#pragma once
#include <picojson.h>
#include <string>
namespace picojson {
namespace convert {
/// Incomplete version of is_class
/// \todo Returns `true` for unions
template<typename T>
class is_class {
typedef char TypeForFalse[1];
typedef char TypeForTrue[2];
template<typename U>
static TypeForTrue& func(bool U::*);
template<typename U>
static TypeForFalse& func(...);
public:
enum { value = sizeof(func<T>(0)) == sizeof(TypeForTrue) };
};
/// http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Member_Detector
/// \todo Fails to compile for unions
template<typename T>
class has_json_member
{
struct Fallback { int json; };
template<typename U, bool really_is_class = is_class<U>::value>
struct Derived : U, Fallback { };
template<typename U>
struct Derived<U, false> : Fallback{};
template<typename U, U> struct Check;
typedef char ArrayOfOne[1]; // typedef for an array of size one.
typedef char ArrayOfTwo[2]; // typedef for an array of size two.
template<typename U>
static ArrayOfOne & func(Check<int Fallback::*, &U::json> *);
template<typename U>
static ArrayOfTwo & func(...);
public:
typedef has_json_member type;
enum { value = sizeof(func<Derived<T> >(0)) == 2 };
};
// pre-c++11 enable_if (http://en.cppreference.com/w/cpp/types/enable_if)
template <bool B, class T = void> struct enable_if {};
template <class T> struct enable_if < true, T > { typedef T type; };
/// Type trait: is the type const qualified?
template<typename T>
struct is_const { enum { value = false }; };
template<typename T>
struct is_const < T const > { enum { value = true }; };
/// Type modifier: remove any const qualification
template<typename T>
struct remove_const { typedef T type; };
template<typename T>
struct remove_const < T const > { typedef T type; };
template<typename T>
struct key_value {
std::string key;
T& value;
};
template <typename T>
key_value<T> member(std::string const& k, T& v) {
key_value<T> res = { k, v };
return res;
}
class access {
object o;
public:
template<typename T>
void operator & (key_value<T> kv);
inline std::string serialize() const;
inline value get_value() const;
};
template <typename T>
class write_access {
T& t;
value const& v;
public:
write_access(value const& v_, T& t_);
template<typename TT>
void operator & (key_value<TT> kv);
};
template<typename T, class Enable = void> struct value_converter {
static value to_value(T const& v) {
access a;
json(a, v);
return a.get_value();
}
static value to_value(T& v) {
access a;
json(a, v);
return a.get_value();
}
static void from_value(value const& ov, T& v) {
write_access<T> a(ov, v);
json(a, v);
}
};
/// standard types
template<> struct value_converter < double > {
static value to_value(double v) { return value(v); }
static void from_value(value const& ov, double& v) { if (ov.is<double>()) v = ov.get<double>(); }
};
template<> struct value_converter < std::string > {
static value to_value(std::string const& v) { return value(v); }
static void from_value(value const& ov, std::string& v) { if (ov.is<std::string>()) v = ov.get<std::string>(); }
};
template<> struct value_converter < char const* > {
static value to_value(char const* v) { return value(v); }
};
template<std::size_t n> struct value_converter < char const (&)[n] > {
static value to_value(char const (&v)[n]) { return value(v, n - 1); }
};
template<> struct value_converter < int > {
static value to_value(int v) { return value(static_cast<double>(v)); }
static void from_value(value const& ov, int& v) { if (ov.is<double>()) v = static_cast<int>(ov.get<double>()); }
};
template<> struct value_converter < bool > {
static value to_value(bool v) { return value(v); }
static void from_value(value const& ov, bool& v) { if (ov.is<bool>()) v = ov.get<bool>(); }
};
#ifdef PICOJSON_USE_INT64
template<> struct value_converter<int64_t> {
static value to_value(int64_t v) { return value(v); }
static void from_value(value const& ov, int64_t& v) { if ( ov.is<int64_t>() ) v = ov.get<int64_t>(); }
};
#endif
template <typename T>
value to_value(T const& t) {
return value_converter<T>::to_value(t);
};
template <typename T>
value to_value(T& t) {
return value_converter<T>::to_value(t);
};
template <std::size_t n>
value to_value(char const (&t)[n]) {
return value_converter<char const (&)[n]>::to_value(t);
};
template <typename T>
void from_value(value const& v, T& t) {
value_converter<T>::from_value(v, t);
};
template<typename T>
void access::operator & (key_value<T> kv) {
o[kv.key] = to_value(kv.value);
}
inline std::string access::serialize() const {
return get_value().serialize();
}
inline value access::get_value() const {
return value(o);
}
template <typename T>
write_access<T>::write_access(value const& v_, T& t_) :t(t_), v(v_){}
template <typename T>
template<typename TT>
void write_access<T>::operator & (key_value<TT> kv) {
if (!v.is<picojson::object>())
return;
picojson::object const& o = v.get<picojson::object>();
picojson::object::const_iterator found =
o.find(kv.key);
if (found == o.end())
return;
if (found->second.is<picojson::object>()) {
from_value(found->second, kv.value);
return;
}
if (found->second.is<picojson::array>()) {
from_value(found->second, kv.value);
return;
}
value_converter<TT>::from_value(found->second, kv.value);
}
/// partial specialisations
template<typename T>
struct value_converter < T, typename enable_if<is_const<T>::value>::type >
{
static value to_value(T& v) { return value_converter<typename remove_const<T>::type>::to_value(v); }
};
template <typename T>
struct value_converter < T, typename enable_if< has_json_member<T>::value >::type > {
static value to_value(T const& v) {
access a;
v.json(a);
return a.get_value();
}
static value to_value(T& v) {
access a;
v.json(a);
return a.get_value();
}
static void from_value(value const& ov, T& v) {
write_access<T> a(ov, v);
v.json(a);
}
};
template < typename T>
std::string to_string(T& t) {
return to_value(t).serialize();
}
template < typename T>
void from_string(std::string const& json, T& t) {
picojson::value v;
std::string err;
picojson::parse(v, json.begin(), json.end(), &err);
from_value(v, t);
}
template < typename T>
void init_from_string(std::string const& json, T& t) {
t = T();
from_string(json, t);
}
namespace operators {
template <typename T>
value to_value_c(T const& t) {
return to_value(t);
}
template <typename T>
value to_value_nc(T& t) {
return to_value(t);
}
template<typename T>
T from_value(picojson::value const& v) {
T t = T();
value_converter<T>::from_value(v, t);
return t;
}
template <typename KeyType, typename ValueType>
value to_value_c(std::pair< KeyType, ValueType > const& p) {
picojson::object o;
o["Key"] = value_converter<KeyType>::to_value(p.first);
o["Value"] = value_converter<ValueType>::to_value(p.second);
return value(o);
}
template <typename KeyType, typename ValueType>
value to_value_nc(std::pair< KeyType const, ValueType >& p) {
picojson::object o;
o["Key"] = value_converter<KeyType>::to_value(p.first);
o["Value"] = value_converter<ValueType>::to_value(p.second);
return value(o);
}
}
}
}
/**
* Copyright 2013 Dmitry Ledentsov
* Copyright 2014, project contributors
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/