QtBase  v6.3.1
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Friends | List of all members
QJsonDocument Class Reference

The QJsonDocument class provides a way to read and write JSON documents. More...

#include <qjsondocument.h>

Public Types

enum  JsonFormat { Indented , Compact }
 

Public Member Functions

 QJsonDocument ()
 
 QJsonDocument (const QJsonObject &object)
 
 QJsonDocument (const QJsonArray &array)
 
 ~QJsonDocument ()
 
 QJsonDocument (const QJsonDocument &other)
 
QJsonDocumentoperator= (const QJsonDocument &other)
 
 QJsonDocument (QJsonDocument &&other) noexcept
 
QJsonDocumentoperator= (QJsonDocument &&other) noexcept
 
void swap (QJsonDocument &other) noexcept
 
QVariant toVariant () const
 
QByteArray toJson (JsonFormat format=Indented) const
 
bool isEmpty () const
 
bool isArray () const
 
bool isObject () const
 
QJsonObject object () const
 
QJsonArray array () const
 
void setObject (const QJsonObject &object)
 
void setArray (const QJsonArray &array)
 
const QJsonValue operator[] (const QString &key) const
 
const QJsonValue operator[] (QStringView key) const
 
const QJsonValue operator[] (QLatin1String key) const
 
const QJsonValue operator[] (qsizetype i) const
 
bool operator== (const QJsonDocument &other) const
 
bool operator!= (const QJsonDocument &other) const
 
bool isNull () const
 

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 &)
 

Detailed Description

The QJsonDocument class provides a way to read and write JSON documents.

\inmodule QtCore

\reentrant

Since
5.0

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.

See also
{JSON Support in Qt}, {JSON Save Game Example}

Definition at line 82 of file qjsondocument.h.

Member Enumeration Documentation

◆ JsonFormat

Since
5.1

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.

Constructor & Destructor Documentation

◆ QJsonDocument() [1/5]

QJsonDocument::QJsonDocument ( )

Constructs an empty and invalid document.

Definition at line 113 of file qjsondocument.cpp.

◆ QJsonDocument() [2/5]

QJsonDocument::QJsonDocument ( const QJsonObject object)
explicit

Creates a QJsonDocument from object.

Definition at line 121 of file qjsondocument.cpp.

Here is the call graph for this function:

◆ QJsonDocument() [3/5]

QJsonDocument::QJsonDocument ( const QJsonArray array)
explicit

Constructs a QJsonDocument from array.

Definition at line 130 of file qjsondocument.cpp.

Here is the call graph for this function:

◆ ~QJsonDocument()

QJsonDocument::~QJsonDocument ( )
default

Deletes the document.

Binary data set with fromRawData is not freed.

◆ QJsonDocument() [4/5]

QJsonDocument::QJsonDocument ( const QJsonDocument other)

Creates a copy of the other document.

Definition at line 155 of file qjsondocument.cpp.

Here is the call graph for this function:

◆ QJsonDocument() [5/5]

QJsonDocument::QJsonDocument ( QJsonDocument &&  other)
noexcept
Since
5.10

Move-constructs a QJsonDocument from other.

Definition at line 166 of file qjsondocument.cpp.

Member Function Documentation

◆ array()

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()

Definition at line 389 of file qjsondocument.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fromJson()

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

See also
toJson(), QJsonParseError, isNull()

Definition at line 315 of file qjsondocument.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fromVariant()

QJsonDocument QJsonDocument::fromVariant ( const QVariant variant)
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.

See also
toVariant()

Definition at line 225 of file qjsondocument.cpp.

Here is the call graph for this function:

◆ isArray()

bool QJsonDocument::isArray ( ) const

Returns true if the document contains an array.

See also
array(), isObject()

Definition at line 343 of file qjsondocument.cpp.

Here is the caller graph for this function:

◆ isEmpty()

bool QJsonDocument::isEmpty ( ) const

Returns true if the document doesn't contain any data.

Definition at line 330 of file qjsondocument.cpp.

◆ isNull()

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.

Here is the caller graph for this function:

◆ isObject()

bool QJsonDocument::isObject ( ) const

Returns true if the document contains an object.

See also
object(), isArray()

Definition at line 356 of file qjsondocument.cpp.

Here is the caller graph for this function:

◆ object()

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()

Definition at line 372 of file qjsondocument.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator!=()

bool QJsonDocument::operator!= ( const QJsonDocument other) const
inline

returns true if other is not equal to this document

Definition at line 141 of file qjsondocument.h.

Here is the call graph for this function:

◆ operator=() [1/2]

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.

Here is the call graph for this function:

◆ operator=() [2/2]

QJsonDocument & QJsonDocument::operator= ( QJsonDocument &&  other)
inlinenoexcept
Since
5.10

Move-assigns other to this document.

Definition at line 101 of file qjsondocument.h.

Here is the call graph for this function:

◆ operator==()

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.

Here is the call graph for this function:

◆ operator[]() [1/4]

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.

Since
5.10
See also
QJsonValue, QJsonValue::isUndefined(), QJsonObject

Definition at line 441 of file qjsondocument.cpp.

◆ operator[]() [2/4]

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.

Since
5.10

Definition at line 463 of file qjsondocument.cpp.

Here is the call graph for this function:

◆ operator[]() [3/4]

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.

Since
5.10
See also
QJsonValue, QJsonValue::isUndefined(), QJsonArray

Definition at line 483 of file qjsondocument.cpp.

Here is the call graph for this function:

◆ operator[]() [4/4]

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.

Since
5.14

Definition at line 451 of file qjsondocument.cpp.

Here is the call graph for this function:

◆ setArray()

void QJsonDocument::setArray ( const QJsonArray array)

Sets array as the main object of this document.

See also
setObject(), array()

Definition at line 418 of file qjsondocument.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setObject()

void QJsonDocument::setObject ( const QJsonObject object)

Sets object as the main object of this document.

See also
setArray(), object()

Definition at line 403 of file qjsondocument.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ swap()

void QJsonDocument::swap ( QJsonDocument other)
noexcept
Since
5.10

Swaps the document other with this. This operation is very fast and never fails.

Definition at line 171 of file qjsondocument.cpp.

Here is the call graph for this function:

◆ toJson()

QByteArray QJsonDocument::toJson ( JsonFormat  format = Indented) const
Since
5.1 Converts the QJsonDocument to a UTF-8 encoded JSON document in the provided format.
See also
fromJson(), JsonFormat

Definition at line 289 of file qjsondocument.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toVariant()

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()

Definition at line 257 of file qjsondocument.cpp.

Here is the call graph for this function:

Friends And Related Function Documentation

◆ operator<<

Q_CORE_EXPORT QDebug operator<< ( QDebug  ,
const QJsonDocument  
)
friend

Definition at line 522 of file qjsondocument.cpp.

◆ QJsonPrivate::Parser

friend class QJsonPrivate::Parser
friend

Definition at line 147 of file qjsondocument.h.

◆ QJsonValue

friend class QJsonValue
friend

Definition at line 146 of file qjsondocument.h.

Member Data Documentation

◆ BinaryFormatTag

const uint QJsonDocument::BinaryFormatTag = ('q' << 24) | ('b' << 16) | ('j' << 8) | ('s')
static

Definition at line 88 of file qjsondocument.h.


The documentation for this class was generated from the following files: