| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU Lesser General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | 
| 21 | ** packaging of this file. Please review the following information to | 
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | 
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | 
| 24 | ** | 
| 25 | ** GNU General Public License Usage | 
| 26 | ** Alternatively, this file may be used under the terms of the GNU | 
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | 
| 28 | ** Public license version 3 or any later version approved by the KDE Free | 
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | 
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | 
| 31 | ** included in the packaging of this file. Please review the following | 
| 32 | ** information to ensure the GNU General Public License requirements will | 
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | 
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | 
| 35 | ** | 
| 36 | ** $QT_END_LICENSE$ | 
| 37 | ** | 
| 38 | ****************************************************************************/ | 
| 39 |  | 
| 40 | #ifndef QJSONVALUE_H | 
| 41 | #define QJSONVALUE_H | 
| 42 |  | 
| 43 | #include <QtCore/qglobal.h> | 
| 44 | #include <QtCore/qstring.h> | 
| 45 | #include <QtCore/qshareddata.h> | 
| 46 | #include <QtCore/qcborvalue.h> | 
| 47 |  | 
| 48 | QT_BEGIN_NAMESPACE | 
| 49 |  | 
| 50 | class QVariant; | 
| 51 | class QJsonArray; | 
| 52 | class QJsonObject; | 
| 53 | class QCborContainerPrivate; | 
| 54 |  | 
| 55 | namespace QJsonPrivate { | 
| 56 | class Value; | 
| 57 | } | 
| 58 |  | 
| 59 | class Q_CORE_EXPORT QJsonValue | 
| 60 | { | 
| 61 | public: | 
| 62 |     enum Type { | 
| 63 |         Null =  0x0, | 
| 64 |         Bool = 0x1, | 
| 65 |         Double = 0x2, | 
| 66 |         String = 0x3, | 
| 67 |         Array = 0x4, | 
| 68 |         Object = 0x5, | 
| 69 |         Undefined = 0x80 | 
| 70 |     }; | 
| 71 |  | 
| 72 |     QJsonValue(Type = Null); | 
| 73 |     QJsonValue(bool b); | 
| 74 |     QJsonValue(double n); | 
| 75 |     QJsonValue(int n); | 
| 76 |     QJsonValue(qint64 v); | 
| 77 |     QJsonValue(const QString &s); | 
| 78 |     QJsonValue(QLatin1String s); | 
| 79 | #ifndef QT_NO_CAST_FROM_ASCII | 
| 80 |     inline QT_ASCII_CAST_WARN QJsonValue(const char *s) | 
| 81 |         : QJsonValue(QString::fromUtf8(str: s)) {} | 
| 82 | #endif | 
| 83 |     QJsonValue(const QJsonArray &a); | 
| 84 |     QJsonValue(const QJsonObject &o); | 
| 85 |  | 
| 86 |     ~QJsonValue(); | 
| 87 |  | 
| 88 |     QJsonValue(const QJsonValue &other); | 
| 89 |     QJsonValue &operator =(const QJsonValue &other); | 
| 90 |  | 
| 91 |     QJsonValue(QJsonValue &&other) noexcept; | 
| 92 |  | 
| 93 |     QJsonValue &operator =(QJsonValue &&other) noexcept | 
| 94 |     { | 
| 95 |         swap(other); | 
| 96 |         return *this; | 
| 97 |     } | 
| 98 |  | 
| 99 |     void swap(QJsonValue &other) noexcept; | 
| 100 |  | 
| 101 |     static QJsonValue fromVariant(const QVariant &variant); | 
| 102 |     QVariant toVariant() const; | 
| 103 |  | 
| 104 |     Type type() const; | 
| 105 |     inline bool isNull() const { return type() == Null; } | 
| 106 |     inline bool isBool() const { return type() == Bool; } | 
| 107 |     inline bool isDouble() const { return type() == Double; } | 
| 108 |     inline bool isString() const { return type() == String; } | 
| 109 |     inline bool isArray() const { return type() == Array; } | 
| 110 |     inline bool isObject() const { return type() == Object; } | 
| 111 |     inline bool isUndefined() const { return type() == Undefined; } | 
| 112 |  | 
| 113 |     bool toBool(bool defaultValue = false) const; | 
| 114 |     int toInt(int defaultValue = 0) const; | 
| 115 |     double toDouble(double defaultValue = 0) const; | 
| 116 |     QString toString() const; | 
| 117 |     QString toString(const QString &defaultValue) const; | 
| 118 |     QJsonArray toArray() const; | 
| 119 |     QJsonArray toArray(const QJsonArray &defaultValue) const; | 
| 120 |     QJsonObject toObject() const; | 
| 121 |     QJsonObject toObject(const QJsonObject &defaultValue) const; | 
| 122 |  | 
| 123 | #if QT_STRINGVIEW_LEVEL < 2 | 
| 124 |     const QJsonValue operator[](const QString &key) const; | 
| 125 | #endif | 
| 126 |     const QJsonValue operator[](QStringView key) const; | 
| 127 |     const QJsonValue operator[](QLatin1String key) const; | 
| 128 |     const QJsonValue operator[](int i) const; | 
| 129 |  | 
| 130 |     bool operator==(const QJsonValue &other) const; | 
| 131 |     bool operator!=(const QJsonValue &other) const; | 
| 132 |  | 
| 133 | private: | 
| 134 |     // avoid implicit conversions from char * to bool | 
| 135 |     QJsonValue(const void *) = delete; | 
| 136 |     friend class QJsonPrivate::Value; | 
| 137 |     friend class QJsonArray; | 
| 138 |     friend class QJsonObject; | 
| 139 |     friend class QCborValue; | 
| 140 |     friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &); | 
| 141 |     friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonValue &); | 
| 142 |  | 
| 143 |     // ### Qt6: Remove this. | 
| 144 |     void stringDataFromQStringHelper(const QString &string); | 
| 145 |  | 
| 146 |     void detach(); | 
| 147 |  | 
| 148 |     // ### Qt6: change to an actual QCborValue | 
| 149 |     qint64 n = 0; | 
| 150 |     QExplicitlySharedDataPointer<QCborContainerPrivate> d; // needed for Objects, Arrays, Strings | 
| 151 |     QCborValue::Type t; | 
| 152 |  | 
| 153 |     // Assert binary compatibility with pre-5.15 QJsonValue | 
| 154 |     Q_STATIC_ASSERT(sizeof(QExplicitlySharedDataPointer<QCborContainerPrivate>) == sizeof(void *)); | 
| 155 |     Q_STATIC_ASSERT(sizeof(QCborValue::Type) == sizeof(QJsonValue::Type)); | 
| 156 | }; | 
| 157 |  | 
| 158 | class Q_CORE_EXPORT QJsonValueRef | 
| 159 | { | 
| 160 | public: | 
| 161 |     QJsonValueRef(const QJsonValueRef &) = default; // ### Qt6: delete (maybe) | 
| 162 |     QJsonValueRef(QJsonArray *array, int idx) | 
| 163 |         : a(array), is_object(false), index(static_cast<uint>(idx)) {} | 
| 164 |     QJsonValueRef(QJsonObject *object, int idx) | 
| 165 |         : o(object), is_object(true), index(static_cast<uint>(idx)) {} | 
| 166 |  | 
| 167 |     inline operator QJsonValue() const { return toValue(); } | 
| 168 |     QJsonValueRef &operator = (const QJsonValue &val); | 
| 169 |     QJsonValueRef &operator = (const QJsonValueRef &val); | 
| 170 |  | 
| 171 |     QVariant toVariant() const; | 
| 172 |     inline QJsonValue::Type type() const { return toValue().type(); } | 
| 173 |     inline bool isNull() const { return type() == QJsonValue::Null; } | 
| 174 |     inline bool isBool() const { return type() == QJsonValue::Bool; } | 
| 175 |     inline bool isDouble() const { return type() == QJsonValue::Double; } | 
| 176 |     inline bool isString() const { return type() == QJsonValue::String; } | 
| 177 |     inline bool isArray() const { return type() == QJsonValue::Array; } | 
| 178 |     inline bool isObject() const { return type() == QJsonValue::Object; } | 
| 179 |     inline bool isUndefined() const { return type() == QJsonValue::Undefined; } | 
| 180 |  | 
| 181 |     inline bool toBool() const { return toValue().toBool(); } | 
| 182 |     inline int toInt() const { return toValue().toInt(); } | 
| 183 |     inline double toDouble() const { return toValue().toDouble(); } | 
| 184 |     inline QString toString() const { return toValue().toString(); } | 
| 185 |     QJsonArray toArray() const; | 
| 186 |     QJsonObject toObject() const; | 
| 187 |  | 
| 188 |     // ### Qt 6: Add default values | 
| 189 |     inline bool toBool(bool defaultValue) const { return toValue().toBool(defaultValue); } | 
| 190 |     inline int toInt(int defaultValue) const { return toValue().toInt(defaultValue); } | 
| 191 |     inline double toDouble(double defaultValue) const { return toValue().toDouble(defaultValue); } | 
| 192 |     inline QString toString(const QString &defaultValue) const { return toValue().toString(defaultValue); } | 
| 193 |  | 
| 194 |     inline bool operator==(const QJsonValue &other) const { return toValue() == other; } | 
| 195 |     inline bool operator!=(const QJsonValue &other) const { return toValue() != other; } | 
| 196 |  | 
| 197 | private: | 
| 198 |     QJsonValue toValue() const; | 
| 199 |  | 
| 200 |     union { | 
| 201 |         QJsonArray *a; | 
| 202 |         QJsonObject *o; | 
| 203 |     }; | 
| 204 |     uint is_object : 1; | 
| 205 |     uint index : 31; | 
| 206 | }; | 
| 207 |  | 
| 208 | // ### Qt 6: Get rid of these fake pointer classes | 
| 209 | class QJsonValuePtr | 
| 210 | { | 
| 211 |     QJsonValue value; | 
| 212 | public: | 
| 213 |     explicit QJsonValuePtr(const QJsonValue& val) | 
| 214 |         : value(val) {} | 
| 215 |  | 
| 216 |     QJsonValue& operator*() { return value; } | 
| 217 |     QJsonValue* operator->() { return &value; } | 
| 218 | }; | 
| 219 |  | 
| 220 | class QJsonValueRefPtr | 
| 221 | { | 
| 222 |     QJsonValueRef valueRef; | 
| 223 | public: | 
| 224 |     QJsonValueRefPtr(QJsonArray *array, int idx) | 
| 225 |         : valueRef(array, idx) {} | 
| 226 |     QJsonValueRefPtr(QJsonObject *object, int idx) | 
| 227 |         : valueRef(object, idx)  {} | 
| 228 |  | 
| 229 |     QJsonValueRef& operator*() { return valueRef; } | 
| 230 |     QJsonValueRef* operator->() { return &valueRef; } | 
| 231 | }; | 
| 232 |  | 
| 233 | Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QJsonValue) | 
| 234 |  | 
| 235 | inline bool operator==(const QJsonValueRef &lhs, const QJsonValueRef &rhs) | 
| 236 | { return QJsonValue(lhs) == QJsonValue(rhs); } | 
| 237 | inline bool operator!=(const QJsonValueRef &lhs, const QJsonValueRef &rhs) | 
| 238 | { return !(lhs == rhs); } | 
| 239 |  | 
| 240 | Q_CORE_EXPORT uint qHash(const QJsonValue &value, uint seed = 0); | 
| 241 |  | 
| 242 | #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY) | 
| 243 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &); | 
| 244 | #endif | 
| 245 |  | 
| 246 | #ifndef QT_NO_DATASTREAM | 
| 247 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonValue &); | 
| 248 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonValue &); | 
| 249 | #endif | 
| 250 |  | 
| 251 | QT_END_NAMESPACE | 
| 252 |  | 
| 253 | #endif // QJSONVALUE_H | 
| 254 |  |