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