5#include <qjsonobject.h>
8#include <qjsondocument.h>
9#include <qstringlist.h>
16#include <private/qcborvalue_p.h>
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
55
56
57
58
61
62
63
64
67
68
69
70
73
74
75
76
79
80
81
82
86
87
88
89
90QJsonObject::QJsonObject() =
default;
93
94
95
96
97
98
99
100
101
102
103
104
107
108
109QJsonObject::QJsonObject(QCborContainerPrivate *object)
115
116
117QJsonObject::~QJsonObject() =
default;
119QJsonObject::QJsonObject(std::initializer_list<std::pair<QString, QJsonValue> > args)
121 for (
const auto &arg : args)
122 insert(arg.first, arg.second);
126
127
128
129
130
131QJsonObject::QJsonObject(
const QJsonObject &other)
noexcept =
default;
134
135
136
137
138QJsonObject::QJsonObject(QJsonObject &&other)
noexcept
145
146
147QJsonObject &QJsonObject::operator =(
const QJsonObject &other)
noexcept =
default;
151
152
153
154
155
158
159
160
161
165
166
167
168
169
170
171
172
173
174
175QJsonObject QJsonObject::fromVariantMap(
const QVariantMap &map)
177 return QJsonPrivate::Variant::toJsonObject(map);
181
182
183
184
185
186
187QVariantMap QJsonObject::toVariantMap()
const
189 return QCborMap::fromJsonObject(*
this).toVariantMap();
193
194
195
196
197
198
199
200
201
202
203
204QJsonObject QJsonObject::fromVariantHash(
const QVariantHash &hash)
209 for (QVariantHash::const_iterator it = hash.constBegin(); it != hash.constEnd(); ++it)
210 object.insert(it.key(), QJsonValue::fromVariant(it.value()));
215
216
217
218
219
220
221
222QVariantHash QJsonObject::toVariantHash()
const
224 return QCborMap::fromJsonObject(*
this).toVariantHash();
229
230
231
232
233QStringList QJsonObject::keys()
const
237 keys.reserve(o->elements.size() / 2);
238 for (qsizetype i = 0, end = o->elements.size(); i < end; i += 2)
239 keys.append(o->stringAt(i));
245
246
247qsizetype QJsonObject::size()
const
249 return o ? o->elements.size() / 2 : 0;
253
254
255
256
257bool QJsonObject::isEmpty()
const
259 return !o || o->elements.isEmpty();
262template<
typename String>
264 String key,
bool *keyExists)
266 const auto begin = QJsonPrivate::ConstKeyIterator(o->elements.constBegin());
267 const auto end = QJsonPrivate::ConstKeyIterator(o->elements.constEnd());
269 const auto it =
std::lower_bound(
271 [&](
const QJsonPrivate::ConstKeyIterator::value_type &e,
const String &key) {
272 return o->stringCompareElement(e.key(), key, QtCbor::Comparison::ForOrdering) < 0;
275 *keyExists = (it != end) && o->stringEqualsElement((*it).key(), key);
276 return it.it - begin.it;
280
281
282
283
284
285
286QJsonValue QJsonObject::value(
const QString &key)
const
288 return value(QStringView(key));
292
293
294
295QJsonValue QJsonObject::value(QStringView key)
const
297 return valueImpl(key);
301
302
303
304QJsonValue QJsonObject::value(QLatin1StringView key)
const
306 return valueImpl(key);
310
311
313QJsonValue QJsonObject::valueImpl(T key)
const
316 return QJsonValue(QJsonValue::Undefined);
319 auto i = indexOf(o, key, &keyExists);
321 return QJsonValue(QJsonValue::Undefined);
322 return QJsonPrivate::Value::fromTrustedCbor(o->valueAt(i + 1));
326
327
328
329
330
331
332
333
334QJsonValue QJsonObject::operator [](
const QString &key)
const
336 return (*
this)[QStringView(key)];
340
341
342
343
344
347
348
349
350
351
354
355
356
357
358
359
360
361
362
363
364
365
366QJsonValueRef QJsonObject::operator [](
const QString &key)
368 return (*
this)[QStringView(key)];
372
373
374
375QJsonValueRef QJsonObject::operator [](QStringView key)
381
382
383
384QJsonValueRef QJsonObject::operator [](QLatin1StringView key)
390
391
393QJsonValueRef QJsonObject::atImpl(T key)
396 o =
new QCborContainerPrivate;
398 bool keyExists =
false;
399 auto index = indexOf(o, key, &keyExists);
401 detach(o->elements.size() / 2 + 1);
402 o->insertAt(index, key);
403 o->insertAt(index + 1, QCborValue::fromJsonValue(QJsonValue()));
406 return QJsonValueRef(
this, index / 2);
410
411
412
413
414
415
416
417
418
419
420
421
422QJsonObject::iterator QJsonObject::insert(
const QString &key,
const QJsonValue &value)
424 return insert(QStringView(key), value);
428
429
430
431QJsonObject::iterator QJsonObject::insert(QStringView key,
const QJsonValue &value)
433 return insertImpl(key, value);
437
438
439
440QJsonObject::iterator QJsonObject::insert(QLatin1StringView key,
const QJsonValue &value)
442 return insertImpl(key, value);
446
447
449QJsonObject::iterator QJsonObject::insertImpl(T key,
const QJsonValue &value)
451 if (value.type() == QJsonValue::Undefined) {
455 bool keyExists =
false;
456 auto pos = o ? indexOf(o, key, &keyExists) : 0;
457 return insertAt(pos, key, value, keyExists);
461
462
464QJsonObject::iterator QJsonObject::insertAt(qsizetype pos, T key,
const QJsonValue &value,
bool keyExists)
467 detach(o->elements.size() / 2 + (keyExists ? 0 : 1));
469 o =
new QCborContainerPrivate;
472 o->replaceAt(pos + 1, QCborValue::fromJsonValue(value));
474 o->insertAt(pos, key);
475 o->insertAt(pos + 1, QCborValue::fromJsonValue(value));
477 return {
this, pos / 2};
481
482
483
484
485void QJsonObject::remove(
const QString &key)
487 remove(QStringView(key));
491
492
493
494void QJsonObject::remove(QStringView key)
500
501
502
503void QJsonObject::remove(QLatin1StringView key)
509
510
512void QJsonObject::removeImpl(T key)
518 auto index = indexOf(o, key, &keyExists);
526
527
528
529
530
531
532
533
534QJsonValue QJsonObject::take(
const QString &key)
536 return take(QStringView(key));
540
541
542
543QJsonValue QJsonObject::take(QStringView key)
545 return takeImpl(key);
549
550
551
552QJsonValue QJsonObject::take(QLatin1StringView key)
554 return takeImpl(key);
558
559
561QJsonValue QJsonObject::takeImpl(T key)
564 return QJsonValue(QJsonValue::Undefined);
567 auto index = indexOf(o, key, &keyExists);
569 return QJsonValue(QJsonValue::Undefined);
572 const QJsonValue v = QJsonPrivate::Value::fromTrustedCbor(o->extractAt(index + 1));
578
579
580
581
582bool QJsonObject::contains(
const QString &key)
const
584 return contains(QStringView(key));
588
589
590
591bool QJsonObject::contains(QStringView key)
const
593 return containsImpl(key);
597
598
599
600bool QJsonObject::contains(QLatin1StringView key)
const
602 return containsImpl(key);
606
607
609bool QJsonObject::containsImpl(T key)
const
615 indexOf(o, key, &keyExists);
620
621
622
623
630 return !rhs.o->elements.size();
632 return !lhs.o->elements.size();
633 if (lhs.o->elements.size() != rhs.o->elements.size())
636 for (qsizetype i = 0, end = lhs.o->elements.size(); i < end; ++i) {
637 if (lhs.o->valueAt(i) != rhs.o->valueAt(i))
645
646
647
648
651
652
653
654
655
656
657QJsonObject::iterator QJsonObject::erase(QJsonObject::iterator it)
659 removeAt(it.item.index * 2);
664 return {
this, qsizetype(it.item.index) };
668
669
670
671
672
673
674QJsonObject::iterator QJsonObject::find(
const QString &key)
676 return find(QStringView(key));
680
681
682
683QJsonObject::iterator QJsonObject::find(QStringView key)
685 return findImpl(key);
689
690
691
692QJsonObject::iterator QJsonObject::find(QLatin1StringView key)
694 return findImpl(key);
698
699
701QJsonObject::iterator QJsonObject::findImpl(T key)
703 bool keyExists =
false;
704 auto index = o ? indexOf(o, key, &keyExists) : 0;
708 return {
this, index / 2};
712
713
714
717
718
719
720
723
724
725
726
729
730
731
732
733
734
735QJsonObject::const_iterator QJsonObject::constFind(
const QString &key)
const
737 return constFind(QStringView(key));
741
742
743
744QJsonObject::const_iterator QJsonObject::constFind(QStringView key)
const
746 return constFindImpl(key);
750
751
752
753QJsonObject::const_iterator QJsonObject::constFind(QLatin1StringView key)
const
755 return constFindImpl(key);
759
760
762QJsonObject::const_iterator QJsonObject::constFindImpl(T key)
const
764 bool keyExists =
false;
765 auto index = o ? indexOf(o, key, &keyExists) : 0;
768 return {
this, index / 2};
772
773
774
775
776
779
780
781
782
783
786
787
788
789
790
791
794
795
796
799
800
801
802
803
804
805
808
809
810
811
812
813
816
817
818
819
820
821
824
825
826
829
830
831
832
833
834
835
838
839
840
841
842
843
846
847
848
849
850
851
854
855
856
857
858
859
860
861
862
863
864
867
868
869
870
871
872
873
874
875
876
877
880
881
882
883
884
885
886
889
890
891
892
893
894
895
898
899
900
901
902
903
904
907
908
909
910
911
912
913
916
917
918
919
920
921
922
925
926
927
928
929
930
931
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
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
986
987
988
991
992
993
994
995
996
997
1000
1001
1002
1005
1006
1007
1010
1011
1012
1015
1016
1017
1018
1019
1020
1021
1022
1023
1026
1027
1030
1031
1032
1033
1034
1035
1036
1037
1038
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1089
1090
1091
1094
1095
1096
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1116
1117
1118
1119
1120
1121
1122
1123
1126
1127
1128
1129
1130
1131
1132
1133
1136
1137
1138
1139
1140
1141
1144
1145
1146
1147
1148
1149
1152
1153
1154
1155
1156
1157
1160
1161
1162
1163
1164
1165
1168
1169
1170
1171
1172
1173
1174
1175
1176
1179
1180
1181
1182
1183
1184
1185
1188
1189
1190
1191
1192
1193
1194
1195
1196
1199
1200
1201
1202
1203
1204
1205
1208
1209
1210
1211
1212
1213
1214
1217
1218
1219
1220
1221
1222
1225
1226
1227
1228
1229
1230
1233
1234
1235
1236
1237
1238
1241
1242
1243
1244
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1277
1278
1279
1282
1283
1284
1285
1286
1287
1288
1291
1292
1293
1296
1297
1298
1301
1302
1303
1306
1307
1308
1309
1310
1311
1312
1313
1314
1317
1318
1321
1322
1323
1326
1327
1328
1329
1330
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1346
1347
1348
1349
1350
1353
1354
1355
1356
1357
1358
1359
1362
1363
1364
1367
1368
1369
1370
1371
1372
1373
1374
1375
1379
1380
1381
1382
1383
1384
1387
1388
1389
1390
1391
1392
1395
1396
1397
1398
1399
1402
1403
1404
1405
1406
1409
1410
1411
1412
1413
1416
1417
1418
1419
1420
1423
1424
1425
1426
1427
1428
1429
1430
1431
1434
1435
1436
1437
1438
1439
1440
1443
1444
1445
1446
1447
1448
1449
1450
1451
1454
1455
1456
1457
1458
1459
1460
1463
1464
1465
1466
1467
1468
1469
1470
1473
1474
1475
1476
1477
1478
1479
1480
1483
1484
1485
1486
1487
1488
1489
1490
1493
1494
1495
1496
1497
1498
1499
1500
1503
1504
1505
1506
1510
1511
1512bool QJsonObject::detach(qsizetype reserve)
1516 o = QCborContainerPrivate::detach(o.data(), reserve ? reserve * 2 : o->elements.size());
1520#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
) && !defined(QT_BOOTSTRAPPED)
1522
1523
1524QString QJsonObject::keyAt(qsizetype i)
const
1526 Q_ASSERT(o && i >= 0 && i * 2 < o->elements.size());
1527 return o->stringAt(i * 2);
1531
1532
1533QJsonValue QJsonObject::valueAt(qsizetype i)
const
1535 if (!o || i < 0 || 2 * i + 1 >= o->elements.size())
1536 return QJsonValue(QJsonValue::Undefined);
1537 return QJsonPrivate::Value::fromTrustedCbor(o->valueAt(2 * i + 1));
1541
1542
1543void QJsonObject::setValueAt(qsizetype i,
const QJsonValue &val)
1545 Q_ASSERT(o && i >= 0 && 2 * i + 1 < o->elements.size());
1547 if (val.isUndefined()) {
1548 o->removeAt(2 * i + 1);
1551 o->replaceAt(2 * i + 1, QCborValue::fromJsonValue(val));
1557
1558
1559void QJsonObject::removeAt(qsizetype index)
1562 o->removeAt(index + 1);
1568 QtPrivate::QHashCombine hash(seed);
1569 for (
auto it = object.begin(), end = object.end(); it != end; ++it) {
1570 const QString key = it.key();
1571 const QJsonValue value = it.value();
1572 seed = hash(seed, std::pair<
const QString&,
const QJsonValue&>(key, value));
1577#if !defined(QT_NO_DEBUG_STREAM)
1578QDebug operator<<(QDebug dbg,
const QJsonObject &o)
1580 QDebugStateSaver saver(dbg);
1582 dbg <<
"QJsonObject()";
1586 QJsonPrivate::Writer::objectToJson(o.o.data(), json, 0,
true);
1587 dbg.nospace() <<
"QJsonObject("
1594#ifndef QT_NO_DATASTREAM
1595QDataStream &operator<<(QDataStream &stream,
const QJsonObject &object)
1598 stream << doc.toJson(QJsonDocument::Compact);
1602QDataStream &
operator>>(QDataStream &stream, QJsonObject &object)
1606 object = doc.object();
\inmodule QtCore\reentrant
Combined button and popup list for selecting options.
size_t qHash(const QJsonObject &object, size_t seed)
QDataStream & operator>>(QDataStream &stream, QJsonObject &object)
bool comparesEqual(const QJsonObject &lhs, const QJsonObject &rhs)
static qsizetype indexOf(const QExplicitlySharedDataPointer< QCborContainerPrivate > &o, String key, bool *keyExists)