File indexing completed on 2026-05-11 08:50:30
0001
0002
0003
0004
0005 #ifndef QJSONOBJECT_H
0006 #define QJSONOBJECT_H
0007
0008 #include <QtCore/qjsonvalue.h>
0009 #include <QtCore/qiterator.h>
0010 #include <QtCore/qpair.h>
0011 #include <QtCore/qshareddata.h>
0012 #include <initializer_list>
0013
0014 QT_BEGIN_NAMESPACE
0015
0016 class QDebug;
0017
0018 class QCborContainerPrivate;
0019
0020 namespace QtPrivate {
0021
0022 template <typename T, typename Iterator>
0023 struct QJsonObjectKeyValues
0024 {
0025 static QAnyStringView key(const Iterator &it) { return it.keyView(); }
0026 static QAnyStringView key(Iterator &it) { return it.keyView(); }
0027 static T value(const Iterator &it) { return it.value(); }
0028 static T value(Iterator &it) { return it.value(); }
0029 };
0030
0031 }
0032
0033 class Q_CORE_EXPORT QJsonObject
0034 {
0035 public:
0036 QJsonObject();
0037
0038 QJsonObject(std::initializer_list<std::pair<QString, QJsonValue> > args);
0039
0040 ~QJsonObject();
0041
0042 QJsonObject(const QJsonObject &other) noexcept;
0043 QJsonObject &operator =(const QJsonObject &other) noexcept;
0044
0045 QJsonObject(QJsonObject &&other) noexcept;
0046
0047 QJsonObject &operator =(QJsonObject &&other) noexcept
0048 {
0049 swap(other);
0050 return *this;
0051 }
0052
0053 void swap(QJsonObject &other) noexcept
0054 {
0055 o.swap(other.o);
0056 }
0057
0058 static QJsonObject fromVariantMap(const QVariantMap &map);
0059 QVariantMap toVariantMap() const;
0060 static QJsonObject fromVariantHash(const QVariantHash &map);
0061 QVariantHash toVariantHash() const;
0062
0063 QStringList keys() const;
0064 qsizetype size() const;
0065 inline qsizetype count() const { return size(); }
0066 inline qsizetype length() const { return size(); }
0067 bool isEmpty() const;
0068
0069 QJsonValue value(const QString &key) const;
0070 QJsonValue operator[] (const QString &key) const;
0071 QJsonValueRef operator[] (const QString &key);
0072 QJsonValue value(QStringView key) const;
0073 QJsonValue value(QLatin1StringView key) const;
0074 QJsonValue operator[] (QStringView key) const { return value(key); }
0075 QJsonValue operator[] (QLatin1StringView key) const { return value(key); }
0076 QJsonValueRef operator[] (QStringView key);
0077 QJsonValueRef operator[] (QLatin1StringView key);
0078
0079 void remove(const QString &key);
0080 QJsonValue take(const QString &key);
0081 bool contains(const QString &key) const;
0082 void remove(QStringView key);
0083 void remove(QLatin1StringView key);
0084 QJsonValue take(QStringView key);
0085 QJsonValue take(QLatin1StringView key);
0086 bool contains(QStringView key) const;
0087 bool contains(QLatin1StringView key) const;
0088
0089 #if QT_CORE_REMOVED_SINCE(6, 8)
0090 bool operator==(const QJsonObject &other) const;
0091 bool operator!=(const QJsonObject &other) const;
0092 #endif
0093 class const_iterator;
0094
0095 class iterator
0096 {
0097 friend class const_iterator;
0098 friend class QJsonObject;
0099 QJsonValueRef item;
0100
0101 public:
0102 typedef std::random_access_iterator_tag iterator_category;
0103 typedef qsizetype difference_type;
0104 typedef QJsonValue value_type;
0105 typedef QJsonValueRef reference;
0106 typedef QJsonValueRef *pointer;
0107
0108 inline iterator() : item(static_cast<QJsonObject*>(nullptr), 0) { }
0109 inline iterator(QJsonObject *obj, qsizetype index) : item(obj, index) { }
0110
0111 constexpr iterator(const iterator &other) = default;
0112 iterator &operator=(const iterator &other)
0113 {
0114 item.rebind(other.item);
0115 return *this;
0116 }
0117
0118 inline QString key() const { return item.objectKey(); }
0119 QAnyStringView keyView() const { return item.objectKeyView(); }
0120 inline QJsonValueRef value() const { return item; }
0121 inline QJsonValueRef operator*() const { return item; }
0122 inline const QJsonValueConstRef *operator->() const { return &item; }
0123 inline QJsonValueRef *operator->() { return &item; }
0124 inline QJsonValueRef operator[](qsizetype j) const { return *(*this + j); }
0125 #if QT_CORE_REMOVED_SINCE(6, 8)
0126 inline bool operator==(const iterator &other) const
0127 { return item.d == other.item.d && item.index == other.item.index; }
0128 inline bool operator!=(const iterator &other) const { return !operator==(other); }
0129 bool operator<(const iterator& other) const
0130 { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
0131 bool operator<=(const iterator& other) const
0132 { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
0133 bool operator>(const iterator& other) const { return !operator<=(other); }
0134 bool operator>=(const iterator& other) const { return !operator<(other); }
0135 #endif
0136 inline iterator &operator++() { ++item.index; return *this; }
0137 inline iterator operator++(int) { iterator r = *this; ++item.index; return r; }
0138 inline iterator &operator--() { --item.index; return *this; }
0139 inline iterator operator--(int) { iterator r = *this; --item.index; return r; }
0140 inline iterator operator+(qsizetype j) const { iterator r = *this; return r += j; }
0141 inline iterator operator-(qsizetype j) const { return operator+(-j); }
0142 inline iterator &operator+=(qsizetype j) { item.index += quint64(j); return *this; }
0143 inline iterator &operator-=(qsizetype j) { item.index -= quint64(j); return *this; }
0144 qsizetype operator-(iterator j) const { return qsizetype(item.index - j.item.index); }
0145
0146 public:
0147 #if QT_CORE_REMOVED_SINCE(6, 8)
0148 inline bool operator==(const const_iterator &other) const
0149 { return item.d == other.item.d && item.index == other.item.index; }
0150 inline bool operator!=(const const_iterator &other) const { return !operator==(other); }
0151 bool operator<(const const_iterator& other) const
0152 { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
0153 bool operator<=(const const_iterator& other) const
0154 { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
0155 bool operator>(const const_iterator& other) const { return operator<=(other); }
0156 bool operator>=(const const_iterator& other) const { return operator<(other); }
0157 #endif
0158 private:
0159
0160 static bool comparesEqual_helper(const iterator &lhs, const iterator &rhs) noexcept
0161 {
0162 return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index;
0163 }
0164 static bool comparesEqual_helper(const iterator &lhs, const const_iterator &rhs) noexcept
0165 {
0166 return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index;
0167 }
0168
0169 static Qt::strong_ordering compareThreeWay_helper(const iterator &lhs,
0170 const iterator &rhs)
0171 {
0172 Q_ASSERT(lhs.item.d == rhs.item.d);
0173 return Qt::compareThreeWay(lhs.item.index, rhs.item.index);
0174 }
0175 static Qt::strong_ordering compareThreeWay_helper(const iterator &lhs,
0176 const const_iterator &rhs)
0177 {
0178 Q_ASSERT(lhs.item.d == rhs.item.d);
0179 return Qt::compareThreeWay(lhs.item.index, rhs.item.index);
0180 }
0181
0182
0183 friend bool comparesEqual(const iterator &lhs, const iterator &rhs) noexcept
0184 {
0185 return comparesEqual_helper(lhs, rhs);
0186 }
0187 friend Qt::strong_ordering compareThreeWay(const iterator &lhs,
0188 const iterator &rhs)
0189 {
0190 return compareThreeWay_helper(lhs, rhs);
0191 }
0192 Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(iterator)
0193
0194 friend bool comparesEqual(const iterator &lhs, const const_iterator &rhs) noexcept
0195 {
0196 return comparesEqual_helper(lhs, rhs);
0197 }
0198 friend Qt::strong_ordering compareThreeWay(const iterator &lhs,
0199 const const_iterator &rhs)
0200 {
0201 return compareThreeWay_helper(lhs, rhs);
0202 }
0203 Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(iterator, const_iterator)
0204 };
0205 friend class iterator;
0206
0207 class const_iterator
0208 {
0209 friend class iterator;
0210 QJsonValueConstRef item;
0211
0212 public:
0213 typedef std::random_access_iterator_tag iterator_category;
0214 typedef qsizetype difference_type;
0215 typedef QJsonValue value_type;
0216 typedef const QJsonValueConstRef reference;
0217 typedef const QJsonValueConstRef *pointer;
0218
0219 inline const_iterator() : item(static_cast<QJsonObject*>(nullptr), 0) { }
0220 inline const_iterator(const QJsonObject *obj, qsizetype index)
0221 : item(const_cast<QJsonObject*>(obj), index) { }
0222 inline const_iterator(const iterator &other)
0223 : item(other.item) { }
0224
0225 constexpr const_iterator(const const_iterator &other) = default;
0226 const_iterator &operator=(const const_iterator &other)
0227 {
0228 item.rebind(other.item);
0229 return *this;
0230 }
0231
0232 inline QString key() const { return item.objectKey(); }
0233 QAnyStringView keyView() const { return item.objectKeyView(); }
0234 inline QJsonValueConstRef value() const { return item; }
0235 inline const QJsonValueConstRef operator*() const { return item; }
0236 inline const QJsonValueConstRef *operator->() const { return &item; }
0237 inline QJsonValueConstRef operator[](qsizetype j) const { return *(*this + j); }
0238 #if QT_CORE_REMOVED_SINCE(6, 8)
0239 inline bool operator==(const const_iterator &other) const
0240 { return item.d == other.item.d && item.index == other.item.index; }
0241 inline bool operator!=(const const_iterator &other) const { return !operator==(other); }
0242 bool operator<(const const_iterator& other) const
0243 { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
0244 bool operator<=(const const_iterator& other) const
0245 { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
0246 bool operator>(const const_iterator& other) const { return !operator<=(other); }
0247 bool operator>=(const const_iterator& other) const { return !operator<(other); }
0248 #endif
0249 inline const_iterator &operator++() { ++item.index; return *this; }
0250 inline const_iterator operator++(int) { const_iterator r = *this; ++item.index; return r; }
0251 inline const_iterator &operator--() { --item.index; return *this; }
0252 inline const_iterator operator--(int) { const_iterator r = *this; --item.index; return r; }
0253 inline const_iterator operator+(qsizetype j) const { const_iterator r = *this; return r += j; }
0254 inline const_iterator operator-(qsizetype j) const { return operator+(-j); }
0255 inline const_iterator &operator+=(qsizetype j) { item.index += quint64(j); return *this; }
0256 inline const_iterator &operator-=(qsizetype j) { item.index -= quint64(j); return *this; }
0257 qsizetype operator-(const_iterator j) const { return qsizetype(item.index - j.item.index); }
0258 #if QT_CORE_REMOVED_SINCE(6, 8)
0259 inline bool operator==(const iterator &other) const
0260 { return item.d == other.item.d && item.index == other.item.index; }
0261 inline bool operator!=(const iterator &other) const { return !operator==(other); }
0262 bool operator<(const iterator& other) const
0263 { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
0264 bool operator<=(const iterator& other) const
0265 { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
0266 bool operator>(const iterator& other) const { return !operator<=(other); }
0267 bool operator>=(const iterator& other) const { return !operator<(other); }
0268 #endif
0269
0270 private:
0271
0272 static bool comparesEqual_helper(const const_iterator &lhs,
0273 const const_iterator &rhs) noexcept
0274 {
0275 return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index;
0276 }
0277 static Qt::strong_ordering compareThreeWay_helper(const const_iterator &lhs,
0278 const const_iterator &rhs)
0279 {
0280 Q_ASSERT(lhs.item.d == rhs.item.d);
0281 return Qt::compareThreeWay(lhs.item.index, rhs.item.index);
0282 }
0283
0284
0285 friend bool comparesEqual(const const_iterator &lhs, const const_iterator &rhs) noexcept
0286 {
0287 return comparesEqual_helper(lhs, rhs);
0288 }
0289 friend Qt::strong_ordering compareThreeWay(const const_iterator &lhs,
0290 const const_iterator &rhs)
0291 {
0292 return compareThreeWay_helper(lhs, rhs);
0293 }
0294 Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(const_iterator)
0295 };
0296 friend class const_iterator;
0297
0298 typedef QKeyValueIterator<QAnyStringView, QJsonValueConstRef, const_iterator,
0299 QtPrivate::QJsonObjectKeyValues<QJsonValueConstRef, const_iterator>>
0300 const_key_value_iterator;
0301 typedef QKeyValueIterator<QAnyStringView, QJsonValueRef, iterator,
0302 QtPrivate::QJsonObjectKeyValues<QJsonValueRef, iterator>>
0303 key_value_iterator;
0304
0305
0306 inline iterator begin() { detach(); return iterator(this, 0); }
0307 inline const_iterator begin() const { return const_iterator(this, 0); }
0308 inline const_iterator constBegin() const { return const_iterator(this, 0); }
0309 inline iterator end() { detach(); return iterator(this, size()); }
0310 inline const_iterator end() const { return const_iterator(this, size()); }
0311 inline const_iterator constEnd() const { return const_iterator(this, size()); }
0312 key_value_iterator keyValueBegin() { return key_value_iterator(begin()); }
0313 key_value_iterator keyValueEnd() { return key_value_iterator(end()); }
0314 const_key_value_iterator keyValueBegin() const { return const_key_value_iterator(begin()); }
0315 const_key_value_iterator constKeyValueBegin() const
0316 {
0317 return const_key_value_iterator(begin());
0318 }
0319 const_key_value_iterator keyValueEnd() const { return const_key_value_iterator(end()); }
0320 const_key_value_iterator constKeyValueEnd() const { return const_key_value_iterator(end()); }
0321 iterator erase(iterator it);
0322
0323 auto asKeyValueRange() & { return QtPrivate::QKeyValueRange<QJsonObject &>(*this); }
0324 auto asKeyValueRange() const & { return QtPrivate::QKeyValueRange<const QJsonObject &>(*this); }
0325 auto asKeyValueRange() && { return QtPrivate::QKeyValueRange<QJsonObject>(std::move(*this)); }
0326 auto asKeyValueRange() const &&
0327 {
0328 return QtPrivate::QKeyValueRange<QJsonObject>(std::move(*this));
0329 }
0330
0331
0332 typedef iterator Iterator;
0333 typedef const_iterator ConstIterator;
0334 iterator find(const QString &key);
0335 const_iterator find(const QString &key) const { return constFind(key); }
0336 const_iterator constFind(const QString &key) const;
0337 iterator insert(const QString &key, const QJsonValue &value);
0338 iterator find(QStringView key);
0339 iterator find(QLatin1StringView key);
0340 const_iterator find(QStringView key) const { return constFind(key); }
0341 const_iterator find(QLatin1StringView key) const { return constFind(key); }
0342 const_iterator constFind(QStringView key) const;
0343 const_iterator constFind(QLatin1StringView key) const;
0344 iterator insert(QStringView key, const QJsonValue &value);
0345 iterator insert(QLatin1StringView key, const QJsonValue &value);
0346
0347
0348 typedef QJsonValue mapped_type;
0349 typedef QString key_type;
0350 typedef qsizetype size_type;
0351
0352 inline bool empty() const { return isEmpty(); }
0353
0354 private:
0355 friend Q_CORE_EXPORT bool comparesEqual(const QJsonObject &lhs,
0356 const QJsonObject &rhs);
0357 friend bool comparesEqual(const QJsonObject &lhs, const QJsonValue &rhs)
0358 {
0359 return comparesEqual(lhs, rhs.toObject());
0360 }
0361 friend bool comparesEqual(const QJsonObject &lhs, const QJsonValueConstRef &rhs)
0362 {
0363 return comparesEqual(lhs, rhs.toObject());
0364 }
0365 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonObject)
0366 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonObject, QJsonValue)
0367 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonObject, QJsonValueConstRef)
0368 friend class QJsonValue;
0369 friend class QJsonDocument;
0370 friend class QJsonPrivate::Value;
0371 friend class QJsonValueConstRef;
0372 friend class QJsonValueRef;
0373 friend class QCborMap;
0374 friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &);
0375
0376 QJsonObject(QCborContainerPrivate *object);
0377 bool detach(qsizetype reserve = 0);
0378
0379 template <typename T> QJsonValue valueImpl(T key) const;
0380 template <typename T> QJsonValueRef atImpl(T key);
0381 template <typename T> void removeImpl(T key);
0382 template <typename T> QJsonValue takeImpl(T key);
0383 template <typename T> bool containsImpl(T key) const;
0384 template <typename T> iterator findImpl(T key);
0385 template <typename T> const_iterator constFindImpl(T key) const;
0386 template <typename T> iterator insertImpl(T key, const QJsonValue &value);
0387
0388 #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED)
0389 QString keyAt(qsizetype i) const;
0390 QJsonValue valueAt(qsizetype i) const;
0391 void setValueAt(qsizetype i, const QJsonValue &val);
0392 #endif
0393 void removeAt(qsizetype i);
0394 template <typename T> iterator insertAt(qsizetype i, T key, const QJsonValue &val, bool exists);
0395
0396 QExplicitlySharedDataPointer<QCborContainerPrivate> o;
0397 };
0398
0399 Q_DECLARE_SHARED(QJsonObject)
0400
0401 #if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)
0402 inline QJsonValueConstRef::QJsonValueConstRef(QJsonObject *o, qsizetype idx)
0403 : d(o ? o->o.data() : nullptr), is_object(true), index(idx)
0404 {}
0405 #endif
0406
0407 Q_CORE_EXPORT size_t qHash(const QJsonObject &object, size_t seed = 0);
0408
0409 #if !defined(QT_NO_DEBUG_STREAM)
0410 Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &);
0411 #endif
0412
0413 #ifndef QT_NO_DATASTREAM
0414 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonObject &);
0415 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonObject &);
0416 #endif
0417
0418 QT_END_NAMESPACE
0419
0420 #endif