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