-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqtjson.hpp
400 lines (320 loc) · 9.89 KB
/
qtjson.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
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
#ifndef QTJSON_H
#define QTJSON_H
#include"reflect.hpp"
#include<QString>
#include<QMap>
#include <QJsonObject>
#include <QJsonDocument>
#include <QStringList>
#include<QJsonArray>
#include<vector>
#include<map>
#include<QDebug>
namespace qtjson {
// 1.===========================对象与json对象的转换=================================================
template <class T>
struct special_traits {
static constexpr bool value = false;
};
template <class T, std::enable_if_t<!reflect::has_member<T>() && !special_traits<T>::value, int> = 0>
QJsonValue objToJson(T const &object) {
return object;
}
template <class T, std::enable_if_t<!reflect::has_member<T>() && special_traits<T>::value, int> = 0>
QJsonValue objToJson(T const &object) {
return special_traits<T>::objToJson(object);
}
template <class T, std::enable_if_t<reflect::has_member<T>(), int> = 0>
QJsonObject objToJson(T const &object) {
QJsonObject root;
reflect::foreach_member(object, [&](const char *key, auto &value) {
root[key] = objToJson(value);
});
return root;
}
template <class T, std::enable_if_t<!reflect::has_member<T>() && !special_traits<T>::value, int> = 0>
T jsonToObj(QJsonValue const &root) {
static_assert(std::is_same<T, T>::value, "Type not supported");
qDebug()<<"不支持转换的类型:"<<root;
return T(); // 返回默认构造的 T 对象
}
template <class T, std::enable_if_t<!reflect::has_member<T>() && special_traits<T>::value, int> = 0>
T jsonToObj(QJsonValue const &root) {
return special_traits<T>::jsonToObj(root);
}
template <class T, std::enable_if_t<reflect::has_member<T>(), int> = 0>
T jsonToObj(QJsonValue const &root) {
T object;
reflect::foreach_member(object, [&](const char *key, auto &value) {
qDebug()<<"jsonToObj==》 key="<<key;
value = jsonToObj<std::decay_t<decltype(value)>>(root[key]);
});
return object;
}
/**
* @brief jsonArrayToVector
* @param arr
* @return
*/
template<class T>
std::vector<T> jsonArrayToVector(QJsonArray & arr){
std::vector<T> v;
for (const QJsonValue &value : arr) {
T t = jsonToObj<T>(value);
v.push_back(t);
}
return v;
}
// 2.============================字符串与json对象的转换=========================================
/**
* QJsonObject -> QString
* @brief jsonToStr
* @param obj
* @return
*/
inline QString jsonToStr(QJsonObject obj) {
// 转换为 QJsonDocument
QJsonDocument doc(obj);
// 将 QJsonDocument 转换为 QByteArray
QByteArray jsonData = doc.toJson(QJsonDocument::Indented); // 使用 Indented 格式化输出
// 将 QByteArray 转换为 QString
return QString::fromUtf8(jsonData);
}
inline QString jsonToStr(QJsonArray array) {
// 创建一个 QJsonDocument,并将 QJsonArray 设置为其根节点
QJsonDocument doc(array);
// 将 QJsonDocument 转换为 QByteArray
QByteArray jsonData = doc.toJson(QJsonDocument::Indented); // 使用 Indented 格式化输出
// 将 QByteArray 转换为 QString
return QString::fromUtf8(jsonData);
}
inline QString jsonToStr(QJsonValue value) {
if(value.isObject()){
return jsonToStr(value.toObject());
}else {
return jsonToStr(value.toArray());
}
}
/**
* QString -> QJsonObject
* @brief strToJson
* @param jsonString
* @return
*/
inline QJsonValue strToJson(QString const &jsonString) {
QJsonParseError parseError;
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toUtf8(), &parseError);
// 检查是否解析成功
if (parseError.error != QJsonParseError::NoError) {
qDebug() << "Parse error at offset" << parseError.offset << ":" << parseError.errorString();
return QJsonObject();
}
if(jsonDoc.isObject()){
// 获取 QJsonObject
return jsonDoc.object();
}else if(jsonDoc.isArray()){
return jsonDoc.array();
}else{
return QJsonValue();
}
}
// 3.==================自定义特化扩展============================================
// 特化 QString
template <>
struct special_traits<QString> {
static constexpr bool value = true;
static QString jsonToObj(QJsonValue const &root) {
if (root.isString()) {
return root.toString();
}
qDebug()<<"转换QString失败:"<<root;
return QString(); // 返回空字符串
}
static QJsonValue objToJson(QString const &object) {
return object;
}
};
// 特化 QJsonArray
template <>
struct special_traits<QJsonArray> {
static constexpr bool value = true;
static QJsonArray jsonToObj(QJsonValue const &root) {
if (root.isArray()) {
qDebug()<<"特化QJsonArray:";
return root.toArray();
}
qDebug()<<"转换QJsonArray失败:"<<root;
return QJsonArray(); // 返回空字符串
}
static QJsonValue objToJson(QJsonArray const &object) {
return object;
}
};
// 特化 int
template <>
struct special_traits<int> {
static constexpr bool value = true;
static int jsonToObj(QJsonValue const &root) {
if (root.isDouble()) {
return root.toInt();
}else if(root.isString()){
return root.toString().toInt();
}
qDebug()<<"转换int失败:"<<root;
return 0; // 返回零
}
static QJsonValue objToJson(int const &object) {
return object;
}
};
// 特化 long
template <>
struct special_traits<long> {
static constexpr bool value = true;
static long jsonToObj(QJsonValue const &root) {
if (root.isDouble()) {
return static_cast<long>(root.toInt());
}
else if(root.isString()){
return root.toString().toLong();
}
qDebug()<<"转换long失败:"<<root;
return 0; // 返回零
}
static QJsonValue objToJson(long const &object) {
return QJsonValue(static_cast<double>(object));
}
};
// 特化 unsigned short
template <>
struct special_traits<unsigned short> {
static constexpr bool value = true;
static unsigned short jsonToObj(QJsonValue const &root) {
if (root.isDouble()) {
return static_cast<int>(root.toInt());
}else if(root.isString()){
return root.toString().toUShort();
}
qDebug()<<"转换unsigned short失败:"<<root;
return 0; // 返回零
}
static QJsonValue objToJson(unsigned short const &object) {
return object;
}
};
// 特化 short
template <>
struct special_traits<short> {
static constexpr bool value = true;
static short jsonToObj(QJsonValue const &root) {
if (root.isDouble()) {
return static_cast<int>(root.toInt());
}else if(root.isString()){
return root.toString().toShort();
}
qDebug()<<"转换short失败:"<<root;
return 0; // 返回零
}
static QJsonValue objToJson( short const &object) {
return object;
}
};
// 特化 bool
template <>
struct special_traits <bool> {
static constexpr bool value = true;
static bool jsonToObj(QJsonValue const &root) {
if (root.isBool()) {
return root.toBool();
}
qDebug()<<"转换bool失败:"<<root;
return false; // 返回 false
}
static QJsonValue objToJson(bool const &object) {
return object;
}
};
// 特化 double
template <>
struct special_traits <double> {
static constexpr bool value = true;
static double jsonToObj(QJsonValue const &root) {
if (root.isDouble()) {
return root.toDouble();
}else if(root.isString()){
return root.toString().toDouble();
}
qDebug()<<"转换double失败:"<<root;
return 0.0; // 返回零
}
static QJsonValue objToJson(double const &object) {
return object;
}
};
// 特化 QJsonObject
template <>
struct special_traits <QJsonObject> {
static constexpr bool value = true;
static QJsonObject jsonToObj(QJsonValue const &root) {
if (root.isObject()) {
return root.toObject();
}
qDebug()<<"转换QJsonObject失败:"<<root;
return QJsonObject(); // 返回空对象
}
static QJsonValue objToJson(QJsonObject const &object) {
return object;
}
};
template <class T, class Alloc>
struct special_traits<std::vector<T, Alloc>> {
static constexpr bool value = true;
static QJsonValue objToJson(std::vector<T, Alloc> const &object) {
QJsonArray root;
for (auto const &elem: object) {
//qDebug()<< elem;
//qDebug()<< qtjson::objToJson(elem);
root.append(qtjson::objToJson(elem));
}
//qDebug()<<root;
return root;
}
static std::vector<T, Alloc> jsonToObj(QJsonValue const &root) {
std::vector<T, Alloc> object;
for (auto const &elem : root.toArray()) {
object.push_back(qtjson::jsonToObj<T>(elem));
}
return object;
}
};
template <class K, class V, class Alloc>
struct special_traits<std::map<K, V, Alloc>> {
static constexpr bool value = true;
static QJsonValue objToJson(std::map<K, V, Alloc> const &object) {
QJsonObject root;
for (auto const &elem: object) {
root[elem.first] = qtjson::objToJson(elem.second);
}
return root;
}
static std::map<K, V, Alloc> jsonToObj(QJsonValue const &root) {
std::map<K, V, Alloc> object;
QJsonObject jsonObject = root.toObject();
for (auto it = jsonObject.begin(); it != jsonObject.end(); ++it) {
const QString &key = it.key();
const QJsonValue &value = it.value();
object[key] = qtjson::jsonToObj<V>(value);
}
return object;
}
};
template <class T>
QString serialize(T const &object) {
return jsonToStr(objToJson(object));
}
template <class T>
T deserialize(QString const &json) {
return jsonToObj<T>(strToJson(json));
}
}
#endif // QTJSON_H