summaryrefslogtreecommitdiffstats
path: root/src/corelib/serialization/qjsonobject.h
blob: 41428325b7f9d4a9e6b6eb4243201ec49334a56c (plain)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
// Copyright (C) 2016 The Qt Company Ltd.// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only#ifndef QJSONOBJECT_H#define QJSONOBJECT_H#include <QtCore/qjsonvalue.h>#include <QtCore/qiterator.h>#include <QtCore/qpair.h>#include <QtCore/qshareddata.h>#include <initializer_list> QT_BEGIN_NAMESPACE class QDebug;class QCborContainerPrivate;class Q_CORE_EXPORT QJsonObject {public:QJsonObject();QJsonObject(std::initializer_list<std::pair<QString, QJsonValue> > args);~QJsonObject();QJsonObject(const QJsonObject &other) noexcept; QJsonObject &operator=(const QJsonObject &other) noexcept;QJsonObject(QJsonObject &&other) noexcept; QJsonObject &operator=(QJsonObject &&other) noexcept {swap(other);return*this;}voidswap(QJsonObject &other) noexcept { o.swap(other.o);}static QJsonObject fromVariantMap(const QVariantMap &map); QVariantMap toVariantMap()const;static QJsonObject fromVariantHash(const QVariantHash &map); QVariantHash toVariantHash()const; QStringList keys()const; qsizetype size()const;inline qsizetype count()const{returnsize(); }inline qsizetype length()const{returnsize(); }boolisEmpty()const; QJsonValue value(const QString &key)const; QJsonValue operator[] (const QString &key)const; QJsonValueRef operator[] (const QString &key); QJsonValue value(QStringView key)const; QJsonValue value(QLatin1StringView key)const; QJsonValue operator[] (QStringView key)const{returnvalue(key); } QJsonValue operator[] (QLatin1StringView key)const{returnvalue(key); } QJsonValueRef operator[] (QStringView key); QJsonValueRef operator[] (QLatin1StringView key);voidremove(const QString &key); QJsonValue take(const QString &key);boolcontains(const QString &key)const;voidremove(QStringView key);voidremove(QLatin1StringView key); QJsonValue take(QStringView key); QJsonValue take(QLatin1StringView key);boolcontains(QStringView key)const;boolcontains(QLatin1StringView key)const;#if QT_CORE_REMOVED_SINCE(6, 8)booloperator==(const QJsonObject &other)const;booloperator!=(const QJsonObject &other)const;#endifclass const_iterator;class iterator {friend class const_iterator;friend class QJsonObject; QJsonValueRef item;public:typedef std::random_access_iterator_tag iterator_category;typedef qsizetype difference_type;typedef QJsonValue value_type;typedef QJsonValueRef reference;typedef QJsonValueRef *pointer;inlineiterator() :item(static_cast<QJsonObject*>(nullptr),0) { }inlineiterator(QJsonObject *obj, qsizetype index) :item(obj, index) { }constexpriterator(const iterator &other) =default; iterator &operator=(const iterator &other){ item.rebind(other.item);return*this;}inline QString key()const{return item.objectKey(); } QAnyStringView keyView()const{return item.objectKeyView(); }inline QJsonValueRef value()const{return item; }inline QJsonValueRef operator*()const{return item; }inlineconst QJsonValueConstRef *operator->()const{return&item; }inline QJsonValueRef *operator->() {return&item; }inline QJsonValueRef operator[](qsizetype j)const{return*(*this+ j); }#if QT_CORE_REMOVED_SINCE(6, 8)inlinebooloperator==(const iterator &other)const{return item.d == other.item.d && item.index == other.item.index; }inlinebooloperator!=(const iterator &other)const{return!operator==(other); }booloperator<(const iterator& other)const{Q_ASSERT(item.d == other.item.d);return item.index < other.item.index; }booloperator<=(const iterator& other)const{Q_ASSERT(item.d == other.item.d);return item.index <= other.item.index; }booloperator>(const iterator& other)const{return!operator<=(other); }booloperator>=(const iterator& other)const{return!operator<(other); }#endifinline iterator &operator++() { ++item.index;return*this; }inline iterator operator++(int) { iterator r = *this; ++item.index;return r; }inline iterator &operator--() { --item.index;return*this; }inline iterator operator--(int) { iterator r = *this; --item.index;return r; }inline iterator operator+(qsizetype j)const{ iterator r = *this;return r += j; }inline iterator operator-(qsizetype j)const{returnoperator+(-j); }inline iterator &operator+=(qsizetype j) { item.index +=quint64(j);return*this; }inline iterator &operator-=(qsizetype j) { item.index -=quint64(j);return*this; } qsizetype operator-(iterator j)const{return item.index - j.item.index; }public:#if QT_CORE_REMOVED_SINCE(6, 8)inlinebooloperator==(const const_iterator &other)const{return item.d == other.item.d && item.index == other.item.index; }inlinebooloperator!=(const const_iterator &other)const{return!operator==(other); }booloperator<(const const_iterator& other)const{Q_ASSERT(item.d == other.item.d);return item.index < other.item.index; }booloperator<=(const const_iterator& other)const{Q_ASSERT(item.d == other.item.d);return item.index <= other.item.index; }booloperator>(const const_iterator& other)const{returnoperator<=(other); }booloperator>=(const const_iterator& other)const{returnoperator<(other); }#endifprivate:// Helper functionsstatic boolcomparesEqual_helper(const iterator &lhs,const iterator &rhs) noexcept {return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index;}static boolcomparesEqual_helper(const iterator &lhs,const const_iterator &rhs) noexcept {return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index;}staticQt::strong_ordering compareThreeWay_helper(const iterator &lhs,const iterator &rhs){Q_ASSERT(lhs.item.d == rhs.item.d);returnQt::compareThreeWay(lhs.item.index, rhs.item.index);}staticQt::strong_ordering compareThreeWay_helper(const iterator &lhs,const const_iterator &rhs){Q_ASSERT(lhs.item.d == rhs.item.d);returnQt::compareThreeWay(lhs.item.index, rhs.item.index);}// Compare friendsfriendboolcomparesEqual(const iterator &lhs,const iterator &rhs) noexcept {returncomparesEqual_helper(lhs, rhs);}friend Qt::strong_ordering compareThreeWay(const iterator &lhs,const iterator &rhs){returncompareThreeWay_helper(lhs, rhs);}Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(iterator)friendboolcomparesEqual(const iterator &lhs,const const_iterator &rhs) noexcept {returncomparesEqual_helper(lhs, rhs);}friend Qt::strong_ordering compareThreeWay(const iterator &lhs,const const_iterator &rhs){returncompareThreeWay_helper(lhs, rhs);}Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(iterator, const_iterator)};friend class iterator;class const_iterator {friend class iterator; QJsonValueConstRef item;public:typedef std::random_access_iterator_tag iterator_category;typedef qsizetype difference_type;typedef QJsonValue value_type;typedefconst QJsonValueConstRef reference;typedefconst QJsonValueConstRef *pointer;inlineconst_iterator() :item(static_cast<QJsonObject*>(nullptr),0) { }inlineconst_iterator(const QJsonObject *obj, qsizetype index):item(const_cast<QJsonObject*>(obj), index) { }inlineconst_iterator(const iterator &other):item(other.item) { }constexprconst_iterator(const const_iterator &other) =default; const_iterator &operator=(const const_iterator &other){ item.rebind(other.item);return*this;}inline QString key()const{return item.objectKey(); } QAnyStringView keyView()const{return item.objectKeyView(); }inline QJsonValueConstRef value()const{return item; }inlineconst QJsonValueConstRef operator*()const{return item; }inlineconst QJsonValueConstRef *operator->()const{return&item; }inline QJsonValueConstRef operator[](qsizetype j)const{return*(*this+ j); }#if QT_CORE_REMOVED_SINCE(6, 8)inlinebooloperator==(const const_iterator &other)const{return item.d == other.item.d && item.index == other.item.index; }inlinebooloperator!=(const const_iterator &other)const{return!operator==(other); }booloperator<(const const_iterator& other)const{Q_ASSERT(item.d == other.item.d);return item.index < other.item.index; }booloperator<=(const const_iterator& other)const{Q_ASSERT(item.d == other.item.d);return item.index <= other.item.index; }booloperator>(const const_iterator& other)const{return!operator<=(other); }booloperator>=(const const_iterator& other)const{return!operator<(other); }#endifinline const_iterator &operator++() { ++item.index;return*this; }inline const_iterator operator++(int) { const_iterator r = *this; ++item.index;return r; }inline const_iterator &operator--() { --item.index;return*this; }inline const_iterator operator--(int) { const_iterator r = *this; --item.index;return r; }inline const_iterator operator+(qsizetype j)const{ const_iterator r = *this;return r += j; }inline const_iterator operator-(qsizetype j)const{returnoperator+(-j); }inline const_iterator &operator+=(qsizetype j) { item.index +=quint64(j);return*this; }inline const_iterator &operator-=(qsizetype j) { item.index -=quint64(j);return*this; } qsizetype operator-(const_iterator j)const{return item.index - j.item.index; }#if QT_CORE_REMOVED_SINCE(6, 8)inlinebooloperator==(const iterator &other)const{return item.d == other.item.d && item.index == other.item.index; }inlinebooloperator!=(const iterator &other)const{return!operator==(other); }booloperator<(const iterator& other)const{Q_ASSERT(item.d == other.item.d);return item.index < other.item.index; }booloperator<=(const iterator& other)const{Q_ASSERT(item.d == other.item.d);return item.index <= other.item.index; }booloperator>(const iterator& other)const{return!operator<=(other); }booloperator>=(const iterator& other)const{return!operator<(other); }#endifprivate:// Helper functionsstatic boolcomparesEqual_helper(const const_iterator &lhs,const const_iterator &rhs) noexcept {return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index;}staticQt::strong_ordering compareThreeWay_helper(const const_iterator &lhs,const const_iterator &rhs){Q_ASSERT(lhs.item.d == rhs.item.d);returnQt::compareThreeWay(lhs.item.index, rhs.item.index);}// Compare friendsfriendboolcomparesEqual(const const_iterator &lhs,const const_iterator &rhs) noexcept {returncomparesEqual_helper(lhs, rhs);}friend Qt::strong_ordering compareThreeWay(const const_iterator &lhs,const const_iterator &rhs){returncompareThreeWay_helper(lhs, rhs);}Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(const_iterator)};friend class const_iterator;// STL styleinline iterator begin() {detach();returniterator(this,0); }inline const_iterator begin()const{returnconst_iterator(this,0); }inline const_iterator constBegin()const{returnconst_iterator(this,0); }inline iterator end() {detach();returniterator(this,size()); }inline const_iterator end()const{returnconst_iterator(this,size()); }inline const_iterator constEnd()const{returnconst_iterator(this,size()); } iterator erase(iterator it);// more Qttypedef iterator Iterator;typedef const_iterator ConstIterator; iterator find(const QString &key); const_iterator find(const QString &key)const{returnconstFind(key); } const_iterator constFind(const QString &key)const; iterator insert(const QString &key,const QJsonValue &value); iterator find(QStringView key); iterator find(QLatin1StringView key); const_iterator find(QStringView key)const{returnconstFind(key); } const_iterator find(QLatin1StringView key)const{returnconstFind(key); } const_iterator constFind(QStringView key)const; const_iterator constFind(QLatin1StringView key)const; iterator insert(QStringView key,const QJsonValue &value); iterator insert(QLatin1StringView key,const QJsonValue &value);// STL compatibilitytypedef QJsonValue mapped_type;typedef QString key_type;typedef qsizetype size_type;inlineboolempty()const{returnisEmpty(); }private:friend Q_CORE_EXPORT boolcomparesEqual(const QJsonObject &lhs,const QJsonObject &rhs);friendboolcomparesEqual(const QJsonObject &lhs,const QJsonValue &rhs){returncomparesEqual(lhs, rhs.toObject());}friendboolcomparesEqual(const QJsonObject &lhs,const QJsonValueConstRef &rhs){returncomparesEqual(lhs, rhs.toObject());}Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonObject)Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonObject, QJsonValue)Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonObject, QJsonValueConstRef)friend class QJsonValue;friend class QJsonDocument;friend class QJsonPrivate::Value;friend class QJsonValueConstRef;friend class QJsonValueRef;friend class QCborMap;friend Q_CORE_EXPORT QDebug operator<<(QDebug,const QJsonObject &);QJsonObject(QCborContainerPrivate *object);booldetach(qsizetype reserve =0);template<typename T> QJsonValue valueImpl(T key)const;template<typename T> QJsonValueRef atImpl(T key);template<typename T>voidremoveImpl(T key);template<typename T> QJsonValue takeImpl(T key);template<typename T>boolcontainsImpl(T key)const;template<typename T> iterator findImpl(T key);template<typename T> const_iterator constFindImpl(T key)const;template<typename T> iterator insertImpl(T key,const QJsonValue &value);#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED) QString keyAt(qsizetype i)const; QJsonValue valueAt(qsizetype i)const;voidsetValueAt(qsizetype i,const QJsonValue &val);#endifvoidremoveAt(qsizetype i);template<typename T> iterator insertAt(qsizetype i, T key,const QJsonValue &val,bool exists); QExplicitlySharedDataPointer<QCborContainerPrivate> o;};Q_DECLARE_SHARED(QJsonObject)#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)inline QJsonValueConstRef::QJsonValueConstRef(QJsonObject *o, qsizetype idx):d(o ? o->o.data() :nullptr),is_object(true),index(idx){}#endif Q_CORE_EXPORT size_tqHash(const QJsonObject &object,size_t seed =0);#if !defined(QT_NO_DEBUG_STREAM) Q_CORE_EXPORT QDebug operator<<(QDebug,const QJsonObject &);#endif#ifndef QT_NO_DATASTREAM Q_CORE_EXPORT QDataStream &operator<<(QDataStream &,const QJsonObject &); Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonObject &);#endif QT_END_NAMESPACE #endif// QJSONOBJECT_H
close