![]() |
Qt 6.x
The Qt SDK
|
\inmodule QtCore\reentrant More...
#include <qjsondocument.h>
Public Types | |
enum | JsonFormat { Indented , Compact } |
Public Member Functions | |
QJsonDocument () | |
Constructs an empty and invalid document. | |
QJsonDocument (const QJsonObject &object) | |
Creates a QJsonDocument from object. | |
QJsonDocument (const QJsonArray &array) | |
Constructs a QJsonDocument from 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 | |
QJsonDocument & | operator= (QJsonDocument &&other) noexcept |
void | swap (QJsonDocument &other) noexcept |
QVariant | toVariant () const |
Returns a QVariant representing the Json document. | |
QByteArray | toJson (JsonFormat format=Indented) const |
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 | operator== (const QJsonDocument &other) const |
Returns true if the other document is equal to this document. | |
bool | operator!= (const QJsonDocument &other) const |
returns true if other is not equal to this document | |
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') |
Friends | |
class | QJsonValue |
class | QJsonPrivate::Parser |
Q_CORE_EXPORT QDebug | operator<< (QDebug, const QJsonDocument &) |
\inmodule QtCore
\reentrant
The QJsonDocument class provides a way to read and write JSON documents.
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 46 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 76 of file qjsondocument.h.
QJsonDocument::QJsonDocument | ( | ) |
Constructs an empty and invalid document.
Definition at line 80 of file qjsondocument.cpp.
|
explicit |
Creates a QJsonDocument from object.
Definition at line 88 of file qjsondocument.cpp.
References setObject().
|
explicit |
Constructs a QJsonDocument from array.
Definition at line 97 of file qjsondocument.cpp.
References setArray().
|
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 122 of file qjsondocument.cpp.
References other().
|
noexcept |
Move-constructs a QJsonDocument from other.
Definition at line 133 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 356 of file qjsondocument.cpp.
References QJsonPrivate::Value::container(), and isArray().
Referenced by deployQmlImports(), fromJson(), QCborValue::fromVariant(), QJsonValue::fromVariant(), operator>>(), scanImports(), and QEglFSEmulatorIntegration::screenInit().
|
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 282 of file qjsondocument.cpp.
References QByteArray::constData(), error, QJsonPrivate::Parser::parse(), and QByteArray::size().
Referenced by AssimpImporter::AssimpImporter(), QCtfLibImpl::QCtfLibImpl(), deployQmlImports(), fromJson(), getRequestedDisplays(), QSSGRenderShaderMetadata::getShaderMetaData(), QDefaultOutputMapping::load(), QGtk3Json::load(), QKmsScreenConfig::loadConfig(), QGeoTiledMappingManagerEngineNokia::loadCopyrightsDescriptorsFromJson(), loadMapObject(), AndroidStyle::loadStyleData(), QV4DebugServiceImpl::messageReceived(), QV4DebugClient::messageReceived(), QQmlNativeDebugServiceImpl::messageReceived(), moduleFromJsonFile(), TileProvider::onNetworkReplyFinished(), operator>>(), CategoryParser::parse(), Moc::parsePluginData(), QGeoRouteParserOsrmV5Private::parseReply(), QGeoRouteParserOsrmV4Private::parseReply(), readFromDevice(), readGpuFeatures(), readInputFile(), readTranslationsCatalogs(), QOffscreenIntegration::resolveConfigFileConfiguration(), QV4DebugClient::response(), QGeoCodeJsonParser::run(), runQmlImportScanner(), scanImports(), and QEglFSEmulatorIntegration::screenInit().
|
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 192 of file qjsondocument.cpp.
References QCborArray::fromStringList(), QJsonObject::fromVariantHash(), QJsonArray::fromVariantList(), QJsonObject::fromVariantMap(), QMetaType::id(), QVariant::metaType(), setArray(), setObject(), QVariant::toHash(), QVariant::toList(), QVariant::toMap(), QVariant::toStringList(), and variant.
Referenced by injectExtra().
bool QJsonDocument::isArray | ( | ) | const |
Returns true
if the document contains an array.
Definition at line 310 of file qjsondocument.cpp.
Referenced by array(), deployQmlImports(), fromJson(), QCborValue::fromVariant(), QJsonValue::fromVariant(), operator[](), and QEglFSEmulatorIntegration::screenInit().
bool QJsonDocument::isEmpty | ( | ) | const |
Returns true
if the document doesn't contain any data.
Definition at line 297 of file qjsondocument.cpp.
Referenced by QGtk3Json::load(), and QGtk3Json::save().
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 481 of file qjsondocument.cpp.
Referenced by QGeoTiledMappingManagerEngineNokia::loadCopyrightsDescriptorsFromJson(), readInputFile(), and scanImports().
bool QJsonDocument::isObject | ( | ) | const |
Returns true
if the document contains an object.
Definition at line 323 of file qjsondocument.cpp.
Referenced by QT_BEGIN_NAMESPACE::checkDocument(), fromJson(), getRequestedDisplays(), QDefaultOutputMapping::load(), QKmsScreenConfig::loadConfig(), loadMapObject(), object(), operator[](), operator[](), and Moc::parsePluginData().
QJsonObject QJsonDocument::object | ( | ) | const |
Returns the QJsonObject contained in the document.
Returns an empty object if the document contains an array.
Definition at line 339 of file qjsondocument.cpp.
References QJsonPrivate::Value::container(), and isObject().
Referenced by QCtfLibImpl::QCtfLibImpl(), QT_BEGIN_NAMESPACE::checkDocument(), fromJson(), QCborValue::fromVariant(), QJsonValue::fromVariant(), getRequestedDisplays(), QGeoJson::importGeoJson(), QDefaultOutputMapping::load(), QGtk3Json::load(), QKmsScreenConfig::loadConfig(), QGeoTiledMappingManagerEngineNokia::loadCopyrightsDescriptorsFromJson(), loadMapObject(), QV4DebugServiceImpl::messageReceived(), QV4DebugClient::messageReceived(), QQmlNativeDebugServiceImpl::messageReceived(), operator>>(), QT_BEGIN_NAMESPACE::parseDocument(), readFromDevice(), readGpuFeatures(), readInputFile(), and QV4DebugClient::response().
|
inline |
returns true
if other is not equal to this document
Definition at line 103 of file qjsondocument.h.
References other().
QJsonDocument & QJsonDocument::operator= | ( | const QJsonDocument & | other | ) |
Assigns the other document to this QJsonDocument.
Returns a reference to this object.
Definition at line 147 of file qjsondocument.cpp.
References other().
|
inlinenoexcept |
Move-assigns other to this document.
Definition at line 65 of file qjsondocument.h.
References other(), and swap().
bool QJsonDocument::operator== | ( | const QJsonDocument & | other | ) | const |
Returns true
if the other document is equal to this document.
Definition at line 459 of file qjsondocument.cpp.
References other().
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 407 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 428 of file qjsondocument.cpp.
References QJsonPrivate::Value::fromTrustedCbor(), isObject(), QJsonValue, and QJsonValue::Undefined.
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 448 of file qjsondocument.cpp.
References QJsonPrivate::Value::fromTrustedCbor(), i, isArray(), QJsonValue, and QJsonValue::Undefined.
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 416 of file qjsondocument.cpp.
References QJsonPrivate::Value::fromTrustedCbor(), isObject(), QJsonValue, and QJsonValue::Undefined.
void QJsonDocument::setArray | ( | const QJsonArray & | array | ) |
Sets array as the main object of this document.
Definition at line 385 of file qjsondocument.cpp.
References QCborValue::fromJsonValue().
Referenced by QJsonDocument(), and fromVariant().
void QJsonDocument::setObject | ( | const QJsonObject & | object | ) |
Sets object as the main object of this document.
Definition at line 370 of file qjsondocument.cpp.
References QCborValue::fromJsonValue().
Referenced by QJsonDocument(), QQmlNativeDebugServiceImpl::emitAsynchronousMessageToClient(), QGeoJson::exportGeoJson(), fromVariant(), QQmlNativeDebugServiceImpl::messageReceived(), and QV4DebugServiceImpl::send().
|
noexcept |
Swaps the document other with this. This operation is very fast and never fails.
Definition at line 138 of file qjsondocument.cpp.
References d, other(), and qSwap().
QByteArray QJsonDocument::toJson | ( | JsonFormat | format = Indented | ) | const |
Definition at line 256 of file qjsondocument.cpp.
References QJsonPrivate::Writer::arrayToJson(), Compact, QJsonPrivate::Value::container(), and QJsonPrivate::Writer::objectToJson().
Referenced by collectJson(), QQmlNativeDebugServiceImpl::emitAsynchronousMessageToClient(), findPatternUnloaded(), Moc::generate(), QQmlNativeDebugServiceImpl::messageReceived(), operator<<(), operator<<(), operator<<(), QV4DebugClientPrivate::packMessage(), QGtk3Json::save(), QV4DebugServiceImpl::send(), QShaderDescription::toJson(), and JsonOutput::toJson().
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 224 of file qjsondocument.cpp.
References QJsonPrivate::Value::container(), QJsonArray::toVariantList(), and QJsonObject::toVariantMap().
|
friend |
Definition at line 487 of file qjsondocument.cpp.
|
friend |
Definition at line 109 of file qjsondocument.h.
|
friend |
Definition at line 108 of file qjsondocument.h.
Referenced by operator[](), operator[](), and operator[]().
Definition at line 52 of file qjsondocument.h.