-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJson.h
410 lines (362 loc) · 14.3 KB
/
Json.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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*================================================================
* Copyright (C) 2022 XUranus All rights reserved.
*
* File: Json.h
* Author: XUranus
* Date: 2022-11-21
* Description: a tiny C++ Json library
*
================================================================*/
#ifndef _XURANUS_MINI_JSON_HEADER_
#define _XURANUS_MINI_JSON_HEADER_
#include <cstddef>
#include <iostream>
#include <stdexcept>
#include <string>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include <list>
#include <map>
#include <unordered_map>
#include <stdarg.h>
/**
* https://www.json.org/json-en.html
*
* JsonElement
* |
* ------------------------------------------
* | | | | |
* null JsonNumber bool JsonObject JsonArray
*
*
* @brief
* add -DLIBRARY_EXPORT build param to export lib on Win32 MSVC
* define LIBRARY_IMPORT before including Json.h to add __declspec(dllimport) to use dll library
* libminijson is linked static by default
*/
#ifdef _WIN32
#ifdef LIBRARY_EXPORT
#define MINIJSON_API __declspec(dllexport)
#else
#ifdef LIBRARY_IMPORT
#define MINIJSON_API __declspec(dllimport)
#else
#define MINIJSON_API
#endif
#endif
#else
#define MINIJSON_API __attribute__((__visibility__("default")))
#endif
#define SERIALIZE_SECTION_BEGIN \
public: \
using __XURANUS_JSON_SERIALIZATION_MAGIC__ = void; \
public: \
void _XURANUS_JSON_CPP_SERIALIZE_METHOD_(xuranus::minijson::JsonObject& object, bool toJson) \
{ \
#define SERIALIZE_SECTION_END \
}; \
#define SERIALIZE_FIELD(KEY_NAME, ATTR_NAME) \
do { \
if (toJson) { \
xuranus::minijson::rules::SerializeTo(object, #KEY_NAME, ATTR_NAME); \
} else { \
xuranus::minijson::rules::DeserializeFrom(object, #KEY_NAME, ATTR_NAME); \
} \
} while (0) \
namespace xuranus {
namespace minijson {
class JsonElement;
class JsonObject;
class JsonArray;
class JsonScanner;
inline void Panic(const char* str, ...)
{
const int PANIC_MESSAGE_MAX = 512;
char message[PANIC_MESSAGE_MAX];
va_list args;
va_start(args, str);
#ifdef _MSC_VER
// supress MSVC warning
vsprintf_s(message, PANIC_MESSAGE_MAX, str, args);
#else
vsprintf(message, str, args);
#endif
va_end(args);
throw std::logic_error(message);
}
// serializable interface
class MINIJSON_API Serializable {
virtual std::string Serialize() const = 0;
};
// base class of json variant
class MINIJSON_API JsonElement: public Serializable {
public:
enum class Type {
JSON_OBJECT,
JSON_ARRAY,
JSON_STRING,
JSON_NUMBER_LONG,
JSON_NUMBER_DOUBLE,
JSON_BOOL,
JSON_NULL
};
union Value {
JsonObject* objectValue;
JsonArray* arrayValue;
std::string* stringValue;
int64_t numberLongValue;
double numberDoubleValue;
bool boolValue;
};
public:
JsonElement();
explicit JsonElement(JsonElement::Type type);
explicit JsonElement(bool value);
explicit JsonElement(double num);
explicit JsonElement(int64_t num);
explicit JsonElement(const std::string &str);
explicit JsonElement(char const *str);
JsonElement(const JsonObject& object);
JsonElement(const JsonArray& array);
JsonElement(const JsonElement& ele);
explicit JsonElement(JsonElement&& ele);
JsonElement& operator = (const JsonElement& ele);
~JsonElement();
bool& AsBool();
double& AsDouble();
int64_t& AsLongInt();
void* AsNull() const;
std::string& AsString();
JsonObject& AsJsonObject();
JsonArray& AsJsonArray();
bool ToBool() const;
double ToDouble() const;
int64_t ToLongInt() const;
void* ToNull() const;
std::string ToString() const;
JsonObject ToJsonObject() const;
JsonArray ToJsonArray() const;
bool IsNull() const;
bool IsBool() const;
bool IsLongInt() const;
bool IsDouble() const;
bool IsString() const;
bool IsJsonObject() const;
bool IsJsonArray() const;
std::string TypeName() const;
std::string Serialize() const override;
private:
Type m_type = Type::JSON_NULL;
Value m_value {};
};
class MINIJSON_API JsonObject: public std::map<std::string, JsonElement>, public Serializable {
public:
std::string Serialize() const override;
};
class MINIJSON_API JsonArray: public std::vector<JsonElement>, public Serializable {
public:
std::string Serialize() const override;
};
class MINIJSON_API JsonParser {
public:
explicit JsonParser(const std::string& str);
~JsonParser();
JsonElement Parse();
bool IsValid();
private:
JsonElement ParseNext();
JsonObject ParseJsonObject();
JsonArray ParseJsonArray();
private:
JsonScanner* m_scanner { nullptr };
};
// use CastFromJsonElement & CastToJsonElement template methods to define some serialization/deserialzation rules
namespace rules {
// cast between struct and JsonElement
template<typename T>
auto CastFromJsonElement(const JsonElement& ele, T& value) -> decltype(typename T::__XURANUS_JSON_SERIALIZATION_MAGIC__()) {
JsonObject object = ele.ToJsonObject();
value._XURANUS_JSON_CPP_SERIALIZE_METHOD_(object, false);
return;
};
template<typename T>
auto CastToJsonElement(JsonElement& ele, const T& value) -> decltype(typename T::__XURANUS_JSON_SERIALIZATION_MAGIC__()) {
JsonObject object {};
T* valueRef = reinterpret_cast<T*>((void*)&value);
valueRef->_XURANUS_JSON_CPP_SERIALIZE_METHOD_(object, true);
ele = JsonElement(object);
return;
};
// cast between std::string and JsonElement
template<typename T, typename std::enable_if<std::is_same<T, std::string>::value>::type* = nullptr>
void CastFromJsonElement(const JsonElement& ele, T& value) {
value = ele.ToString();
return;
}
template<typename T, typename std::enable_if<std::is_same<T, std::string>::value>::type* = nullptr>
void CastToJsonElement(JsonElement& ele, const T& value) {
ele = JsonElement(value);
return;
}
// cast between numeric and JsonElement
template<typename T, typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, bool>::value>::type* = nullptr>
void CastFromJsonElement(const JsonElement& ele, T& value) {
value = static_cast<T>(ele.ToLongInt());
return;
}
template<typename T, typename std::enable_if<std::is_floating_point<T>::value>::type* = nullptr>
void CastFromJsonElement(const JsonElement& ele, T& value) {
value = static_cast<T>(ele.ToDouble());
return;
}
template<typename T, typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, bool>::value>::type* = nullptr>
void CastToJsonElement(JsonElement& ele, const T& value) {
ele = JsonElement(static_cast<int64_t>(value));
return;
}
template<typename T, typename std::enable_if<std::is_floating_point<T>::value>::type* = nullptr>
void CastToJsonElement(JsonElement& ele, const T& value) {
ele = JsonElement(static_cast<double>(value));
return;
}
// cast between bool and JsonElement
template<typename T, typename std::enable_if<std::is_same<T, bool>::value>::type* = nullptr>
void CastFromJsonElement(const JsonElement& ele, T& value) {
value = ele.ToBool();
return;
}
template<typename T, typename std::enable_if<std::is_same<T, bool>::value>::type* = nullptr>
void CastToJsonElement(JsonElement& ele, const T& value) {
ele = JsonElement(value);
return;
}
// cast between std::pair and JsonElement
template<typename T, typename std::enable_if<
std::is_same<T, std::pair<typename T::first_type, typename T::second_type>>::value
>::type* = nullptr>
void CastFromJsonElement(const JsonElement& ele, T& value) {
JsonArray array = ele.ToJsonArray();
if (array.size() < 2) {
return;
}
CastFromJsonElement<typename T::first_type>(array[0], value.first);
CastFromJsonElement<typename T::second_type>(array[1], value.second);
return;
}
template<typename T, typename std::enable_if<
std::is_same<T, std::pair<typename T::first_type, typename T::second_type>>::value
>::type* = nullptr>
void CastToJsonElement(JsonElement& ele, const T& value) {
JsonArray array;
JsonElement firstItemElement;
JsonElement secondItemElement;
CastToJsonElement<typename T::first_type>(firstItemElement, value.first);
CastToJsonElement<typename T::second_type>(secondItemElement, value.second);
array.push_back(firstItemElement);
array.push_back(secondItemElement);
ele = JsonElement(array);
return;
}
// cast between std::vector or std::list and JsonElement
template<typename T, typename std::enable_if<
std::is_same<T, std::vector<typename T::value_type>>::value ||
std::is_same<T, std::list<typename T::value_type>>::value
>::type* = nullptr>
void CastFromJsonElement(const JsonElement& ele, T& value) {
JsonArray array = ele.ToJsonArray();
value.clear();
for (const JsonElement& eleItem: array) {
typename T::value_type t;
CastFromJsonElement<typename T::value_type>(eleItem, t);
value.push_back(t);
}
return;
}
template<typename T, typename std::enable_if<
std::is_same<T, std::vector<typename T::value_type>>::value ||
std::is_same<T, std::list<typename T::value_type>>::value
>::type* = nullptr>
void CastToJsonElement(JsonElement& ele, const T& value) {
JsonArray array;
for (const typename T::value_type& item: value) {
JsonElement itemElement;
CastToJsonElement<typename T::value_type>(itemElement, item);
array.push_back(itemElement);
}
ele = JsonElement(array);
return;
}
// cast between std::map and JsonElement
template<typename T, typename std::enable_if<
std::is_same<std::string, typename T::key_type>::value && (
std::is_same<T, std::map<std::string, typename T::mapped_type>>::value ||
std::is_same<T, std::unordered_map<std::string, typename T::mapped_type>>::value
)>::type* = nullptr>
void CastFromJsonElement(const JsonElement& ele, T& value) {
JsonObject object = ele.ToJsonObject();
value.clear();
for (const std::pair<std::string, JsonElement>& p: object) {
typename T::mapped_type v;
CastFromJsonElement<typename T::mapped_type>(p.second, v);
value[p.first] = v;
}
return;
}
template<typename T, typename std::enable_if<
std::is_same<std::string, typename T::key_type>::value && (
std::is_same<T, std::map<std::string, typename T::mapped_type>>::value ||
std::is_same<T, std::unordered_map<std::string, typename T::mapped_type>>::value
)>::type* = nullptr>
void CastToJsonElement(JsonElement& ele, const T& value) {
JsonObject object;
for (const std::pair<std::string, typename T::mapped_type>& p: value) {
JsonElement valueElement;
CastToJsonElement<typename T::mapped_type>(valueElement, p.second);
object[p.first] = valueElement;
}
ele = JsonElement(object);
return;
}
template<typename T>
void SerializeTo(JsonObject &object, const std::string& key, const T& field)
{
JsonElement ele {};
CastToJsonElement<T>(ele, field);
object[key] = ele;
}
template<typename T>
void DeserializeFrom(const JsonObject& object, const std::string& key, T& field)
{
JsonElement ele = object.find(key)->second;
CastFromJsonElement<T>(ele, field);
}
}
// utils used for user to do serialization and deserialzation
namespace util {
template<typename T>
auto Serialize(const T& value) -> decltype(typename T::__XURANUS_JSON_SERIALIZATION_MAGIC__(), std::string());
template<typename T>
auto Deserialize(const std::string& jsonStr, T& value) -> decltype(typename T::__XURANUS_JSON_SERIALIZATION_MAGIC__());
}
// util template function implement
template<typename T>
auto util::Serialize(const T& value) -> decltype(typename T::__XURANUS_JSON_SERIALIZATION_MAGIC__(), std::string())
{
JsonObject object {};
auto valuePtr = const_cast<typename std::remove_const<T>::type*>(&value);
valuePtr->_XURANUS_JSON_CPP_SERIALIZE_METHOD_(object, true);
return object.Serialize();
}
template<typename T>
auto util::Deserialize(const std::string& jsonStr, T& value) -> decltype(typename T::__XURANUS_JSON_SERIALIZATION_MAGIC__())
{
JsonParser parser(jsonStr);
JsonElement ele = parser.Parse();
JsonObject object = ele.AsJsonObject();
value._XURANUS_JSON_CPP_SERIALIZE_METHOD_(object, false);
}
}
}
#endif