-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJsonWaxSerializer.h
692 lines (574 loc) · 20.5 KB
/
JsonWaxSerializer.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
#ifndef JSONWAX_SERIALIZER_H
#define JSONWAX_SERIALIZER_H
/* Original author: Nikolai S | https://github.com/doublejim
*
* You may use this file under the terms of any of these licenses:
* GNU General Public License version 2.0 https://www.gnu.org/licenses/gpl-2.0.html
* GNU General Public License version 3 https://www.gnu.org/licenses/gpl-3.0.html
*/
#include <QtGlobal>
#include <QVariantList>
#include <QObject>
#include <QDataStream>
#include <QTextStream>
#include <QMetaObject>
#include <QMetaProperty>
#include <QSize>
#include <QDate>
#include <QTimeZone>
#include <QRect>
#include <QUrl>
#include <QLine>
#ifdef QT_GUI_LIB
#include <QColor>
#endif
#include "JsonWaxEditor.h"
namespace JsonWaxInternals {
// This is a modified QTextStream that's used when serializing / deserializing.
class SpecialTextStream : public QTextStream
{
public:
using QTextStream::QTextStream;
};
// If these were just static, and not thread_local, the program would crash
// when using the Serializer-class simultaneously from separate threads.
static thread_local bool SERIALIZE_TO_EDITOR = false;
static thread_local JsonWaxInternals::Editor* SERIALIZE_EDITOR = nullptr;
static thread_local JsonWaxInternals::Editor* DESERIALIZE_EDITOR = nullptr;
static thread_local QVariantList SERIALIZE_KEYS;
static thread_local QVariantList DESERIALIZE_KEYS;
static thread_local SpecialTextStream READ_STREAM;
static thread_local SpecialTextStream WRITE_STREAM;
class Serializer
{
private:
void prepareEditor()
{
SERIALIZE_TO_EDITOR = false; // This value will change if the type was serialized to the Editor.
SERIALIZE_KEYS = {0};
if (SERIALIZE_EDITOR == nullptr)
SERIALIZE_EDITOR = new JsonWaxInternals::Editor;
else
SERIALIZE_EDITOR->clear();
}
public:
Serializer(){}
~Serializer(){
delete SERIALIZE_EDITOR;
SERIALIZE_EDITOR = nullptr;
}
template <class T>
QString serializeToBytes( const T& input)
{
QByteArray bytes;
QDataStream stream( &bytes, QIODevice::WriteOnly);
//stream.setVersion( QDataStream::Qt_5_7); // <--- Uncomment this line, if you want to lock the
stream << input; // serialization to a specific version of Qt.
return QString( bytes.toBase64());
}
template <class T>
JsonWaxInternals::Editor* serializeToJson( const T& input)
{
prepareEditor();
QString serialized;
SpecialTextStream stream( &serialized, QIODevice::WriteOnly);
stream << input;
// If the input was serialized to editor (fx. a QObject), "serialized" is still empty.
// All the JSON data has been stored in the SERIALIZE_EDITOR.
if (!SERIALIZE_TO_EDITOR)
SERIALIZE_EDITOR->setValue({0}, serialized);
return SERIALIZE_EDITOR;
}
template <class T>
void deserializeBytes( QByteArray serializedBytes, T& outputHere) // If you don't want to create a copy constructor
{ // for your QObject.
QByteArray bytes = QByteArray::fromBase64( serializedBytes);
QDataStream stream( &bytes, QIODevice::ReadOnly);
//stream.setVersion( QDataStream::Qt_5_7); // <--- Uncomment this line, if you want to lock the
stream >> outputHere; // deserialization to a specific version of Qt.
}
template <class T>
T deserializeBytes( QByteArray serializedBytes)
{
QByteArray bytes = QByteArray::fromBase64( serializedBytes);
T result;
QDataStream stream( &bytes, QIODevice::ReadOnly);
//stream.setVersion( QDataStream::Qt_5_7); // <--- Uncomment this line, if you want to lock the
stream >> result; // deserialization to a specific version of Qt.
return result;
}
template <class T>
void deserializeJson( JsonWaxInternals::Editor* editor, const QVariantList& keys, T& output)
{
DESERIALIZE_EDITOR = editor; // QObject uses the data in the DESERIALIZE_EDITOR to set its properties.
DESERIALIZE_KEYS = keys; // The keys are used to locate the properties, if it's a QObject,
// or the location, if it's a QMap or a QList.
QString serializedValue; // serializedValue is used if the serialized data is just a string.
if (editor->isValue( keys)) // In case it's a one-line string value.
serializedValue = editor->value(keys, QString()).toString();
SpecialTextStream stream( &serializedValue, QIODevice::ReadOnly);
stream >> output;
}
};
// =============================== READ/WRITE JSON ENTRIES ===============================
SpecialTextStream& operator >> (SpecialTextStream &stream, QDate &date);
template <class T>
static T readFromDeEditor( QString entryName)
{
QString strValue = DESERIALIZE_EDITOR->value( DESERIALIZE_KEYS + QVariantList{entryName}, QVariant()).toString();
READ_STREAM.setString( &strValue, QIODevice::ReadOnly);
T value;
READ_STREAM >> value;
return value;
}
template <class T>
static void writeToSeEditor( QString entryName, T value)
{
QString strValue;
WRITE_STREAM.setString( &strValue, QIODevice::WriteOnly);
WRITE_STREAM << value;
SERIALIZE_EDITOR->setValue( SERIALIZE_KEYS + QVariantList{ entryName}, strValue);
}
// =============================== TEXTSTREAM OVERLOAD ===============================
// QString. The reason why I overload QString is because by default it only reads back
// until space, newline, etc. when deserializing.
inline SpecialTextStream& operator >> (SpecialTextStream &stream, QString &str)
{
str = stream.readAll(); // Read ALL of the string.
return stream;
}
inline SpecialTextStream& operator << (SpecialTextStream &stream, const QString &str)
{
stream.operator<<(str);
return stream;
}
// QByteArray
inline SpecialTextStream& operator << (SpecialTextStream &stream, const QByteArray &value)
{
stream << QString(value);
return stream;
}
inline SpecialTextStream& operator >> (SpecialTextStream &stream, QByteArray &value)
{
QString sValue;
stream.operator>>(sValue);
value = sValue.toUtf8();
return stream;
}
// Char array
inline SpecialTextStream& operator << (SpecialTextStream &stream, const char charray[])
{
stream << QString(charray); // Technically this is incorrect, but it prevent mojibake
return stream; // when directly serializing a const char array:
// json.serializeToJson({"Test"}, "Alpha Beta 測試 æøå");
}
// QColor (only if project has QT += gui)
#ifdef QT_GUI_LIB
inline SpecialTextStream& operator << (SpecialTextStream &stream, const QColor &color)
{
stream << QString("#");
stream << QString::number( color.alpha(), 16).rightJustified(2, '0');
stream << QString::number( color.red(), 16).rightJustified(2, '0');
stream << QString::number( color.green(), 16).rightJustified(2, '0');
stream << QString::number( color.blue(), 16).rightJustified(2, '0');
return stream;
}
inline SpecialTextStream& operator >> (SpecialTextStream &stream, QColor &color)
{
QString sColor;
stream.operator>>(sColor);
color.setNamedColor( sColor);
return stream;
}
#endif
// QDate
inline SpecialTextStream& operator << (SpecialTextStream &stream, const QDate &date)
{
stream << date.toString(Qt::ISODate);
return stream;
}
inline SpecialTextStream& operator >> (SpecialTextStream &stream, QDate &date)
{
QString sDate;
// stream >> sDate;
stream.operator>>(sDate);
date = QDate::fromString( sDate, Qt::ISODate);
return stream;
}
// QDateTime
inline SpecialTextStream& operator << (SpecialTextStream &stream, const QDateTime &dateTime)
{
SERIALIZE_TO_EDITOR = true;
writeToSeEditor("date", dateTime.date());
writeToSeEditor("time", dateTime.time());
writeToSeEditor("timeSpec", int(dateTime.timeSpec()));
switch(dateTime.timeSpec())
{
case Qt::LocalTime: case Qt::UTC:
// Do nothing more.
break;
case Qt::OffsetFromUTC:
writeToSeEditor("utcOffset", dateTime.offsetFromUtc());
break;
case Qt::TimeZone:
writeToSeEditor("timeZoneId", dateTime.timeZone().id());
break;
}
return stream;
}
inline SpecialTextStream& operator >> (SpecialTextStream &stream, QDateTime &dateTime)
{
QDate date = readFromDeEditor<QDate>("date");
QTime time = readFromDeEditor<QTime>("time");
int timeSpec = readFromDeEditor<int>("timeSpec");
dateTime.setDate(date);
dateTime.setTime(time);
switch(Qt::TimeSpec(timeSpec))
{
case Qt::LocalTime:
// default time spec.
break;
case Qt::UTC:
dateTime.setTimeSpec( Qt::UTC);
break;
case Qt::OffsetFromUTC:
{
int offset = readFromDeEditor<int>("utcOffset");
dateTime.setOffsetFromUtc( offset);
break;
}
case Qt::TimeZone:
{
QByteArray bytesTimeZone = readFromDeEditor<QByteArray>("timeZoneId");
dateTime.setTimeZone( QTimeZone( bytesTimeZone));
}
}
return stream;
}
// int
inline SpecialTextStream& operator << (SpecialTextStream &stream, const int &value)
{
stream << QString::number(value);
return stream;
}
inline SpecialTextStream& operator >> (SpecialTextStream &stream, int &value)
{
QString sValue;
stream.operator>>(sValue);
value = sValue.toInt();
return stream;
}
// QLine
inline SpecialTextStream& operator << (SpecialTextStream &stream, const QLine &line)
{
SERIALIZE_TO_EDITOR = true;
writeToSeEditor("x1", line.x1());
writeToSeEditor("y1", line.y1());
writeToSeEditor("x2", line.x2());
writeToSeEditor("y2", line.y2());
return stream;
}
inline SpecialTextStream& operator >> (SpecialTextStream &stream, QLine &line)
{
int x1 = readFromDeEditor<int>("x1");
int y1 = readFromDeEditor<int>("y1");
int x2 = readFromDeEditor<int>("x2");
int y2 = readFromDeEditor<int>("y2");
line.setLine( x1, y1, x2, y2);
return stream;
}
// QLineF
inline SpecialTextStream& operator << (SpecialTextStream &stream, const QLineF &line)
{
SERIALIZE_TO_EDITOR = true;
writeToSeEditor("x1", line.x1());
writeToSeEditor("y1", line.y1());
writeToSeEditor("x2", line.x2());
writeToSeEditor("y2", line.y2());
return stream;
}
inline SpecialTextStream& operator >> (SpecialTextStream &stream, QLineF &line)
{
qreal x1 = readFromDeEditor<qreal>("x1");
qreal y1 = readFromDeEditor<qreal>("y1");
qreal x2 = readFromDeEditor<qreal>("x2");
qreal y2 = readFromDeEditor<qreal>("y2");
line.setLine( x1, y1, x2, y2);
return stream;
}
// QObject
inline SpecialTextStream& operator << (SpecialTextStream &stream, const QObject &obj)
{
// This stores all the QObject properties in the SERIALIZE_EDITOR.
SERIALIZE_TO_EDITOR = true;
for (int i = 0; i < obj.metaObject()->propertyCount(); ++i)
if (obj.metaObject()->property(i).isStored( &obj))
{
QString name = QString( obj.metaObject()->property(i).name());
QString value = obj.metaObject()->property(i).read( &obj).toString();
SERIALIZE_EDITOR->setValue({0, name}, value);
}
return stream;
}
inline SpecialTextStream& operator >> (SpecialTextStream &stream, QObject &obj)
{
// This inserts property values from a JSON-document into the QObject.
// The DESERIALIZE_EDITOR is a pointer to the main editor in "JsonWax.h".
// We find all the subkeys in that location, see if they are stored
// in the object, and insert them if they are.
for (QVariant& key : DESERIALIZE_EDITOR->keys({ DESERIALIZE_KEYS}))
{
int index = obj.metaObject()->indexOfProperty( key.toString().toUtf8());
if (index == -1)
return stream;
if (obj.metaObject()->property( index).isStored( &obj))
{
QVariant value = DESERIALIZE_EDITOR->value( DESERIALIZE_KEYS + QVariantList{key}, QVariant());
obj.metaObject()->property( index).write( &obj, value);
}
}
return stream;
}
// QPoint
inline SpecialTextStream& operator << (SpecialTextStream &stream, const QPoint &point)
{
SERIALIZE_TO_EDITOR = true;
writeToSeEditor("x", point.x());
writeToSeEditor("y", point.y());
return stream;
}
inline SpecialTextStream& operator >> (SpecialTextStream &stream, QPoint &point)
{
int x = readFromDeEditor<int>("x");
int y = readFromDeEditor<int>("y");
point.setX( x);
point.setY( y);
return stream;
}
// QPointF
inline SpecialTextStream& operator << (SpecialTextStream &stream, const QPointF &point)
{
SERIALIZE_TO_EDITOR = true;
writeToSeEditor("x", point.x());
writeToSeEditor("y", point.y());
return stream;
}
inline SpecialTextStream& operator >> (SpecialTextStream &stream, QPointF &point)
{
qreal x = readFromDeEditor<qreal>("x");
qreal y = readFromDeEditor<qreal>("y");
point.setX( x);
point.setY( y);
return stream;
}
// qreal
inline SpecialTextStream& operator << (SpecialTextStream &stream, const qreal &value)
{
stream << QString::number(value);
return stream;
}
inline SpecialTextStream& operator >> (SpecialTextStream &stream, qreal &value)
{
QString sValue;
stream.operator>>(sValue);
value = sValue.toDouble();
return stream;
}
// QRect
inline SpecialTextStream& operator << (SpecialTextStream &stream, const QRect &rect)
{
SERIALIZE_TO_EDITOR = true;
writeToSeEditor("left", rect.left());
writeToSeEditor("top", rect.top());
writeToSeEditor("right", rect.right());
writeToSeEditor("bottom", rect.bottom());
return stream;
}
inline SpecialTextStream& operator >> (SpecialTextStream &stream, QRect &rect)
{
int left = readFromDeEditor<int>("left");
int top = readFromDeEditor<int>("top");
int right = readFromDeEditor<int>("right");
int bottom = readFromDeEditor<int>("bottom");
rect.setLeft( left);
rect.setTop( top);
rect.setRight( right);
rect.setBottom( bottom);
return stream;
}
// QRectF
inline SpecialTextStream& operator << (SpecialTextStream &stream, const QRectF &rect)
{
SERIALIZE_TO_EDITOR = true;
writeToSeEditor("left", rect.left());
writeToSeEditor("top", rect.top());
writeToSeEditor("right", rect.right());
writeToSeEditor("bottom", rect.bottom());
return stream;
}
inline SpecialTextStream& operator >> (SpecialTextStream &stream, QRectF &rect)
{
qreal left = readFromDeEditor<qreal>("left");
qreal top = readFromDeEditor<qreal>("top");
qreal right = readFromDeEditor<qreal>("right");
qreal bottom = readFromDeEditor<qreal>("bottom");
rect.setLeft( left);
rect.setTop( top);
rect.setRight( right);
rect.setBottom( bottom);
return stream;
}
// QSize
inline SpecialTextStream& operator << (SpecialTextStream &stream, const QSize &size)
{
SERIALIZE_TO_EDITOR = true;
writeToSeEditor("width", size.width());
writeToSeEditor("height", size.height());
return stream;
}
inline SpecialTextStream& operator >> (SpecialTextStream &stream, QSize &size)
{
int width = readFromDeEditor<int>("width");
int height = readFromDeEditor<int>("height");
size.setWidth( width);
size.setHeight( height);
return stream;
}
// QTime
inline SpecialTextStream& operator << (SpecialTextStream &stream, const QTime &time)
{
#if (QT_VERSION >= 0x050800)
stream << time.toString(Qt::ISODateWithMs); // This format is only available from Qt 5.8.
#else
stream << time.toString(Qt::ISODate) << "." << time.msec();
#endif
//}
return stream;
}
inline SpecialTextStream& operator >> (SpecialTextStream &stream, QTime &time)
{
QString value;
stream.operator>>(value);
#if (QT_VERSION >= 0x050800)
time = QTime::fromString( value, Qt::ISODateWithMs); // This format is only available from Qt 5.8.
#else
time = QTime::fromString( value, Qt::ISODate);
#endif
return stream;
}
// QUrl
inline SpecialTextStream& operator << (SpecialTextStream &stream, const QUrl &url)
{
stream << url.toString();
return stream;
}
inline SpecialTextStream& operator >> (SpecialTextStream &stream, QUrl &url)
{
QString strUrl;
stream.operator>>(strUrl);
url = QUrl(strUrl);
return stream;
}
// QVariant
inline SpecialTextStream& operator << (SpecialTextStream &stream, const QVariant &variant)
{
SERIALIZE_TO_EDITOR = true;
writeToSeEditor("type", int(variant.type()));
if (!variant.toString().isEmpty())
writeToSeEditor("value", variant.toString());
return stream;
}
inline SpecialTextStream& operator >> (SpecialTextStream &stream, QVariant &variant)
{
int type = readFromDeEditor<int>({"type"});
QString value = readFromDeEditor<QString>({"value"});;
variant.setValue( value);
variant.convert( QVariant::Type( type));
return stream;
}
// QList
template <class T>
inline SpecialTextStream& operator << (SpecialTextStream &stream, const QList<T>& list)
{
SERIALIZE_TO_EDITOR = true;
for (int i = 0; i < list.count(); ++i)
{
SERIALIZE_KEYS.append(i);
QString value;
SpecialTextStream stream2( &value, QIODevice::WriteOnly);
stream2 << list.at(i); // This can be self-referential.
if (!SERIALIZE_EDITOR->exists( SERIALIZE_KEYS)) // Check here, so that it doesn't overwrite (with an empty value).
SERIALIZE_EDITOR->setValue( SERIALIZE_KEYS, value);
SERIALIZE_KEYS.removeLast();
}
return stream;
}
template <class T>
inline SpecialTextStream& operator >> (SpecialTextStream &stream, QList<T>& list)
{
for (QVariant& key : DESERIALIZE_EDITOR->keys({ DESERIALIZE_KEYS}))
{
DESERIALIZE_KEYS.append( key);
QString strValue = DESERIALIZE_EDITOR->value( DESERIALIZE_KEYS, QVariant()).toString();
SpecialTextStream stream2( &strValue, QIODevice::ReadOnly);
T value;
stream2 >> value; // This can be self-referential.
list.append( value);
DESERIALIZE_KEYS.removeLast();
}
return stream;
}
// QMap
template <class T>
inline SpecialTextStream& operator << (SpecialTextStream &stream, const QMap<QString, T>& map)
{
SERIALIZE_TO_EDITOR = true;
for (const QString& key : map.keys())
{
SERIALIZE_KEYS.append( key);
QString value;
SpecialTextStream stream2( &value, QIODevice::WriteOnly);
stream2 << map.value( key); // This can be self-referential.
if (!SERIALIZE_EDITOR->exists( SERIALIZE_KEYS)) // Check here, so that it doesn't overwrite (with an empty value).
SERIALIZE_EDITOR->setValue( SERIALIZE_KEYS, value);
SERIALIZE_KEYS.removeLast();
}
return stream;
}
template <class T>
inline SpecialTextStream& operator >> (SpecialTextStream &stream, QMap<QString, T>& map)
{
for (const QVariant& key : DESERIALIZE_EDITOR->keys({ DESERIALIZE_KEYS}))
{
DESERIALIZE_KEYS.append( key.toString());
QString strValue = DESERIALIZE_EDITOR->value( DESERIALIZE_KEYS, QVariant()).toString();
SpecialTextStream stream2( &strValue, QIODevice::ReadOnly);
T value;
stream2 >> value; // This can be self-referential.
map.insert( key.toString(), value);
DESERIALIZE_KEYS.removeLast();
}
return stream;
}
// =============================== DATASTREAM OVERLOAD ===============================
// QObject
inline QDataStream& operator << (QDataStream &stream, const QObject &obj)
{
for (int i = 1; i < obj.metaObject()->propertyCount(); ++i)
if (obj.metaObject()->property(i).isStored( &obj))
stream << obj.metaObject()->property(i).read( &obj);
return stream;
}
inline QDataStream& operator >> (QDataStream &stream, QObject &obj)
{
QVariant value;
for (int i = 1; i < obj.metaObject()->propertyCount(); ++i)
if (obj.metaObject()->property(i).isStored( &obj))
{
stream >> value;
obj.metaObject()->property(i).write( &obj, value);
}
return stream;
}
}
#endif // JSONWAX_SERIALIZER_H