- Qt 6.9
- Qt 5 Core Compatibility APIs
- QBinaryJson Namespace
- Qt 6.9.1 Reference Documentation

# QBinaryJson Namespace

Contains functions for converting QJsonDocument to and from JSON binary format. More...

Header: | `#include <QBinaryJson>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Core5Compat)` `target_link_libraries(mytarget PRIVATE Qt6::Core5Compat)`
qmake: | `QT += core5compat`

## Types

enum | DataValidation { Validate, BypassValidation }

## Functions

QJsonDocument | fromBinaryData(const QByteArray &data, QBinaryJson::DataValidation validation = Validate)
QJsonDocument | fromRawData(const char *data, int size, QBinaryJson::DataValidation validation = Validate)
QByteArray | toBinaryData(const QJsonDocument &document)
const char * | toRawData(const QJsonDocument &document, int *size)

## Detailed Description

This namespace provides utility functions to keep compatibility with older code, which uses the JSON binary format for serializing JSON. Qt JSON types can be converted to Qt CBOR types, which can in turn be serialized into the CBOR binary format and vice versa.

## Type Documentation

### enum QBinaryJson::DataValidation

This enum is used to tell QJsonDocument whether to validate the binary data when converting to a QJsonDocument using fromBinaryData() or fromRawData().

Constant | Value | Description
--- | --- | ---
`QBinaryJson::Validate` | `0` | Validate the data before using it. This is the default.
`QBinaryJson::BypassValidation` | `1` | 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.

## Function Documentation

### QJsonDocument QBinaryJson::fromBinaryData(const QByteArray &data, QBinaryJson::DataValidation validation = Validate)

Creates a QJsonDocument from data.

validation decides whether the data is checked for validity before being used. By default the data is validated. If the data is not valid, the method returns a null document.

Note: 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(), DataValidation, and QCborValue.

### QJsonDocument QBinaryJson::fromRawData(const char *data, int size, QBinaryJson::DataValidation validation = Validate)

Creates a QJsonDocument that uses the first size bytes from data. It assumes data contains a binary encoded JSON document. The created document does not take ownership of data. 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 the data is not valid, the method returns a null document.

Returns a QJsonDocument representing the data.

Note: 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 any QJsonDocument, QJsonObject or QJsonArray still referenced the data. From Qt 5.15 on, this is not necessary anymore.

See also toRawData(), fromBinaryData(), DataValidation, and QCborValue.

### QByteArray QBinaryJson::toBinaryData(const QJsonDocument &document)

Returns a binary representation of 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: 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 fromBinaryData() and QCborValue.

### const char *QBinaryJson::toRawData(const QJsonDocument &document, int *size)

Returns the raw binary representation of document. 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: 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 fromRawData(), fromBinaryData(), toBinaryData(), and QCborValue.