QCborStreamReader Class
The QCborStreamReader class is a simple CBOR stream decoder, operating on either a QByteArray or a QIODevice. More…
| Header | #include <QCborStreamReader> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
| Since | 5.12 |
List of all members, including inherited members
Public Types
| enum | Type |
| enum | StringResultCode |
Public Functions
| QCborStreamReader() | |
| QCborStreamReader(const char *data, qsizetype len) | |
| QCborStreamReader(const quint8 *data, qsizetype len) | |
| QCborStreamReader(const QByteArray &data) | |
| QCborStreamReader(QIODevice *device) | |
| ~QCborStreamReader() | |
| void | setDevice(QIODevice *device) |
| QIODevice * | device() const |
| void | addData(const QByteArray &data) |
| void | addData(const char *data, qsizetype len) |
| void | addData(const quint8 *data, qsizetype len) |
| void | reparse() |
| void | clear() |
| void | reset() |
| QCborError | lastError() const |
| qint64 | currentOffset() const |
| bool | isValid() const |
| int | containerDepth() const |
| Type | parentContainerType() const |
| bool | hasNext() const |
| bool | next(int maxRecursion = 10000) |
| Type | type() const |
| bool | isUnsignedInteger() const |
| bool | isNegativeInteger() const |
| bool | isInteger() const |
| bool | isByteArray() const |
| bool | isString() const |
| bool | isArray() const |
| bool | isMap() const |
| bool | isTag() const |
| bool | isSimpleType() const |
| bool | isSimpleType(QCborSimpleType st) const |
| bool | isFloat16() const |
| bool | isFloat() const |
| bool | isDouble() const |
| bool | isFalse() const |
| bool | isTrue() const |
| bool | isBool() const |
| bool | isNull() const |
| bool | isUndefined() const |
| bool | isContainer() const |
| bool | isLengthKnown() const |
| quint64 | length() const |
| bool | enterContainer() |
| bool | leaveContainer() |
| StringResult<QString> | readString() |
| StringResult<QByteArray> | readUtf8String() |
| StringResult<QByteArray> | readByteArray() |
| QString | readAllString() |
| QByteArray | readAllUtf8String() |
| QByteArray | readAllByteArray() |
| bool | toBool() const |
| QCborTag | toTag() const |
| quint64 | toUnsignedInteger() const |
| QCborNegativeInteger | toNegativeInteger() const |
| QCborSimpleType | toSimpleType() const |
| qfloat16 | toFloat16() const |
| float | toFloat() const |
| double | toDouble() const |
| qint64 | toInteger() const |
Detailed Description
The reader reads CBOR (RFC 8949) item by item. type() says what the current item is, the to functions return its value, and next() moves on to the next item. An array or map is entered with enterContainer(); then its items are read until hasNext() is false, and leaveContainer() moves on to the item after the container. In a map the items alternate between keys and values. Strings are read with readString() and readByteArray(), which return them in chunks, or whole with readAllString() and readAllByteArray().
QCborStreamReader reader(data);
if (reader.isMap() && reader.enterContainer()) {
while (reader.hasNext()) {
const QString key = reader.readAllString();
if (key == u"name" && reader.isString())
name = reader.readAllString();
else
reader.next(); // skips the value, whatever it is
}
reader.leaveContainer();
}
if (reader.lastError() != QCborError::NoError)
qWarning() << reader.lastError().toString();
Data can arrive in pieces: when the reader runs out of data in the middle of an item, it reports QCborError::EndOfFile. Add the next piece with addData() and call reparse(), and the reader goes on from where it was.
See also QCborStreamWriter and QCborValue.
Differences from Qt
The reader keeps all the data it has read from a device, so that it can go back to an item that was cut short. There is no currentStringChunkSize() or readStringChunk(), and no readAndAppendToString() family. A tag counts as an item of its own, as in Qt: next() moves from the tag to the item that it applies to.
Member Type Documentation
enum QCborStreamReader::StringResultCode
The status of a chunk of a string.
| Constant | Description |
|---|---|
QCborStreamReader::EndOfString | The string has ended: there was no more data, and the reader has moved on to the next item. |
QCborStreamReader::Ok | A chunk was read. |
QCborStreamReader::Error | The chunk could not be read; see lastError(). |
enum QCborStreamReader::Type
The types of item that the reader can be at. The values are those of the first byte of the item in CBOR.
| Constant | Description |
|---|---|
QCborStreamReader::UnsignedInteger | An integer from 0 to 2^64-1. |
QCborStreamReader::NegativeInteger | An integer from -1 to -2^64. |
QCborStreamReader::ByteString | A string of bytes. |
QCborStreamReader::ByteArray | The same as ByteString. |
QCborStreamReader::TextString | A string of text in UTF-8. |
QCborStreamReader::String | The same as TextString. |
QCborStreamReader::Array | An array. |
QCborStreamReader::Map | A map of keys to values. |
QCborStreamReader::Tag | A tag, which says how the item that follows is to be understood. |
QCborStreamReader::SimpleType | A simple type, among them false, true, null and undefined. |
QCborStreamReader::HalfFloat | A floating-point number of 16 bits. |
QCborStreamReader::Float16 | The same as HalfFloat. |
QCborStreamReader::Float | A floating-point number of 32 bits. |
QCborStreamReader::Double | A floating-point number of 64 bits. |
QCborStreamReader::Invalid | No item: the end of a container, the end of the data, or an error. |
Member Function Documentation
QCborStreamReader::QCborStreamReader()
Constructs a reader without data. Give it data with addData() or a device with setDevice().
QCborStreamReader::QCborStreamReader(const char *data, qsizetype len)
Constructs a reader that reads the len bytes at data, which it copies.
QCborStreamReader::QCborStreamReader(const quint8 *data, qsizetype len)
This is an overloaded function.
Constructs a reader that reads the len bytes at data, which it copies.
QCborStreamReader::QCborStreamReader(const QByteArray &data)
Constructs a reader that reads data.
QCborStreamReader::QCborStreamReader(QIODevice *device)
Constructs a reader that reads from device, which must be open for reading.
QCborStreamReader::~QCborStreamReader()
Destroys the reader. The device is not closed.
void QCborStreamReader::addData(const QByteArray &data)
Adds data to the end of the data that the reader reads. Call reparse() afterwards if the reader stopped at the end of the data before.
See also reparse().
void QCborStreamReader::addData(const char *data, qsizetype len)
This is an overloaded function.
Adds the len bytes at data.
void QCborStreamReader::addData(const quint8 *data, qsizetype len)
This is an overloaded function.
Adds the len bytes at data.
void QCborStreamReader::clear()
Forgets the data, the device and the place, and puts the reader back into the state of a new one.
See also reset().
int QCborStreamReader::containerDepth() const
Returns the number of containers that the reader has entered and not left.
See also enterContainer().
qint64 QCborStreamReader::currentOffset() const
Returns the offset in bytes at which the current item starts.
QIODevice * QCborStreamReader::device() const
Returns the device that the reader reads from, or nullptr if it reads data that was given to it.
See also setDevice().
bool QCborStreamReader::enterContainer()
Enters the current array or map, so that its items can be read. Returns false if the current item is not a container.
See also leaveContainer() and next().
bool QCborStreamReader::hasNext() const
Returns true if there is an item to read: in a container, until its end; at the top level, until the end of the data.
See also next() and leaveContainer().
bool QCborStreamReader::isArray() const
Returns true if the current item is an array.
bool QCborStreamReader::isBool() const
Returns true if the current item is false or true.
bool QCborStreamReader::isByteArray() const
Returns true if the current item is a byte string.
bool QCborStreamReader::isContainer() const
Returns true if the current item is an array or a map.
See also enterContainer().
bool QCborStreamReader::isDouble() const
Returns true if the current item is a floating-point number of 64 bits.
bool QCborStreamReader::isFalse() const
Returns true if the current item is the value false.
bool QCborStreamReader::isFloat() const
Returns true if the current item is a floating-point number of 32 bits.
bool QCborStreamReader::isFloat16() const
Returns true if the current item is a floating-point number of 16 bits.
bool QCborStreamReader::isInteger() const
Returns true if the current item is an integer, unsigned or negative.
bool QCborStreamReader::isLengthKnown() const
Returns true if the length of the current string, array or map is known before it is read. Strings, arrays and maps of unknown length end with a break.
See also length().
bool QCborStreamReader::isMap() const
Returns true if the current item is a map.
bool QCborStreamReader::isNegativeInteger() const
Returns true if the current item is a negative integer.
bool QCborStreamReader::isNull() const
Returns true if the current item is null.
bool QCborStreamReader::isSimpleType() const
Returns true if the current item is a simple type, among them false, true, null and undefined.
bool QCborStreamReader::isSimpleType(QCborSimpleType st) const
This is an overloaded function.
Returns true if the current item is the simple type st.
bool QCborStreamReader::isString() const
Returns true if the current item is a text string.
bool QCborStreamReader::isTag() const
Returns true if the current item is a tag.
bool QCborStreamReader::isTrue() const
Returns true if the current item is the value true.
bool QCborStreamReader::isUndefined() const
Returns true if the current item is undefined.
bool QCborStreamReader::isUnsignedInteger() const
Returns true if the current item is an unsigned integer.
bool QCborStreamReader::isValid() const
Returns true if the reader is at an item: if type() is not Invalid.
See also hasNext().
QCborError QCborStreamReader::lastError() const
Returns the error that stopped the reader, or QCborError::NoError.
bool QCborStreamReader::leaveContainer()
Leaves the container after its last item has been read, and moves to the item after it. Returns false if the reader is in no container, has not read all its items, or the data is not valid.
See also enterContainer() and hasNext().
quint64 QCborStreamReader::length() const
Returns the length of the current string in bytes, of the current array in elements or of the current map in pairs. It is 0 if the length is not known.
See also isLengthKnown().
bool QCborStreamReader::next(int maxRecursion = 10000)
Moves to the next item, skipping the current one with everything in it: the elements of an array or map, and the chunks of a string. Containers may be nested maxRecursion levels deep. Returns false if there is no item to skip or the data is not valid.
See also enterContainer().
Type QCborStreamReader::parentContainerType() const
Returns the type of the container that the reader is in, or Invalid at the top level.
QByteArray QCborStreamReader::readAllByteArray()
Reads the whole current byte string and moves to the next item. Returns a null byte array if the current item is not a byte string or there is an error.
This function was introduced in 6.7.
QString QCborStreamReader::readAllString()
Reads the whole current text string and moves to the next item. Returns a null string if the current item is not a text string or there is an error.
This function was introduced in 6.7.
See also readString().
QByteArray QCborStreamReader::readAllUtf8String()
Reads the whole current text string in UTF-8 and moves to the next item. Returns a null byte array if the current item is not a text string or there is an error.
This function was introduced in 6.7.
StringResult<QByteArray> QCborStreamReader::readByteArray()
Reads the next chunk of the current byte string. Call it until the status is EndOfString, after which the reader is at the next item.
See also readAllByteArray().
StringResult<QString> QCborStreamReader::readString()
Reads the next chunk of the current text string. Call it until the status is EndOfString, after which the reader is at the next item; a string of known length comes in one chunk.
See also readAllString() and readByteArray().
StringResult<QByteArray> QCborStreamReader::readUtf8String()
Reads the next chunk of the current text string, in UTF-8.
See also readString().
void QCborStreamReader::reparse()
Reads the current item again, after more data has been added. Call it when lastError() is QCborError::EndOfFile, or the reader has reached the end of the data at the top level, and addData() has given the reader more.
See also addData().
void QCborStreamReader::reset()
Goes back to the start of the data, to read it again.
See also clear().
void QCborStreamReader::setDevice(QIODevice *device)
Makes the reader read from device, from its current position, and forget the data it was reading.
See also device() and clear().
bool QCborStreamReader::toBool() const
Returns the value of the current true or false item.
double QCborStreamReader::toDouble() const
Returns the current floating-point number of 64 bits.
float QCborStreamReader::toFloat() const
Returns the current floating-point number of 32 bits.
qfloat16 QCborStreamReader::toFloat16() const
Returns the current floating-point number of 16 bits.
qint64 QCborStreamReader::toInteger() const
Returns the current integer, unsigned or negative, as a qint64. A number that does not fit wraps around.
QCborNegativeInteger QCborStreamReader::toNegativeInteger() const
Returns the absolute value of the current negative integer: 1 for -1.
QCborSimpleType QCborStreamReader::toSimpleType() const
Returns the current simple type, among them false, true, null and undefined.
QCborTag QCborStreamReader::toTag() const
Returns the number of the current tag.
quint64 QCborStreamReader::toUnsignedInteger() const
Returns the value of the current unsigned integer.
Type QCborStreamReader::type() const
Returns the type of the current item.