File indexing completed on 2025-01-18 10:07:26
0001
0002
0003
0004 #ifndef QJSONDOCUMENT_H
0005 #define QJSONDOCUMENT_H
0006
0007 #include <QtCore/qjsonvalue.h>
0008 #include <QtCore/qscopedpointer.h>
0009
0010 #include <memory>
0011
0012 QT_BEGIN_NAMESPACE
0013
0014 class QDebug;
0015 class QCborValue;
0016
0017 namespace QJsonPrivate { class Parser; }
0018
0019 struct Q_CORE_EXPORT QJsonParseError
0020 {
0021 enum ParseError {
0022 NoError = 0,
0023 UnterminatedObject,
0024 MissingNameSeparator,
0025 UnterminatedArray,
0026 MissingValueSeparator,
0027 IllegalValue,
0028 TerminationByNumber,
0029 IllegalNumber,
0030 IllegalEscapeSequence,
0031 IllegalUTF8String,
0032 UnterminatedString,
0033 MissingObject,
0034 DeepNesting,
0035 DocumentTooLarge,
0036 GarbageAtEnd
0037 };
0038
0039 QString errorString() const;
0040
0041 int offset = -1;
0042 ParseError error = NoError;
0043 };
0044
0045 class QJsonDocumentPrivate;
0046 class Q_CORE_EXPORT QJsonDocument
0047 {
0048 public:
0049 #ifdef Q_LITTLE_ENDIAN
0050 static const uint BinaryFormatTag = ('q') | ('b' << 8) | ('j' << 16) | ('s' << 24);
0051 #else
0052 static const uint BinaryFormatTag = ('q' << 24) | ('b' << 16) | ('j' << 8) | ('s');
0053 #endif
0054
0055 QJsonDocument();
0056 explicit QJsonDocument(const QJsonObject &object);
0057 explicit QJsonDocument(const QJsonArray &array);
0058 ~QJsonDocument();
0059
0060 QJsonDocument(const QJsonDocument &other);
0061 QJsonDocument &operator =(const QJsonDocument &other);
0062
0063 QJsonDocument(QJsonDocument &&other) noexcept;
0064
0065 QJsonDocument &operator =(QJsonDocument &&other) noexcept
0066 {
0067 swap(other);
0068 return *this;
0069 }
0070
0071 void swap(QJsonDocument &other) noexcept;
0072
0073 static QJsonDocument fromVariant(const QVariant &variant);
0074 QVariant toVariant() const;
0075
0076 enum JsonFormat {
0077 Indented,
0078 Compact
0079 };
0080
0081 static QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error = nullptr);
0082
0083 #if !defined(QT_JSON_READONLY) || defined(Q_QDOC)
0084 QByteArray toJson(JsonFormat format = Indented) const;
0085 #endif
0086
0087 bool isEmpty() const;
0088 bool isArray() const;
0089 bool isObject() const;
0090
0091 QJsonObject object() const;
0092 QJsonArray array() const;
0093
0094 void setObject(const QJsonObject &object);
0095 void setArray(const QJsonArray &array);
0096
0097 const QJsonValue operator[](const QString &key) const;
0098 const QJsonValue operator[](QStringView key) const;
0099 const QJsonValue operator[](QLatin1StringView key) const;
0100 const QJsonValue operator[](qsizetype i) const;
0101
0102 bool operator==(const QJsonDocument &other) const;
0103 bool operator!=(const QJsonDocument &other) const { return !(*this == other); }
0104
0105 bool isNull() const;
0106
0107 private:
0108 friend class QJsonValue;
0109 friend class QJsonPrivate::Parser;
0110 friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonDocument &);
0111
0112 QJsonDocument(const QCborValue &data);
0113
0114 std::unique_ptr<QJsonDocumentPrivate> d;
0115 };
0116
0117 Q_DECLARE_SHARED(QJsonDocument)
0118
0119 #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
0120 Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonDocument &);
0121 #endif
0122
0123 #ifndef QT_NO_DATASTREAM
0124 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonDocument &);
0125 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonDocument &);
0126 #endif
0127
0128 QT_END_NAMESPACE
0129
0130 #endif