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 QJSONDOCUMENT_H 41 #define QJSONDOCUMENT_H 42 43 #include <QtCore/qjsonvalue.h> 44 #include <QtCore/qscopedpointer.h> 45 46 #include <memory> 47 48 QT_BEGIN_NAMESPACE 49 50 class QDebug; 51 class QCborValue; 52 53 namespace QJsonPrivate { class Parser; } 54 55 struct Q_CORE_EXPORT QJsonParseError 56 { 57 enum ParseError { 58 NoError = 0, 59 UnterminatedObject, 60 MissingNameSeparator, 61 UnterminatedArray, 62 MissingValueSeparator, 63 IllegalValue, 64 TerminationByNumber, 65 IllegalNumber, 66 IllegalEscapeSequence, 67 IllegalUTF8String, 68 UnterminatedString, 69 MissingObject, 70 DeepNesting, 71 DocumentTooLarge, 72 GarbageAtEnd 73 }; 74 75 QString errorString() const; 76 77 int offset; 78 ParseError error; 79 }; 80 81 class QJsonDocumentPrivate; 82 class Q_CORE_EXPORT QJsonDocument 83 { 84 public: 85 #ifdef Q_LITTLE_ENDIAN 86 static const uint BinaryFormatTag = ('q') | ('b' << 8) | ('j' << 16) | ('s' << 24); 87 #else 88 static const uint BinaryFormatTag = ('q' << 24) | ('b' << 16) | ('j' << 8) | ('s'); 89 #endif 90 91 QJsonDocument(); 92 explicit QJsonDocument(const QJsonObject &object); 93 explicit QJsonDocument(const QJsonArray &array); 94 ~QJsonDocument(); 95 96 QJsonDocument(const QJsonDocument &other); 97 QJsonDocument &operator =(const QJsonDocument &other); 98 99 QJsonDocument(QJsonDocument &&other) noexcept; 100 101 QJsonDocument &operator =(QJsonDocument &&other) noexcept 102 { 103 swap(other); 104 return *this; 105 } 106 107 void swap(QJsonDocument &other) noexcept; 108 109 enum DataValidation { 110 Validate, 111 BypassValidation 112 }; 113 114 #if QT_CONFIG(binaryjson) && QT_DEPRECATED_SINCE(5, 15) 115 QT_DEPRECATED_X("Use CBOR format instead") 116 static QJsonDocument fromRawData(const char *data, int size, DataValidation validation = Validate); 117 118 QT_DEPRECATED_X("Use CBOR format instead") 119 const char *rawData(int *size) const; 120 121 QT_DEPRECATED_X("Use CBOR format instead") 122 static QJsonDocument fromBinaryData(const QByteArray &data, DataValidation validation = Validate); 123 124 QT_DEPRECATED_X("Use CBOR format instead") 125 QByteArray toBinaryData() const; 126 #endif // QT_CONFIG(binaryjson) && QT_DEPRECATED_SINCE(5, 15) 127 128 static QJsonDocument fromVariant(const QVariant &variant); 129 QVariant toVariant() const; 130 131 enum JsonFormat { 132 Indented, 133 Compact 134 }; 135 136 static QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error = nullptr); 137 138 #if !defined(QT_JSON_READONLY) || defined(Q_CLANG_QDOC) 139 QByteArray toJson() const; //### Merge in Qt6 140 QByteArray toJson(JsonFormat format) const; 141 #endif 142 143 bool isEmpty() const; 144 bool isArray() const; 145 bool isObject() const; 146 147 QJsonObject object() const; 148 QJsonArray array() const; 149 150 void setObject(const QJsonObject &object); 151 void setArray(const QJsonArray &array); 152 153 #if QT_STRINGVIEW_LEVEL < 2 154 const QJsonValue operator[](const QString &key) const; 155 #endif 156 const QJsonValue operator[](QStringView key) const; 157 const QJsonValue operator[](QLatin1String key) const; 158 const QJsonValue operator[](int i) const; 159 160 bool operator==(const QJsonDocument &other) const; 161 bool operator!=(const QJsonDocument &other) const { return !(*this == other); } 162 163 bool isNull() const; 164 165 private: 166 friend class QJsonValue; 167 friend class QJsonPrivate::Parser; 168 friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonDocument &); 169 170 QJsonDocument(const QCborValue &data); 171 172 std::unique_ptr<QJsonDocumentPrivate> d; 173 }; 174 175 Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QJsonDocument) 176 177 #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY) 178 Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonDocument &); 179 #endif 180 181 #ifndef QT_NO_DATASTREAM 182 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonDocument &); 183 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonDocument &); 184 #endif 185 186 QT_END_NAMESPACE 187 188 #endif // QJSONDOCUMENT_H 189