| 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 QJSONDOCUMENT_H | 
|---|
| 5 | #define QJSONDOCUMENT_H | 
|---|
| 6 |  | 
|---|
| 7 | #include <QtCore/qcompare.h> | 
|---|
| 8 | #include <QtCore/qjsonvalue.h> | 
|---|
| 9 | #include <QtCore/qscopedpointer.h> | 
|---|
| 10 |  | 
|---|
| 11 | #include <memory> | 
|---|
| 12 |  | 
|---|
| 13 | QT_BEGIN_NAMESPACE | 
|---|
| 14 |  | 
|---|
| 15 | class QDebug; | 
|---|
| 16 | class QCborValue; | 
|---|
| 17 |  | 
|---|
| 18 | namespace QJsonPrivate { class Parser; } | 
|---|
| 19 |  | 
|---|
| 20 | struct Q_CORE_EXPORT QJsonParseError | 
|---|
| 21 | { | 
|---|
| 22 | enum ParseError { | 
|---|
| 23 | NoError = 0, | 
|---|
| 24 | UnterminatedObject, | 
|---|
| 25 | MissingNameSeparator, | 
|---|
| 26 | UnterminatedArray, | 
|---|
| 27 | MissingValueSeparator, | 
|---|
| 28 | IllegalValue, | 
|---|
| 29 | TerminationByNumber, | 
|---|
| 30 | IllegalNumber, | 
|---|
| 31 | IllegalEscapeSequence, | 
|---|
| 32 | IllegalUTF8String, | 
|---|
| 33 | UnterminatedString, | 
|---|
| 34 | MissingObject, | 
|---|
| 35 | DeepNesting, | 
|---|
| 36 | DocumentTooLarge, | 
|---|
| 37 | GarbageAtEnd | 
|---|
| 38 | }; | 
|---|
| 39 |  | 
|---|
| 40 | QString    errorString() const; | 
|---|
| 41 |  | 
|---|
| 42 | int        offset = -1; | 
|---|
| 43 | ParseError error = NoError; | 
|---|
| 44 | }; | 
|---|
| 45 |  | 
|---|
| 46 | class QJsonDocumentPrivate; | 
|---|
| 47 | class Q_CORE_EXPORT QJsonDocument | 
|---|
| 48 | { | 
|---|
| 49 | public: | 
|---|
| 50 | #ifdef Q_LITTLE_ENDIAN | 
|---|
| 51 | static const uint BinaryFormatTag = ('q') | ('b' << 8) | ('j' << 16) | ('s' << 24); | 
|---|
| 52 | #else | 
|---|
| 53 | static const uint BinaryFormatTag = ('q' << 24) | ('b' << 16) | ('j' << 8) | ('s'); | 
|---|
| 54 | #endif | 
|---|
| 55 |  | 
|---|
| 56 | QJsonDocument(); | 
|---|
| 57 | explicit QJsonDocument(const QJsonObject &object); | 
|---|
| 58 | explicit QJsonDocument(const QJsonArray &array); | 
|---|
| 59 | ~QJsonDocument(); | 
|---|
| 60 |  | 
|---|
| 61 | QJsonDocument(const QJsonDocument &other); | 
|---|
| 62 | QJsonDocument &operator =(const QJsonDocument &other); | 
|---|
| 63 |  | 
|---|
| 64 | QJsonDocument(QJsonDocument &&other) noexcept; | 
|---|
| 65 |  | 
|---|
| 66 | QJsonDocument &operator =(QJsonDocument &&other) noexcept | 
|---|
| 67 | { | 
|---|
| 68 | swap(other); | 
|---|
| 69 | return *this; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | void swap(QJsonDocument &other) noexcept; | 
|---|
| 73 |  | 
|---|
| 74 | static QJsonDocument fromVariant(const QVariant &variant); | 
|---|
| 75 | QVariant toVariant() const; | 
|---|
| 76 |  | 
|---|
| 77 | enum JsonFormat { | 
|---|
| 78 | Indented, | 
|---|
| 79 | Compact | 
|---|
| 80 | }; | 
|---|
| 81 |  | 
|---|
| 82 | static QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error = nullptr); | 
|---|
| 83 |  | 
|---|
| 84 | #if !defined(QT_JSON_READONLY) || defined(Q_QDOC) | 
|---|
| 85 | QByteArray toJson(JsonFormat format = Indented) const; | 
|---|
| 86 | #endif | 
|---|
| 87 |  | 
|---|
| 88 | bool isEmpty() const; | 
|---|
| 89 | bool isArray() const; | 
|---|
| 90 | bool isObject() const; | 
|---|
| 91 |  | 
|---|
| 92 | QJsonObject object() const; | 
|---|
| 93 | QJsonArray array() const; | 
|---|
| 94 |  | 
|---|
| 95 | void setObject(const QJsonObject &object); | 
|---|
| 96 | void setArray(const QJsonArray &array); | 
|---|
| 97 |  | 
|---|
| 98 | const QJsonValue operator[](const QString &key) const; | 
|---|
| 99 | const QJsonValue operator[](QStringView key) const; | 
|---|
| 100 | const QJsonValue operator[](QLatin1StringView key) const; | 
|---|
| 101 | const QJsonValue operator[](qsizetype i) const; | 
|---|
| 102 | #if QT_CORE_REMOVED_SINCE(6, 8) | 
|---|
| 103 | bool operator==(const QJsonDocument &other) const; | 
|---|
| 104 | bool operator!=(const QJsonDocument &other) const { return !operator==(other); } | 
|---|
| 105 | #endif | 
|---|
| 106 | bool isNull() const; | 
|---|
| 107 |  | 
|---|
| 108 | private: | 
|---|
| 109 | friend class QJsonValue; | 
|---|
| 110 | friend class QJsonPrivate::Parser; | 
|---|
| 111 | friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonDocument &); | 
|---|
| 112 | friend Q_CORE_EXPORT bool comparesEqual(const QJsonDocument &lhs, | 
|---|
| 113 | const QJsonDocument &rhs) noexcept; | 
|---|
| 114 | Q_DECLARE_EQUALITY_COMPARABLE(QJsonDocument) | 
|---|
| 115 |  | 
|---|
| 116 | QJsonDocument(const QCborValue &data); | 
|---|
| 117 |  | 
|---|
| 118 | std::unique_ptr<QJsonDocumentPrivate> d; | 
|---|
| 119 | }; | 
|---|
| 120 |  | 
|---|
| 121 | Q_DECLARE_SHARED(QJsonDocument) | 
|---|
| 122 |  | 
|---|
| 123 | #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY) | 
|---|
| 124 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonDocument &); | 
|---|
| 125 | #endif | 
|---|
| 126 |  | 
|---|
| 127 | #ifndef QT_NO_DATASTREAM | 
|---|
| 128 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonDocument &); | 
|---|
| 129 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonDocument &); | 
|---|
| 130 | #endif | 
|---|
| 131 |  | 
|---|
| 132 | QT_END_NAMESPACE | 
|---|
| 133 |  | 
|---|
| 134 | #endif // QJSONDOCUMENT_H | 
|---|
| 135 |  | 
|---|