QCborValue¶
The
QCborValueclass encapsulates a value in CBOR. More…

New in version 5.12.
Synopsis¶
Functions¶
def
__eq__(other)def
__lt__(other)def
__ne__(other)def
compare(other)def
isArray()def
isBool()def
isByteArray()def
isContainer()def
isDateTime()def
isDouble()def
isFalse()def
isInteger()def
isInvalid()def
isMap()def
isNull()def
isRegularExpression()def
isSimpleType()def
isSimpleType(st)def
isString()def
isTag()def
isTrue()def
isUndefined()def
isUrl()def
isUuid()def
operator[](key)def
operator[](key)def
swap(other)def
taggedValue([defaultValue=QCborValue()])def
toArray()def
toArray(defaultValue)def
toBool([defaultValue=false])def
toByteArray([defaultValue={}])def
toCbor([opt=QCborValue.NoTransformation])def
toCbor(writer[, opt=QCborValue.NoTransformation])def
toDateTime([defaultValue={}])def
toDiagnosticNotation([opts=QCborValue.Compact])def
toDouble([defaultValue=0])def
toInteger([defaultValue=0])def
toJsonValue()def
toMap()def
toMap(defaultValue)def
toRegularExpression([defaultValue={}])def
toSimpleType([defaultValue=QCborSimpleType.Undefined])def
toString([defaultValue={}])def
toUrl([defaultValue={}])def
toUuid([defaultValue={}])def
toVariant()def
type()
Static functions¶
def
fromCbor(ba[, error=None])def
fromCbor(data, len[, error=None])def
fromCbor(data, len[, error=None])def
fromCbor(reader)def
fromJsonValue(v)def
fromVariant(variant)
Detailed Description¶
This class can be used to hold one of the many types available in CBOR. CBOR is the Concise Binary Object Representation, a very compact form of binary data encoding that is a superset of 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.
CBOR has three groups of built-in types:
Basic types: integers, floating point (double), boolean, null, etc.
String-like types: strings and byte arrays
Containers: arrays and maps
Additionally, CBOR supports a form of type extensibility by associating a “tag” to one of the above types to convey more information. For example, a UUID is represented by a tag and a byte array containing the 16 bytes of the UUID content.
QCborValuesupports creating and decoding several of those extended types directly with Qt classes (likeQUuid).For the complete list, see
Type. The type of aQCborValuecan be queried usingtype()or one of the “isXxxx” functions.
Extended types and tagged values¶
A tagged value is a normal
QCborValuethat is paired with a number that is its tag. SeeQCborKnownTagsfor more information on what tags are in the API as well as the full, official list. Such combinations form extended types.
QCborValuehas support for certain extended types in the API, like URL (withQUrl) and UUID (withQUuid). Other extended types not supported in the API are represented by aQCborValueofTagtype. The tag can later be retrieved bytag()and the tagged value usingtaggedValue().In order to support future compatibility, QCborValues containing extended Qt types compare equal to the tag type of the same contents. In other words, the following expression is true:
QCborValue(uuid) == QCborValue(QCborKnownTags::Uuid, uuid.toRfc4122());
Undefined and null values¶
QCborValuecan contain a value of “null”, which is not of any specific type. It resembles the C++std::nullptr_ttype, whose only possible value isnullptr.QCborValuehas a constructor taking such a type and creates a nullQCborValue.Null values are used to indicate that an optional value is not present. In that aspect, it is similar to the C++ Standard Library type
std::optionalwhen that is disengaged. Unlike the C++ type, CBOR nulls are simply of type “Null” and it is not possible to determine what concrete type it is replacing.
QCborValuecan also be of the undefined type, which represents a value of “undefined”. In fact, that is what theQCborValuedefault constructor creates.Undefined values are different from null values. While nulls are used to indicate an optional value that is not provided, Undefined is usually used to indicate that an expected value could not be provided, usually due to an error or a precondition that could not be satisfied.
Such values are completely valid and may appear in CBOR streams, unlike JSON content and
QJsonValue‘s undefined bit. But likeQJsonValue‘s Undefined, it is returned by QCborArray::value() when out of range or QCborMap::operator[] when the key is not found in the container. It is not possible to tell such a case apart from the value of Undefined, so if that is required, check theQCborArraysize and use theQCborMapiterator API.
Simple types¶
CBOR supports additional simple types that, like Null and Undefined, carry no other value. They are called interchangeably “Simple Types” and “Simple Values”. CBOR encodes booleans as two distinct types (one for
trueand one forfalse), butQCborValuehas a convenience API for them.There are currently no other defined CBOR simple types.
QCborValuesupports them simply by their number with API likeisSimpleType()andtoSimpleType(), available for compatibility with future specifications before the Qt API can be updated. Their use before such a specification is discouraged, as other CBOR implementations may not support them fully.
CBOR support¶
QCborValuesupports all CBOR features required to create canonical and strict streams. It implements almost all of the features specified in RFC 7049.The following table lists the CBOR features that
QCborValuesupports.
Feature
Support
Unsigned numbers
Yes (
qint64range)Negative numbers
Yes (
qint64range)Byte strings
Yes
Text strings
Yes
Chunked strings
See below
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)
Integers in
QCborValueare limited to the range of theqint64type. That is, from -9,223,372,036,854,775,808 (-2 63 ) to 9,223,372,036,854,775,807 (2 63 - 1). CBOR itself can represent integer values outside of this range, whichQCborValuedoes not support. When decoding a stream usingfromCbor()containing one of those values,QCborValuewill convert automatically toDouble, but that may lose up to 11 bits of precision.
fromCbor()is able to decode chunked strings, but will always merge the chunks together into a singleQCborValue. For that reason, it always writes non-chunked strings when usingtoCbor()(which is required by the Canonical format anyway).
QCborValuewill always convert half- and single-precision floating point values in the CBOR stream to double-precision. ThetoCbor()function can take a parameter indicating to recreate them.
QCborValueRef¶
QCborValueRefis a helper class forQCborArrayandQCborMap. It is the type you get when using one of the mutating APIs in those classes. UnlikeQCborValue, new values can be assigned to that class. When that is done, the array or map it refers to will be modified with the new value. In all other aspects, its API is identical toQCborValue.
-
class
QCborValue¶ QCborValue(a)
QCborValue(t_[, tv=QCborValue()])
QCborValue(m)
QCborValue(st)
QCborValue(t_)
QCborValue(b_)
QCborValue(ba)
QCborValue(a)
QCborValue(m)
QCborValue(other)
QCborValue(dt)
QCborValue(rx)
QCborValue(s)
QCborValue(url)
QCborValue(uuid)
QCborValue(s)
QCborValue(v)
QCborValue(i)
QCborValue(i)
QCborValue(u)
Creates a
QCborValueof theUndefinedtype.CBOR undefined values are used to indicate missing information, usually as a result of a previous operation that did not complete as expected. They are also used by the
QCborArrayandQCborMapAPI to indicate the searched item was not found.Undefined values are represented by the
Undefined simple type. Because of that, QCborValues with undefined values will also return true forisSimpleType()andisSimpleType(QCborSimpleType::Undefined).Undefined values are different from null values.
QCborValueobjects with undefined values are also different from invalidQCborValueobjects. The API will not create invalid QCborValues, but they may exist as a result of a parsing error.See also
Creates a
QCborValueof simple typest. The type can later later be retrieved usingtoSimpleType()as well asisSimpleType(st).CBOR simple types are types that do not have any associated value, like C++’s
std::nullptr_ttype, whose only possible value isnullptr.If
stisQCborSimpleType::Null, the resultingQCborValuewill be of theNulltype and similarly forQCborSimpleType::Undefined. IfstisQCborSimpleType::FalseorQCborSimpleType::True, the createdQCborValuewill be a boolean containing a value of false or true, respectively.This function can be used with simple types not defined in the API. For example, to create a
QCborValuewith simple type 12, one could write:QCborValue value(QCborSimpleType(12));
Simple types should not be used until a specification for them has been published, since other implementations may not support them properly. Simple type values 24 to 31 are reserved and must not be used.
isSimpleType(),isNull(),isUndefined(),isTrue(),isFalse()Creates a
QCborValueof typet_. The value associated with such a type (if any) will be default constructed.See also
Creates a
QCborValuewith boolean valueb. The value can later be retrieved usingtoBool().Internally, CBOR booleans are represented by a pair of types, one for true and one for false. For that reason, boolean QCborValues will return true for
isSimpleType()and one ofisSimpleType(QCborSimpleType::False)orisSimpleType(QCborSimpleType::True).See also
Creates a
QCborValuewith floating point valued. The value can later be retrieved usingtoDouble().CBOR floating point values are distinct from integer values. Therefore,
QCborValueobjects with integers will compare differently toQCborValueobjects containing floating-point, even if the values contained in the objects are equivalent.See also
Creates a
QCborValuewith integer valuei. The value can later be retrieved usingtoInteger().CBOR integer values are distinct from floating point values. Therefore,
QCborValueobjects with integers will compare differently toQCborValueobjects containing floating-point, even if the values contained in the objects are equivalent.See also
-
PySide2.QtCore.QCborValue.EncodingOption¶ This enum is used in the options argument to
toCbor(), modifying the behavior of the encoder.Constant
Description
QCborValue.NoTransformation
(Default) Performs no transformations.
QCborValue.UseFloat
Tells the encoder to use IEEE 754 single-precision floating point (that is,
float) whenever possible.QCborValue.UseFloat16
Tells the encoder to use IEEE 754 half-precision floating point (that is,
qfloat16), whenever possible. ImpliesUseFloat.QCborValue.UseIntegers
Tells the encoder to use integers whenever a value of type
Doublecontains an integer.The use of
UseFloat16is required to encode the stream in Canonical Format, but is not otherwise necessary.See also
-
PySide2.QtCore.QCborValue.DiagnosticNotationOption¶ This enum is used in the option argument to
toDiagnosticNotation(), to modify the output format.Constant
Description
QCborValue.Compact
Does not use any line-breaks, producing a compact representation.
QCborValue.LineWrapped
Uses line-breaks, one
QCborValueper line.QCborValue.ExtendedFormat
Uses some different options to represent values, not found in RFC 7049. Those options are subject to change.
Currently,
ExtendedFormatwill change how byte arrays are represented. Without it, they are always hex-encoded and without spaces. With it,toCbor()will either use hex with spaces, base64 or base64url encoding, depending on the context.See also
-
PySide2.QtCore.QCborValue.Type¶ This enum represents the
QCborValuetype. It is returned by thetype()function.The CBOR built-in types are:
Constant
Description
QCborValue.Integer
qint64: An integer valueQCborValue.ByteArray
QByteArray: a byte array (“byte string”)QCborValue.String
QString: a Unicode string (“text string”)QCborValue.Array
QCborArray: an array of QCborValuesQCborValue.Map
QCborMap: an associative container of QCborValuesQCborValue.SimpleType
QCborSimpleType: one of several simple types/valuesQCborValue.False
bool: the simple type for valuefalseQCborValue.True
bool: the simple type for valuetrueQCborValue.Null
std::nullptr_t: the simple type for the null valueQCborValue.Undefined
(no type) the simple type for the undefined value
QCborValue.Double
double: a double-precision floating pointQCborValue.Invalid
Not a valid value, this usually indicates a CBOR decoding error
Additionally,
QCborValuecan represent extended types:Constant
Description
QCborValue.Tag
An unknown or unrecognized extended type, represented by its tag (a
QCborTag) and the tagged value (aQCborValue)QCborValue.DateTime
QDateTime: a date and time stampQCborValue.Url
QUrl: a URL or URIQCborValue.RegularExpression
QRegularExpression: the pattern of a regular expressionQCborValue.Uuid
QUuid: a UUIDSee also
-
PySide2.QtCore.QCborValue.compare(other)¶ - Parameters
other –
QCborValue- Return type
int
Compares this value and
other, and returns an integer that indicates whether this value should be sorted prior to (if the result is negative) or afterother(if the result is positive). If this function returns 0, the two values are equal and hold the same contents.If each
QCborValuecontains an array or map, the comparison is recursive to elements contained in them.
Extended types¶
QCborValuecompares equal aQCborValuecontaining an extended type, likeUrlandUrland its equivalent tagged representation. So, for example, the following expression is true:QCborValue(QUrl("https://example.com")) == QCborValue(QCborKnownTags::Url, "https://example.com");Do note that Qt types like
QUrlandQDateTimewill normalize and otherwise modify their arguments. The expression above is true only because the string on the right side is the normalized value that theQCborValueon the left would take. If, for example, the “https” part were uppercase in both sides, the comparison would fail. For information on normalizations performed byQCborValue, please consult the documentation of the constructor taking the Qt type in question.
Sorting order¶
Sorting order in CBOR is defined in RFC 7049 {https://tools.ietf.org/html/rfc7049#section-3.9}{section 3.9}, which discusses the sorting of keys in a map when following the Canonical encoding. According to the specification, “sorting is performed on the bytes of the representation of the key data items” and lists as consequences that:
“If two keys have different lengths, the shorter one sorts earlier;”
“If two keys have the same length, the one with the lower value in (byte-wise) lexical order sorts earlier.”
This results in surprising sorting of QCborValues, where the result of this function is different from that which would later be retrieved by comparing the contained elements. For example, the
QCborValuecontaining string “zzz” sorts before theQCborValuewith string “foobar”, even though when comparing asQStringsorQByteArraysthe “zzz” sorts after “foobar” (dictionary order).The specification does not clearly indicate what sorting order should be done for values of different types (it says sorting should not pay “attention to the 3/5 bit splitting for major types”).
QCborValuemakes the assumption that types should be sorted too. The numeric values of theTypeenumeration are in that order, with the exception of the extended types, which compare as their tagged equivalents.Note
Sorting order is preliminary and is subject to change. Applications should not depend on the order returned by this function for the time being.
-
static
PySide2.QtCore.QCborValue.fromCbor(reader)¶ - Parameters
reader –
QCborStreamReader- Return type
Decodes one item from the CBOR stream found in
readerand returns the equivalent representation. This function is recursive: if the item is a map or array, it will decode all items found in that map or array, until the outermost object is finished.This function need not be used on the root element of a
QCborStreamReader. For example, the following code illustrates how to skip the CBOR signature tag from the beginning of a file:if (reader.isTag() && reader.toTag() == QCborKnownTags::Signature) reader.next(); QCborValue contents = QCborValue::fromCbor(reader);
The returned value may be partially complete and indistinguishable from a valid
QCborValueeven if the decoding failed. To determine if there was an error, check iflastError()is indicating an error condition. This function stops decoding immediately after the first error.
-
static
PySide2.QtCore.QCborValue.fromCbor(ba[, error=None]) - Parameters
ba –
QByteArrayerror –
QCborParserError
- Return type
This is an overloaded function.
Decodes one item from the CBOR stream found in the byte array
baand returns the equivalent representation. This function is recursive: if the item is a map or array, it will decode all items found in that map or array, until the outermost object is finished.This function stores the error state, if any, in the object pointed to by
error, along with the offset of where the error occurred. If no error happened, it storesNoErrorin the error state and the number of bytes that it consumed (that is, it stores the offset for the first unused byte). Using that information makes it possible to parse further data that may exist in the same byte array.The returned value may be partially complete and indistinguishable from a valid
QCborValueeven if the decoding failed. To determine if there was an error, check if there was an error stored inerror. This function stops decoding immediately after the first error.
-
static
PySide2.QtCore.QCborValue.fromCbor(data, len[, error=None]) - Parameters
data – str
len –
long longerror –
QCborParserError
- Return type
-
static
PySide2.QtCore.QCborValue.fromCbor(data, len[, error=None]) - Parameters
data –
quint8len –
long longerror –
QCborParserError
- Return type
-
static
PySide2.QtCore.QCborValue.fromJsonValue(v)¶ - Parameters
v –
QJsonValue- Return type
Converts the JSON value contained in
vinto its corresponding CBOR value and returns it. There is no data loss in converting from JSON to CBOR, as the CBOR type set is richer than JSON’s. Additionally, values converted to CBOR using this function can be converted back to JSON usingtoJsonValue()with no data loss.The following table lists the mapping of JSON types to CBOR types:
JSON Type
CBOR Type
Bool
Bool
Number
Integer (if the number has no fraction and is in the
qint64range) or DoubleString
String
Array
Array
Object
Map
Null
Null
QJsonValuecan also be undefined, indicating a previous operation that failed to complete (for example, searching for a key not present in an object). Undefined values are not JSON types and may not appear in JSON arrays and objects, but this function does return theQCborValueundefined value if the correspondingQJsonValueis undefined.
-
static
PySide2.QtCore.QCborValue.fromVariant(variant)¶ - Parameters
variant – object
- Return type
Converts the
QVariantvariantintoQCborValueand returns it.QVariants may contain a large list of different meta types, many of which have no corresponding representation in CBOR. That includes all user-defined meta types. When preparing transmission using CBOR, it is suggested to encode carefully each value to prevent loss of representation.
The following table lists the conversion this function will apply:
Qt (C++) type
CBOR type
invalid (QVariant())
Undefined
boolBool
std::nullptr_tNull
short,ushort,int,uint,qint64Integer
quint64Integer, but they are cast to
qint64first so values higher than 2 63 -1 (INT64_MAX) will be wrapped to negativefloat,doubleDouble
ByteArrayDateTimeQCborSimpleTypeSimple type
Array, converted using QCborArray::formJsonArray()
Array or Map
QJsonObjectMap, converted using
fromJsonObject()converted using
fromJsonValue()RegularExpressionQStringString
QStringListArray
QVariantHashMap
QVariantListArray
QVariantMapMap
Url
Uuid
For any other types, this function will return Null if the
QVariantitself is null, and otherwise will try to convert to string usingtoString(). If the conversion to string fails, this function returns Undefined.Please note that the conversions via
toString()are subject to change at any time.QCborValuemay be extended in the future to support more types, which will result in a change in how this function performs conversions.
-
PySide2.QtCore.QCborValue.isArray()¶ - Return type
bool
Returns true if this
QCborValueis of the array type. The array value can be retrieved usingtoArray().
-
PySide2.QtCore.QCborValue.isBool()¶ - Return type
bool
Returns true if this
QCborValueis a boolean. The value can be retrieved usingtoBool().
-
PySide2.QtCore.QCborValue.isByteArray()¶ - Return type
bool
Returns true if this
QCborValueis of the byte array type. The byte array value can be retrieved usingtoByteArray().See also
-
PySide2.QtCore.QCborValue.isContainer()¶ - Return type
bool
This convenience function returns true if the
QCborValueis either an array or a map.
-
PySide2.QtCore.QCborValue.isDateTime()¶ - Return type
bool
Returns true if this
QCborValueis of the date/time type. The value can be retrieved usingtoDateTime(). Date/times are extended types that use the tagDateTime.Additionally, when decoding from a CBOR stream,
QCborValuewill interpret tags of valueUnixTime_tand convert them to the equivalent date/time.See also
-
PySide2.QtCore.QCborValue.isDouble()¶ - Return type
bool
Returns true if this
QCborValueis of the floating-point type. The value can be retrieved usingtoDouble().See also
-
PySide2.QtCore.QCborValue.isFalse()¶ - Return type
bool
Returns true if this
QCborValueis a boolean with false value. This function exists because, internally, CBOR booleans are stored as two separate types, one for true and one for false.
-
PySide2.QtCore.QCborValue.isInteger()¶ - Return type
bool
Returns true if this
QCborValueis of the integer type. The integer value can be retrieved usingtoInteger().See also
-
PySide2.QtCore.QCborValue.isInvalid()¶ - Return type
bool
Returns true if this
QCborValueis not of any valid type. Invalid QCborValues are distinct from those with undefined values and they usually represent a decoding error.See also
-
PySide2.QtCore.QCborValue.isMap()¶ - Return type
bool
Returns true if this
QCborValueis of the map type. The map value can be retrieved usingtoMap().
-
PySide2.QtCore.QCborValue.isNull()¶ - Return type
bool
Returns true if this
QCborValueis of the null type.CBOR null values are used to indicate optional values that were not provided. They are distinct from undefined values, in that null values are usually not the result of an earlier error or problem.
Null values are distinct from undefined values and from invalid
QCborValueobjects. The API will not create invalid QCborValues, but they may exist as a result of a parsing error.See also
-
PySide2.QtCore.QCborValue.isRegularExpression()¶ - Return type
bool
Returns true if this
QCborValuecontains a regular expression’s pattern. The pattern can be retrieved usingtoRegularExpression().See also
-
PySide2.QtCore.QCborValue.isSimpleType(st)¶ - Parameters
st –
QCborSimpleType- Return type
bool
This is an overloaded function.
Returns true if this
QCborValueis of a simple type andtoSimpleType()would returnst, false otherwise. This function can be used to check for any CBOR simple type, even those for which there is no enumeration in the API. For example, for the simple type of value 12, you could write:value.isSimpleType(QCborSimpleType(12));
See also
QCborValue(QCborSimpleType)isSimpleType()isFalse()isTrue()isNullisUndefined()toSimpleType()
-
PySide2.QtCore.QCborValue.isSimpleType() - Return type
bool
Returns true if this
QCborValueis of one of the CBOR simple types. The type itself can later be retrieved usingtype(), even for types that don’t have an enumeration in the API. They can also be checked with theisSimpleType(QCborSimpleType)overload.See also
QCborSimpleTypeisSimpleType(QCborSimpleType)toSimpleType()
-
PySide2.QtCore.QCborValue.isString()¶ - Return type
bool
Returns true if this
QCborValueis of the string type. The string value can be retrieved usingtoString().See also
-
PySide2.QtCore.QCborValue.isTag()¶ - Return type
bool
Returns true if this
QCborValueis of the tag type. The tag value can be retrieved usingtag()and the tagged value usingtaggedValue().This function also returns true for extended types that the API recognizes. For code that handles extended types directly before the Qt API is updated to support them, it is possible to recreate the tag + tagged value pair by using
taggedValue().See also
type()tag()taggedValue()taggedValue()
-
PySide2.QtCore.QCborValue.isTrue()¶ - Return type
bool
Returns true if this
QCborValueis a boolean with true value. This function exists because, internally, CBOR booleans are stored as two separate types, one for false and one for true.
-
PySide2.QtCore.QCborValue.isUndefined()¶ - Return type
bool
Returns true if this
QCborValueis of the undefined type.CBOR undefined values are used to indicate missing information, usually as a result of a previous operation that did not complete as expected. They are also used by the
QCborArrayandQCborMapAPI to indicate the searched item was not found.Undefined values are distinct from null values.
QCborValueobjects with undefined values are also different from invalidQCborValueobjects. The API will not create invalid QCborValues, but they may exist as a result of a parsing error.See also
-
PySide2.QtCore.QCborValue.isUrl()¶ - Return type
bool
Returns true if this
QCborValueis of the URL type. The URL value can be retrieved usingtoUrl().
-
PySide2.QtCore.QCborValue.isUuid()¶ - Return type
bool
Returns true if this
QCborValuecontains a UUID. The value can be retrieved usingtoUuid().
-
PySide2.QtCore.QCborValue.__ne__(other)¶ - Parameters
other –
QCborValue- Return type
bool
Compares this value and
other, and returns true if contents differ, false otherwise. If eachQCborValuecontains an array or map, the comparison is recursive to elements contained in them.For more information on CBOR equality in Qt, see,
compare().See also
compare()operator==()operator==()operator==()operator
-
PySide2.QtCore.QCborValue.__lt__(other)¶ - Parameters
other –
QCborValue- Return type
bool
-
PySide2.QtCore.QCborValue.__eq__(other)¶ - Parameters
other –
QCborValue- Return type
bool
Compares this value and
other, and returns true if they hold the same contents, false otherwise. If eachQCborValuecontains an array or map, the comparison is recursive to elements contained in them.For more information on CBOR equality in Qt, see,
compare().See also
compare()operator==()operator!=()operator
-
PySide2.QtCore.QCborValue.operator[](key) - Parameters
key – unicode
- Return type
-
PySide2.QtCore.QCborValue.operator[](key) - Parameters
key –
qint64- Return type
If this
QCborValueis aQCborMap, searches elements for the value whose key matcheskey. If this is an array, returns the element whose index iskey. If there’s no matching value in the array or map, or if thisQCborValueobject is not an array or map, returns the undefined value.
-
PySide2.QtCore.QCborValue.swap(other)¶ - Parameters
other –
QCborValue
Swaps the contents of this
QCborValueobject andother.
-
PySide2.QtCore.QCborValue.taggedValue([defaultValue=QCborValue()])¶ - Parameters
defaultValue –
QCborValue- Return type
Returns the tagged value of this extended
QCborValueobject, if it is of the tag type,defaultValueotherwise.CBOR represents extended types by associating a number (the tag) with a stored representation. This function returns that representation. To retrieve the tag, use
tag().See also
isTag()tag()isDateTime()isUrl()isRegularExpression()isUuid()
-
PySide2.QtCore.QCborValue.toArray(defaultValue)¶ - Parameters
defaultValue –
QCborArray- Return type
Returns the array value stored in this
QCborValue, if it is of the array type. Otherwise, it returnsdefaultValue.Note that this function performs no conversion from other types to
QCborArray.See also
-
PySide2.QtCore.QCborValue.toArray() - Return type
-
PySide2.QtCore.QCborValue.toBool([defaultValue=false])¶ - Parameters
defaultValue –
bool- Return type
bool
Returns the boolean value stored in this
QCborValue, if it is of a boolean type. Otherwise, it returnsdefaultValue.
-
PySide2.QtCore.QCborValue.toByteArray([defaultValue={}])¶ - Parameters
defaultValue –
QByteArray- Return type
Returns the byte array value stored in this
QCborValue, if it is of the byte array type. Otherwise, it returnsdefaultValue.Note that this function performs no conversion from other types to
QByteArray.See also
-
PySide2.QtCore.QCborValue.toCbor(writer[, opt=QCborValue.NoTransformation])¶ - Parameters
writer –
QCborStreamWriteropt –
EncodingOptions
This is an overloaded function.
Encodes this
QCborValueobject to its CBOR representation, using the options specified inopt, to the writer specified bywriter. The same writer can be used by multiple QCborValues, for example, in order to encode different elements in a larger array.This function will not fail, except if this
QCborValueor any of the contained items, if this is a map or array, are invalid. Invalid types are not produced normally by the API, but can result from decoding errors.By default, this function performs no transformation on the values in the
QCborValue, writing all floating point directly as double-precision (binary64) types. If theUseFloatoption is specified, it will use single precision (binary32) for any floating point value for which there’s no loss of precision in using that representation. That includes infinities and NaN values.Similarly, if
UseFloat16is specified, this function will try to use half-precision (binary16) floating point if the conversion to that results in no loss of precision. This is always true for infinities and NaN.If
UseIntegersis specified, it will use integers for any floating point value that contains an actual integer.See also
-
PySide2.QtCore.QCborValue.toCbor([opt=QCborValue.NoTransformation]) - Parameters
opt –
EncodingOptions- Return type
Encodes this
QCborValueobject to its CBOR representation, using the options specified inopt, and return the byte array containing that representation.This function will not fail, except if this
QCborValueor any of the contained items, if this is a map or array, are invalid. Invalid types are not produced normally by the API, but can result from decoding errors.By default, this function performs no transformation on the values in the
QCborValue, writing all floating point directly as double-precision (double) types. If theUseFloatoption is specified, it will use single precision (float) for any floating point value for which there’s no loss of precision in using that representation. That includes infinities and NaN values.Similarly, if
UseFloat16is specified, this function will try to use half-precision (qfloat16) floating point if the conversion to that results in no loss of precision. This is always true for infinities and NaN.If
UseIntegersis specified, it will use integers for any floating point value that contains an actual integer.See also
-
PySide2.QtCore.QCborValue.toDateTime([defaultValue={}])¶ -
Returns the date/time value stored in this
QCborValue, if it is of the date/time extended type. Otherwise, it returnsdefaultValue.Note that this function performs no conversion from other types to
QDateTime.See also
-
PySide2.QtCore.QCborValue.toDiagnosticNotation([opts=QCborValue.Compact])¶ - Parameters
opts –
DiagnosticNotationOptions- Return type
unicode
Creates the diagnostic notation equivalent of this CBOR object and returns it. The
optsparameter controls the dialect of the notation. Diagnostic notation is useful in debugging, to aid the developer in understanding what value is stored in theQCborValueor in a CBOR stream. For that reason, the Qt API provides no support for parsing the diagnostic back into the in-memory format or CBOR stream, though the representation is unique and it would be possible.CBOR diagnostic notation is specified by section 6 of RFC 7049. It is a text representation of the CBOR stream and it is very similar to JSON, but it supports the CBOR types not found in JSON. The extended format enabled by the
ExtendedFormatflag is currently in some IETF drafts and its format is subject to change.This function produces the equivalent representation of the stream that
toCbor()would produce, without any transformation option provided there. This also implies this function may not produce a representation of the stream that was used to create the object, if it was created usingfromCbor(), as that function may have applied transformations. For a high-fidelity notation of a stream, without transformation, see thecbordumpexample.
-
PySide2.QtCore.QCborValue.toDouble([defaultValue=0])¶ - Parameters
defaultValue –
double- Return type
double
Returns the floating point value stored in this
QCborValue, if it is of the Double type. If it is of the Integer type, this function returns the integer value converted to double. In any other case, it returnsdefaultValue.See also
-
PySide2.QtCore.QCborValue.toInteger([defaultValue=0])¶ - Parameters
defaultValue –
qint64- Return type
qint64
Returns the integer value stored in this
QCborValue, if it is of the integer type. If it is of the Double type, this function returns the floating point value converted to integer. In any other case, it returnsdefaultValue.See also
-
PySide2.QtCore.QCborValue.toJsonValue()¶ - Return type
Converts this
QCborValueobject to an equivalent representation in JSON and returns it as aQJsonValue.Please note that CBOR contains a richer and wider type set than JSON, so some information may be lost in this conversion. The following table compares CBOR types to JSON types and indicates whether information may be lost or not.
CBOR Type
JSON Type
Comments
Bool
Bool
No data loss possible
Double
Number
Infinities and NaN will be converted to Null; no data loss for other values
Integer
Number
Data loss possible in the conversion if the integer is larger than 2 53 or smaller than -2 53 .
Null
Null
No data loss possible
Undefined
Null
Type information lost
String
String
No data loss possible
Byte Array
String
Converted to a lossless encoding like Base64url, but the distinction between strings and byte arrays is lost
Other simple types
String
Type information lost
Array
Array
Conversion applies to each contained value
Map
Object
Keys are converted to string; values converted according to this table
Tags and extended types
Special
The tag number itself is lost and the tagged value is converted to JSON
For information on the conversion of CBOR map keys to string, see
toJsonObject().If this
QCborValuecontains the undefined value, this function will return an undefinedQJsonValuetoo. Note that JSON does not support undefined values and undefined QJsonValues are an extension to the specification. They cannot be held in aQJsonArrayorQJsonObject, but can be returned from functions to indicate a failure. For all other intents and purposes, they are the same as null.
Special handling of tags and extended types¶
Some tags are handled specially and change the transformation of the tagged value from CBOR to JSON. The following table lists those special cases:
Tag
CBOR type
Transformation
ExpectedBase64urlByte array
Encodes the byte array as Base64url
ExpectedBase64Byte array
Encodes the byte array as Base64
ExpectedBase16Byte array
Encodes the byte array as hex
Url
Url and String
Uses
toEncoded()to normalize the encoding to the URL’s fully encoded formatUuid
Uuid and Byte array
Uses
toString()to create the string representation
-
PySide2.QtCore.QCborValue.toMap(defaultValue) -
Returns the map value stored in this
QCborValue, if it is of the map type. Otherwise, it returnsdefaultValue.Note that this function performs no conversion from other types to
QCborMap.See also
-
PySide2.QtCore.QCborValue.toRegularExpression([defaultValue={}])¶ - Parameters
defaultValue –
QRegularExpression- Return type
Returns the regular expression value stored in this
QCborValue, if it is of the regular expression pattern extended type. Otherwise, it returnsdefaultValue.Note that this function performs no conversion from other types to
QRegularExpression.See also
-
PySide2.QtCore.QCborValue.toSimpleType([defaultValue=QCborSimpleType.Undefined])¶ - Parameters
defaultValue –
QCborSimpleType- Return type
QCborSimpleType
Returns the simple type this
QCborValueis of, if it is a simple type. If it is not a simple type, it returnsdefaultValue.The following types are simple types and this function will return the listed values:
FalseQCborSimpleType::False
TrueQCborSimpleType::True
NullQCborSimpleType::Null
UndefinedQCborSimpleType::Undefined
-
PySide2.QtCore.QCborValue.toString([defaultValue={}])¶ - Parameters
defaultValue – unicode
- Return type
unicode
Returns the string value stored in this
QCborValue, if it is of the string type. Otherwise, it returnsdefaultValue.Note that this function performs no conversion from other types to
QString.See also
-
PySide2.QtCore.QCborValue.toUrl([defaultValue={}])¶ -
Returns the URL value stored in this
QCborValue, if it is of the URL extended type. Otherwise, it returnsdefaultValue.Note that this function performs no conversion from other types to
QUrl.See also
-
PySide2.QtCore.QCborValue.toUuid([defaultValue={}])¶ -
Returns the UUID value stored in this
QCborValue, if it is of the UUID extended type. Otherwise, it returnsdefaultValue.Note that this function performs no conversion from other types to
QUuid.See also
-
PySide2.QtCore.QCborValue.toVariant()¶ - Return type
object
Converts this value to a native Qt type and returns the corresponding
QVariant.The following table lists the mapping performed between
QCborValue typesandQt meta types.CBOR Type
Qt or C++ type
Notes
Integer
qint64Double
doubleBool
boolNull
std::nullptr_tUndefined
no type (QVariant())
Byte array
String
QStringArray
QVariantListRecursively converts all values
Map
QVariantMapKey types are “stringified”
Other simple types
QCborSimpleTypeDateTimeUrl
RegularExpressionUuid
Other tags
Special
The tag is ignored and the tagged value is converted using this function
Note that values in both CBOR Maps and Arrays are converted recursively using this function too and placed in
QVariantMapandQVariantListinstead. You will not findQCborMapandQCborArraystored inside the QVariants.QVariantMaps have string keys, unlike CBOR, so the conversion of a
QCborMaptoQVariantMapwill imply a step of “stringification” of the key values. SeetoJsonObject()for details.
-
PySide2.QtCore.QCborValue.type()¶ - Return type
Returns the type of this
QCborValue. The type can also later be retrieved by one of the “isXxx” functions.
© 2018 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.