CopperSpice API
1.9.1
|
Provides a way to read and write JSON documents. More...
Public Types | |
enum | DataValidation |
enum | JsonFormat |
Public Methods | |
QJsonDocument () | |
QJsonDocument (const QJsonArray &array) | |
QJsonDocument (const QJsonDocument &other) | |
QJsonDocument (const QJsonObject &object) | |
QJsonDocument (QJsonDocument &&other) | |
~QJsonDocument () | |
QJsonArray | array () const |
bool | isArray () const |
bool | isEmpty () const |
bool | isNull () const |
bool | isObject () const |
QJsonObject | object () const |
bool | operator!= (const QJsonDocument &other) const |
QJsonDocument & | operator= (const QJsonDocument &other) |
bool | operator== (const QJsonDocument &other) const |
void | setArray (const QJsonArray &array) |
void | setObject (const QJsonObject &object) |
QByteArray | toJson (JsonFormat format=Indented) const |
QByteArray | toJsonByteArray (JsonFormat format=Indented) const |
QString | toJsonString (JsonFormat format=Indented) const |
QVariant | toVariant () const |
Static Public Methods | |
static QJsonDocument | fromJson (const QByteArray &json, QJsonParseError *error=nullptr) |
static QJsonDocument | fromJson (QStringView json, QJsonParseError *error=nullptr) |
static QJsonDocument | fromVariant (const QVariant &variant) |
The QJsonDocument class is used to read and write JSON data. A JSON document can be converted from its text based representation to a QJsonDocument using QJsonDocument::fromJson(). The method QJsonDocument::toJson() converts a document back to text.
The validity of the document can be queried with the method isNull(). A document can also be queried to determine whether it contains an array or an object using isArray() and isObject(). The array or object contained in the document can be retrieved from the document using array() or object() and then read or modified.
This value is used to indicate whether to validate the binary data when converting to a QJsonDocument.
Constant | Value | Description |
---|---|---|
QJsonDocument::Validate | 0 | Validate the data before using it. This is the default. |
QJsonDocument::BypassValidation | 1 | Bypasses data validation. Only use if you received the data from a trusted place and know it is valid, as using of invalid data can crash the application. |
This value defines the format of the JSON byte array produced when converting to a QJsonDocument using toJson().
Constant | Value | Description |
---|---|---|
QJsonDocument::Indented | 0 | Defines human readable output as follows: {
"Array": [
true,
999,
"string"
],
"Key": "Value",
"null": null
}
|
QJsonDocument::Compact | 1 | Defines a compact output as follows: {"Array": [true,999,"string"],"Key": "Value","null": null}
|
QJsonDocument::QJsonDocument | ( | ) |
Constructs an empty and invalid document.
|
explicit |
Creates a QJsonDocument from object.
|
explicit |
Constructs a QJsonDocument from array.
QJsonDocument::QJsonDocument | ( | const QJsonDocument & | other | ) |
Copy constructs a new QJsonDocument from other.
QJsonDocument::QJsonDocument | ( | QJsonDocument && | other | ) |
Move constructs a new QJsonDocument from other.
QJsonDocument::~QJsonDocument | ( | ) |
Deletes the document.
QJsonArray QJsonDocument::array | ( | ) | const |
Returns the QJsonArray contained in the document. Returns an empty array if the document contains an object.
|
static |
Parses a UTF-8 encoded JSON document and creates a QJsonDocument from it. The byte array json contains the json document to be parsed.
The optional error variable can be used to pass in a QJsonParseError data structure that will contain information about possible errors encountered during parsing.
|
static |
Parses a UTF-8 encoded JSON document and creates a QJsonDocument from it. The string view json contains the json document to be parsed.
The optional error variable can be used to pass in a QJsonParseError data structure that will contain information about possible errors encountered during parsing.
|
static |
Creates a QJsonDocument from the QVariant variant. IF the variant contains any other type than a QVariantMap, QVariantList or QStringList, the returned document is invalid.
bool QJsonDocument::isArray | ( | ) | const |
Returns true
if the document contains an array.
bool QJsonDocument::isEmpty | ( | ) | const |
Returns true
if the document does not contain any data.
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.
bool QJsonDocument::isObject | ( | ) | const |
QJsonObject QJsonDocument::object | ( | ) | const |
Returns the QJsonObject contained in the document. Returns an empty object if the document contains an array.
|
inline |
Returns true
if other is not equal to this document.
QJsonDocument & QJsonDocument::operator= | ( | const QJsonDocument & | other | ) |
Copy assigns from other and returns a reference to this object.
bool QJsonDocument::operator== | ( | const QJsonDocument & | other | ) | const |
Returns true
if the other document is equal to this document.
void QJsonDocument::setArray | ( | const QJsonArray & | array | ) |
Sets array as the main object of this document.
void QJsonDocument::setObject | ( | const QJsonObject & | object | ) |
Sets object as the main object of this document.
|
inline |
Converts the QJsonDocument to a UTF-8 encoded JSON document in the provided format.
|
inline |
Converts the QJsonDocument to a QByteArray JSON document in the provided format.
QString QJsonDocument::toJsonString | ( | JsonFormat | format = Indented | ) | const |
Converts the QJsonDocument to a UTF-8 encoded JSON document in the provided format.
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.