-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.html
999 lines (996 loc) · 29.5 KB
/
func.html
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
<!DOCTYPE html>
<html lang="en">
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
<title>
JsonWax for Qt
</title>
<link href='css/style.css' rel='stylesheet'>
</head>
<body>
<h1>
JsonWax for Qt
</h1>
<h2>
NOW WITH SERIALIZATION
</h2>
<div id='blackspace'></div>
<svg height='7.5em' id='SVG' viewBox='20 20 50 50' width='100%'>
<text fill='red' id='jsonwaxtext' transform='translate(420, -57) rotate(45) scale(1.2)'>
<tspan dy='1em' x='-1.1em'>
------
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-2.1em'>
---
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-3.1em'>
------------
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-4.1em'>
----
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-5.1em'>
-------------
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-6.1em'>
----------
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-7.1em'>
-----------
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-8.1em'>
--------
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-9.1em'>
------------
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-10.1em'>
-------
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-11.1em'>
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-12.1em'>
---------
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-13.1em'>
-----------
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-14.1em'>
--
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-15.1em'>
--------------
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-16.1em'>
----------
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-17.1em'>
--------
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-18.1em'>
----
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-19.1em'>
---------
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-20.1em'>
----
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-21.1em'>
--
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-22.1em'>
----------
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-23.1em'>
---------
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-24.1em'>
-------------
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-25.1em'>
-
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-26.1em'>
----------
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-27.1em'>
-----------
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-28.1em'>
------------
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-29.1em'>
----------
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-30.1em'>
--------------
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-31.1em'>
-------
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-32.1em'>
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-33.1em'>
--------
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-34.1em'>
-----
JsonWax JsonWax JsonWax
</tspan>
<tspan dy='1em' x='-35.1em'>
JsonWax JsonWax JsonWax
</tspan>
</text>
</svg>
<div id='menucontainer'>
<div id='menu'>
<div class='MENU_ELEMENT' id='menu_element0'>
<a href='index.html'>
WHAT IS IT?
</a>
</div>
<div class='MENU_ELEMENT' id='menu_element1'>
<a href='start.html'>
GETTING STARTED
</a>
</div>
<div class='MENU_ELEMENT' id='menu_element2'>
<a href='examples.html'>
EXAMPLES
</a>
</div>
<div class='MENU_ELEMENT' id='menu_element3'>
<a href='func.html'>
FUNCTIONALITY
</a>
</div>
<div class='MENU_ELEMENT' id='menu_element4'>
<a href='behav.html'>
BEHAVIOR
</a>
</div>
</div>
</div>
<div id='content'>
<nav>
<i>
functions:<br>
</i>
<a href='func.html#append'>
append
</a>
<a href='func.html#copy'>
copy
</a>
<a href='func.html#deserializeBytes'>
deserializeBytes
</a>
<a href='func.html#deserializeJson'>
deserializeJson
</a>
<a href='func.html#errorCode'>
errorCode
</a>
<a href='func.html#errorMsg'>
errorMsg
</a>
<a href='func.html#errorPos'>
errorPos
</a>
<a href='func.html#exists'>
exists
</a>
<a href='func.html#fromByteArray'>
fromByteArray
</a>
<a href='func.html#isArray'>
isArray
</a>
<a href='func.html#isNullValue'>
isNullValue
</a>
<a href='func.html#isObject'>
isObject
</a>
<a href='func.html#isValue'>
isValue
</a>
<a href='func.html#keys'>
keys
</a>
<a href='func.html#loadFile'>
loadFile
</a>
<a href='func.html#move'>
move
</a>
<a href='func.html#popFirst'>
popFirst
</a>
<a href='func.html#popLast'>
popLast
</a>
<a href='func.html#prepend'>
prepend
</a>
<a href='func.html#remove'>
remove
</a>
<a href='func.html#save'>
save
</a>
<a href='func.html#saveAs'>
saveAs
</a>
<a href='func.html#serializeToBytes'>
serializeToBytes
</a>
<a href='func.html#serializeToJson'>
serializeToJson
</a>
<a href='func.html#setEmptyArray'>
setEmptyArray
</a>
<a href='func.html#setEmptyObject'>
setEmptyObject
</a>
<a href='func.html#setNull'>
setNull
</a>
<a href='func.html#setValue'>
setValue
</a>
<a href='func.html#size'>
size
</a>
<a href='func.html#toString'>
toString
</a>
<a href='func.html#type'>
type
</a>
<a href='func.html#value'>
value
</a>
</nav>
<div id='articles'>
<article class='wide'>
<h3>
FUNCTIONALITY
</h3>
<p>
Here is all that you can do in JsonWax!<br>
</p>
</article>
<article class='wide'>
<h4 id='append'>
int append(const QVariantList& keys, const QVariant& value)
</h4>
<p>
Appends a value to the end of the JSON-array found at the location.
If there's no array at the location, the location's data is removed, and an array is created in its place, containing <em>value</em>.
</p>
</article>
<article class='wide'>
<h4 id='copy'>
void copy(const QVariantList& keysFrom, const QVariantList& keysTo)
</h4>
<p>
Copies the data at a location, to another location.
This function cannot enter a recursive loop, since the data is first copied
to a temporary location, before it overwrites the data at the destination.
</p>
</article>
<article class='wide'>
<h4>
void copy(const QVariantList& keysFrom, JsonWax& jsonTo, const QVariantList& keysTo)
</h4>
<p>
Copies the data at a location in this JsonWax-object, to a location in another JsonWax-object.
</p>
</article>
<article class='wide'>
<h4 id='deserializeBytes'>
T deserializeBytes(const QVariantList& keys, const T defaultValue = T())
</h4>
<p>
Deserialize the Base64-encoded object located at <em>keys</em>.
Use like this:
</p>
<div class='code'>
<div class='source'>
QColor color = deserializeBytes<QColor>({0,"serializedColor"});
</div>
</div>
<p>
See also serializeToBytes().
</p>
</article>
<article class='wide'>
<h4>
void deserializeBytes(T& outputHere, const QVariantList& keys)
</h4>
<p>
Deserialize the Base64-encoded object located at <em>keys</em>.
The data will be stored in the provided variable - this is useful if you are outputting
to a QObject, since it doesn't have a default copy constructor (and you don't need to write
<Type>).<br>
Use like this:
</p>
<div class='code'>
<div class='source'>
QColor color;<br>
qDebug() << "old color:" << color;<br>
json.deserializeBytes( color, {0,"serializedColor"});<br>
qDebug() << "new color:" << color;
</div>
</div>
<p>
See also serializeToBytes().
</p>
</article>
<article class='wide'>
<h4 id='deserializeJson'>
T deserializeJson(const QVariantList& keys, T defaultValue = T())
</h4>
<p>
Deserialize the 'serialized-to-JSON' object located at <em>keys</em>.<br>
Use like this:
</p>
<div class='code'>
<div class='source'>
QDateTime datetime = json.deserializeJson<QDateTime>({0, "date1});
</div>
</div>
<p>
See also serializeToJson().
</p>
</article>
<article class='wide'>
<h4>
void deserializeJson(T& outputHere, const QVariantList& keys)
</h4>
<p>
Deserialize the 'serialized-to-JSON' object located at <em>keys</em>, and store
it in the provided variable.
Like mentioned in the second <em>deserializeBytes()</em>-function, this can be useful for
deserializing QObjects, since they don't have copy constructors.
</p>
<div class='code'>
<div class='source'>
QDateTime datetime;<br>
qDebug() << "old:" << datetime;<br>
json.deserializeJson( datetime, {0, "date1"});<br>
qDebug() << "new:" << datetime;
</div>
</div>
<p>
See also serializeToJson().
</p>
</article>
<article class='wide'>
<h4 id='errorCode'>
int errorCode()
</h4>
<p>
Returns the integer code for the last parsing (0 means that it was OK).
</p>
</article>
<article class='wide'>
<h4 id='errorMsg'>
QString errorMsg()
</h4>
<p>
Returns the error message for the latest <em>errorCode</em>.
</p>
</article>
<article class='wide'>
<h4 id='errorPos'>
int errorPos()
</h4>
<p>
Returns the position of the last parser error (character number).
(I'm not sure that the position is completely accurate)<br>
Returns -1 if the last parsing was without error.
</p>
</article>
<article class='wide'>
<h4 id='exists'>
bool exists(const QVariantList& keys)
</h4>
<p>
Returns true if the location exists, and false if it doesn't. <em>exists({})</em> always returns true, since {} is the root element.
</p>
<div class='code'>
<div class='source'>
JsonWax json;<br>
json.fromByteArray("{\"A\":{\"B\":\"cow\"}}");<br>
qDebug() << json.exists({"A"});<br>
qDebug() << json.exists({"A","B"});<br>
qDebug() << json.exists({"A","B","cow"});<br>
</div>
</div>
<p>
It returns:
</p>
<div class='code'>
<div class='json'>
true<br>
true<br>
false
</div>
</div>
</article>
<article class='wide'>
<h4 id='fromByteArray'>
bool fromByteArray(const QByteArray& bytes)
</h4>
<p>
Loads a JSON-document from a <em>QByteArray</em>. To load a document from a <em>QString</em> do this:<br>
</p>
<div class='code'>
<div class='source'>
JsonWax json;<br>
json.fromByteArray( jsonString.toUtf8());
<br>
</div>
</div>
<p>
The function returns true if the string is a valid JSON-document, and false if it isn't.
See also <em>loadFile()</em>.
</p>
</article>
<article class='wide'>
<h4 id='isArray'>
bool isArray( const QVariantList& keys)
</h4>
<p>
Returns true if the location is an Array, and false if it isn't.
</p>
</article>
<article class='wide'>
<h4 id='isNullValue'>
bool isNullValue( const QVariantList& keys)
</h4>
<p>
Returns true if the location is a value with value "null", and false if it's anything else.
</p>
</article>
<article class='wide'>
<h4 id='isObject'>
bool isObject( const QVariantList& keys)
</h4>
<p>
Returns true if the location is an Object, and false if it isn't.
</p>
</article>
<article class='wide'>
<h4 id='isValue'>
bool isValue( const QVariantList& keys)
</h4>
<p>
Returns true if the location is a Value, and false if it isn't.
</p>
</article>
<article class='wide'>
<h4 id='keys'>
QVariantList keys(const QVariantList& keys)
</h4>
<p>
Returns a list of all keys found at the location.
If the location is an array, the returned <em>QList</em> contains ints, which are the existing array positions.
If the location is a map, the command returns the keys in that map.
</p>
</article>
<article class='wide'>
<h4 id='loadFile'>
bool loadFile(const QString& fileName)
</h4>
<p>
Loads a JSON-document from file. If the function returns <em>true</em>, the file existed, and was a valid
JSON-document. If it returns false, the file didn't exist or the document was invalid. <em>loadFile()</em> can
be used when constructing an object:<br>
</p>
<div class='code'>
<div class='source'>
JsonWax json ("jsonFile.json");
</div>
</div>
<p>
If you use the above, you can't know whether the document was valid or invalid. Instead you
could do:
</p>
<div class='code'>
<div class='source'>
JsonWax json;<br>
if (json.loadFile("jsonFile.json"))<br>
qDebug() << "valid document";<br>
else<br>
qDebug() << "invalid document";
</div>
</div>
<p>
It is tested whether the input is a relative or an absolute path, and the file is loaded if
it exists. So you can do both:
</p>
<div class='code'>
<div class='source'>
JsonWax json;<br>
json.loadFile("subfolder/jsonFile.json");<br>
</div>
</div>
<p>
and<br>
</p>
<div class='code'>
<div class='source'>
JsonWax json;<br>
json.loadFile("c:/jsonFile.json");<br>
</div>
</div>
</article>
<article class='wide'>
<h4 id='move'>
void move(const QVariantList& keysFrom, const QVariantList& keysTo)
</h4>
<p>
Moves data from one location to another. This function is very efficient,
since it just moves a pointer from A to B.
</p>
</article>
<article class='wide'>
<h4>
void move(const QVariantList& keysFrom, JsonWax& jsonTo, const QVariantList& keysTo)
</h4>
<p>
Moves data from one location in this JsonWax-object, to a location in another JsonWax-document.
</p>
</article>
<article class='wide'>
<h4 id='popFirst'>
void popFirst(const QVariantList& keys, int removeTimes = 1)
</h4>
<p>
Removes the first value from the array found at the location,
decreasing its size by one.
If it's not an array nothing will happen.
</p>
</article>
<article class='wide'>
<h4 id='popLast'>
void popLast(const QVariantList& keys, int removeTimes = 1)
</h4>
<p>
Removes the last value from the array found at the location,
decreasing its size by one.
If it's not an array nothing will happen.
</p>
</article>
<article class='wide'>
<h4 id='prepend'>
void prepend(const QVariantList& keys, const QVariant& value)
</h4>
<p>
Prepends a value to the array found at the location. If it's not an array,
the data at the location is removed, and an array is created.
</p>
</article>
<article class='wide'>
<h4 id='remove'>
void remove(const QVariantList& keys)
</h4>
<p>
Removes what exists at a location, and all of its sub-keys and values. If the location doesn't exist, nothing happens.
An empty <em>keys-list</em> removes everything. If the last key is an int, it removes
an element from the QList, reducing its size by 1.<br>
If array positions are important
to your data structure (and you insist on using ints in your <em>keys-list</em>), instead
of using <em>remove()</em>, you could use <em>setNull(keys)</em>, which would keep your list
at the same size. An alternative to this situation is to only use strings to reference locations
within the document.
</p>
</article>
<article class='wide'>
<h4 id='save'>
bool save(StringStyle style = Readable, bool convertToCodePoints = false)
</h4>
<p>
Saves the document to the file which it was loaded from. If the document wasn't loaded from a file,
an error will be returned, and the document won't be saved.
Like <em>saveAs()</em> it returns true if all bytes were written to the file, and false if they weren't.
See also <em>saveAs()</em>.
</p>
</article>
<article class='wide'>
<h4 id='saveAs'>
bool saveAs(const QString& fileName, StringStyle style = Readable, bool convertToCodePoints = false, bool overwriteAllowed = true)
</h4>
<p>
<em>saveAs()</em> converts the document to a <em>QString</em>, and writes the document to a file.
If you've loaded the document from a file using <em>loadFile()</em>, you can use <em>save()</em> instead,
to overwrite the loaded document, without specifying a name.<br>
Keys within a JSON-object are always output in alphabetical order. Lists are always kept in the same order.
<em>style</em> can be either <em>JsonWax::Compact</em> or <em>JsonWax::Readable</em>, and determines whether the output will
be a JSON-document without spaces and newlines, or if it will be nicely formatted.<br>
<em>convertToCodePoints</em> converts unicode characters to the \uXXXX notation.
</p>
</article>
<article class='wide'>
<h4 id='serializeToBytes'>
void serializeToBytes( const QVariantList& keys, const T& object)
</h4>
<p>
Serialize the given object as a Base64-encoded byte array. It becomes
an unreadable string which can be deserialized back to an object.
It would be much more efficient to write and read an object as a regular
byte array, and the data is unreadable, which is why many would probably call this function an abomination. I recommend that you generally use <em>serializeToJson()</em>,
since the object then becomes readable, editable strings. However, serializeToBytes is much faster than serializeToJson.
<em>serializeToBytes</em> can be very useful to store fx. small pixmaps.
It also works for QObjects.<br><br>
Don't store large amounts of data with this function. Instead, do something more efficient.
</p>
</article>
<article class='wide'>
<h4 id='serializeToJson'>
void serializeToJson( const QVariantList& keys, const T& object)
</h4>
<p>
Serialize the given object as a JSON-Object or string, and store it at the specified location.<br><br>
It's currently working for these data types:<br><em>QColor, QDate, QDateTime, QLine, QLineF, QList, QMap, QObject, QPoint, QPointF, QRect, QRectF, QSize, QTime, QUrl, QVariant.</em><br><br>
The QMap must be JSON-compatible, so it is required to have QString as the key: QMap<QString,T>.<br>
The QLists and QMaps may be arbitrarily nested.<br>
Use <em>deserializeJson()</em> to deserialize back to an object.<br><br>
Here's an example where a QDateTime object is serialized to JSON, and then immediately deserialized.
</p>
<div class='code'>
<div class='source'>
QDateTime datetime = QDateTime::currentDateTimeUtc();<br>
JsonWax json;<br>
json.serializeToJson({0}, datetime);<br>
datetime = json.deserializeJson<QDateTime>({0});<br>
qDebug() << datetime;<br>
json.saveAs("SerializerTest.json");
</div>
</div>
<p>
The above code outputs the current UTC date and time to console:
</p>
<div class='code'>
<div class='json'>
QDateTime(2017-05-26 15:59:05.131 UTC Qt::TimeSpec(UTC))
</div>
</div>
<p>
And here are the contents of SerializerTest.json:
</p>
<div class='code'>
<div class='json'>
[<br>
{<br>
"date": "2017-05-26",<br>
"time": "15:59:05.131",<br>
"timeSpec": "1"<br>
}<br>
]
</div>
</div>
<p>
That's it! With one line of code you can serialize a number of Qt data types directly to JSON.<br><br>
Make your class inherit from QObject, in order to make it serializable (to a limit).
</p>
<div class='code'>
<div class='source'>
class SerializerClass1 : public QObject<br>
{<br>
Q_OBJECT<br>
Q_PROPERTY(QString name MEMBER m_name)<br>
Q_PROPERTY(int superNumber MEMBER m_number)<br>
Q_PROPERTY(Enume1 walker MEMBER m_enume1)<br>
Q_PROPERTY(QDate bestDate MEMBER m_date)<br>
Q_PROPERTY(QColor coolColor MEMBER m_color)<br>
Q_PROPERTY(QImage imageAttempt MEMBER m_image) // Can't save its data.<br>
Q_ENUMS(Enume1) // Required to store the enum.<br>
public:<br>
SerializerClass1( QObject* parent = 0): QObject(parent){}<br>
enum Enume1 {Crab, Spider};<br>
private:<br>
QString m_name = "One\nWonderful Name";<br>
int m_number = 1900000;<br>
Enume1 m_enume1 = Crab;<br>
QDate m_date = QDate(1950,10,10);<br>
QColor m_color = QColor(15,16,16,255);<br>
QImage m_image = QImage(512,512,QImage::Format_Mono);<br>
};
</div>
</div>
<p>
All of the variables specified using Q_PROPERTY(T name MEMBER variable_name)
will be saved. The serialization in a QObject is independent from non-QObject
JsonWax serialization, which explains the differences in supported data types and in the serialized data.<br>
If, for instance, JsonWax serializes a QTime object directly, it will include ms, whereas
in QObject serialization it will only include hh:mm:ss.<br>
See below how to serialize and deserialize a SerializerClass1 object:
</p>
<div class='code'>
<div class='source'>
SerializerClass1 object;<br>
JsonWax json;<br>
json.serializeToJson( {}, object);<br>
json.deserializeJson( object, {});<br>
json.saveAs("SerializerTest.json");
</div>
</div>
<p>
The contents of "SerializerTest.json" are:
</p>
<div class='code'>
<div class='json'>
{<br>
"bestDate": "1950-10-10",<br>
"coolColor": "#0f1010",<br>
"imageAttempt": "",<br>
"name": "One\nWonderful Name",<br>
"objectName": "",<br>
"superNumber": "1900000",<br>
"walker": "0"<br>
}
</div>
</div>
<p>
The "objectName" is part of the <em>QObject</em>. Please don't store a variable under that name. Or under the name of any other variable.<br>
Even though <em>QObject</em> supports calling Properties the same name, JsonWax serialization doesn't support that.
</p>
</article>
<article class='wide'>
<h4 id='setEmptyArray'>
void setEmptyArray( const QVariantList& keys)
</h4>
<p>
Turns the location into an empty array.
</p>
</article>
<article class='wide'>
<h4 id='setEmptyObject'>
void setEmptyObject( const QVariantList& keys)
</h4>
<p>
Turns the location into an empty object.
</p>
</article>
<article class='wide'>
<h4 id='setNull'>
void setNull( const QVariantList& keys)
</h4>
<p>
Sets the value of the location to null.
</p>
<div class='code'>
<div class='source'>
JsonWax json;<br>
json.setValue({"A", "B"}, "this");<br>
json.setValue({"A", "C"}, "that");<br>
qDebug() << "1:" << json.toString(JsonWax::Compact);<br>
json.setNull({"A"});<br>
qDebug() << "2:" << json.toString(JsonWax::Compact);<br>
</div>
</div>
<p>
This returns:
</p>
<div class='code'>
<div class='json'>
1: "{\"A\":{\"B\":\"this\",\"C\":\"that\"}}"<br>
2: "{\"A\":null}"
</div>
</div>
<p>
You can check whether a value is null by:
</p>
<div class='code'>
<div class='source'>
JsonWax json;<br>
json.setValue({"A", "X"}, "this");<br>
json.setNull({"A"});<br>
qDebug() << json.value({"A"}).isNull();<br>
qDebug() << json.value({"B"}).isNull();<br>
</div>
</div>
<div class='code'>
<div class='json'>
true<br>
true
</div>
</div>
<p>
Notice that the existing null, and the non-existing value are <em>both</em> null.
</p>
</article>
<article class='wide'>
<h4 id='setValue'>
void setValue(const QVariantList& keys, const QVariant& value)
</h4>
<p>
Stores the <em>value</em> at the keys-location. This overwrites
anything that already exists at that location, or creates the value.<br>Example:<br>
</p>
<div class='code'>
<div class='source'>
JsonWax json;<br>
json.setValue({"this","is","my","place"}, 1565);<br>
json.setValue({"this","is","my","place"}, 12.535);<br>
json.setValue({"this","is","my","document"}, "friendship");<br>
json.setValue({"this","is","my","area"}, false);<br>
json.setValue({"this","is","my","life"}, QVariant());<br>
json.setValue({"this","is","my","area"}, true;
</div>
</div>
<p>
which gives the JSON-document:<br>
</p>
<div class='code'>
<div class='json'>
{<br>
"this": {<br>
"is": {<br>
"my": {<br>
"area": true,<br>
"document": "friendship",<br>
"life": null,<br>
"place": 12.535<br>
}<br>
}<br>
}<br>
}
</div>
</div>
<p>
See also <em>setNull()</em>.
</p>
</article>
<article class='wide'>
<h4 id='size'>
int size(const QVariantList& keys = {})
</h4>
<p>
Returns the number of keys found at the location, no matter if it's an Object or an Array.
Returns 1 if the location is a JsonValue, and returns -1 if the location doesn't exist.
Use <em>size({})</em> or just <em>size()</em> to get the size of the root Object or Array.
</p>
<div class='code'>
<div class='source'>
JsonWax json;<br>
json.setValue({"A", "B"}, "this");<br>
json.setValue({"C", "D"}, "thing");<br>
json.setValue({"E", 0}, "is");<br>
json.setValue({"E", 1}, "a");<br>
json.setValue({"E", 2}, "thing");<br>
json.setValue({"E", 3}, ".");<br>
qDebug() << "object:" << json.size({});<br>
qDebug() << "array:" << json.size({"E"});
</div>
</div>
<p>
Which outputs:
</p>
<div class='code'>
<div class='json'>
object: 3<br>
array: 4
</div>
</div>
</article>
<article class='wide'>
<h4 id='toString'>
QString toString(StringStyle style = Readable, bool convertToCodePoints = false, const QVariantList& keys = {})
</h4>
<p>
This function converts the data in the JsonWax-object into a <em>QString</em>.
If <em>keys</em> isn't changed from its default, the function outputs the entire document as string.
With <em>keys</em> you can choose to stringify just part of the document.<br>
If you attempt to turn a non-existing location, or a value, into a string, this function returns "{}".
Therefore, the result is always a valid JSON-document.<br>
See also <em>save()</em> and <em>saveAs()</em>.
</p>
</article>
<article class='wide'>
<h4 id='type'>
Type type(const QVariantList& keys)
</h4>
<p>
Returns the type of the location. The Type can be either: Value, Object, Array, Null.<br>
An entry with value "null" is considered a Value; it's only considered Null if it doesn't exist.
</p>
</article>
<article class='wide'>
<h4 id='value'>
QVariant value(const QVariantList& keys, const QVariant& defaultValue = QVariant())
</h4>
<p>
Returns the <em>QVariant</em> found by following the sequence of keys. You can check if a value is null by
using <em>isNull()</em> on the returned value. <em>isNull()</em> returns <em>true</em> both when a value is <em>null</em> in the JSON-document, and when it doesn't exist at all.<br>
You can set a default value, which will be returned if the location isn't a value, or
the keys-list is empty.<br>
When you use a number as a key, remember that 0 is the first element in the Array.
</p>
<div class='code'>
<div class='source'>
JsonWax json;<br>
json.setValue({"alpha","beta","gamma","delta"}, "ding");<br>
json.setValue({"alpha","beta","gamma","echo", 4}, "location");<br>
qDebug() << json.value({"alpha","beta","gamma","delta"}).toString();<br>
qDebug() << json.value({"alpha","beta","gamma","echo",4}).toString();<br>
qDebug() << json.value({"trout"}, "no trout").toString();<br>
json.saveAs("output.json");
</div>
</div>
<p>
qDebug() outputs:
</p>
<div class='code'>
<div class='json'>
"ding"<br>
"location"<br>
"no trout"
</div>
</div>
<p>
And "output.json" looks like this:
</p>
<div class='code'>
<div class='json'>
{<br>
"alpha": {<br>
"beta": {<br>
"gamma": {<br>
"delta": "ding",<br>
"echo": [<br>
null,<br>
null,<br>
null,<br>
null,<br>
"location"<br>
]<br>
}<br>
}<br>
}<br>
}
</div>
</div>
</article>
</div></div>
<footer></footer>
</body></html>