QCborStreamReader Class

The QCborStreamReader class is a simple CBOR stream decoder, operating on either a QByteArray or QIODevice. More...

Header: #include <QCborStreamReader>
qmake: QT += core
Since: Qt 5.12

This class was introduced in Qt 5.12.

Note: All functions in this class are reentrant.

Public Types

struct StringResult
enum StringResultCode { EndOfString, Ok, Error }
enum Type { UnsignedInteger, NegativeInteger, ByteArray, ByteString, String, …, Invalid }

Detailed Description

This class can be used to decode a stream of CBOR content directly from either a QByteArray or a QIODevice. CBOR is the Concise Binary Object Representation, a very compact form of binary data encoding that is compatible with JSON. It was created by the IETF Constrained RESTful Environments (CoRE) WG, which has used it in many new RFCs. It is meant to be used alongside the CoAP protocol.

QCborStreamReader provides a StAX-like API, similar to that of QXmlStreamReader. Using it requires a bit of knowledge of CBOR encoding. For a simpler API, see QCborValue and especially the decoding function QCborValue::fromCbor().

Typically, one creates a QCborStreamReader by passing the source QByteArray or QIODevice as a parameter to the constructor, then pop elements off the stream if there were no errors in decoding. There are three kinds of CBOR types:

KindTypesBehavior
Fixed-widthIntegers, Tags, Simple types, Floating pointValue is pre-parsed by QCborStreamReader, so accessor functions are const. Must call next() to advance.
StringsByte arrays, Text stringsLength (if known) is pre-parsed, but the string itself is not. The accessor functions are not const and may allocate memory. Once called, the accessor functions automatically advance to the next element.
ContainersArrays, MapsLength (if known) is pre-parsed. To access the elements, you must call enterContainer(), read all elements, then call leaveContainer(). That function advances to the next element.

So a processor function typically looks like this:

    void handleStream(QCborStreamReader &reader)
    {
        switch (reader.type())
        case QCborStreamReader::UnsignedInteger:
        case QCborStreamReader::NegativeInteger:
        case QCborStreamReader::SimpleType:
        case QCborStreamReader::Float16:
        case QCborStreamReader::Float:
        case QCborStreamReader::Double:
            handleFixedWidth(reader);
            reader.next();
            break;
        case QCborStreamReader::ByteArray:
        case QCborStreamReader::String:
            handleString(reader);
            break;
        case QCborStreamReader::Array:
        case QCborStreamReader::Map:
            reader.enterContainer();
            while (reader.lastError() == QCborError::NoError)
                handleStream(reader);
            if (reader.lastError() == QCborError::NoError)
                reader.leaveContainer();
        }
    }

CBOR support

The following table lists the CBOR features that QCborStreamReader supports.

FeatureSupport
Unsigned numbersYes (full range)
Negative numbersYes (full range)
Byte stringsYes
Text stringsYes
Chunked stringsYes
TagsYes (arbitrary)
BooleansYes
NullYes
UndefinedYes
Arbitrary simple valuesYes
Half-precision float (16-bit)Yes
Single-precision float (32-bit)Yes
Double-precision float (64-bit)Yes
Infinities and NaN floating pointYes
Determinate-length arrays and mapsYes
Indeterminate-length arrays and mapsYes
Map key types other than strings and integersYes (arbitrary)

Dealing with invalid or incomplete CBOR streams

QCborStreamReader is capable of detecting corrupt input on its own. The library it uses has been extensively tested against invalid input of any kind and is quite able to report errors. If any is detected, QCborStreamReader will set lastError() to a value besides QCborError::NoError, indicating which situation was detected.

Most errors detected by QCborStreamReader during normal item parsing are not recoverable. The code using QCborStreamReader may opt to handle the data that was properly decoded or it can opt to discard the entire data.

The only recoverable error is QCborError::EndOfFile, which indicates that more data is required in order to complete the parsing. This situation is useful when data is being read from an asynchronous source, such as a pipe (QProcess) or a socket (QTcpSocket, QUdpSocket, QNetworkReply, etc.). When more data arrives, the surrounding code needs to call either addData(), if parsing from a QByteArray, or reparse(), if it is instead reading directly a the QIDOevice that now has more data available (see setDevice()).

See also QCborStreamWriter, QCborValue, and QXmlStreamReader.

Member Type Documentation

enum QCborStreamReader::StringResultCode

This enum is returned by readString() and readByteArray() and is used to indicate what the status of the parsing is.

ConstantValueDescription
QCborStreamReader::EndOfString0The parsing for the string is complete, with no error.
QCborStreamReader::Ok1The function returned data; there was no error.
QCborStreamReader::Error-1Parsing failed with an error.

enum QCborStreamReader::Type

This enumeration contains all possible CBOR types as decoded by QCborStreamReader. CBOR has 7 major types, plus a number of simple types carrying no value, and floating point values.

ConstantValueDescription
QCborStreamReader::UnsignedInteger0x00(Major type 0) Ranges from 0 to 264 - 1 (18,446,744,073,709,551,616)
QCborStreamReader::NegativeInteger0x20(Major type 1) Ranges from -1 to -264 (-18,446,744,073,709,551,616)
QCborStreamReader::ByteArrayByteString(Major type 2) Arbitrary binary data.
QCborStreamReader::ByteString0x40An alias to ByteArray.
QCborStreamReader::StringTextString(Major type 3) Unicode text, possibly containing NULs.
QCborStreamReader::TextString0x60An alias to String
QCborStreamReader::Array0x80(Major type 4) Array of heterogeneous items.
QCborStreamReader::Map0xa0(Major type 5) Map/dictionary of heterogeneous items.
QCborStreamReader::Tag0xc0(Major type 6) Numbers giving further semantic value to generic CBOR items. See QCborTag for more information.
QCborStreamReader::SimpleType0xe0(Major type 7) Types carrying no further value. Includes booleans (true and false), null, undefined.
QCborStreamReader::Float16HalfFloatIEEE 754 half-precision floating point (qfloat16).
QCborStreamReader::HalfFloat0xf9An alias to Float16.
QCborStreamReader::Float0xfaIEEE 754 single-precision floating point (float).
QCborStreamReader::Double0xfbIEEE 754 double-precision floating point (double).
QCborStreamReader::Invalid0xffNot a valid type, either due to parsing error or due to reaching the end of an array or map.