| 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 = -1; |
| 78 | ParseError error = NoError; |
| 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 | static QJsonDocument fromVariant(const QVariant &variant); |
| 110 | QVariant toVariant() const; |
| 111 | |
| 112 | enum JsonFormat { |
| 113 | Indented, |
| 114 | Compact |
| 115 | }; |
| 116 | |
| 117 | static QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error = nullptr); |
| 118 | |
| 119 | #if !defined(QT_JSON_READONLY) || defined(Q_CLANG_QDOC) |
| 120 | QByteArray toJson(JsonFormat format = Indented) const; |
| 121 | #endif |
| 122 | |
| 123 | bool isEmpty() const; |
| 124 | bool isArray() const; |
| 125 | bool isObject() const; |
| 126 | |
| 127 | QJsonObject object() const; |
| 128 | QJsonArray array() const; |
| 129 | |
| 130 | void setObject(const QJsonObject &object); |
| 131 | void setArray(const QJsonArray &array); |
| 132 | |
| 133 | #if QT_STRINGVIEW_LEVEL < 2 |
| 134 | const QJsonValue operator[](const QString &key) const; |
| 135 | #endif |
| 136 | const QJsonValue operator[](QStringView key) const; |
| 137 | const QJsonValue operator[](QLatin1String key) const; |
| 138 | const QJsonValue operator[](qsizetype i) const; |
| 139 | |
| 140 | bool operator==(const QJsonDocument &other) const; |
| 141 | bool operator!=(const QJsonDocument &other) const { return !(*this == other); } |
| 142 | |
| 143 | bool isNull() const; |
| 144 | |
| 145 | private: |
| 146 | friend class QJsonValue; |
| 147 | friend class QJsonPrivate::Parser; |
| 148 | friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonDocument &); |
| 149 | |
| 150 | QJsonDocument(const QCborValue &data); |
| 151 | |
| 152 | std::unique_ptr<QJsonDocumentPrivate> d; |
| 153 | }; |
| 154 | |
| 155 | Q_DECLARE_SHARED(QJsonDocument) |
| 156 | |
| 157 | #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY) |
| 158 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonDocument &); |
| 159 | #endif |
| 160 | |
| 161 | #ifndef QT_NO_DATASTREAM |
| 162 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonDocument &); |
| 163 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonDocument &); |
| 164 | #endif |
| 165 | |
| 166 | QT_END_NAMESPACE |
| 167 | |
| 168 | #endif // QJSONDOCUMENT_H |
| 169 | |