| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QJSONVALUE_H |
| 5 | #define QJSONVALUE_H |
| 6 | |
| 7 | #include <QtCore/qcborvalue.h> |
| 8 | #include <QtCore/qcompare.h> |
| 9 | #include <QtCore/qglobal.h> |
| 10 | #include <QtCore/qstring.h> |
| 11 | #include <QtCore/qshareddata.h> |
| 12 | |
| 13 | QT_BEGIN_NAMESPACE |
| 14 | |
| 15 | class QVariant; |
| 16 | class QJsonArray; |
| 17 | class QJsonObject; |
| 18 | class QCborContainerPrivate; |
| 19 | |
| 20 | namespace QJsonPrivate { |
| 21 | class Value; |
| 22 | } |
| 23 | |
| 24 | class Q_CORE_EXPORT QJsonValue |
| 25 | { |
| 26 | public: |
| 27 | enum Type { |
| 28 | Null = 0x0, |
| 29 | Bool = 0x1, |
| 30 | Double = 0x2, |
| 31 | String = 0x3, |
| 32 | Array = 0x4, |
| 33 | Object = 0x5, |
| 34 | Undefined = 0x80 |
| 35 | }; |
| 36 | |
| 37 | QJsonValue(Type = Null); |
| 38 | QJsonValue(bool b); |
| 39 | QJsonValue(double n); |
| 40 | QJsonValue(int n); |
| 41 | QJsonValue(qint64 v); |
| 42 | QJsonValue(const QString &s); |
| 43 | QJsonValue(QLatin1StringView s); |
| 44 | #ifndef QT_NO_CAST_FROM_ASCII |
| 45 | QT_ASCII_CAST_WARN inline QJsonValue(const char *s) |
| 46 | : QJsonValue(QString::fromUtf8(utf8: s)) {} |
| 47 | #endif |
| 48 | QJsonValue(const QJsonArray &a); |
| 49 | QJsonValue(QJsonArray &&a) noexcept; |
| 50 | QJsonValue(const QJsonObject &o); |
| 51 | QJsonValue(QJsonObject &&o) noexcept; |
| 52 | |
| 53 | ~QJsonValue(); |
| 54 | |
| 55 | QJsonValue(const QJsonValue &other) noexcept; |
| 56 | QJsonValue &operator =(const QJsonValue &other) noexcept; |
| 57 | |
| 58 | QJsonValue(QJsonValue &&other) noexcept; |
| 59 | |
| 60 | QJsonValue &operator =(QJsonValue &&other) noexcept |
| 61 | { |
| 62 | swap(other); |
| 63 | return *this; |
| 64 | } |
| 65 | |
| 66 | void swap(QJsonValue &other) noexcept; |
| 67 | |
| 68 | static QJsonValue fromVariant(const QVariant &variant); |
| 69 | QVariant toVariant() const; |
| 70 | |
| 71 | Type type() const; |
| 72 | inline bool isNull() const { return type() == Null; } |
| 73 | inline bool isBool() const { return type() == Bool; } |
| 74 | inline bool isDouble() const { return type() == Double; } |
| 75 | inline bool isString() const { return type() == String; } |
| 76 | inline bool isArray() const { return type() == Array; } |
| 77 | inline bool isObject() const { return type() == Object; } |
| 78 | inline bool isUndefined() const { return type() == Undefined; } |
| 79 | |
| 80 | bool toBool(bool defaultValue = false) const; |
| 81 | int toInt(int defaultValue = 0) const; |
| 82 | qint64 toInteger(qint64 defaultValue = 0) const; |
| 83 | double toDouble(double defaultValue = 0) const; |
| 84 | QString toString() const; |
| 85 | QString toString(const QString &defaultValue) const; |
| 86 | QJsonArray toArray() const; |
| 87 | QJsonArray toArray(const QJsonArray &defaultValue) const; |
| 88 | QJsonObject toObject() const; |
| 89 | QJsonObject toObject(const QJsonObject &defaultValue) const; |
| 90 | |
| 91 | const QJsonValue operator[](const QString &key) const; |
| 92 | const QJsonValue operator[](QStringView key) const; |
| 93 | const QJsonValue operator[](QLatin1StringView key) const; |
| 94 | const QJsonValue operator[](qsizetype i) const; |
| 95 | |
| 96 | #if QT_CORE_REMOVED_SINCE(6, 8) |
| 97 | bool operator==(const QJsonValue &other) const; |
| 98 | bool operator!=(const QJsonValue &other) const; |
| 99 | #endif |
| 100 | |
| 101 | private: |
| 102 | friend Q_CORE_EXPORT bool comparesEqual(const QJsonValue &lhs, |
| 103 | const QJsonValue &rhs); |
| 104 | Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonValue) |
| 105 | |
| 106 | // avoid implicit conversions from char * to bool |
| 107 | QJsonValue(const void *) = delete; |
| 108 | friend class QJsonPrivate::Value; |
| 109 | friend class QJsonArray; |
| 110 | friend class QJsonObject; |
| 111 | friend class QCborValue; |
| 112 | friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &); |
| 113 | friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonValue &); |
| 114 | |
| 115 | QCborValue value; |
| 116 | |
| 117 | // Assert binary compatibility with pre-5.15 QJsonValue |
| 118 | static_assert(sizeof(QExplicitlySharedDataPointer<QCborContainerPrivate>) == sizeof(void *)); |
| 119 | static_assert(sizeof(QCborValue::Type) == sizeof(QJsonValue::Type)); |
| 120 | }; |
| 121 | |
| 122 | Q_DECLARE_SHARED(QJsonValue) |
| 123 | |
| 124 | class QJsonValueConstRef |
| 125 | { |
| 126 | public: |
| 127 | QJsonValueConstRef(const QJsonValueConstRef &) = default; |
| 128 | QJsonValueConstRef &operator=(const QJsonValueConstRef &) = delete; |
| 129 | inline operator QJsonValue() const { return concrete(self: *this); } |
| 130 | |
| 131 | Q_CORE_EXPORT QVariant toVariant() const; |
| 132 | QJsonValue::Type type() const { return concreteType(self: *this); } |
| 133 | bool isNull() const { return type() == QJsonValue::Null; } |
| 134 | bool isBool() const { return type() == QJsonValue::Bool; } |
| 135 | bool isDouble() const { return type() == QJsonValue::Double; } |
| 136 | bool isString() const { return type() == QJsonValue::String; } |
| 137 | bool isArray() const { return type() == QJsonValue::Array; } |
| 138 | bool isObject() const { return type() == QJsonValue::Object; } |
| 139 | bool isUndefined() const { return type() == QJsonValue::Undefined; } |
| 140 | |
| 141 | bool toBool(bool defaultValue = false) const |
| 142 | { return concreteBool(self: *this, defaultValue); } |
| 143 | int toInt(int defaultValue = 0) const |
| 144 | { return int(concreteInt(self: *this, defaultValue, clamp: true)); } |
| 145 | qint64 toInteger(qint64 defaultValue = 0) const |
| 146 | { return concreteInt(self: *this, defaultValue, clamp: false); } |
| 147 | double toDouble(double defaultValue = 0) const |
| 148 | { return concreteDouble(self: *this, defaultValue); } |
| 149 | QString toString(const QString &defaultValue = {}) const |
| 150 | { return concreteString(self: *this, defaultValue); } |
| 151 | Q_CORE_EXPORT QJsonArray toArray() const; |
| 152 | Q_CORE_EXPORT QJsonObject toObject() const; |
| 153 | |
| 154 | const QJsonValue operator[](QStringView key) const { return concrete(self: *this)[key]; } |
| 155 | const QJsonValue operator[](QLatin1StringView key) const { return concrete(self: *this)[key]; } |
| 156 | const QJsonValue operator[](qsizetype i) const { return concrete(self: *this)[i]; } |
| 157 | |
| 158 | protected: |
| 159 | friend bool comparesEqual(const QJsonValueConstRef &lhs, |
| 160 | const QJsonValueConstRef &rhs) |
| 161 | { |
| 162 | return comparesEqual(lhs: concrete(self: lhs), rhs: concrete(self: rhs)); |
| 163 | } |
| 164 | friend bool comparesEqual(const QJsonValueConstRef &lhs, |
| 165 | const QJsonValue &rhs) |
| 166 | { |
| 167 | return comparesEqual(lhs: concrete(self: lhs), rhs); |
| 168 | } |
| 169 | Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonValueConstRef) |
| 170 | Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonValueConstRef, QJsonValue) |
| 171 | |
| 172 | Q_CORE_EXPORT static QJsonValue::Type |
| 173 | concreteType(QJsonValueConstRef self) noexcept Q_DECL_PURE_FUNCTION; |
| 174 | Q_CORE_EXPORT static bool |
| 175 | concreteBool(QJsonValueConstRef self, bool defaultValue) noexcept Q_DECL_PURE_FUNCTION; |
| 176 | Q_CORE_EXPORT static qint64 |
| 177 | concreteInt(QJsonValueConstRef self, qint64 defaultValue, bool clamp) noexcept Q_DECL_PURE_FUNCTION; |
| 178 | Q_CORE_EXPORT static double |
| 179 | concreteDouble(QJsonValueConstRef self, double defaultValue) noexcept Q_DECL_PURE_FUNCTION; |
| 180 | Q_CORE_EXPORT static QString concreteString(QJsonValueConstRef self, const QString &defaultValue); |
| 181 | Q_CORE_EXPORT static QJsonValue concrete(QJsonValueConstRef self) noexcept; |
| 182 | |
| 183 | // for iterators |
| 184 | Q_CORE_EXPORT static QString objectKey(QJsonValueConstRef self); |
| 185 | QString objectKey() const { return objectKey(self: *this); } |
| 186 | |
| 187 | #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED) |
| 188 | QJsonValueConstRef(QJsonArray *array, qsizetype idx) |
| 189 | : a(array), is_object(false), index(static_cast<quint64>(idx)) {} |
| 190 | QJsonValueConstRef(QJsonObject *object, qsizetype idx) |
| 191 | : o(object), is_object(true), index(static_cast<quint64>(idx)) {} |
| 192 | |
| 193 | void rebind(QJsonValueConstRef other) |
| 194 | { |
| 195 | Q_ASSERT(is_object == other.is_object); |
| 196 | if (is_object) |
| 197 | o = other.o; |
| 198 | else |
| 199 | a = other.a; |
| 200 | index = other.index; |
| 201 | } |
| 202 | |
| 203 | union { |
| 204 | QJsonArray *a; |
| 205 | QJsonObject *o; |
| 206 | void *d; |
| 207 | }; |
| 208 | quint64 is_object : 1; |
| 209 | quint64 index : 63; |
| 210 | #else |
| 211 | constexpr QJsonValueConstRef(QCborContainerPrivate *d, size_t index, bool is_object) |
| 212 | : d(d), is_object(is_object), index(index) |
| 213 | {} |
| 214 | |
| 215 | // implemented in qjsonarray.h & qjsonobject.h, to get their d |
| 216 | QJsonValueConstRef(QJsonArray *array, qsizetype idx); |
| 217 | QJsonValueConstRef(QJsonObject *object, qsizetype idx); |
| 218 | |
| 219 | void rebind(QJsonValueConstRef other) |
| 220 | { |
| 221 | d = other.d; |
| 222 | index = other.index; |
| 223 | } |
| 224 | |
| 225 | QCborContainerPrivate *d = nullptr; |
| 226 | size_t is_object : 1; |
| 227 | size_t index : std::numeric_limits<size_t>::digits - 1; |
| 228 | #endif |
| 229 | |
| 230 | friend class QJsonArray; |
| 231 | friend class QJsonObject; |
| 232 | friend class QJsonPrivate::Value; |
| 233 | }; |
| 234 | |
| 235 | QT_WARNING_PUSH |
| 236 | QT6_ONLY(QT_WARNING_DISABLE_MSVC(4275)) // non dll-interface class 'QJsonValueConstRef' used as base for dll-interface class 'QJsonValueRef' |
| 237 | class QT6_ONLY(Q_CORE_EXPORT) QJsonValueRef : public QJsonValueConstRef |
| 238 | { |
| 239 | public: |
| 240 | QJsonValueRef(const QJsonValueRef &) = default; |
| 241 | QT7_ONLY(Q_CORE_EXPORT) QJsonValueRef &operator = (const QJsonValue &val); |
| 242 | QT7_ONLY(Q_CORE_EXPORT) QJsonValueRef &operator = (const QJsonValueRef &val); |
| 243 | |
| 244 | #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED) |
| 245 | // retained for binary compatibility (due to the Q_CORE_EXPORT) because at |
| 246 | // least one compiler emits and exports all inlines in an exported class |
| 247 | |
| 248 | QJsonValueRef(QJsonArray *array, qsizetype idx) |
| 249 | : QJsonValueConstRef(array, idx) {} |
| 250 | QJsonValueRef(QJsonObject *object, qsizetype idx) |
| 251 | : QJsonValueConstRef(object, idx) {} |
| 252 | |
| 253 | operator QJsonValue() const { return toValue(); } |
| 254 | |
| 255 | QVariant toVariant() const; |
| 256 | inline QJsonValue::Type type() const { return QJsonValueConstRef::type(); } |
| 257 | inline bool isNull() const { return type() == QJsonValue::Null; } |
| 258 | inline bool isBool() const { return type() == QJsonValue::Bool; } |
| 259 | inline bool isDouble() const { return type() == QJsonValue::Double; } |
| 260 | inline bool isString() const { return type() == QJsonValue::String; } |
| 261 | inline bool isArray() const { return type() == QJsonValue::Array; } |
| 262 | inline bool isObject() const { return type() == QJsonValue::Object; } |
| 263 | inline bool isUndefined() const { return type() == QJsonValue::Undefined; } |
| 264 | |
| 265 | inline bool toBool(bool defaultValue = false) const { return QJsonValueConstRef::toBool(defaultValue); } |
| 266 | inline int toInt(int defaultValue = 0) const { return QJsonValueConstRef::toInt(defaultValue); } |
| 267 | inline qint64 toInteger(qint64 defaultValue = 0) const { return QJsonValueConstRef::toInteger(defaultValue); } |
| 268 | inline double toDouble(double defaultValue = 0) const { return QJsonValueConstRef::toDouble(defaultValue); } |
| 269 | inline QString toString(const QString &defaultValue = {}) const { return QJsonValueConstRef::toString(defaultValue); } |
| 270 | QJsonArray toArray() const; |
| 271 | QJsonObject toObject() const; |
| 272 | |
| 273 | const QJsonValue operator[](QStringView key) const { return QJsonValueConstRef::operator[](key); } |
| 274 | const QJsonValue operator[](QLatin1StringView key) const { return QJsonValueConstRef::operator[](key); } |
| 275 | const QJsonValue operator[](qsizetype i) const { return QJsonValueConstRef::operator[](i); } |
| 276 | |
| 277 | #if QT_CORE_REMOVED_SINCE(6, 8) |
| 278 | inline bool operator==(const QJsonValue &other) const { return comparesEqual(*this, other); } |
| 279 | inline bool operator!=(const QJsonValue &other) const { return !comparesEqual(*this, other); } |
| 280 | #endif |
| 281 | |
| 282 | private: |
| 283 | friend bool comparesEqual(const QJsonValueRef &lhs, const QJsonValueRef &rhs) |
| 284 | { |
| 285 | return comparesEqual(lhs: QJsonValue(lhs), rhs: QJsonValue(rhs)); |
| 286 | } |
| 287 | friend bool comparesEqual(const QJsonValueRef &lhs, const QJsonValueConstRef &rhs) |
| 288 | { |
| 289 | return comparesEqual(lhs: QJsonValue(lhs), rhs: QJsonValue(rhs)); |
| 290 | } |
| 291 | Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonValueRef) |
| 292 | Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonValueRef, QJsonValueConstRef) |
| 293 | |
| 294 | QJsonValue toValue() const; |
| 295 | #else |
| 296 | using QJsonValueConstRef::operator[]; |
| 297 | Q_CORE_EXPORT QJsonValueRef operator[](QAnyStringView key); |
| 298 | Q_CORE_EXPORT QJsonValueRef operator[](qsizetype i); |
| 299 | |
| 300 | private: |
| 301 | using QJsonValueConstRef::QJsonValueConstRef; |
| 302 | #endif // < Qt 7 |
| 303 | |
| 304 | QT7_ONLY(Q_CORE_EXPORT) void detach(); |
| 305 | friend class QJsonArray; |
| 306 | friend class QJsonObject; |
| 307 | }; |
| 308 | QT_WARNING_POP |
| 309 | |
| 310 | inline QJsonValue QCborValueConstRef::toJsonValue() const |
| 311 | { |
| 312 | return concrete().toJsonValue(); |
| 313 | } |
| 314 | |
| 315 | Q_CORE_EXPORT size_t qHash(const QJsonValue &value, size_t seed = 0); |
| 316 | |
| 317 | #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY) |
| 318 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &); |
| 319 | #endif |
| 320 | |
| 321 | #ifndef QT_NO_DATASTREAM |
| 322 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonValue &); |
| 323 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonValue &); |
| 324 | #endif |
| 325 | |
| 326 | QT_END_NAMESPACE |
| 327 | |
| 328 | #endif // QJSONVALUE_H |
| 329 | |