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
807
808
809
810
811
812
815
816
817
820
821
822
823
824
825
828
829
830
831
832
833
836
837
838
839
840
841
842
843
844
845
846
849
850
851
852
853
854
855
856
857
858
859
862
863
864
865
866
867
868
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
923
924
925
926
927
928
929
930
931
932
933
934
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
968
969
970
973
974
975
976
977
978
979
982
983
984
987
988
989
992
993
994
997
998
999
1000
1001
1002
1003
1004
1005
1008
1009
1012
1013
1014
1015
1016
1017
1018
1019
1020
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1071
1072
1073
1076
1077
1078
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1098
1099
1100
1101
1102
1103
1104
1105
1108
1109
1110
1111
1112
1113
1114
1115
1118
1119
1120
1121
1122
1123
1126
1127
1128
1129
1130
1131
1134
1135
1136
1137
1138
1139
1142
1143
1144
1145
1146
1147
1150
1151
1152
1153
1154
1155
1156
1157
1158
1161
1162
1163
1164
1165
1166
1167
1170
1171
1172
1173
1174
1175
1176
1177
1178
1181
1182
1183
1184
1185
1186
1187
1190
1191
1192
1193
1194
1195
1196
1199
1200
1201
1202
1203
1204
1207
1208
1209
1210
1211
1212
1215
1216
1217
1218
1219
1220
1223
1224
1225
1226
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1259
1260
1261
1264
1265
1266
1267
1268
1269
1270
1273
1274
1275
1278
1279
1280
1283
1284
1285
1288
1289
1290
1291
1292
1293
1294
1295
1296
1299
1300
1303
1304
1305
1308
1309
1310
1311
1312
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1328
1329
1330
1331
1332
1335
1336
1337
1338
1339
1340
1341
1344
1345
1346
1349
1350
1351
1352
1353
1354
1355
1356
1357
1361
1362
1363
1364
1365
1366
1369
1370
1371
1372
1373
1374
1377
1378
1379
1380
1381
1384
1385
1386
1387
1388
1391
1392
1393
1394
1395
1398
1399
1400
1401
1402
1405
1406
1407
1408
1409
1410
1411
1412
1413
1416
1417
1418
1419
1420
1421
1422
1425
1426
1427
1428
1429
1430
1431
1432
1433
1436
1437
1438
1439
1440
1441
1442
1445
1446
1447
1448
1449
1450
1451
1452
1455
1456
1457
1458
1459
1460
1461
1462
1465
1466
1467
1468
1469
1470
1471
1472
1475
1476
1477
1478
1479
1480
1481
1482
1485
1486
1487
1488
1492
1493
1494bool QJsonObject::detach(qsizetype reserve)
1498 o = QCborContainerPrivate::detach(o.data(), reserve ? reserve * 2 : o->elements.size());
1502#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
) && !defined(QT_BOOTSTRAPPED)
1504
1505
1506QString QJsonObject::keyAt(qsizetype i)
const
1508 Q_ASSERT(o && i >= 0 && i * 2 < o->elements.size());
1509 return o->stringAt(i * 2);
1513
1514
1515QJsonValue QJsonObject::valueAt(qsizetype i)
const
1517 if (!o || i < 0 || 2 * i + 1 >= o->elements.size())
1518 return QJsonValue(QJsonValue::Undefined);
1519 return QJsonPrivate::Value::fromTrustedCbor(o->valueAt(2 * i + 1));
1523
1524
1525void QJsonObject::setValueAt(qsizetype i,
const QJsonValue &val)
1527 Q_ASSERT(o && i >= 0 && 2 * i + 1 < o->elements.size());
1529 if (val.isUndefined()) {
1530 o->removeAt(2 * i + 1);
1533 o->replaceAt(2 * i + 1, QCborValue::fromJsonValue(val));
1539
1540
1541void QJsonObject::removeAt(qsizetype index)
1544 o->removeAt(index + 1);
1550 QtPrivate::QHashCombine hash(seed);
1551 for (
auto it = object.begin(), end = object.end(); it != end; ++it) {
1552 const QString key = it.key();
1553 const QJsonValue value = it.value();
1554 seed = hash(seed, std::pair<
const QString&,
const QJsonValue&>(key, value));
1559#if !defined(QT_NO_DEBUG_STREAM)
1560QDebug operator<<(QDebug dbg,
const QJsonObject &o)
1562 QDebugStateSaver saver(dbg);
1564 dbg <<
"QJsonObject()";
1568 QJsonPrivate::Writer::objectToJson(o.o.data(), json, 0,
true);
1569 dbg.nospace() <<
"QJsonObject("
1576#ifndef QT_NO_DATASTREAM
1577QDataStream &operator<<(QDataStream &stream,
const QJsonObject &object)
1580 stream << doc.toJson(QJsonDocument::Compact);
1584QDataStream &
operator>>(QDataStream &stream, QJsonObject &object)
1588 object = doc.object();
\inmodule QtCore\reentrant
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)