Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qjsondocument.h
Go to the documentation of this file.
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
13
14class QDebug;
15class QCborValue;
16
17namespace QJsonPrivate { class Parser; }
18
19struct Q_CORE_EXPORT QJsonParseError
20{
36 GarbageAtEnd
37 };
38
39 QString errorString() const;
40
41 int offset = -1;
43};
44
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
56 explicit QJsonDocument(const QJsonObject &object);
57 explicit QJsonDocument(const QJsonArray &array);
59
61 QJsonDocument &operator =(const QJsonDocument &other);
62
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
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;
110 friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonDocument &);
111
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 &);
126#endif
127
129
130#endif // QJSONDOCUMENT_H
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore\reentrant
Definition qcborvalue.h:50
\inmodule QtCore\reentrant
Definition qdatastream.h:30
\inmodule QtCore
\inmodule QtCore\reentrant
Definition qjsonarray.h:18
\inmodule QtCore\reentrant
~QJsonDocument()
Deletes the document.
bool operator!=(const QJsonDocument &other) const
returns true if other is not equal to this document
\inmodule QtCore\reentrant
Definition qjsonobject.h:20
\inmodule QtCore\reentrant
Definition qjsonvalue.h:24
\inmodule QtCore
Definition qstringview.h:76
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
\inmodule QtCore
Definition qvariant.h:64
Combined button and popup list for selecting options.
DBusConnection const char DBusError * error
Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonDocument &)
Q_CORE_EXPORT QDataStream & operator>>(QDataStream &, QJsonDocument &)
static QCborValue fromJson(const QByteArray &json, QJsonParseError *error)
GLuint64 key
GLuint object
[3]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLuint GLintptr offset
GLint GLsizei GLsizei GLenum format
GLenum array
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
Definition qrandom.cpp:1219
@ NoError
Definition main.cpp:34
ptrdiff_t qsizetype
Definition qtypes.h:70
unsigned int uint
Definition qtypes.h:29
static QVariant toVariant(const QV4::Value &value, QMetaType typeHint, bool createJSValueForObjectsAndSymbols, V4ObjectSet *visitedObjects)
QDataStream & operator<<(QDataStream &out, const MyClass &myObj)
[4]
QVariant variant
[1]
QSharedPointer< T > other(t)
[5]
this swap(other)
\inmodule QtCore\reentrant
ParseError
This enum describes the type of error that occurred during the parsing of a JSON document.