QJsonDocument¶
The
QJsonDocument
class provides a way to read and write JSON documents. More…
Synopsis¶
Functions¶
def
__eq__
(other)def
__ne__
(other)def
array
()def
isArray
()def
isEmpty
()def
isNull
()def
isObject
()def
object
()def
operator[]
(i)def
operator[]
(key)def
rawData
(size)def
setArray
(array)def
setObject
(object)def
swap
(other)def
toBinaryData
()def
toJson
()def
toJson
(format)def
toVariant
()
Static functions¶
def
fromBinaryData
(data[, validation=Validate])def
fromJson
(json[, error=None])def
fromRawData
(data, size[, validation=Validate])def
fromVariant
(variant)
Detailed Description¶
QJsonDocument
is a class that wraps a complete JSON document and can read and write this document both from a UTF-8 encoded text based representation as well as Qt’s own binary format.A JSON document can be converted from its text-based representation to a
QJsonDocument
usingfromJson()
.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()
andisObject()
. The array or object contained in the document can be retrieved usingarray()
orobject()
and then read or manipulated.A document can also be created from a stored binary representation using
fromBinaryData()
orfromRawData()
.See also
JSON Support in Qt JSON Save Game Example
- class PySide2.QtCore.QJsonDocument¶
PySide2.QtCore.QJsonDocument(array)
PySide2.QtCore.QJsonDocument(other)
PySide2.QtCore.QJsonDocument(object)
- param array:
- param other:
- param object:
QJsonObject
Constructs an empty and invalid document.
- PySide2.QtCore.QJsonDocument.DataValidation¶
This value is used to tell
QJsonDocument
whether to validate the binary data when converting to aQJsonDocument
usingfromBinaryData()
orfromRawData()
.Constant
Description
QJsonDocument.Validate
Validate the data before using it. This is the default.
QJsonDocument.BypassValidation
Bypasses data validation. Only use if you received the data from a trusted place and know it’s valid, as using of invalid data can crash the application.
- PySide2.QtCore.QJsonDocument.JsonFormat¶
This value defines the format of the JSON byte array produced when converting to a
QJsonDocument
usingtoJson()
.Constant
Description
QJsonDocument.Indented
Defines human readable output as follows:
{ "Array": [ true, 999, "string" ], "Key": "Value", "null": null }
QJsonDocument.Compact
Defines a compact output as follows:
{"Array":[true,999,"string"],"Key":"Value","null":null}
- PySide2.QtCore.QJsonDocument.array()¶
- Return type:
Returns the
QJsonArray
contained in the document.Returns an empty array if the document contains an object.
See also
- static PySide2.QtCore.QJsonDocument.fromBinaryData(data[, validation=Validate])¶
- Parameters:
data –
PySide2.QtCore.QByteArray
validation –
DataValidation
- Return type:
Note
This function is deprecated.
Creates a
QJsonDocument
fromdata
.validation
decides whether the data is checked for validity before being used. By default the data is validated. If thedata
is not valid, the method returns a null document.Note
Deprecated in Qt 5.15. The binary JSON encoding is only retained for backwards compatibility. It is undocumented and restrictive in the maximum size of JSON documents that can be encoded. Qt JSON types can be converted to Qt CBOR types, which can in turn be serialized into the CBOR binary format and vice versa. The CBOR format is a well-defined and less restrictive binary representation for a superset of JSON.
See also
toBinaryData()
fromRawData()
isNull()
DataValidation
QCborValue
- static PySide2.QtCore.QJsonDocument.fromJson(json[, error=None])¶
- Parameters:
json –
PySide2.QtCore.QByteArray
error –
PySide2.QtCore.QJsonParseError
- Return type:
Parses
json
as a UTF-8 encoded JSON document, and creates aQJsonDocument
from it.Returns a valid (non-null)
QJsonDocument
if the parsing succeeds. If it fails, the returned document will be null, and the optionalerror
variable will contain further details about the error.See also
- static PySide2.QtCore.QJsonDocument.fromRawData(data, size[, validation=Validate])¶
- Parameters:
data – str
size – int
validation –
DataValidation
- Return type:
Note
This function is deprecated.
Creates a
QJsonDocument
that uses the firstsize
bytes fromdata
. It assumesdata
contains a binary encoded JSON document. The created document does not take ownership ofdata
. The data is copied into a different data structure, and the original data can be deleted or modified afterwards.data
has to be aligned to a 4 byte boundary.validation
decides whether the data is checked for validity before being used. By default the data is validated. If thedata
is not valid, the method returns a null document.Returns a
QJsonDocument
representing the data.Note
Deprecated in Qt 5.15. The binary JSON encoding is only retained for backwards compatibility. It is undocumented and restrictive in the maximum size of JSON documents that can be encoded. Qt JSON types can be converted to Qt CBOR types, which can in turn be serialized into the CBOR binary format and vice versa. The CBOR format is a well-defined and less restrictive binary representation for a superset of JSON.
Note
Before Qt 5.15, the caller had to guarantee that
data
would not be deleted or modified as long as anyQJsonDocument
,QJsonObject
orQJsonArray
still referenced the data. From Qt 5.15 on, this is not necessary anymore.See also
rawData()
fromBinaryData()
isNull()
DataValidation
QCborValue
- static PySide2.QtCore.QJsonDocument.fromVariant(variant)¶
- Parameters:
variant – object
- Return type:
Creates a
QJsonDocument
from theQVariant
variant
.If the
variant
contains any other type than aQVariantMap
,QVariantHash
,QVariantList
orQStringList
, the returned document is invalid.See also
- PySide2.QtCore.QJsonDocument.isArray()¶
- Return type:
bool
Returns
true
if the document contains an array.See also
- PySide2.QtCore.QJsonDocument.isEmpty()¶
- Return type:
bool
Returns
true
if the document doesn’t contain any data.
- PySide2.QtCore.QJsonDocument.isNull()¶
- Return type:
bool
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.
- PySide2.QtCore.QJsonDocument.isObject()¶
- Return type:
bool
Returns
true
if the document contains an object.
- PySide2.QtCore.QJsonDocument.object()¶
- Return type:
QJsonObject
Returns the
QJsonObject
contained in the document.Returns an empty object if the document contains an array.
See also
- PySide2.QtCore.QJsonDocument.__ne__(other)¶
- Parameters:
other –
PySide2.QtCore.QJsonDocument
- Return type:
bool
returns
true
ifother
is not equal to this document
- PySide2.QtCore.QJsonDocument.__eq__(other)¶
- Parameters:
other –
PySide2.QtCore.QJsonDocument
- Return type:
bool
Returns
true
if theother
document is equal to this document.
- PySide2.QtCore.QJsonDocument.operator[](key)
- Parameters:
key – str
- Return type:
- PySide2.QtCore.QJsonDocument.operator[](i)
- Parameters:
i – int
- Return type:
Returns a
QJsonValue
representing the value for indexi
.Equivalent to calling
array()
.at(i).The returned
QJsonValue
isUndefined
, ifi
is out of bounds, or ifisArray()
is false.See also
- PySide2.QtCore.QJsonDocument.rawData(size)¶
- Parameters:
size – int
- Return type:
str
Note
This function is deprecated.
Returns the raw binary representation of the data
size
will contain the size of the returned data.This method is useful to e.g. stream the JSON document in its binary form to a file.
Note
Deprecated in Qt 5.15. The binary JSON encoding is only retained for backwards compatibility. It is undocumented and restrictive in the maximum size of JSON documents that can be encoded. Qt JSON types can be converted to Qt CBOR types, which can in turn be serialized into the CBOR binary format and vice versa. The CBOR format is a well-defined and less restrictive binary representation for a superset of JSON.
See also
- PySide2.QtCore.QJsonDocument.setArray(array)¶
- Parameters:
array –
PySide2.QtCore.QJsonArray
Sets
array
as the main object of this document.See also
- PySide2.QtCore.QJsonDocument.setObject(object)¶
- Parameters:
object –
QJsonObject
Sets
object
as the main object of this document.See also
- PySide2.QtCore.QJsonDocument.swap(other)¶
- Parameters:
other –
PySide2.QtCore.QJsonDocument
Swaps the document
other
with this. This operation is very fast and never fails.
- PySide2.QtCore.QJsonDocument.toBinaryData()¶
- Return type:
Note
This function is deprecated.
Returns a binary representation of the document.
The binary representation is also the native format used internally in Qt, and is very efficient and fast to convert to and from.
The binary format can be stored on disk and interchanged with other applications or computers.
fromBinaryData()
can be used to convert it back into a JSON document.Note
Deprecated in Qt 5.15. The binary JSON encoding is only retained for backwards compatibility. It is undocumented and restrictive in the maximum size of JSON documents that can be encoded. Qt JSON types can be converted to Qt CBOR types, which can in turn be serialized into the CBOR binary format and vice versa. The CBOR format is a well-defined and less restrictive binary representation for a superset of JSON.
See also
- PySide2.QtCore.QJsonDocument.toJson()¶
- Return type:
Converts the
QJsonDocument
to an indented, UTF-8 encoded JSON document.See also
- PySide2.QtCore.QJsonDocument.toJson(format)
- Parameters:
format –
JsonFormat
- Return type:
Converts the
QJsonDocument
to a UTF-8 encoded JSON document in the providedformat
.See also
fromJson()
JsonFormat
- PySide2.QtCore.QJsonDocument.toVariant()¶
- Return type:
object
Returns a
QVariant
representing the Json document.The returned variant will be a
QVariantList
if the document is aQJsonArray
and aQVariantMap
if the document is aQJsonObject
.See also
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.