summaryrefslogtreecommitdiffstats
path: root/src/corelib/serialization/qjsoncbor.cpp
blob: 0f72dc3031ac100a5fd74951a064e512aafc7ee2 (plain)
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
// Copyright (C) 2020 Intel Corporation.// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only#include"qcborvalue.h"#include"qcborvalue_p.h"#include"qcborarray.h"#include"qcbormap.h"#include"qjsonarray.h"#include"qjsonobject.h"#include"qjsondocument.h"#include"qjson_p.h"#include <qmap.h>#include <qhash.h>#include <private/qnumeric_p.h>#include <quuid.h> QT_BEGIN_NAMESPACE using namespace QtCbor;enumclass ConversionMode { FromRaw, FromVariantToJson };static QJsonValue fpToJson(double v){returnqt_is_finite(v) ?QJsonValue(v) :QJsonValue();}static QString simpleTypeString(QCborValue::Type t){int simpleType = t -QCborValue::SimpleType;if(unsigned(simpleType) <0x100)returnQString::fromLatin1("simple(%1)").arg(simpleType);// if we got here, we got an unknown typeqWarning("QCborValue: found unknown type 0x%x", t);returnQString();}static QString encodeByteArray(const QCborContainerPrivate *d, qsizetype idx, QCborTag encoding){const ByteData *b = d->byteData(idx);if(!b)returnQString(); QByteArray data =QByteArray::fromRawData(b->byte(), b->len);if(encoding ==QCborKnownTags::ExpectedBase16) data = data.toHex();else if(encoding ==QCborKnownTags::ExpectedBase64) data = data.toBase64();else data = data.toBase64(QByteArray::Base64UrlEncoding |QByteArray::OmitTrailingEquals);returnQString::fromLatin1(data);}static QString makeString(const QCborContainerPrivate *d, qsizetype idx, ConversionMode mode =ConversionMode::FromRaw);static QString maybeEncodeTag(const QCborContainerPrivate *d){ qint64 tag = d->elements.at(0).value;const Element &e = d->elements.at(1);switch(tag) {caseqint64(QCborKnownTags::DateTimeString):caseqint64(QCborKnownTags::Url):if(e.type ==QCborValue::String)returnmakeString(d,1);break;caseqint64(QCborKnownTags::ExpectedBase64url):caseqint64(QCborKnownTags::ExpectedBase64):caseqint64(QCborKnownTags::ExpectedBase16):if(e.type ==QCborValue::ByteArray)returnencodeByteArray(d,1,QCborTag(tag));break;caseqint64(QCborKnownTags::Uuid):#ifndef QT_BOOTSTRAPPEDif(const ByteData *b = d->byteData(e); e.type ==QCborValue::ByteArray && b && b->len ==sizeof(QUuid))returnQUuid::fromRfc4122(b->asByteArrayView()).toString(QUuid::WithoutBraces);#endifbreak;}// don't know what to do, bail outreturnQString();}static QString encodeTag(const QCborContainerPrivate *d){ QString s;if(!d || d->elements.size() !=2)return s;// invalid (incomplete?) tag state s =maybeEncodeTag(d);if(s.isNull()) {// conversion failed, ignore the tag and convert the tagged value s =makeString(d,1);}return s;} Q_NEVER_INLINE static QString makeString(const QCborContainerPrivate *d, qsizetype idx, ConversionMode mode){constauto&e = d->elements.at(idx);switch(e.type) {caseQCborValue::Integer:returnQString::number(qint64(e.value));caseQCborValue::Double:returnQString::number(e.fpvalue());caseQCborValue::ByteArray:return mode ==ConversionMode::FromVariantToJson ? d->stringAt(idx):encodeByteArray(d, idx,QCborTag(QCborKnownTags::ExpectedBase64url));caseQCborValue::String:return d->stringAt(idx);caseQCborValue::Array:caseQCborValue::Map:#if defined(QT_BOOTSTRAPPED)Q_UNREACHABLE_RETURN(QString());#elsereturn d->valueAt(idx).toDiagnosticNotation(QCborValue::Compact);#endifcaseQCborValue::SimpleType:break;caseQCborValue::False:returnQStringLiteral("false");caseQCborValue::True:returnQStringLiteral("true");caseQCborValue::Null:returnQStringLiteral("null");caseQCborValue::Undefined:returnQStringLiteral("undefined");caseQCborValue::Invalid:returnQString();caseQCborValue::Tag:caseQCborValue::DateTime:caseQCborValue::Url:caseQCborValue::RegularExpression:caseQCborValue::Uuid:returnencodeTag(e.flags &Element::IsContainer ? e.container :nullptr);}// maybe it's a simple typereturnsimpleTypeString(e.type);} QJsonValue qt_convertToJson(QCborContainerPrivate *d, qsizetype idx, ConversionMode mode =ConversionMode::FromRaw);static QJsonValue convertExtendedTypeToJson(QCborContainerPrivate *d){ qint64 tag = d->elements.at(0).value;switch(tag) {caseqint64(QCborKnownTags::Url):#ifdef QT_BOOTSTRAPPEDbreak;#else// use the fully-encoded URL formif(d->elements.at(1).type ==QCborValue::String)returnQUrl::fromEncoded(d->byteData(1)->asByteArrayView()).toString(QUrl::FullyEncoded);Q_FALLTHROUGH();#endifcaseqint64(QCborKnownTags::DateTimeString):caseqint64(QCborKnownTags::ExpectedBase64url):caseqint64(QCborKnownTags::ExpectedBase64):caseqint64(QCborKnownTags::ExpectedBase16):caseqint64(QCborKnownTags::Uuid): {// use the string conversion QString s =maybeEncodeTag(d);if(!s.isNull())return s;}}// for all other tags, ignore it and return the converted tagged itemreturnqt_convertToJson(d,1);}// We need to do this because sub-objects may need conversion.static QJsonArray convertToJsonArray(QCborContainerPrivate *d, ConversionMode mode =ConversionMode::FromRaw){ QJsonArray a;if(d) {for(qsizetype idx =0; idx < d->elements.size(); ++idx) a.append(qt_convertToJson(d, idx, mode));}return a;}// We need to do this because the keys need to be sorted and converted to strings// and sub-objects may need recursive conversion.static QJsonObject convertToJsonObject(QCborContainerPrivate *d, ConversionMode mode =ConversionMode::FromRaw){ QJsonObject o;if(d) {for(qsizetype idx =0; idx < d->elements.size(); idx +=2) o.insert(makeString(d, idx),qt_convertToJson(d, idx +1, mode));}return o;} QJsonValue qt_convertToJson(QCborContainerPrivate *d, qsizetype idx, ConversionMode mode){// encoding the container itselfif(idx == -QCborValue::Array)returnconvertToJsonArray(d, mode);if(idx == -QCborValue::Map)returnconvertToJsonObject(d, mode);if(idx <0) {// tag-like typeif(!d || d->elements.size() !=2)returnQJsonValue::Undefined;// invalid statereturnconvertExtendedTypeToJson(d);}// an element in the containerconstauto&e = d->elements.at(idx);switch(e.type) {caseQCborValue::Integer:returnQJsonValue(e.value);caseQCborValue::ByteArray:if(mode ==ConversionMode::FromVariantToJson) {constauto value =makeString(d, idx, mode);return value.isEmpty() ?QJsonValue() :QJsonPrivate::Value::fromTrustedCbor(value);}break;caseQCborValue::RegularExpression:if(mode ==ConversionMode::FromVariantToJson)returnQJsonValue();break;caseQCborValue::String:caseQCborValue::SimpleType:// make stringbreak;caseQCborValue::Array:caseQCborValue::Map:caseQCborValue::Tag:caseQCborValue::DateTime:caseQCborValue::Url:caseQCborValue::Uuid:// recursereturnqt_convertToJson(e.flags &Element::IsContainer ? e.container :nullptr, -e.type, mode);caseQCborValue::Null:caseQCborValue::Undefined:caseQCborValue::Invalid:returnQJsonValue();caseQCborValue::False:return false;caseQCborValue::True:return true;caseQCborValue::Double:returnfpToJson(e.fpvalue());}returnQJsonPrivate::Value::fromTrustedCbor(makeString(d, idx, mode));}/*! Converts this QCborValue object to an equivalent representation in JSON and returns it as a QJsonValue. 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. \table \header \li CBOR Type \li JSON Type \li Comments \row \li Bool \li Bool \li No data loss possible \row \li Double \li Number \li Infinities and NaN will be converted to Null; no data loss for other values \row \li Integer \li Number \li Data loss possible in the conversion if the integer is larger than 2\sup{53} or smaller than -2\sup{53}. \row \li Null \li Null \li No data loss possible \row \li Undefined \li Null \li Type information lost \row \li String \li String \li No data loss possible \row \li Byte Array \li String \li Converted to a lossless encoding like Base64url, but the distinction between strings and byte arrays is lost \row \li Other simple types \li String \li Type information lost \row \li Array \li Array \li Conversion applies to each contained value \row \li Map \li Object \li Keys are converted to string; values converted according to this table \row \li Tags and extended types \li Special \li The tag number itself is lost and the tagged value is converted to JSON \endtable For information on the conversion of CBOR map keys to string, see QCborMap::toJsonObject(). If this QCborValue contains the undefined value, this function will return an undefined QJsonValue too. Note that JSON does not support undefined values and undefined QJsonValues are an extension to the specification. They cannot be held in a QJsonArray or QJsonObject, but can be returned from functions to indicate a failure. For all other intents and purposes, they are the same as null. \section3 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: \table \header \li Tag \li CBOR type \li Transformation \row \li ExpectedBase64url \li Byte array \li Encodes the byte array as Base64url \row \li ExpectedBase64 \li Byte array \li Encodes the byte array as Base64 \row \li ExpectedBase16 \li Byte array \li Encodes the byte array as hex \row \li Url \li Url and String \li Uses QUrl::toEncoded() to normalize the encoding to the URL's fully encoded format \row \li Uuid \li Uuid and Byte array \li Uses QUuid::toString() to create the string representation \endtable \sa fromJsonValue(), toVariant(), QCborArray::toJsonArray(), QCborMap::toJsonObject() */ QJsonValue QCborValue::toJsonValue()const{if(container)returnqt_convertToJson(container, n <0? -type() : n);// simple valuesswitch(type()) {case False:return false;case Integer:returnQJsonPrivate::Value::fromTrustedCbor(*this);case True:return true;case Null:case Undefined:case Invalid:returnQJsonValue();case Double:returnfpToJson(fp_helper());case SimpleType:break;case ByteArray:case String:// empty stringsreturnQJsonValue::String;case Array:// empty arrayreturnQJsonArray();case Map:// empty mapreturnQJsonObject();case Tag:case DateTime:case Url:case RegularExpression:case Uuid:// Reachable, but invalid in JsonreturnQJsonValue::Undefined;}returnQJsonPrivate::Value::fromTrustedCbor(simpleTypeString(type()));}#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED) QJsonValue QCborValueRef::toJsonValue()const{returnqt_convertToJson(d, i);}#endif/*! Recursively converts every \l QCborValue element in this array to JSON using QCborValue::toJsonValue() and returns the corresponding QJsonArray composed of those elements. Please note that CBOR contains a richer and wider type set than JSON, so some information may be lost in this conversion. For more details on what conversions are applied, see QCborValue::toJsonValue(). \sa fromJsonArray(), QCborValue::toJsonValue(), QCborMap::toJsonObject(), toVariantList() */ QJsonArray QCborArray::toJsonArray()const{returnconvertToJsonArray(d.data());}#ifndef QT_NO_VARIANT QJsonArray QJsonPrivate::Variant::toJsonArray(const QVariantList &list){constauto cborArray =QCborArray::fromVariantList(list);returnconvertToJsonArray(cborArray.d.data(),ConversionMode::FromVariantToJson);}#endif// !QT_NO_VARIANT/*! Recursively converts every \l QCborValue value in this map to JSON using QCborValue::toJsonValue() and creates a string key for all keys that aren't strings, then returns the corresponding QJsonObject composed of those associations. Please note that CBOR contains a richer and wider type set than JSON, so some information may be lost in this conversion. For more details on what conversions are applied, see QCborValue::toJsonValue(). \section3 Map key conversion to string JSON objects are defined as having string keys, unlike CBOR, so the conversion of a QCborMap to QJsonObject will imply a step of "stringification" of the key values. The conversion will use the special handling of tags and extended types from above and will also convert the rest of the types as follows: \table \header \li Type \li Transformation \row \li Bool \li "true" and "false" \row \li Null \li "null" \row \li Undefined \li "undefined" \row \li Integer \li The decimal string form of the number \row \li Double \li The decimal string form of the number \row \li Byte array \li Unless tagged differently (see above), encoded as Base64url \row \li Array \li Replaced by the compact form of its \l{QCborValue::toDiagnosticNotation()}{Diagnostic notation} \row \li Map \li Replaced by the compact form of its \l{QCborValue::toDiagnosticNotation()}{Diagnostic notation} \row \li Tags and extended types \li Tag number is dropped and the tagged value is converted to string \endtable \sa fromJsonObject(), QCborValue::toJsonValue(), QCborArray::toJsonArray(), toVariantMap() */ QJsonObject QCborMap::toJsonObject()const{returnconvertToJsonObject(d.data());}#ifndef QT_NO_VARIANT QJsonObject QJsonPrivate::Variant::toJsonObject(const QVariantMap &map){constauto cborMap =QCborMap::fromVariantMap(map);returnconvertToJsonObject(cborMap.d.data(),ConversionMode::FromVariantToJson);}/*! Converts this value to a native Qt type and returns the corresponding QVariant. The following table lists the mapping performed between \l{Type}{QCborValue types} and \l{QMetaType::Type}{Qt meta types}. \table \header \li CBOR Type \li Qt or C++ type \li Notes \row \li Integer \li \l qint64 \li \row \li Double \li \c double \li \row \li Bool \li \c bool \li \row \li Null \li \c std::nullptr_t \li \row \li Undefined \li no type (QVariant()) \li \row \li Byte array \li \l QByteArray \li \row \li String \li \l QString \li \row \li Array \li \l QVariantList \li Recursively converts all values \row \li Map \li \l QVariantMap \li Key types are "stringified" \row \li Other simple types \li \l QCborSimpleType \li \row \li DateTime \li \l QDateTime \li \row \li Url \li \l QUrl \li \row \li RegularExpression \li \l QRegularExpression \li \row \li Uuid \li \l QUuid \li \row \li Other tags \li Special \li The tag is ignored and the tagged value is converted using this function \endtable Note that values in both CBOR Maps and Arrays are converted recursively using this function too and placed in QVariantMap and QVariantList instead. You will not find QCborMap and QCborArray stored inside the QVariants. QVariantMaps have string keys, unlike CBOR, so the conversion of a QCborMap to QVariantMap will imply a step of "stringification" of the key values. See QCborMap::toJsonObject() for details. \sa fromVariant(), toJsonValue(), QCborArray::toVariantList(), QCborMap::toVariantMap() */ QVariant QCborValue::toVariant()const{switch(type()) {case Integer:returntoInteger();case Double:returntoDouble();case SimpleType:break;case False:case True:returnisTrue();case Null:returnQVariant::fromValue(nullptr);case Undefined:returnQVariant();case ByteArray:returntoByteArray();case String:returntoString();case Array:returntoArray().toVariantList();case Map:returntoMap().toVariantMap();case Tag:// ignore tagsreturntaggedValue().toVariant();#if QT_CONFIG(datestring)case DateTime:returntoDateTime();#endif#ifndef QT_BOOTSTRAPPEDcase Url:returntoUrl();# if QT_CONFIG(regularexpression)case RegularExpression:returntoRegularExpression();# endifcase Uuid:returntoUuid();#endifcase Invalid:returnQVariant();default:break;}if(isSimpleType())returnQVariant::fromValue(toSimpleType());Q_UNREACHABLE_RETURN(QVariant());}#endif// !QT_NO_VARIANT/*! Converts the JSON value contained in \a v into 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 using toJsonValue() with no data loss. The following table lists the mapping of JSON types to CBOR types: \table \header \li JSON Type \li CBOR Type \row \li Bool \li Bool \row \li Number \li Integer (if the number has no fraction and is in the \l qint64 range) or Double \row \li String \li String \row \li Array \li Array \row \li Object \li Map \row \li Null \li Null \endtable \l QJsonValue can 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 the QCborValue undefined value if the corresponding QJsonValue is undefined. \sa toJsonValue(), fromVariant(), QCborArray::fromJsonArray(), QCborMap::fromJsonObject() */ QCborValue QCborValue::fromJsonValue(const QJsonValue &v){switch(v.type()) {caseQJsonValue::Bool:return v.toBool();caseQJsonValue::Double: {if(v.value.t == Integer)return v.toInteger();return v.toDouble();}caseQJsonValue::String:return v.toString();caseQJsonValue::Array:returnQCborArray::fromJsonArray(v.toArray());caseQJsonValue::Object:returnQCborMap::fromJsonObject(v.toObject());caseQJsonValue::Null:returnnullptr;caseQJsonValue::Undefined:break;}returnQCborValue();}#ifndef QT_NO_VARIANTstatic voidappendVariant(QCborContainerPrivate *d,const QVariant &variant){// Handle strings and byte arrays directly, to avoid creating a temporary// dummy container to hold their data.int type = variant.metaType().id();if(type ==QMetaType::QString) { d->append(variant.toString());}else if(type ==QMetaType::QByteArray) { QByteArray ba = variant.toByteArray(); d->appendByteData(ba.constData(), ba.size(),QCborValue::ByteArray);}else{// For everything else, use the function below. d->append(QCborValue::fromVariant(variant));}}/*! Converts the QVariant \a variant into QCborValue and 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: \table \header \li Qt (C++) type \li CBOR type \row \li invalid (QVariant()) \li Undefined \row \li \c bool \li Bool \row \li \c std::nullptr_t \li Null \row \li \c short, \c ushort, \c int, \c uint, \l qint64 \li Integer \row \li \l quint64 \li Integer, or Double if outside the range of qint64 \row \li \c float, \c double \li Double \row \li \l QByteArray \li ByteArray \row \li \l QDateTime \li DateTime \row \li \l QCborSimpleType \li Simple type \row \li \l QJsonArray \li Array, converted using QCborArray::formJsonArray() \row \li \l QJsonDocument \li Array or Map \row \li \l QJsonObject \li Map, converted using QCborMap::fromJsonObject() \row \li \l QJsonValue \li converted using fromJsonValue() \row \li \l QRegularExpression \li RegularExpression \row \li \l QString \li String \row \li \l QStringList \li Array \row \li \l QVariantHash \li Map \row \li \l QVariantList \li Array \row \li \l QVariantMap \li Map \row \li \l QUrl \li Url \row \li \l QUuid \li Uuid \endtable If QVariant::isNull() returns true, a null QCborValue is returned or inserted into the list or object, regardless of the type carried by QVariant. Note the behavior change in Qt 6.0 affecting QVariant::isNull() also affects this function. For other types not listed above, a conversion to string will be attempted, usually but not always by calling QVariant::toString(). If the conversion fails the value is replaced by an Undefined CBOR value. Note that QVariant::toString() is also lossy for the majority of types. Please note that the conversions via QVariant::toString() are subject to change at any time. Both QVariant and QCborValue may be extended in the future to support more types, which will result in a change in how this function performs conversions. \sa toVariant(), fromJsonValue(), QCborArray::toVariantList(), QCborMap::toVariantMap(), QJsonValue::fromVariant() */ QCborValue QCborValue::fromVariant(const QVariant &variant){switch(variant.metaType().id()) {caseQMetaType::UnknownType:return{};caseQMetaType::Nullptr:returnnullptr;caseQMetaType::Bool:return variant.toBool();caseQMetaType::Short:caseQMetaType::UShort:caseQMetaType::Int:caseQMetaType::LongLong:caseQMetaType::Long:caseQMetaType::UInt:return variant.toLongLong();caseQMetaType::ULong:caseQMetaType::ULongLong:if(variant.toULongLong() <=static_cast<uint64_t>(std::numeric_limits<qint64>::max()))return variant.toLongLong();Q_FALLTHROUGH();caseQMetaType::Float16:caseQMetaType::Float:caseQMetaType::Double:return variant.toDouble();caseQMetaType::QString:return variant.toString();caseQMetaType::QStringList:returnQCborArray::fromStringList(variant.toStringList());caseQMetaType::QByteArray:return variant.toByteArray();#if QT_CONFIG(datestring)caseQMetaType::QDateTime:returnQCborValue(variant.toDateTime());#endif#ifndef QT_BOOTSTRAPPEDcaseQMetaType::QUrl:returnQCborValue(variant.toUrl());caseQMetaType::QUuid:returnQCborValue(variant.toUuid());#endifcaseQMetaType::QVariantList:returnQCborArray::fromVariantList(variant.toList());caseQMetaType::QVariantMap:returnQCborMap::fromVariantMap(variant.toMap());caseQMetaType::QVariantHash:returnQCborMap::fromVariantHash(variant.toHash());#ifndef QT_BOOTSTRAPPED#if QT_CONFIG(regularexpression)caseQMetaType::QRegularExpression:returnQCborValue(variant.toRegularExpression());#endifcaseQMetaType::QJsonValue:returnfromJsonValue(variant.toJsonValue());caseQMetaType::QJsonObject:returnQCborMap::fromJsonObject(variant.toJsonObject());caseQMetaType::QJsonArray:returnQCborArray::fromJsonArray(variant.toJsonArray());caseQMetaType::QJsonDocument: { QJsonDocument doc = variant.toJsonDocument();if(doc.isArray())returnQCborArray::fromJsonArray(doc.array());returnQCborMap::fromJsonObject(doc.object());}caseQMetaType::QCborValue:return qvariant_cast<QCborValue>(variant);caseQMetaType::QCborArray:return qvariant_cast<QCborArray>(variant);caseQMetaType::QCborMap:return qvariant_cast<QCborMap>(variant);caseQMetaType::QCborSimpleType:return qvariant_cast<QCborSimpleType>(variant);#endifdefault:break;}if(variant.isNull())returnQCborValue(nullptr); QString string = variant.toString();if(string.isNull())returnQCborValue();// undefinedreturn string;}/*! Recursively converts each \l QCborValue in this array using QCborValue::toVariant() and returns the QVariantList composed of the converted items. Conversion to \l QVariant is not completely lossless. Please see the documentation in QCborValue::toVariant() for more information. \sa fromVariantList(), fromStringList(), toJsonArray(), QCborValue::toVariant(), QCborMap::toVariantMap() */ QVariantList QCborArray::toVariantList()const{ QVariantList retval; retval.reserve(size());for(qsizetype i =0; i <size(); ++i) retval.append(d->valueAt(i).toVariant());return retval;}/*! Returns a QCborArray containing all the strings found in the \a list list. \sa fromVariantList(), fromJsonArray() */ QCborArray QCborArray::fromStringList(const QStringList &list){ QCborArray a; a.detach(list.size());for(const QString &s : list) a.d->append(s);return a;}/*! Converts all the items in the \a list to CBOR using QCborValue::fromVariant() and returns the array composed of those elements. Conversion from \l QVariant is not completely lossless. Please see the documentation in QCborValue::fromVariant() for more information. \sa toVariantList(), fromStringList(), fromJsonArray(), QCborMap::fromVariantMap() */ QCborArray QCborArray::fromVariantList(const QVariantList &list){ QCborArray a; a.detach(list.size());for(const QVariant &v : list)appendVariant(a.d.data(), v);return a;}#endif// !QT_NO_VARIANT/*! Converts all JSON items found in the \a array array to CBOR using QCborValue::fromJson(), and returns the CBOR array composed of those elements. This conversion is lossless, as the CBOR type system is a superset of JSON's. Moreover, the array returned by this function can be converted back to the original \a array by using toJsonArray(). \sa toJsonArray(), toVariantList(), QCborValue::fromJsonValue(), QCborMap::fromJsonObject() */ QCborArray QCborArray::fromJsonArray(const QJsonArray &array){ QCborArray result; result.d = array.a;return result;}/*! \overload \since 6.3 */ QCborArray QCborArray::fromJsonArray(QJsonArray &&array) noexcept { QCborArray result; result.d =std::exchange(array.a, {});return result;}#ifndef QT_NO_VARIANT/*! Converts the CBOR values to QVariant using QCborValue::toVariant() and "stringifies" all the CBOR keys in this map, returning the QVariantMap that results from that association list. QVariantMaps have string keys, unlike CBOR, so the conversion of a QCborMap to QVariantMap will imply a step of "stringification" of the key values. See QCborMap::toJsonObject() for details. In addition, the conversion to \l QVariant is not completely lossless. Please see the documentation in QCborValue::toVariant() for more information. \sa fromVariantMap(), toVariantHash(), toJsonObject(), QCborValue::toVariant(), QCborArray::toVariantList() */ QVariantMap QCborMap::toVariantMap()const{ QVariantMap retval;for(qsizetype i =0; i <2*size(); i +=2) retval.insert(makeString(d.data(), i), d->valueAt(i +1).toVariant());return retval;}/*! Converts the CBOR values to QVariant using QCborValue::toVariant() and "stringifies" all the CBOR keys in this map, returning the QVariantHash that results from that association list. QVariantMaps have string keys, unlike CBOR, so the conversion of a QCborMap to QVariantMap will imply a step of "stringification" of the key values. See QCborMap::toJsonObject() for details. In addition, the conversion to \l QVariant is not completely lossless. Please see the documentation in QCborValue::toVariant() for more information. \sa fromVariantHash(), toVariantMap(), toJsonObject(), QCborValue::toVariant(), QCborArray::toVariantList() */ QVariantHash QCborMap::toVariantHash()const{ QVariantHash retval; retval.reserve(size());for(qsizetype i =0; i <2*size(); i +=2) retval.insert(makeString(d.data(), i), d->valueAt(i +1).toVariant());return retval;}/*! Converts all the items in \a map to CBOR using QCborValue::fromVariant() and returns the map composed of those elements. Conversion from \l QVariant is not completely lossless. Please see the documentation in QCborValue::fromVariant() for more information. \sa toVariantMap(), fromVariantHash(), fromJsonObject(), QCborValue::fromVariant() */ QCborMap QCborMap::fromVariantMap(const QVariantMap &map){ QCborMap m; m.detach(map.size()); QCborContainerPrivate *d = m.d.data();auto it = map.begin();auto end = map.end();for( ; it != end; ++it) { d->append(it.key());appendVariant(d, it.value());}return m;}/*! Converts all the items in \a hash to CBOR using QCborValue::fromVariant() and returns the map composed of those elements. Conversion from \l QVariant is not completely lossless. Please see the documentation in QCborValue::fromVariant() for more information. \sa toVariantHash(), fromVariantMap(), fromJsonObject(), QCborValue::fromVariant() */ QCborMap QCborMap::fromVariantHash(const QVariantHash &hash){ QCborMap m; m.detach(hash.size()); QCborContainerPrivate *d = m.d.data();auto it = hash.begin();auto end = hash.end();for( ; it != end; ++it) { d->append(it.key());appendVariant(d, it.value());}return m;}#endif// !QT_NO_VARIANT/*! Converts all JSON items found in the \a obj object to CBOR using QCborValue::fromJson(), and returns the map composed of those elements. This conversion is lossless, as the CBOR type system is a superset of JSON's. Moreover, the map returned by this function can be converted back to the original \a obj by using toJsonObject(). \sa toJsonObject(), toVariantMap(), QCborValue::fromJsonValue(), QCborArray::fromJsonArray() */ QCborMap QCborMap::fromJsonObject(const QJsonObject &obj){ QCborMap result; result.d = obj.o;return result;}/*! \overload \since 6.3 */ QCborMap QCborMap::fromJsonObject(QJsonObject &&obj) noexcept { QCborMap result; result.d =std::exchange(obj.o, {});return result;} QT_END_NAMESPACE 
close