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