![]() |
Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
|
\inmodule QtCore\reentrant More...
#include <qjsondocument.h>
Public Types | |
| using | JsonFormat = QJsonValue::JsonFormat |
Public Member Functions | |
| QJsonDocument () | |
| Constructs an empty and invalid document. | |
| QJsonDocument (const QJsonObject &object) | |
| Creates a QJsonDocument from object. | |
| QJsonDocument (QJsonObject &&object) | |
| QJsonDocument (const QJsonArray &array) | |
| Constructs a QJsonDocument from array. | |
| QJsonDocument (QJsonArray &&array) | |
| ~QJsonDocument () | |
| Deletes the document. | |
| QJsonDocument (const QJsonDocument &other) | |
| Creates a copy of the other document. | |
| QJsonDocument & | operator= (const QJsonDocument &other) |
| Assigns the other document to this QJsonDocument. | |
| QJsonDocument (QJsonDocument &&other) noexcept | |
| void | swap (QJsonDocument &other) noexcept |
| QVariant | toVariant () const |
| Returns a QVariant representing the Json document. | |
| QByteArray | toJson (JsonFormat format=JsonFormat::Indented) const |
| \typealias QJsonDocument::JsonFormat | |
| bool | isEmpty () const |
Returns true if the document doesn't contain any data. | |
| bool | isArray () const |
Returns true if the document contains an array. | |
| bool | isObject () const |
Returns true if the document contains an object. | |
| QJsonObject | object () const |
| Returns the QJsonObject contained in the document. | |
| QJsonArray | array () const |
| Returns the QJsonArray contained in the document. | |
| void | setObject (const QJsonObject &object) |
| Sets object as the main object of this document. | |
| void | setArray (const QJsonArray &array) |
| Sets array as the main object of this document. | |
| const QJsonValue | operator[] (const QString &key) const |
| Returns a QJsonValue representing the value for the key key. | |
| const QJsonValue | operator[] (QStringView key) const |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
| const QJsonValue | operator[] (QLatin1StringView key) const |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
| const QJsonValue | operator[] (qsizetype i) const |
| Returns a QJsonValue representing the value for index i. | |
| bool | isNull () const |
returns true if this document is null. | |
Static Public Member Functions | |
| static QJsonDocument | fromVariant (const QVariant &variant) |
| Creates a QJsonDocument from the QVariant variant. | |
| static QJsonDocument | fromJson (const QByteArray &json, QJsonParseError *error=nullptr) |
| Parses json as a UTF-8 encoded JSON document, and creates a QJsonDocument from it. | |
Static Public Attributes | |
| static const uint | BinaryFormatTag = ('q' << 24) | ('b' << 16) | ('j' << 8) | ('s') |
| static constexpr auto | Indented = JsonFormat::Indented |
| static constexpr auto | Compact = JsonFormat::Compact |
Friends | |
| class | QJsonValue |
| class | QJsonPrivate::Parser |
| Q_CORE_EXPORT QDebug | operator<< (QDebug dbg, const QJsonDocument &o) |
| Q_CORE_EXPORT bool | comparesEqual (const QJsonDocument &lhs, const QJsonDocument &rhs) |
\inmodule QtCore
\reentrant
The QJsonDocument class provides a way to read and write JSON documents.
\compares equality
QJsonDocument is a class that wraps a complete JSON document and can read this document from, and write it to, a UTF-8 encoded text-based representation.
A JSON document can be converted from its text-based representation to a QJsonDocument using QJsonDocument::fromJson(). toJson() converts it back to text. The parser is very fast and efficient and converts the JSON to the binary representation used by Qt.
Validity of the parsed document can be queried with !isNull()
A document can be queried as to whether it contains an array or an object using isArray() and isObject(). The array or object contained in the document can be retrieved using array() or object() and then read or manipulated.
Definition at line 28 of file qjsondocument.h.
Definition at line 63 of file qjsondocument.h.
| QJsonDocument::QJsonDocument | ( | ) |
Constructs an empty and invalid document.
Definition at line 65 of file qjsondocument.cpp.
|
explicit |
Creates a QJsonDocument from object.
Definition at line 73 of file qjsondocument.cpp.
|
explicit |
Definition at line 82 of file qjsondocument.cpp.
|
explicit |
Constructs a QJsonDocument from array.
Definition at line 91 of file qjsondocument.cpp.
|
explicit |
Definition at line 100 of file qjsondocument.cpp.
| QJsonDocument::~QJsonDocument | ( | ) |
Deletes the document.
Binary data set with fromRawData is not freed.
Definition at line 120 of file qjsondocument.cpp.
| QJsonDocument::QJsonDocument | ( | const QJsonDocument & | other | ) |
Creates a copy of the other document.
Definition at line 128 of file qjsondocument.cpp.
|
noexcept |
Move-constructs a QJsonDocument from other.
| QJsonArray QJsonDocument::array | ( | ) | const |
Returns the QJsonArray contained in the document.
Returns an empty array if the document contains an object.
Definition at line 351 of file qjsondocument.cpp.
|
static |
Parses json as a UTF-8 encoded JSON document, and creates a QJsonDocument from it.
Returns a valid (non-null) QJsonDocument if the parsing succeeds. If it fails, the returned document will be null, and the optional error variable will contain further details about the error.
Definition at line 273 of file qjsondocument.cpp.
|
static |
Creates a QJsonDocument from the QVariant variant.
If the variant contains any other type than a QVariantMap, QVariantHash, QVariantList or QStringList, the returned document is invalid.
Definition at line 182 of file qjsondocument.cpp.
| bool QJsonDocument::isArray | ( | ) | const |
Returns true if the document contains an array.
Definition at line 305 of file qjsondocument.cpp.
| bool QJsonDocument::isEmpty | ( | ) | const |
Returns true if the document doesn't contain any data.
Definition at line 292 of file qjsondocument.cpp.
| bool QJsonDocument::isNull | ( | ) | const |
returns true if this document is null.
Null documents are documents created through the default constructor.
Documents created from UTF-8 encoded text or the binary format are validated during parsing. If validation fails, the returned document will also be null.
Definition at line 468 of file qjsondocument.cpp.
| bool QJsonDocument::isObject | ( | ) | const |
Returns true if the document contains an object.
Definition at line 318 of file qjsondocument.cpp.
| QJsonObject QJsonDocument::object | ( | ) | const |
Returns the QJsonObject contained in the document.
Returns an empty object if the document contains an array.
Definition at line 334 of file qjsondocument.cpp.
| QJsonDocument & QJsonDocument::operator= | ( | const QJsonDocument & | other | ) |
Assigns the other document to this QJsonDocument.
Returns a reference to this object.
Move-assigns other to this document.
Definition at line 137 of file qjsondocument.cpp.
| const QJsonValue QJsonDocument::operator[] | ( | const QString & | key | ) | const |
Returns a QJsonValue representing the value for the key key.
Equivalent to calling object().value(key).
The returned QJsonValue is QJsonValue::Undefined if the key does not exist, or if isObject() is false.
Definition at line 392 of file qjsondocument.cpp.
| const QJsonValue QJsonDocument::operator[] | ( | QLatin1StringView | key | ) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 413 of file qjsondocument.cpp.
| const QJsonValue QJsonDocument::operator[] | ( | qsizetype | i | ) | const |
Returns a QJsonValue representing the value for index i.
Equivalent to calling array().at(i).
The returned QJsonValue is QJsonValue::Undefined, if i is out of bounds, or if isArray() is false.
Definition at line 433 of file qjsondocument.cpp.
| const QJsonValue QJsonDocument::operator[] | ( | QStringView | key | ) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 401 of file qjsondocument.cpp.
| void QJsonDocument::setArray | ( | const QJsonArray & | array | ) |
Sets array as the main object of this document.
Definition at line 375 of file qjsondocument.cpp.
| void QJsonDocument::setObject | ( | const QJsonObject & | object | ) |
Sets object as the main object of this document.
Definition at line 365 of file qjsondocument.cpp.
|
noexcept |
| QByteArray QJsonDocument::toJson | ( | JsonFormat | format = JsonFormat::Indented | ) | const |
\typealias QJsonDocument::JsonFormat
Same as \l QJsonValue::JsonFormat.
Definition at line 252 of file qjsondocument.cpp.
| QVariant QJsonDocument::toVariant | ( | ) | const |
Returns a QVariant representing the Json document.
The returned variant will be a QVariantList if the document is a QJsonArray and a QVariantMap if the document is a QJsonObject.
Definition at line 213 of file qjsondocument.cpp.
|
friend |
Definition at line 446 of file qjsondocument.cpp.
|
friend |
Definition at line 474 of file qjsondocument.cpp.
|
friend |
Definition at line 99 of file qjsondocument.h.
|
friend |
Definition at line 98 of file qjsondocument.h.
Definition at line 34 of file qjsondocument.h.
|
staticconstexpr |
Definition at line 69 of file qjsondocument.h.
|
staticconstexpr |
Definition at line 68 of file qjsondocument.h.