-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.html
688 lines (685 loc) · 17.2 KB
/
start.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
<!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>
GETTING STARTED
</h3>
<p>
Download the newest version from the GitHub repository.<br><br>
</p>
<a href='https://github.com/doublejim/JsonWax/'>
Github Repository
</a>
<p>
<br>You'll need 4 files:<br> JsonWax.h, JsonWaxEditor.h, JsonWaxParser.h & JsonWaxSerializer.h<br>
Put all 4 files in your Qt-project folder. You'll only need to:
</p>
<div class='code'>
<div class='source'>
#include "JsonWax.h"<br>
</div>
</div>
<p>
in your project to use JsonWax.<br><br>
If you haven't enabled C++11 support in your project, you'll need to include this text in your .pro file:
</p>
<div class='code'>
<div class='source'>
CONFIG += c++11
</div>
</div>
</article>
<article class='wide'>
<h3>
HOW TO READ VALUES
</h3>
<p>
Let's start out by creating a new JsonWax object, and save it to a file, which we shall call "test.json":
</p>
<div class='code'>
<div class='source'>
JsonWax json;<br>
json.saveAs("test.json");
</div>
</div>
<p>
The file "test.json" now contains:
</p>
<div class='code'>
<div class='json'>
{<br>
<br>
}
</div>
</div>
<p>
because the JsonWax-object was empty.<br><br>
What if you have a JSON-document (called "input.json"), and would like to load it?
Your JSON-document looks like this:
</p>
<div class='code'>
<div class='json'>
{<br>
"component1": "value1",<br>
"component2": {<br>
"detail1": "value2"<br>
}<br>
}
</div>
</div>
<p>
You write:
</p>
<div class='code'>
<div class='source'>
JsonWax json;<br>
if (json.loadFile("input.json"))<br>
{<br>
qDebug() << json.value({"component1"}).toString();<br>
} else {<br>
qDebug() << "The document wasn't found.";<br>
}
</div>
</div>
<p>
This prints out:
</p>
<div class='code'>
<div class='json'>
"value1"
</div>
</div>
<p>
since you asked for the value of "component1", and converted it to a string.<br><br>
But what if you wanted the value of "detail1" inside of "component2"?
You write:
</p>
<div class='code'>
<div class='source'>
JsonWax json;<br>
if (json.loadFile("input.json"))<br>
{<br>
qDebug() << json.value({"component2","detail1"}).toString();<br>
} else {<br>
qDebug() << "The document wasn't found.";<br>
}
</div>
</div>
<p>
Which prints out:
</p>
<div class='code'>
<div class='json'>
"value2"
</div>
</div>
<p>
If you check what arguments <em>.value(...)</em> requires, it says:
</p>
<div class='code'>
<div class='source'>
value(const QVariantList& keys, const QVariant& defaultValue = QVariant())
</div>
</div>
<p>
The arguments you gave in the two previous examples were: <em>{"component1"}</em> and <em>{"component2","detail1"}</em>. These were accepted as QVariantLists. So, whenever
you see a function with the argument <em>QVariantList keys</em>, it expects a list like this.<br><br>
But, as you might have noticed, you can have a second argument when using that function.
That's a default value for, if the location didn't exist, as a value, in the document.
For example, if you wrote:
</p>
<div class='code'>
<div class='source'>
JsonWax json;<br>
if (json.loadFile("input.json"))<br>
{<br>
qDebug() << json.value({"component3"}, "not there").toString();<br>
} else {<br>
qDebug() << "The document wasn't found.";<br>
}
</div>
</div>
<p>
it would print out:
</p>
<div class='code'>
<div class='json'>
"not there"
</div>
</div>
<p>
because there is no such value as <em>component3</em>. The same thing goes for:
</p>
<div class='code'>
<div class='source'>
json.value({"component2"}, "not there").toString();
</div>
</div>
<p>
because the location didn't contain a value - it contained another object.<br><br>
But you can also have arrays in JSON-documents (square bracket notation).<br>How would you retrieve an array element
with a list of keys like this?<br><br>This is your document:
</p>
<div class='code'>
<div class='json'>
[<br>
"value1",<br>
"value2",<br>
[<br>
"value3"<br>
]<br>
]
</div>
</div>
<p>
We want to retrieve the first value. It's done like this:
</p>
<div class='code'>
<div class='source'>
JsonWax json;<br>
if (json.loadFile("input.json"))<br>
{<br>
qDebug() << json.value({0}).toString();<br>
} else {<br>
qDebug() << "The document wasn't found.";<br>
}
</div>
</div>
<p>
This prints out:
</p>
<div class='code'>
<div class='json'>
"value1"
</div>
</div>
<p>
because you asked for the first element in the array (0 is first, 1 is second, etc.).<br><br>
You may have guessed the next step... retrieving "value3" from the document.
</p>
<div class='code'>
<div class='source'>
JsonWax json;<br>
if (json.loadFile("input.json"))<br>
{<br>
qDebug() << json.value({2, 0}).toString();<br>
} else {<br>
qDebug() << "The document wasn't found.";<br>
}
</div>
</div>
<p>
It outputs:
</p>
<div class='code'>
<div class='json'>
"value3"
</div>
</div>
<p>
because you asked for the 3rd element in the root array (number 2), and then asked for
the 1st element in that array (number 0).<br>
But, what if you had both? Objects inside an array? And the values weren't
strings, but integers?
</p>
<div class='code'>
<div class='json'>
[<br>
{<br>
"component": 15648<br>
},<br>
{<br>
"component": 77<br>
}<br>
]
</div>
</div>
<p>
You mix the numbers and strings. First, a number specifying the position in the array,
then the name of the object, as a string.
</p>
<div class='code'>
<div class='source'>
JsonWax json;<br>
if (json.loadFile("input.json"))<br>
{<br>
qDebug() << json.value({0, "component"}).toInt();<br>
} else {<br>
qDebug() << "The document wasn't found.";<br>
}
</div>
</div>
<p>
And you get this:
</p>
<div class='code'>
<div class='json'>
15648
</div>
</div>
<p>
<br>Those were the basics of reading values from JSON-data.<br><br>
As a final note, I need to tell you the meaning of an empty keys-list: <em>{}</em>.<br>
It refers to the <em>root</em> array or object, which is the top-level element,
that exists in every valid JSON-document. Since the JSON document format doesn't allow
that to be a value, writing <em>.value({})</em> will always return the defaultValue.<br>
However, you can use this whenever a function works on arrays or objects, fx.
when using the <em>.move</em> or <em>.keys</em> functions.<br>
</p>
</article>
<article class='wide'>
<h3>
HOW TO SET VALUES
</h3>
<p>
You've learned how to read values, but what if you wanted to create your own documents,
or edit existing ones?<br>
The function you need is <em>setValue()</em>. Our goal is to create this:
</p>
<div class='code'>
<div class='json'>
{<br>
"mask1": "value1",<br>
"mask2": 15000,<br>
"mask3": {<br>
"part1": "value2",<br>
"part2": [<br>
"value3",<br>
"value4"<br>
]<br>
}<br>
}
</div>
</div>
<p>
This can be created with just a few lines of code. Let's start with <em>mask1</em> and
<em>mask2</em>.
</p>
<div class='code'>
<div class='source'>
JsonWax json;<br>
json.setValue({"mask1"}, "value1");<br>
json.setValue({"mask2"}, 15000);
</div>
</div>
<p>
See, if it were a string, we used quotes, and if it were a number, we didn't.<br>
Now, <em>"value2"</em> is the value of an object within an object. We refer to this element the same
way as when reading a value, with a list of keys:
</p>
<div class='code'>
<div class='source'>
json.setValue({"mask3","part1"}, "value2");
</div>
</div>
<p>
And the last part are two values within an array. So, we end the list with a number:
</p>
<div class='code'>
<div class='source'>
json.setValue({"mask3","part2", 0}, "value3");<br>
json.setValue({"mask3","part2", 1}, "value4");
</div>
</div>
<p>
Finally, we want to save this to a file. We write:
</p>
<div class='code'>
<div class='source'>
json.saveAs("result.json");
</div>
</div>
<p>
<br>And that's it! We've successfully created a JSON-document, and saved it to a file.<br>
You should notice that <em>setValue</em> always, automatically, overwrites any existing values,
or changes the document, so that your setValue-request may be fulfilled.<br><br>
Okay, but what would happen if the array element number were a negative one, or not just increasing
the array size by one?<br>
If we added one more line to the previous code:
</p>
<div class='code'>
<div class='source'>
json.setValue({"mask3","part2", 5}, "value5");
</div>
</div>
<p>
We would get this document:
</p>
<div class='code'>
<div class='json'>
{<br>
"mask1": "value1",<br>
"mask2": 15000,<br>
"mask3": {<br>
"part1": "value2",<br>
"part2": [<br>
"value3",<br>
"value4",<br>
null,<br>
null,<br>
null,<br>
"value5"<br>
]<br>
}<br>
}
</div>
</div>
<p>
So, it just inserts <em>null</em> values in place of the non-existing array elements.<br><br>
And using a negative array element value, like this:
</p>
<div class='code'>
<div class='source'>
json.setValue({"mask3","part2", -1}, "value5");
</div>
</div>
<p>
attempts to fulfill your request as far as possible: it creates "mask3", "part2", and it turns the
location into an array. But it can't save the value.<br>So you'll receive the error message:
</p>
<div class='code'>
<div class='json'>
JsonWax-insert error: invalid key.
</div>
</div>
<p>
<br><br>
</p>
<a href='func.html'>
See all available functions here.
</a>
<p>
<br>
</p>
</article>
</div></div>
<footer></footer>
</body></html>