Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-02 09:18:54

0001 // Copyright (C) 2016 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 // Qt-Security score:critical reason:data-parser
0004 
0005 #ifndef QJSONVALUE_H
0006 #define QJSONVALUE_H
0007 
0008 #include <QtCore/qcborvalue.h>
0009 #include <QtCore/qcompare.h>
0010 #include <QtCore/qglobal.h>
0011 #if (QT_VERSION < QT_VERSION_CHECK(7, 0, 0)) && !defined(QT_BOOTSTRAPPED)
0012 #include <QtCore/qjsondocument.h>
0013 #endif
0014 #include <QtCore/qjsonparseerror.h>
0015 #include <QtCore/qstring.h>
0016 #include <QtCore/qshareddata.h>
0017 
0018 QT_BEGIN_NAMESPACE
0019 
0020 class QVariant;
0021 class QJsonArray;
0022 class QJsonObject;
0023 class QCborContainerPrivate;
0024 
0025 namespace QJsonPrivate {
0026 class Value;
0027 }
0028 
0029 class Q_CORE_EXPORT QJsonValue
0030 {
0031 public:
0032     enum Type {
0033         Null =  0x0,
0034         Bool = 0x1,
0035         Double = 0x2,
0036         String = 0x3,
0037         Array = 0x4,
0038         Object = 0x5,
0039         Undefined = 0x80
0040     };
0041 
0042 #if (QT_VERSION < QT_VERSION_CHECK(7, 0, 0)) && !defined(QT_BOOTSTRAPPED)
0043     using JsonFormat = QJsonDocument::JsonFormat;
0044 #else
0045     enum class JsonFormat {
0046         Indented,
0047         Compact,
0048     };
0049 #endif
0050 
0051     QJsonValue(Type = Null);
0052     QJsonValue(bool b);
0053     QJsonValue(double n);
0054     QJsonValue(int n);
0055     QJsonValue(qint64 v);
0056     QJsonValue(const QString &s);
0057     QJsonValue(QLatin1StringView s);
0058 #ifndef QT_NO_CAST_FROM_ASCII
0059     QT_ASCII_CAST_WARN inline QJsonValue(const char *s)
0060         : QJsonValue(QString::fromUtf8(s)) {}
0061 #endif
0062     QJsonValue(const QJsonArray &a);
0063     QJsonValue(QJsonArray &&a) noexcept;
0064     QJsonValue(const QJsonObject &o);
0065     QJsonValue(QJsonObject &&o) noexcept;
0066 
0067     ~QJsonValue();
0068 
0069     QJsonValue(const QJsonValue &other) noexcept;
0070     QJsonValue &operator =(const QJsonValue &other) noexcept;
0071 
0072     QJsonValue(QJsonValue &&other) noexcept;
0073 
0074     QJsonValue &operator =(QJsonValue &&other) noexcept
0075     {
0076         swap(other);
0077         return *this;
0078     }
0079 
0080     void swap(QJsonValue &other) noexcept;
0081 
0082     static QJsonValue fromVariant(const QVariant &variant);
0083     QVariant toVariant() const;
0084 
0085     static QJsonValue fromJson(QByteArrayView json, QJsonParseError *error = nullptr);
0086 
0087     QByteArray toJson(JsonFormat format = JsonFormat::Indented) const;
0088 
0089     Type type() const;
0090     inline bool isNull() const { return type() == Null; }
0091     inline bool isBool() const { return type() == Bool; }
0092     inline bool isDouble() const { return type() == Double; }
0093     inline bool isString() const { return type() == String; }
0094     inline bool isArray() const { return type() == Array; }
0095     inline bool isObject() const { return type() == Object; }
0096     inline bool isUndefined() const { return type() == Undefined; }
0097 
0098     bool toBool(bool defaultValue = false) const;
0099     int toInt(int defaultValue = 0) const;
0100     qint64 toInteger(qint64 defaultValue = 0) const;
0101     double toDouble(double defaultValue = 0) const;
0102     QString toString() const;
0103     QString toString(const QString &defaultValue) const;
0104     QAnyStringView toStringView(QAnyStringView defaultValue = {}) const;
0105     QJsonArray toArray() const;
0106     QJsonArray toArray(const QJsonArray &defaultValue) const;
0107     QJsonObject toObject() const;
0108     QJsonObject toObject(const QJsonObject &defaultValue) const;
0109 
0110     const QJsonValue operator[](const QString &key) const;
0111     const QJsonValue operator[](QStringView key) const;
0112     const QJsonValue operator[](QLatin1StringView key) const;
0113     const QJsonValue operator[](qsizetype i) const;
0114 
0115 #if QT_CORE_REMOVED_SINCE(6, 8)
0116     bool operator==(const QJsonValue &other) const;
0117     bool operator!=(const QJsonValue &other) const;
0118 #endif
0119 
0120 private:
0121     friend Q_CORE_EXPORT bool comparesEqual(const QJsonValue &lhs,
0122                                             const QJsonValue &rhs);
0123     Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonValue)
0124 
0125     // avoid implicit conversions from char * to bool
0126     QJsonValue(const void *) = delete;
0127     friend class QJsonPrivate::Value;
0128     friend class QJsonArray;
0129     friend class QJsonObject;
0130     friend class QCborValue;
0131     friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &);
0132     friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonValue &);
0133 
0134     QCborValue value;
0135 
0136     // Assert binary compatibility with pre-5.15 QJsonValue
0137     static_assert(sizeof(QExplicitlySharedDataPointer<QCborContainerPrivate>) == sizeof(void *));
0138     static_assert(sizeof(QCborValue::Type) == sizeof(QJsonValue::Type));
0139 };
0140 Q_DECLARE_SHARED(QJsonValue)
0141 
0142 Q_CORE_EXPORT size_t qHash(const QJsonValue &value, size_t seed = 0);
0143 
0144 class QJsonValueConstRef
0145 {
0146 public:
0147     QJsonValueConstRef(const QJsonValueConstRef &) = default;
0148     QJsonValueConstRef &operator=(const QJsonValueConstRef &) = delete;
0149     inline operator QJsonValue() const { return concrete(*this); }
0150 
0151     Q_CORE_EXPORT QVariant toVariant() const;
0152     QJsonValue::Type type() const { return concreteType(*this); }
0153     bool isNull() const { return type() == QJsonValue::Null; }
0154     bool isBool() const { return type() == QJsonValue::Bool; }
0155     bool isDouble() const { return type() == QJsonValue::Double; }
0156     bool isString() const { return type() == QJsonValue::String; }
0157     bool isArray() const { return type() == QJsonValue::Array; }
0158     bool isObject() const { return type() == QJsonValue::Object; }
0159     bool isUndefined() const { return type() == QJsonValue::Undefined; }
0160 
0161     bool toBool(bool defaultValue = false) const
0162     { return concreteBool(*this, defaultValue); }
0163     int toInt(int defaultValue = 0) const
0164     { return int(concreteInt(*this, defaultValue, true)); }
0165     qint64 toInteger(qint64 defaultValue = 0) const
0166     { return concreteInt(*this, defaultValue, false); }
0167     double toDouble(double defaultValue = 0) const
0168     { return concreteDouble(*this, defaultValue); }
0169     QString toString(const QString &defaultValue = {}) const
0170     { return concreteString(*this, defaultValue); }
0171     QAnyStringView toStringView(QAnyStringView defaultValue = {}) const
0172     { return concreteStringView(*this, defaultValue); }
0173     Q_CORE_EXPORT QJsonArray toArray() const;
0174     Q_CORE_EXPORT QJsonObject toObject() const;
0175 
0176     const QJsonValue operator[](QStringView key) const { return concrete(*this)[key]; }
0177     const QJsonValue operator[](QLatin1StringView key) const { return concrete(*this)[key]; }
0178     const QJsonValue operator[](qsizetype i) const { return concrete(*this)[i]; }
0179 
0180 protected:
0181     friend size_t qHash(const QJsonValueConstRef &key, size_t seed = 0) noexcept
0182     { return QHashPrivate::ex1to2arg(concrete(key), seed); }
0183 
0184     friend bool comparesEqual(const QJsonValueConstRef &lhs,
0185                               const QJsonValueConstRef &rhs)
0186     {
0187         return comparesEqual(concrete(lhs), concrete(rhs));
0188     }
0189     friend bool comparesEqual(const QJsonValueConstRef &lhs,
0190                               const QJsonValue &rhs)
0191     {
0192         return comparesEqual(concrete(lhs), rhs);
0193     }
0194     Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonValueConstRef)
0195     Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonValueConstRef, QJsonValue)
0196 
0197     Q_CORE_EXPORT static QJsonValue::Type
0198     Q_DECL_PURE_FUNCTION concreteType(QJsonValueConstRef self) noexcept;
0199     Q_CORE_EXPORT static bool
0200     Q_DECL_PURE_FUNCTION concreteBool(QJsonValueConstRef self, bool defaultValue) noexcept;
0201     Q_CORE_EXPORT static qint64
0202     Q_DECL_PURE_FUNCTION concreteInt(QJsonValueConstRef self, qint64 defaultValue, bool clamp) noexcept;
0203     Q_CORE_EXPORT static double
0204     Q_DECL_PURE_FUNCTION concreteDouble(QJsonValueConstRef self, double defaultValue) noexcept;
0205     Q_CORE_EXPORT static QString concreteString(QJsonValueConstRef self, const QString &defaultValue);
0206     Q_CORE_EXPORT static QAnyStringView concreteStringView(QJsonValueConstRef self, QAnyStringView defaultValue);
0207     Q_CORE_EXPORT static QJsonValue concrete(QJsonValueConstRef self) noexcept;
0208 
0209     // for iterators
0210     Q_CORE_EXPORT static QString objectKey(QJsonValueConstRef self);
0211     QString objectKey() const { return objectKey(*this); }
0212 
0213     Q_CORE_EXPORT static QAnyStringView objectKeyView(QJsonValueConstRef self);
0214     QAnyStringView objectKeyView() const { return objectKeyView(*this); }
0215 
0216 #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED)
0217     QJsonValueConstRef(QJsonArray *array, qsizetype idx)
0218         : a(array), is_object(false), index(static_cast<quint64>(idx)) {}
0219     QJsonValueConstRef(QJsonObject *object, qsizetype idx)
0220         : o(object), is_object(true), index(static_cast<quint64>(idx)) {}
0221 
0222     void rebind(QJsonValueConstRef other)
0223     {
0224         Q_ASSERT(is_object == other.is_object);
0225         if (is_object)
0226             o = other.o;
0227         else
0228             a = other.a;
0229         index = other.index;
0230     }
0231 
0232     union {
0233         QJsonArray *a;
0234         QJsonObject *o;
0235         void *d;
0236     };
0237     quint64 is_object : 1;
0238     quint64 index : 63;
0239 #else
0240     constexpr QJsonValueConstRef(QCborContainerPrivate *d, size_t index, bool is_object)
0241         : d(d), is_object(is_object), index(index)
0242     {}
0243 
0244     // implemented in qjsonarray.h & qjsonobject.h, to get their d
0245     QJsonValueConstRef(QJsonArray *array, qsizetype idx);
0246     QJsonValueConstRef(QJsonObject *object, qsizetype idx);
0247 
0248     void rebind(QJsonValueConstRef other)
0249     {
0250         d = other.d;
0251         index = other.index;
0252     }
0253 
0254     QCborContainerPrivate *d = nullptr;
0255     size_t is_object : 1;
0256     size_t index : std::numeric_limits<size_t>::digits - 1;
0257 #endif
0258 
0259     friend class QJsonArray;
0260     friend class QJsonObject;
0261     friend class QJsonPrivate::Value;
0262 };
0263 
0264 QT_WARNING_PUSH
0265 QT6_ONLY(QT_WARNING_DISABLE_MSVC(4275)) // non dll-interface class 'QJsonValueConstRef' used as base for dll-interface class 'QJsonValueRef'
0266 class QT6_ONLY(Q_CORE_EXPORT) QJsonValueRef : public QJsonValueConstRef
0267 {
0268 public:
0269     QJsonValueRef(const QJsonValueRef &) = default;
0270     QT7_ONLY(Q_CORE_EXPORT) QJsonValueRef &operator = (const QJsonValue &val);
0271     QT7_ONLY(Q_CORE_EXPORT) QJsonValueRef &operator = (const QJsonValueRef &val);
0272 
0273 #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED)
0274     // retained for binary compatibility (due to the Q_CORE_EXPORT) because at
0275     // least one compiler emits and exports all inlines in an exported class
0276 
0277     QJsonValueRef(QJsonArray *array, qsizetype idx)
0278         : QJsonValueConstRef(array, idx) {}
0279     QJsonValueRef(QJsonObject *object, qsizetype idx)
0280         : QJsonValueConstRef(object, idx) {}
0281 
0282     operator QJsonValue() const { return toValue(); }
0283 
0284     QVariant toVariant() const;
0285     inline QJsonValue::Type type() const { return QJsonValueConstRef::type(); }
0286     inline bool isNull() const { return type() == QJsonValue::Null; }
0287     inline bool isBool() const { return type() == QJsonValue::Bool; }
0288     inline bool isDouble() const { return type() == QJsonValue::Double; }
0289     inline bool isString() const { return type() == QJsonValue::String; }
0290     inline bool isArray() const { return type() == QJsonValue::Array; }
0291     inline bool isObject() const { return type() == QJsonValue::Object; }
0292     inline bool isUndefined() const { return type() == QJsonValue::Undefined; }
0293 
0294     inline bool toBool(bool defaultValue = false) const { return QJsonValueConstRef::toBool(defaultValue); }
0295     inline int toInt(int defaultValue = 0) const { return QJsonValueConstRef::toInt(defaultValue); }
0296     inline qint64 toInteger(qint64 defaultValue = 0) const { return QJsonValueConstRef::toInteger(defaultValue); }
0297     inline double toDouble(double defaultValue = 0) const { return QJsonValueConstRef::toDouble(defaultValue); }
0298     inline QString toString(const QString &defaultValue = {}) const { return QJsonValueConstRef::toString(defaultValue); }
0299     QAnyStringView toStringView(QAnyStringView defaultValue = {}) const
0300     { return QJsonValueConstRef::toStringView(defaultValue); }
0301     QJsonArray toArray() const;
0302     QJsonObject toObject() const;
0303 
0304     const QJsonValue operator[](QStringView key) const { return QJsonValueConstRef::operator[](key); }
0305     const QJsonValue operator[](QLatin1StringView key) const { return QJsonValueConstRef::operator[](key); }
0306     const QJsonValue operator[](qsizetype i) const { return QJsonValueConstRef::operator[](i); }
0307 
0308 #if QT_CORE_REMOVED_SINCE(6, 8)
0309     inline bool operator==(const QJsonValue &other) const { return comparesEqual(*this, other); }
0310     inline bool operator!=(const QJsonValue &other) const { return !comparesEqual(*this, other); }
0311 #endif
0312 
0313 private:
0314     friend bool comparesEqual(const QJsonValueRef &lhs, const QJsonValueRef &rhs)
0315     {
0316         return comparesEqual(QJsonValue(lhs), QJsonValue(rhs));
0317     }
0318     friend bool comparesEqual(const QJsonValueRef &lhs, const QJsonValueConstRef &rhs)
0319     {
0320         return comparesEqual(QJsonValue(lhs), QJsonValue(rhs));
0321     }
0322     Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonValueRef)
0323     Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonValueRef, QJsonValueConstRef)
0324 
0325     QJsonValue toValue() const;
0326 #else
0327     using QJsonValueConstRef::operator[];
0328     Q_CORE_EXPORT QJsonValueRef operator[](QAnyStringView key);
0329     Q_CORE_EXPORT QJsonValueRef operator[](qsizetype i);
0330 
0331 private:
0332     using QJsonValueConstRef::QJsonValueConstRef;
0333 #endif // < Qt 7
0334 
0335     QT7_ONLY(Q_CORE_EXPORT) void detach();
0336     friend class QJsonArray;
0337     friend class QJsonObject;
0338 };
0339 QT_WARNING_POP
0340 
0341 inline QJsonValue QCborValueConstRef::toJsonValue() const
0342 {
0343     return concrete().toJsonValue();
0344 }
0345 
0346 #if !defined(QT_NO_DEBUG_STREAM)
0347 Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &);
0348 #endif
0349 
0350 #ifndef QT_NO_DATASTREAM
0351 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonValue &);
0352 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonValue &);
0353 #endif
0354 
0355 QT_END_NAMESPACE
0356 
0357 #endif // QJSONVALUE_H