QProtobufJsonSerializer Class

The QProtobufJsonSerializer class is an interface that represents basic functions for serialization/deserialization of QProtobufMessage objects to JSON. More...

Header: #include <QProtobufJsonSerializer>
CMake: find_package(Qt6 REQUIRED COMPONENTS Protobuf)
target_link_libraries(mytarget PRIVATE Qt6::Protobuf)
Since: Qt 6.7
Inherits: QAbstractProtobufSerializer

Public Functions

(since 6.13) int deserializationDepthLimit() const
(since 6.13) void setDeserializationDepthLimit(int limit)

Reimplemented Public Functions

virtual QAbstractProtobufSerializer::Error lastError() const override
virtual QString lastErrorString() const override

Detailed Description

The QProtobufJsonSerializer class registers serializers/deserializers for classes implementing a protobuf message, inheriting QProtobufMessage. These classes are generated automatically, based on a .proto file, using the CMake function qt_add_protobuf or by running qtprotobufgen directly.

Member Function Documentation

[since 6.13] int QProtobufJsonSerializer::deserializationDepthLimit() const

Returns the maximum nesting depth of messages that deserialize() accepts.

If the input data contains messages nested deeper than this limit, deserialize() returns false and lastError() returns QAbstractProtobufSerializer::Error::InvalidFormat. This limit does not affect serialize(). The default is the same as in the upstream C++ and Java protobuf implementations. The top-level message is not counted: with a limit of N, a message may contain N levels of nested messages.

The value is chosen in this order:

  1. The limit set with setDeserializationDepthLimit().
  2. The QT_PROTOBUF_NESTING_LIMIT environment variable, if it is set to a positive integer. It is read once per process, the first time the limit is needed. Later changes to it are not used. Any other value is ignored with a warning.
  3. The default: 100.

Note: The JSON parser has its own limit of 1024 nested JSON objects and arrays. Input nested deeper than that fails even if this limit is higher. A repeated message field adds an array level, so the effective message nesting can be lower than 1024.

This function was introduced in Qt 6.13.

See also setDeserializationDepthLimit().

[override virtual] QAbstractProtobufSerializer::Error QProtobufJsonSerializer::lastError() const

Reimplements: QAbstractProtobufSerializer::lastError() const.

Returns the last deserialization error for the serializer instance.

See also lastErrorString().

[override virtual] QString QProtobufJsonSerializer::lastErrorString() const

Reimplements: QAbstractProtobufSerializer::lastErrorString() const.

Returns the last deserialization error string for the serializer instance.

See also lastError().

[since 6.13] void QProtobufJsonSerializer::setDeserializationDepthLimit(int limit)

Sets the maximum nesting depth of messages that deserialize() accepts to limit. It does not affect serialize(). The limit must be a positive integer; otherwise a warning is printed and the limit is not changed. This limit is used instead of the QT_PROTOBUF_NESTING_LIMIT environment variable.

It is recommended to bound the nesting of messages received from untrusted sources.

This function was introduced in Qt 6.13.

See also deserializationDepthLimit().