QCborValue Class
The QCborValue class encapsulates a value in CBOR. More…
| Header | #include <QCborValue> |
|---|---|
| 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 | EncodingOption |
| typedef | EncodingOptions |
| enum | DiagnosticNotationOption |
| typedef | DiagnosticNotationOptions |
| enum | Type |
Public Functions
| QCborValue() | |
| QCborValue(Type t_) | |
| QCborValue(std::nullptr_t) | |
| QCborValue(bool b) | |
| QCborValue(int i) | |
| QCborValue(unsigned u) | |
| QCborValue(qint64 i) | |
| QCborValue(T i) | |
| QCborValue(double v) | |
| QCborValue(QCborSimpleType st) | |
| QCborValue(const QByteArray &ba) | |
| QCborValue(const QString &s) | |
| QCborValue(QStringView s) | |
| QCborValue(QLatin1StringView s) | |
| QCborValue(const char *s) | |
| QCborValue(const QCborArray &a) | |
| QCborValue(const QCborMap &m) | |
| QCborValue(QCborTag tag, const QCborValue &taggedValue = QCborValue()) | |
| QCborValue(QCborKnownTags tag, const QCborValue &taggedValue = QCborValue()) | |
| QCborValue(const QDateTime &dt) | |
| QCborValue(const QUrl &url) | |
| QCborValue(const QRegularExpression &rx) | |
| QCborValue(const QUuid &uuid) | |
| ~QCborValue() | |
| QCborValue(const QCborValue &other) | |
| QCborValue(QCborValue &&other) | |
| QCborValue & | operator=(const QCborValue &other) |
| QCborValue & | operator=(QCborValue &&other) |
| void | swap(QCborValue &other) |
| Type | type() const |
| bool | isInteger() const |
| bool | isByteArray() const |
| bool | isString() const |
| bool | isArray() const |
| bool | isMap() const |
| bool | isTag() const |
| bool | isFalse() const |
| bool | isTrue() const |
| bool | isBool() const |
| bool | isNull() const |
| bool | isUndefined() const |
| bool | isDouble() const |
| bool | isDateTime() const |
| bool | isUrl() const |
| bool | isRegularExpression() const |
| bool | isUuid() const |
| bool | isInvalid() const |
| bool | isContainer() const |
| bool | isSimpleType() const |
| bool | isSimpleType(QCborSimpleType st) const |
| QCborSimpleType | toSimpleType(QCborSimpleType defaultValue = QCborSimpleType::Undefined) const |
| qint64 | toInteger(qint64 defaultValue = 0) const |
| bool | toBool(bool defaultValue = false) const |
| double | toDouble(double defaultValue = 0) const |
| QCborTag | tag(QCborTag defaultValue = QCborTag(~0ULL)) const |
| QCborValue | taggedValue(const QCborValue &defaultValue = QCborValue()) const |
| QByteArray | toByteArray(const QByteArray &defaultValue = {}) const |
| QString | toString(const QString &defaultValue = {}) const |
| QDateTime | toDateTime(const QDateTime &defaultValue = {}) const |
| QUrl | toUrl(const QUrl &defaultValue = {}) const |
| QRegularExpression | toRegularExpression(const QRegularExpression &defaultValue = {}) const |
| QUuid | toUuid(const QUuid &defaultValue = {}) const |
| QCborArray | toArray() const |
| QCborArray | toArray(const QCborArray &defaultValue) const |
| QCborMap | toMap() const |
| QCborMap | toMap(const QCborMap &defaultValue) const |
| const QCborValue | operator[](const QString &key) const |
| const QCborValue | operator[](QLatin1StringView key) const |
| const QCborValue | operator[](qint64 key) const |
| QCborValueRef | operator[](qint64 key) |
| QCborValueRef | operator[](QLatin1StringView key) |
| QCborValueRef | operator[](const QString &key) |
| int | compare(const QCborValue &other) const |
| QVariant | toVariant() const |
| QJsonValue | toJsonValue() const |
| QByteArray | toCbor(EncodingOptions opt = NoTransformation) const |
| void | toCbor(QCborStreamWriter &writer, EncodingOptions opt = NoTransformation) const |
| QString | toDiagnosticNotation(DiagnosticNotationOptions opts = Compact) const |
Static Public Members
| QCborValue | fromVariant(const QVariant &variant) |
| QCborValue | fromJsonValue(const QJsonValue &v) |
| QCborValue | fromCbor(QCborStreamReader &reader) |
| QCborValue | fromCbor(const QByteArray &ba, QCborParserError *error = nullptr) |
| QCborValue | fromCbor(const char *data, qsizetype len, QCborParserError *error = nullptr) |
| QCborValue | fromCbor(const quint8 *data, qsizetype len, QCborParserError *error = nullptr) |
Detailed Description
A QCborValue holds one value of CBOR (RFC 8949): an integer, a floating-point number, a string, a byte array, true, false, null or undefined, an array (QCborArray), a map (QCborMap), or a tag and the value it applies to. Some tags are known and turned into Nt types: a date and time, a URL, a regular expression and a UUID.
QCborMap map;
map[u"name"_s] = u"Nt"_s;
map[u"sizes"_s] = QCborArray{1, 2.5, true};
const QByteArray cbor = QCborValue(map).toCbor();
QCborParserError error;
const QCborValue back = QCborValue::fromCbor(cbor, &error);
if (error.error == QCborError::NoError)
qDebug() << back[u"name"_s].toString() << back.toDiagnosticNotation();
Values convert to and from QVariant and QJsonValue, and show themselves in the diagnostic notation of CBOR with toDiagnosticNotation().
A QCborValue shares its string, byte array or container with its copies until one of them is changed. The operator[] that is not const returns a QCborValueRef, through which a value in a container is changed in place.
See also QCborArray, QCborMap, QCborStreamReader and QCborStreamWriter.
Differences from Qt
An unsigned integer above the largest qint64, and a negative one below the smallest, become a Double, as in Qt. A date and time is read only from the tag QCborKnownTags::DateTimeString, not from QCborKnownTags::UnixTime_t. An invalid value is written as undefined by toCbor(), so that the containers around it stay valid. A value that could not be read whole by fromCbor() is returned as far as it was read. There is no QDebug operator, and the functions that take a key take a const QString & and not a QAnyStringView.
Member Type Documentation
enum QCborValue::DiagnosticNotationOption
The options of toDiagnosticNotation().
| Constant | Description |
|---|---|
QCborValue::Compact | Everything on one line. |
QCborValue::LineWrapped | Each element of an array and each pair of a map on a line of its own, indented by its depth. |
QCborValue::ExtendedFormat | Show more of the encoding: the tag of an extended type, such as 0("...") for a date and time, and the size of a floating-point number. |
typedef QCborValue::DiagnosticNotationOptions
The DiagnosticNotationOptions type is a typedef for QFlags<DiagnosticNotationOption>. It stores an OR combination of DiagnosticNotationOption values.
enum QCborValue::EncodingOption
The options of toCbor(). They may be combined.
| Constant | Description |
|---|---|
QCborValue::NoTransformation | Write each value as it is. |
QCborValue::SortKeysInMaps | Write the pairs of every map ordered by their encoded keys, shorter first, as the canonical encoding of RFC 7049 does. Use it to get the same bytes for equal maps. |
QCborValue::UseFloat | Write a floating-point number in 32 bits when that loses nothing. |
QCborValue::UseFloat16 | Write a floating-point number in 16 bits, or else in 32, when that loses nothing. |
QCborValue::UseIntegers | Write a floating-point number that is a whole number as an integer. |
typedef QCborValue::EncodingOptions
The EncodingOptions type is a typedef for QFlags<EncodingOption>. It stores an OR combination of EncodingOption values.
enum QCborValue::Type
The types of value. The extended types are tags of CBOR that QCborValue knows: it converts their contents to the Nt type.
| Constant | Description |
|---|---|
QCborValue::Integer | An integer of 64 bits. |
QCborValue::ByteArray | A byte array. |
QCborValue::String | A string. |
QCborValue::Array | A QCborArray. |
QCborValue::Map | A QCborMap. |
QCborValue::Tag | A tag and the value it applies to. |
QCborValue::SimpleType | A simple type that has no meaning of its own. |
QCborValue::False | The value false. |
QCborValue::True | The value true. |
QCborValue::Null | The value null. |
QCborValue::Undefined | The value undefined, which is what a default-constructed QCborValue holds. |
QCborValue::Double | A floating-point number. |
QCborValue::DateTime | A QDateTime, as the tag QCborKnownTags::DateTimeString. |
QCborValue::Url | A QUrl, as the tag QCborKnownTags::Url. |
QCborValue::RegularExpression | A QRegularExpression, as the tag QCborKnownTags::RegularExpression. |
QCborValue::Uuid | A QUuid, as the tag QCborKnownTags::Uuid. |
QCborValue::Invalid | Not a value: what an operation that fails returns. |
Member Function Documentation
QCborValue::QCborValue()
Constructs an undefined value.
QCborValue::QCborValue(Type t_)
Constructs a value of the type t_ with the default value of that type: 0, an empty string or container, false, the simple type 0. For a tag or an extended type the tag is 0 and the tagged value is undefined.
QCborValue::QCborValue(std::nullptr_t)
Constructs a null value.
QCborValue::QCborValue(bool b)
Constructs true or false, according to b.
QCborValue::QCborValue(int i)
Constructs an integer from i.
QCborValue::QCborValue(unsigned u)
Constructs an integer from u.
QCborValue::QCborValue(qint64 i)
Constructs an integer from i.
template <typename T, std::enable_if_t<std::is_same_v<T, long long> && !std::is_same_v<long long, qint64>, bool> = true>QCborValue::QCborValue(T i)
This is an overloaded function.
Constructs an integer from i, where long long is another type than qint64.
QCborValue::QCborValue(double v)
Constructs a floating-point number from v.
QCborValue::QCborValue(QCborSimpleType st)
Constructs the simple type st. The four with a meaning give false, true, null and undefined.
QCborValue::QCborValue(const QByteArray &ba)
Constructs a byte array from ba.
QCborValue::QCborValue(const QString &s)
Constructs a string from s.
QCborValue::QCborValue(QStringView s)
This is an overloaded function.
Constructs a string from s.
QCborValue::QCborValue(QLatin1StringView s)
This is an overloaded function.
Constructs a string from the Latin-1 text s.
QCborValue::QCborValue(const char *s)
This is an overloaded function.
Constructs a string from the UTF-8 text s.
QCborValue::QCborValue(const QCborArray &a)
Constructs an array from a.
QCborValue::QCborValue(const QCborMap &m)
Constructs a map from m.
QCborValue::QCborValue(QCborTag tag, const QCborValue &taggedValue = QCborValue())
Constructs the tag tag, which applies to taggedValue. A known tag whose value is of the right kind gives one of the extended types: QCborKnownTags::DateTimeString with a string that is a date and time gives a DateTime.
See also tag() and taggedValue().
QCborValue::QCborValue(QCborKnownTags tag, const QCborValue &taggedValue = QCborValue())
This is an overloaded function.
Constructs the known tag tag, which applies to taggedValue.
QCborValue::QCborValue(const QDateTime &dt)
Constructs a date and time from dt, as the tag QCborKnownTags::DateTimeString with the date and time in ISO 8601 with milliseconds.
QCborValue::QCborValue(const QUrl &url)
Constructs a URL from url, as the tag QCborKnownTags::Url with the URL as text.
QCborValue::QCborValue(const QRegularExpression &rx)
Constructs a regular expression from rx, as the tag QCborKnownTags::RegularExpression with its pattern. The options of the expression are not kept.
QCborValue::QCborValue(const QUuid &uuid)
Constructs a UUID from uuid, as the tag QCborKnownTags::Uuid with its 16 bytes.
QCborValue::QCborValue(const QCborValue &other)
Constructs a copy of other. The copy shares the string or container of other until one of them is changed.
QCborValue::QCborValue(QCborValue &&other)
Move-constructs a value from other, which becomes undefined.
QCborValue::~QCborValue()
Destroys the value.
int QCborValue::compare(const QCborValue &other) const
Compares this value with other and returns a negative number if it comes first, 0 if they are equal and a positive number if it comes after. Values of different types are ordered by their type(); integers and floating-point numbers by their value, with NaN after all others; strings, byte arrays, arrays and maps first by their length and then element by element; tags by their number and then by what they apply to.
QCborValue QCborValue::fromCbor(QCborStreamReader &reader)
Reads the item that reader is at, with everything in it, and returns it. The reader is left at the next item. If the data is not valid, the value is what could be read, and the reader's lastError() says what went wrong.
See also toCbor().
QCborValue QCborValue::fromCbor(const QByteArray &ba, QCborParserError *error = nullptr)
This is an overloaded function.
Reads the CBOR in ba, which must hold one item and nothing after it. If error is not nullptr, it is set to the error and the offset where it was found, or to QCborError::NoError.
QCborValue QCborValue::fromCbor(const char *data, qsizetype len, QCborParserError *error = nullptr)
This is an overloaded function.
Reads the CBOR in the len bytes at data. If error is not nullptr, it is set to the error that was found.
QCborValue QCborValue::fromCbor(const quint8 *data, qsizetype len, QCborParserError *error = nullptr)
This is an overloaded function.
Reads the CBOR in the len bytes at data. If error is not nullptr, it is set to the error that was found.
QCborValue QCborValue::fromJsonValue(const QJsonValue &v)
Returns the JSON value v as a QCborValue. A number that is a whole number of no more than 53 bits becomes an Integer, and any other a Double.
See also toJsonValue().
QCborValue QCborValue::fromVariant(const QVariant &variant)
Returns the value of variant as a QCborValue: numbers, bool, strings, byte arrays, the extended types, lists, maps, JSON values and nullptr are converted; a null variant gives undefined. Any other type gives its conversion to a string if it has one, and undefined otherwise.
See also toVariant().
bool QCborValue::isArray() const
Returns true if the value is an array.
bool QCborValue::isBool() const
Returns true if the value is false or true.
bool QCborValue::isByteArray() const
Returns true if the value is a byte array.
bool QCborValue::isContainer() const
Returns true if the value is an array or a map.
bool QCborValue::isDateTime() const
Returns true if the value is a date and time.
bool QCborValue::isDouble() const
Returns true if the value is a floating-point number.
bool QCborValue::isFalse() const
Returns true if the value is false.
bool QCborValue::isInteger() const
Returns true if the value is an integer.
bool QCborValue::isInvalid() const
Returns true if the value is invalid.
bool QCborValue::isMap() const
Returns true if the value is a map.
bool QCborValue::isNull() const
Returns true if the value is null.
bool QCborValue::isRegularExpression() const
Returns true if the value is a regular expression.
bool QCborValue::isSimpleType() const
Returns true if the value is a simple type, among them false, true, null and undefined.
bool QCborValue::isSimpleType(QCborSimpleType st) const
This is an overloaded function.
Returns true if the value is the simple type st.
bool QCborValue::isString() const
Returns true if the value is a string.
bool QCborValue::isTag() const
Returns true if the value is a tag, also one of the extended types, which are tags too.
See also tag() and taggedValue().
bool QCborValue::isTrue() const
Returns true if the value is true.
bool QCborValue::isUndefined() const
Returns true if the value is undefined.
bool QCborValue::isUrl() const
Returns true if the value is a URL.
bool QCborValue::isUuid() const
Returns true if the value is a UUID.
QCborValue & QCborValue::operator=(const QCborValue &other)
Assigns other to this value and returns a reference to it.
QCborValue & QCborValue::operator=(QCborValue &&other)
Move-assigns other to this value and returns a reference to it.
const QCborValue QCborValue::operator[](const QString &key) const
Returns the value of key in the map, or undefined if the value is not a map or has no such key.
const QCborValue QCborValue::operator[](QLatin1StringView key) const
This is an overloaded function.
Returns the value of key in the map, or undefined.
const QCborValue QCborValue::operator[](qint64 key) const
This is an overloaded function.
Returns the element at key of an array, or the value of the integer key in a map, or undefined if there is none.
QCborValueRef QCborValue::operator[](qint64 key)
Returns a reference to the element at key. An array that is shorter grows, with undefined elements, unless key is far beyond its end, in which case it becomes a map with the indexes as keys; a map gets the key with an undefined value if it has none. A value that is neither becomes an empty array first.
QCborValueRef QCborValue::operator[](QLatin1StringView key)
This is an overloaded function.
Returns a reference to the value of key in the map, which gets the key with an undefined value if it has none. An array becomes a map with its indexes as keys first, and any other value an empty map.
QCborValueRef QCborValue::operator[](const QString &key)
This is an overloaded function.
Returns a reference to the value of key in the map.
void QCborValue::swap(QCborValue &other)
Exchanges this value with other.
QCborTag QCborValue::tag(QCborTag defaultValue = QCborTag(~0ULL)) const
Returns the tag of the value, also of an extended type, or defaultValue if it is not a tag.
See also taggedValue() and isTag().
QCborValue QCborValue::taggedValue(const QCborValue &defaultValue = QCborValue()) const
Returns the value that the tag applies to, or defaultValue if this is not a tag. For an extended type this is the string or byte array of the encoding.
See also tag().
QCborArray QCborValue::toArray() const
Returns the array of the value, or an empty array if it is not an array.
QCborArray QCborValue::toArray(const QCborArray &defaultValue) const
This is an overloaded function.
Returns the array of the value, or defaultValue if it is not an array.
bool QCborValue::toBool(bool defaultValue = false) const
Returns true or false, or defaultValue if the value is not a boolean.
QByteArray QCborValue::toByteArray(const QByteArray &defaultValue = {}) const
Returns the byte array of the value, or defaultValue if it is not a byte array.
QByteArray QCborValue::toCbor(EncodingOptions opt = NoTransformation) const
Returns the value encoded in CBOR, with the options opt.
See also fromCbor().
void QCborValue::toCbor(QCborStreamWriter &writer, EncodingOptions opt = NoTransformation) const
This is an overloaded function.
Writes the value to writer, with the options opt.
QDateTime QCborValue::toDateTime(const QDateTime &defaultValue = {}) const
Returns the date and time of the value, or defaultValue if it is not a date and time.
QString QCborValue::toDiagnosticNotation(DiagnosticNotationOptions opts = Compact) const
Returns the value in the diagnostic notation of CBOR (RFC 8949 section 8), a text form for people to read, with the options opts:
QCborValue(QCborArray{1, u"two"_s, QByteArray("\x03")}).toDiagnosticNotation(); // [1, "two", h'03']
Floating-point numbers that are whole numbers end in .0, tags show their number before the value in parentheses, and simple types with no meaning appear as simple(n).
See also toCbor().
double QCborValue::toDouble(double defaultValue = 0) const
Returns the floating-point number of the value, or the integer converted to one, or defaultValue if the value is no number.
qint64 QCborValue::toInteger(qint64 defaultValue = 0) const
Returns the integer of the value, or defaultValue if it is not an integer. A floating-point number is not converted.
See also toDouble().
QJsonValue QCborValue::toJsonValue() const
Returns the value as a QJsonValue. Numbers, strings, bool, null, arrays and maps convert to their JSON kind; a floating-point number that is not finite, undefined and an invalid value become null. A byte array becomes text in Base64url, or in Base64 or hexadecimal if it is in a tag that asks for that. The extended types become their text: a date and time in ISO 8601, a URL, the pattern of a regular expression, and a UUID without braces. Another tag becomes what it applies to, and a simple type with no meaning the text simple(n). The keys of a map that are not strings become their text.
See also fromJsonValue().
QCborMap QCborValue::toMap() const
Returns the map of the value, or an empty map if it is not a map.
QCborMap QCborValue::toMap(const QCborMap &defaultValue) const
This is an overloaded function.
Returns the map of the value, or defaultValue if it is not a map.
QRegularExpression QCborValue::toRegularExpression(const QRegularExpression &defaultValue = {}) const
Returns the regular expression of the value, or defaultValue if it is not one.
QCborSimpleType QCborValue::toSimpleType(QCborSimpleType defaultValue = QCborSimpleType::Undefined) const
Returns the simple type of the value, or defaultValue if it is not a simple type.
QString QCborValue::toString(const QString &defaultValue = {}) const
Returns the string of the value, or defaultValue if it is not a string.
QUrl QCborValue::toUrl(const QUrl &defaultValue = {}) const
Returns the URL of the value, or defaultValue if it is not a URL.
QUuid QCborValue::toUuid(const QUuid &defaultValue = {}) const
Returns the UUID of the value, or defaultValue if it is not a UUID.
QVariant QCborValue::toVariant() const
Returns the value as a QVariant: an integer as qint64, a floating-point number as double, strings and byte arrays as such, the extended types as QDateTime, QUrl, QRegularExpression and QUuid, an array as QVariantList, a map as QVariantMap (with keys that are not strings converted to text, as toJsonValue() does), null as nullptr and undefined as a null QVariant. A tag that is not known, and a simple type that has no meaning, are returned as the QCborValue itself and as a QCborSimpleType.
See also fromVariant().
Type QCborValue::type() const
Returns the type of the value.