File indexing completed on 2026-01-03 10:16:31
0001
0002
0003
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
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
0137 static_assert(sizeof(QExplicitlySharedDataPointer<QCborContainerPrivate>) == sizeof(void *));
0138 static_assert(sizeof(QCborValue::Type) == sizeof(QJsonValue::Type));
0139 };
0140
0141 Q_DECLARE_SHARED(QJsonValue)
0142
0143 class QJsonValueConstRef
0144 {
0145 public:
0146 QJsonValueConstRef(const QJsonValueConstRef &) = default;
0147 QJsonValueConstRef &operator=(const QJsonValueConstRef &) = delete;
0148 inline operator QJsonValue() const { return concrete(*this); }
0149
0150 Q_CORE_EXPORT QVariant toVariant() const;
0151 QJsonValue::Type type() const { return concreteType(*this); }
0152 bool isNull() const { return type() == QJsonValue::Null; }
0153 bool isBool() const { return type() == QJsonValue::Bool; }
0154 bool isDouble() const { return type() == QJsonValue::Double; }
0155 bool isString() const { return type() == QJsonValue::String; }
0156 bool isArray() const { return type() == QJsonValue::Array; }
0157 bool isObject() const { return type() == QJsonValue::Object; }
0158 bool isUndefined() const { return type() == QJsonValue::Undefined; }
0159
0160 bool toBool(bool defaultValue = false) const
0161 { return concreteBool(*this, defaultValue); }
0162 int toInt(int defaultValue = 0) const
0163 { return int(concreteInt(*this, defaultValue, true)); }
0164 qint64 toInteger(qint64 defaultValue = 0) const
0165 { return concreteInt(*this, defaultValue, false); }
0166 double toDouble(double defaultValue = 0) const
0167 { return concreteDouble(*this, defaultValue); }
0168 QString toString(const QString &defaultValue = {}) const
0169 { return concreteString(*this, defaultValue); }
0170 QAnyStringView toStringView(QAnyStringView defaultValue = {}) const
0171 { return concreteStringView(*this, defaultValue); }
0172 Q_CORE_EXPORT QJsonArray toArray() const;
0173 Q_CORE_EXPORT QJsonObject toObject() const;
0174
0175 const QJsonValue operator[](QStringView key) const { return concrete(*this)[key]; }
0176 const QJsonValue operator[](QLatin1StringView key) const { return concrete(*this)[key]; }
0177 const QJsonValue operator[](qsizetype i) const { return concrete(*this)[i]; }
0178
0179 protected:
0180 friend bool comparesEqual(const QJsonValueConstRef &lhs,
0181 const QJsonValueConstRef &rhs)
0182 {
0183 return comparesEqual(concrete(lhs), concrete(rhs));
0184 }
0185 friend bool comparesEqual(const QJsonValueConstRef &lhs,
0186 const QJsonValue &rhs)
0187 {
0188 return comparesEqual(concrete(lhs), rhs);
0189 }
0190 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonValueConstRef)
0191 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonValueConstRef, QJsonValue)
0192
0193 Q_CORE_EXPORT static QJsonValue::Type
0194 concreteType(QJsonValueConstRef self) noexcept Q_DECL_PURE_FUNCTION;
0195 Q_CORE_EXPORT static bool
0196 concreteBool(QJsonValueConstRef self, bool defaultValue) noexcept Q_DECL_PURE_FUNCTION;
0197 Q_CORE_EXPORT static qint64
0198 concreteInt(QJsonValueConstRef self, qint64 defaultValue, bool clamp) noexcept Q_DECL_PURE_FUNCTION;
0199 Q_CORE_EXPORT static double
0200 concreteDouble(QJsonValueConstRef self, double defaultValue) noexcept Q_DECL_PURE_FUNCTION;
0201 Q_CORE_EXPORT static QString concreteString(QJsonValueConstRef self, const QString &defaultValue);
0202 Q_CORE_EXPORT static QAnyStringView concreteStringView(QJsonValueConstRef self, QAnyStringView defaultValue);
0203 Q_CORE_EXPORT static QJsonValue concrete(QJsonValueConstRef self) noexcept;
0204
0205
0206 Q_CORE_EXPORT static QString objectKey(QJsonValueConstRef self);
0207 QString objectKey() const { return objectKey(*this); }
0208
0209 Q_CORE_EXPORT static QAnyStringView objectKeyView(QJsonValueConstRef self);
0210 QAnyStringView objectKeyView() const { return objectKeyView(*this); }
0211
0212 #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED)
0213 QJsonValueConstRef(QJsonArray *array, qsizetype idx)
0214 : a(array), is_object(false), index(static_cast<quint64>(idx)) {}
0215 QJsonValueConstRef(QJsonObject *object, qsizetype idx)
0216 : o(object), is_object(true), index(static_cast<quint64>(idx)) {}
0217
0218 void rebind(QJsonValueConstRef other)
0219 {
0220 Q_ASSERT(is_object == other.is_object);
0221 if (is_object)
0222 o = other.o;
0223 else
0224 a = other.a;
0225 index = other.index;
0226 }
0227
0228 union {
0229 QJsonArray *a;
0230 QJsonObject *o;
0231 void *d;
0232 };
0233 quint64 is_object : 1;
0234 quint64 index : 63;
0235 #else
0236 constexpr QJsonValueConstRef(QCborContainerPrivate *d, size_t index, bool is_object)
0237 : d(d), is_object(is_object), index(index)
0238 {}
0239
0240
0241 QJsonValueConstRef(QJsonArray *array, qsizetype idx);
0242 QJsonValueConstRef(QJsonObject *object, qsizetype idx);
0243
0244 void rebind(QJsonValueConstRef other)
0245 {
0246 d = other.d;
0247 index = other.index;
0248 }
0249
0250 QCborContainerPrivate *d = nullptr;
0251 size_t is_object : 1;
0252 size_t index : std::numeric_limits<size_t>::digits - 1;
0253 #endif
0254
0255 friend class QJsonArray;
0256 friend class QJsonObject;
0257 friend class QJsonPrivate::Value;
0258 };
0259
0260 QT_WARNING_PUSH
0261 QT6_ONLY(QT_WARNING_DISABLE_MSVC(4275))
0262 class QT6_ONLY(Q_CORE_EXPORT) QJsonValueRef : public QJsonValueConstRef
0263 {
0264 public:
0265 QJsonValueRef(const QJsonValueRef &) = default;
0266 QT7_ONLY(Q_CORE_EXPORT) QJsonValueRef &operator = (const QJsonValue &val);
0267 QT7_ONLY(Q_CORE_EXPORT) QJsonValueRef &operator = (const QJsonValueRef &val);
0268
0269 #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED)
0270
0271
0272
0273 QJsonValueRef(QJsonArray *array, qsizetype idx)
0274 : QJsonValueConstRef(array, idx) {}
0275 QJsonValueRef(QJsonObject *object, qsizetype idx)
0276 : QJsonValueConstRef(object, idx) {}
0277
0278 operator QJsonValue() const { return toValue(); }
0279
0280 QVariant toVariant() const;
0281 inline QJsonValue::Type type() const { return QJsonValueConstRef::type(); }
0282 inline bool isNull() const { return type() == QJsonValue::Null; }
0283 inline bool isBool() const { return type() == QJsonValue::Bool; }
0284 inline bool isDouble() const { return type() == QJsonValue::Double; }
0285 inline bool isString() const { return type() == QJsonValue::String; }
0286 inline bool isArray() const { return type() == QJsonValue::Array; }
0287 inline bool isObject() const { return type() == QJsonValue::Object; }
0288 inline bool isUndefined() const { return type() == QJsonValue::Undefined; }
0289
0290 inline bool toBool(bool defaultValue = false) const { return QJsonValueConstRef::toBool(defaultValue); }
0291 inline int toInt(int defaultValue = 0) const { return QJsonValueConstRef::toInt(defaultValue); }
0292 inline qint64 toInteger(qint64 defaultValue = 0) const { return QJsonValueConstRef::toInteger(defaultValue); }
0293 inline double toDouble(double defaultValue = 0) const { return QJsonValueConstRef::toDouble(defaultValue); }
0294 inline QString toString(const QString &defaultValue = {}) const { return QJsonValueConstRef::toString(defaultValue); }
0295 QAnyStringView toStringView(QAnyStringView defaultValue = {}) const
0296 { return QJsonValueConstRef::toStringView(defaultValue); }
0297 QJsonArray toArray() const;
0298 QJsonObject toObject() const;
0299
0300 const QJsonValue operator[](QStringView key) const { return QJsonValueConstRef::operator[](key); }
0301 const QJsonValue operator[](QLatin1StringView key) const { return QJsonValueConstRef::operator[](key); }
0302 const QJsonValue operator[](qsizetype i) const { return QJsonValueConstRef::operator[](i); }
0303
0304 #if QT_CORE_REMOVED_SINCE(6, 8)
0305 inline bool operator==(const QJsonValue &other) const { return comparesEqual(*this, other); }
0306 inline bool operator!=(const QJsonValue &other) const { return !comparesEqual(*this, other); }
0307 #endif
0308
0309 private:
0310 friend bool comparesEqual(const QJsonValueRef &lhs, const QJsonValueRef &rhs)
0311 {
0312 return comparesEqual(QJsonValue(lhs), QJsonValue(rhs));
0313 }
0314 friend bool comparesEqual(const QJsonValueRef &lhs, const QJsonValueConstRef &rhs)
0315 {
0316 return comparesEqual(QJsonValue(lhs), QJsonValue(rhs));
0317 }
0318 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonValueRef)
0319 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonValueRef, QJsonValueConstRef)
0320
0321 QJsonValue toValue() const;
0322 #else
0323 using QJsonValueConstRef::operator[];
0324 Q_CORE_EXPORT QJsonValueRef operator[](QAnyStringView key);
0325 Q_CORE_EXPORT QJsonValueRef operator[](qsizetype i);
0326
0327 private:
0328 using QJsonValueConstRef::QJsonValueConstRef;
0329 #endif
0330
0331 QT7_ONLY(Q_CORE_EXPORT) void detach();
0332 friend class QJsonArray;
0333 friend class QJsonObject;
0334 };
0335 QT_WARNING_POP
0336
0337 inline QJsonValue QCborValueConstRef::toJsonValue() const
0338 {
0339 return concrete().toJsonValue();
0340 }
0341
0342 Q_CORE_EXPORT size_t qHash(const QJsonValue &value, size_t seed = 0);
0343
0344 #if !defined(QT_NO_DEBUG_STREAM)
0345 Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &);
0346 #endif
0347
0348 #ifndef QT_NO_DATASTREAM
0349 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonValue &);
0350 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonValue &);
0351 #endif
0352
0353 QT_END_NAMESPACE
0354
0355 #endif