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:
| Kind | Types | Behavior |
|---|---|---|
| Fixed-width | Integers, Tags, Simple types, Floating point | Value is pre-parsed by QCborStreamReader, so accessor functions are const. Must call next() to advance. |
| Strings | Byte arrays, Text strings | Length (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. |
| Containers | Arrays, Maps | Length (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.
| Feature | Support |
|---|---|
| Unsigned numbers | Yes (full range) |
| Negative numbers | Yes (full range) |
| Byte strings | Yes |
| Text strings | Yes |
| Chunked strings | Yes |
| Tags | Yes (arbitrary) |
| Booleans | Yes |
| Null | Yes |
| Undefined | Yes |
| Arbitrary simple values | Yes |
| Half-precision float (16-bit) | Yes |
| Single-precision float (32-bit) | Yes |
| Double-precision float (64-bit) | Yes |
| Infinities and NaN floating point | Yes |
| Determinate-length arrays and maps | Yes |
| Indeterminate-length arrays and maps | Yes |
| Map key types other than strings and integers | Yes (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.
| Constant | Value | Description |
|---|---|---|
QCborStreamReader::EndOfString | 0 | The parsing for the string is complete, with no error. |
QCborStreamReader::Ok | 1 | The function returned data; there was no error. |
QCborStreamReader::Error | -1 | Parsing 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.
| Constant | Value | Description |
|---|---|---|
QCborStreamReader::UnsignedInteger | 0x00 | (Major type 0) Ranges from 0 to 264 - 1 (18,446,744,073,709,551,616) |
QCborStreamReader::NegativeInteger | 0x20 | (Major type 1) Ranges from -1 to -264 (-18,446,744,073,709,551,616) |
QCborStreamReader::ByteArray | ByteString | (Major type 2) Arbitrary binary data. |
QCborStreamReader::ByteString | 0x40 | An alias to ByteArray. |
QCborStreamReader::String | TextString | (Major type 3) Unicode text, possibly containing NULs. |
QCborStreamReader::TextString | 0x60 | An alias to String |
QCborStreamReader::Array | 0x80 | (Major type 4) Array of heterogeneous items. |
QCborStreamReader::Map | 0xa0 | (Major type 5) Map/dictionary of heterogeneous items. |
QCborStreamReader::Tag | 0xc0 | (Major type 6) Numbers giving further semantic value to generic CBOR items. See QCborTag for more information. |
QCborStreamReader::SimpleType | 0xe0 | (Major type 7) Types carrying no further value. Includes booleans (true and false), null, undefined. |
QCborStreamReader::Float16 | HalfFloat | IEEE 754 half-precision floating point (qfloat16). |
QCborStreamReader::HalfFloat | 0xf9 | An alias to Float16. |
QCborStreamReader::Float | 0xfa | IEEE 754 single-precision floating point (float). |
QCborStreamReader::Double | 0xfb | IEEE 754 double-precision floating point (double). |
QCborStreamReader::Invalid | 0xff | Not a valid type, either due to parsing error or due to reaching the end of an array or map. |