CopperSpice API  1.9.1
QJsonDocument Class Reference

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)
 

Detailed Description

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.

See also
JSON Support

Member Enumeration Documentation

This value is used to indicate whether to validate the binary data when converting to a QJsonDocument.

ConstantValueDescription
QJsonDocument::Validate0 Validate the data before using it. This is the default.
QJsonDocument::BypassValidation1 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().

ConstantValueDescription
QJsonDocument::Indented0Defines human readable output as follows:
{
"Array": [
true,
999,
"string"
],
"Key": "Value",
"null": null
}
QJsonDocument::Compact1Defines a compact output as follows:
{"Array": [true,999,"string"],"Key": "Value","null": null}

Constructor & Destructor Documentation

QJsonDocument::QJsonDocument ( )

Constructs an empty and invalid document.

QJsonDocument::QJsonDocument ( const QJsonObject object)
explicit

Creates a QJsonDocument from object.

QJsonDocument::QJsonDocument ( const QJsonArray array)
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.

Method Documentation

QJsonArray QJsonDocument::array ( ) const

Returns the QJsonArray contained in the document. Returns an empty array if the document contains an object.

See also
isArray(), object(), setArray()
QJsonDocument QJsonDocument::fromJson ( const QByteArray json,
QJsonParseError error = nullptr 
)
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.

See also
toJsonByteArray(), toJsonString(), QJsonParseError
QJsonDocument QJsonDocument::fromJson ( QStringView  json,
QJsonParseError error = nullptr 
)
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.

See also
toJson(), QJsonParseError
QJsonDocument QJsonDocument::fromVariant ( const QVariant variant)
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.

See also
toVariant()
bool QJsonDocument::isArray ( ) const

Returns true if the document contains an array.

See also
array(), isObject()
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

Returns true if the document contains an object.

See also
object(), isArray()
QJsonObject QJsonDocument::object ( ) const

Returns the QJsonObject contained in the document. Returns an empty object if the document contains an array.

See also
isObject(), array(), setObject()
bool QJsonDocument::operator!= ( const QJsonDocument &  other) const
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.

See also
setObject(), array()
void QJsonDocument::setObject ( const QJsonObject object)

Sets object as the main object of this document.

See also
setArray(), object()
QByteArray QJsonDocument::toJson ( JsonFormat  format = Indented) const
inline

Converts the QJsonDocument to a UTF-8 encoded JSON document in the provided format.

See also
fromJson(), JsonFormat
QByteArray QJsonDocument::toJsonByteArray ( JsonFormat  format = Indented) const
inline

Converts the QJsonDocument to a QByteArray JSON document in the provided format.

See also
fromJson(), JsonFormat
QString QJsonDocument::toJsonString ( JsonFormat  format = Indented) const

Converts the QJsonDocument to a UTF-8 encoded JSON document in the provided format.

See also
fromJson(), JsonFormat
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.

See also
fromVariant(), QJsonValue::toVariant()