QtBase
v6.3.1
|
The QJsonDocument class provides a way to read and write JSON documents. More...
#include <qjsondocument.h>
Public Types | |
enum | JsonFormat { Indented , Compact } |
Static Public Member Functions | |
static QJsonDocument | fromVariant (const QVariant &variant) |
static QJsonDocument | fromJson (const QByteArray &json, QJsonParseError *error=nullptr) |
Static Public Attributes | |
static const uint | BinaryFormatTag = ('q' << 24) | ('b' << 16) | ('j' << 8) | ('s') |
Friends | |
class | QJsonValue |
class | QJsonPrivate::Parser |
Q_CORE_EXPORT QDebug | operator<< (QDebug, const QJsonDocument &) |
The QJsonDocument class provides a way to read and write JSON documents.
\inmodule QtCore
\reentrant
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 82 of file qjsondocument.h.
This value defines the format of the JSON byte array produced when converting to a QJsonDocument using toJson().
\value Indented Defines human readable output as follows:
\value Compact Defines a compact output as follows:
Enumerator | |
---|---|
Indented | |
Compact |
Definition at line 112 of file qjsondocument.h.
QJsonDocument::QJsonDocument | ( | ) |
Constructs an empty and invalid document.
Definition at line 113 of file qjsondocument.cpp.
|
explicit |
Creates a QJsonDocument from object.
Definition at line 121 of file qjsondocument.cpp.
|
explicit |
Constructs a QJsonDocument from array.
Definition at line 130 of file qjsondocument.cpp.
|
default |
Deletes the document.
Binary data set with fromRawData is not freed.
QJsonDocument::QJsonDocument | ( | const QJsonDocument & | other | ) |
Creates a copy of the other document.
Definition at line 155 of file qjsondocument.cpp.
|
noexcept |
Move-constructs a QJsonDocument from other.
Definition at line 166 of file qjsondocument.cpp.
QJsonArray QJsonDocument::array | ( | ) | const |
Returns the QJsonArray contained in the document.
Returns an empty array if the document contains an object.
Definition at line 389 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 315 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 225 of file qjsondocument.cpp.
bool QJsonDocument::isArray | ( | ) | const |
Returns true
if the document contains an array.
Definition at line 343 of file qjsondocument.cpp.
bool QJsonDocument::isEmpty | ( | ) | const |
Returns true
if the document doesn't contain any data.
Definition at line 330 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 516 of file qjsondocument.cpp.
bool QJsonDocument::isObject | ( | ) | const |
Returns true
if the document contains an object.
Definition at line 356 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 372 of file qjsondocument.cpp.
|
inline |
returns true
if other is not equal to this document
Definition at line 141 of file qjsondocument.h.
QJsonDocument & QJsonDocument::operator= | ( | const QJsonDocument & | other | ) |
Assigns the other document to this QJsonDocument. Returns a reference to this object.
Definition at line 180 of file qjsondocument.cpp.
|
inlinenoexcept |
Move-assigns other to this document.
Definition at line 101 of file qjsondocument.h.
bool QJsonDocument::operator== | ( | const QJsonDocument & | other | ) | const |
Returns true
if the other document is equal to this document.
Definition at line 494 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 441 of file qjsondocument.cpp.
const QJsonValue QJsonDocument::operator[] | ( | QLatin1String | 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 463 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 483 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 451 of file qjsondocument.cpp.
void QJsonDocument::setArray | ( | const QJsonArray & | array | ) |
Sets array as the main object of this document.
Definition at line 418 of file qjsondocument.cpp.
void QJsonDocument::setObject | ( | const QJsonObject & | object | ) |
Sets object as the main object of this document.
Definition at line 403 of file qjsondocument.cpp.
|
noexcept |
Swaps the document other with this. This operation is very fast and never fails.
Definition at line 171 of file qjsondocument.cpp.
QByteArray QJsonDocument::toJson | ( | JsonFormat | format = Indented | ) | const |
Definition at line 289 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 257 of file qjsondocument.cpp.
|
friend |
Definition at line 522 of file qjsondocument.cpp.
|
friend |
Definition at line 147 of file qjsondocument.h.
|
friend |
Definition at line 146 of file qjsondocument.h.
Definition at line 88 of file qjsondocument.h.