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

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