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

source code of qtbase/src/corelib/serialization/qjsondocument.h