在 Qt 中支持 CBOR

Qt provides support for dealing with CBOR data. CBOR is a binary format to store data that has a superset of the types available in JSON, but is more compact.

The CBOR support in Qt provides an easy to use C++ API to parse, modify and save CBOR data.

More details about the CBOR data format can be found in RFC 7049 .

概述

CBOR is a format to store structured data. It has three groups of built-in types:

  • Basic types: integers, floating point, boolean, null, etc.
  • String-like types: strings and byte arrays
  • Containers: arrays and maps

In addition, CBOR can add a "tag" to extend the meaning of the type. The container types can contain basic types, string-like types and containers.

The CBOR Classes

The QCborValue Class

The QCborValue class represents any CBOR type. It also has a simple API for reading and writing to QCborStreamReader and QCborStreamWriter objects, as well as manipulating such objects in memory, with the help of QCborArray and QCborMap . The CborValue API is simplified from the full CBOR data type and always represents all integers as qint64 and all floating-point as double 。这意味着 QCborValue is unable to represent CBOR integer values outside of the range of qint64 (-2^63 to 2^63-1). When creating a CBOR stream, QCborValue::toCbor () can be configured to attempt to write the shorter single- and half-precision floating-point representations.

The QCborArray Class

The QCborArray class is used to hold an array of QCborValue objects. A QCborValue object can contain a QCborArray object. It has functions for converting to and from QVariantList, QStringList , QJsonArray .

The QCborMap Class

The QCborMap class is used to hold an map of QCborValue objects. A QCborValue object can contain a QCborMap object. It has functions for converting to and from QVariantMap, QVariantMap, and QJsonObject , but it can have keys of any type, not just QString .

The QCborStreamReader Class

The QCborStreamReader class is a low level API for reading CBOR data from a QIODevice QByteArray , or a pointer to memory. It has an API similar to the QXmlStreamReader 类。

The QCborStreamWriter Class

The QCborStreamWriter class is a low level API for writing CBOR data to a QIODevice QByteArray . It has an API similar to the QXmlStreamWriter 类。

另请参阅 Parsing and displaying CBOR data , Convert Example ,和 JSON 保存游戏范例 .

QCborArray 用于保持 CBOR (简明二进制对象表示) 元素的数组
QCborArray::ConstIterator QCborArray::ConstIterator class provides an STL-style const iterator for QCborArray
QCborArray::Iterator QCborArray::Iterator class provides an STL-style non-const iterator for QCborArray
QCborMap 用于保持以 CBOR 表示的关联容器
QCborMap::ConstIterator QCborMap::ConstIterator 类为 QCborMap 提供 STL 样式的常量迭代器
QCborMap::Iterator QCborMap::Iterator 类为 QCborMap 提供 STL 样式的非常量迭代器
QCborParserError 用于 QCborValue 以报告剖析错误
QCborStreamReader 操作 QByteArray 或 QIODevice 的简单 CBOR 流解码器
QCborStreamWriter 操作单向流的简单 CBOR 编码器
QCborValue 封装值在 CBOR 中