CBOR: Complete the conversions between CBOR, JSON and Qt meta types
Change-Id: I56b444f9d6274221a3b7fffd150d3130db6ef1a0 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
6c64a9b2d2
commit
fcb0f68e77
@ -170,6 +170,8 @@ static qlonglong qMetaTypeNumber(const QVariant::Private *d)
|
||||
#ifndef QT_BOOTSTRAPPED
|
||||
case QMetaType::QJsonValue:
|
||||
return v_cast<QJsonValue>(d)->toDouble();
|
||||
case QMetaType::QCborValue:
|
||||
return v_cast<QCborValue>(d)->toInteger();
|
||||
#endif
|
||||
}
|
||||
Q_ASSERT(false);
|
||||
@ -208,6 +210,10 @@ static qlonglong qConvertToNumber(const QVariant::Private *d, bool *ok)
|
||||
case QVariant::Bool:
|
||||
return qlonglong(d->data.b);
|
||||
#ifndef QT_BOOTSTRAPPED
|
||||
case QMetaType::QCborValue:
|
||||
if (!v_cast<QCborValue>(d)->isInteger() && !v_cast<QCborValue>(d)->isDouble())
|
||||
break;
|
||||
return qMetaTypeNumber(d);
|
||||
case QMetaType::QJsonValue:
|
||||
if (!v_cast<QJsonValue>(d)->isDouble())
|
||||
break;
|
||||
@ -232,7 +238,7 @@ static qlonglong qConvertToNumber(const QVariant::Private *d, bool *ok)
|
||||
}
|
||||
|
||||
QMetaType typeInfo(d->type);
|
||||
if (typeInfo.flags() & QMetaType::IsEnumeration) {
|
||||
if (typeInfo.flags() & QMetaType::IsEnumeration || d->type == QMetaType::QCborSimpleType) {
|
||||
switch (typeInfo.sizeOf()) {
|
||||
case 1:
|
||||
return d->is_shared ? *reinterpret_cast<signed char *>(d->data.shared->ptr) : d->data.sc;
|
||||
@ -264,6 +270,8 @@ static qreal qConvertToRealNumber(const QVariant::Private *d, bool *ok)
|
||||
case QMetaType::ULong:
|
||||
return qreal(qMetaTypeUNumber(d));
|
||||
#ifndef QT_BOOTSTRAPPED
|
||||
case QMetaType::QCborValue:
|
||||
return v_cast<QCborValue>(d)->toDouble();
|
||||
case QMetaType::QJsonValue:
|
||||
return v_cast<QJsonValue>(d)->toDouble();
|
||||
#endif
|
||||
@ -287,6 +295,12 @@ static qulonglong qConvertToUnsignedNumber(const QVariant::Private *d, bool *ok)
|
||||
case QVariant::Bool:
|
||||
return qulonglong(d->data.b);
|
||||
#ifndef QT_BOOTSTRAPPED
|
||||
case QMetaType::QCborValue:
|
||||
if (v_cast<QCborValue>(d)->isDouble())
|
||||
return qulonglong(qConvertToRealNumber(d, ok));
|
||||
if (!v_cast<QCborValue>(d)->isInteger())
|
||||
return false;
|
||||
return qulonglong(qMetaTypeNumber(d));
|
||||
case QMetaType::QJsonValue:
|
||||
if (!v_cast<QJsonValue>(d)->isDouble())
|
||||
break;
|
||||
@ -394,6 +408,12 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
|
||||
case QVariant::String:
|
||||
*static_cast<QUrl *>(result) = QUrl(*v_cast<QString>(d));
|
||||
break;
|
||||
case QMetaType::QCborValue:
|
||||
if (v_cast<QCborValue>(d)->isUrl()) {
|
||||
*static_cast<QUrl *>(result) = v_cast<QCborValue>(d)->toUrl();
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
@ -479,6 +499,11 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
|
||||
else if (!v_cast<QJsonValue>(d)->isNull())
|
||||
return false;
|
||||
break;
|
||||
case QMetaType::QCborValue:
|
||||
if (v_cast<QCborValue>(d)->isContainer() || v_cast<QCborValue>(d)->isTag())
|
||||
return false;
|
||||
*str = v_cast<QCborValue>(d)->toVariant().toString();
|
||||
break;
|
||||
#endif
|
||||
case QVariant::Uuid:
|
||||
*str = v_cast<QUuid>(d)->toString();
|
||||
@ -624,6 +649,14 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
|
||||
case QVariant::String:
|
||||
*dt = QDateTime::fromString(*v_cast<QString>(d), Qt::ISODate);
|
||||
break;
|
||||
# ifndef QT_BOOTSTRAPPED
|
||||
case QMetaType::QCborValue:
|
||||
if (v_cast<QCborValue>(d)->isDateTime())
|
||||
*dt = v_cast<QCborValue>(d)->toDateTime();
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
# endif
|
||||
#endif
|
||||
case QVariant::Date:
|
||||
*dt = QDateTime(*v_cast<QDate>(d));
|
||||
@ -671,6 +704,14 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
|
||||
case QMetaType::Nullptr:
|
||||
*ba = QByteArray();
|
||||
break;
|
||||
#ifndef QT_BOOTSTRAPPED
|
||||
case QMetaType::QCborValue:
|
||||
if (v_cast<QCborValue>(d)->isByteArray())
|
||||
*ba = v_cast<QCborValue>(d)->toByteArray();
|
||||
else
|
||||
return false;
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
#ifndef QT_NO_QOBJECT
|
||||
{
|
||||
@ -749,6 +790,11 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
|
||||
*b = qMetaTypeUNumber(d) != Q_UINT64_C(0);
|
||||
break;
|
||||
#ifndef QT_BOOTSTRAPPED
|
||||
case QMetaType::QCborValue:
|
||||
*b = v_cast<QCborValue>(d)->toBool();
|
||||
if (!v_cast<QCborValue>(d)->isBool())
|
||||
return false;
|
||||
break;
|
||||
case QMetaType::QJsonValue:
|
||||
*b = v_cast<QJsonValue>(d)->toBool(false);
|
||||
if (!v_cast<QJsonValue>(d)->isBool())
|
||||
@ -792,6 +838,11 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
|
||||
*f = double(qMetaTypeUNumber(d));
|
||||
break;
|
||||
#ifndef QT_BOOTSTRAPPED
|
||||
case QMetaType::QCborValue:
|
||||
*f = v_cast<QCborValue>(d)->toDouble();
|
||||
if (!v_cast<QCborValue>(d)->isDouble())
|
||||
return false;
|
||||
break;
|
||||
case QMetaType::QJsonValue:
|
||||
*f = v_cast<QJsonValue>(d)->toDouble(0.0);
|
||||
if (!v_cast<QJsonValue>(d)->isDouble())
|
||||
@ -835,6 +886,11 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
|
||||
*f = float(qMetaTypeUNumber(d));
|
||||
break;
|
||||
#ifndef QT_BOOTSTRAPPED
|
||||
case QMetaType::QCborValue:
|
||||
*f = v_cast<QCborValue>(d)->toDouble();
|
||||
if (!v_cast<QCborValue>(d)->isDouble())
|
||||
return false;
|
||||
break;
|
||||
case QMetaType::QJsonValue:
|
||||
*f = v_cast<QJsonValue>(d)->toDouble(0.0);
|
||||
if (!v_cast<QJsonValue>(d)->isDouble())
|
||||
@ -859,6 +915,12 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
|
||||
*static_cast<QVariantList *>(result) =
|
||||
*static_cast<QList<QVariant> *>(d->data.shared->ptr);
|
||||
#ifndef QT_BOOTSTRAPPED
|
||||
} else if (d->type == QMetaType::QCborValue) {
|
||||
if (!v_cast<QCborValue>(d)->isArray())
|
||||
return false;
|
||||
*static_cast<QVariantList *>(result) = v_cast<QCborValue>(d)->toArray().toVariantList();
|
||||
} else if (d->type == QMetaType::QCborArray) {
|
||||
*static_cast<QVariantList *>(result) = v_cast<QCborArray>(d)->toVariantList();
|
||||
} else if (d->type == QMetaType::QJsonValue) {
|
||||
if (!v_cast<QJsonValue>(d)->isArray())
|
||||
return false;
|
||||
@ -881,6 +943,12 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
|
||||
for (auto it = hash->begin(); it != end; ++it)
|
||||
map->insertMulti(it.key(), it.value());
|
||||
#ifndef QT_BOOTSTRAPPED
|
||||
} else if (d->type == QMetaType::QCborValue) {
|
||||
if (!v_cast<QCborValue>(d)->isMap())
|
||||
return false;
|
||||
*static_cast<QVariantMap *>(result) = v_cast<QCborValue>(d)->toMap().toVariantMap();
|
||||
} else if (d->type == QMetaType::QCborMap) {
|
||||
*static_cast<QVariantMap *>(result) = v_cast<QCborMap>(d)->toVariantMap();
|
||||
} else if (d->type == QMetaType::QJsonValue) {
|
||||
if (!v_cast<QJsonValue>(d)->isObject())
|
||||
return false;
|
||||
@ -903,6 +971,12 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
|
||||
for (auto it = map->begin(); it != end; ++it)
|
||||
hash->insertMulti(it.key(), it.value());
|
||||
#ifndef QT_BOOTSTRAPPED
|
||||
} else if (d->type == QMetaType::QCborValue) {
|
||||
if (!v_cast<QCborValue>(d)->isMap())
|
||||
return false;
|
||||
*static_cast<QVariantHash *>(result) = v_cast<QCborValue>(d)->toMap().toVariantHash();
|
||||
} else if (d->type == QMetaType::QCborMap) {
|
||||
*static_cast<QVariantHash *>(result) = v_cast<QCborMap>(d)->toVariantHash();
|
||||
} else if (d->type == QMetaType::QJsonValue) {
|
||||
if (!v_cast<QJsonValue>(d)->isObject())
|
||||
return false;
|
||||
@ -953,11 +1027,36 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
|
||||
case QVariant::ByteArray:
|
||||
*static_cast<QUuid *>(result) = QUuid(*v_cast<QByteArray>(d));
|
||||
break;
|
||||
#ifndef QT_BOOTSTRAPPED
|
||||
case QMetaType::QCborValue:
|
||||
if (!v_cast<QCborValue>(d)->isUuid())
|
||||
return false;
|
||||
*static_cast<QUuid *>(result) = v_cast<QCborValue>(d)->toUuid();
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case QMetaType::Nullptr:
|
||||
*static_cast<std::nullptr_t *>(result) = nullptr;
|
||||
if (QMetaType::typeFlags(t) & (QMetaType::PointerToGadget | QMetaType::PointerToQObject)
|
||||
|| d->type == QMetaType::VoidStar) {
|
||||
if (v_cast<const void *>(d) == nullptr)
|
||||
break;
|
||||
}
|
||||
#ifndef QT_BOOTSTRAPPED
|
||||
if (d->type == QMetaType::QCborValue && v_cast<QCborValue>(d)->isNull())
|
||||
break;
|
||||
#endif
|
||||
return false;
|
||||
|
||||
#ifndef QT_BOOTSTRAPPED
|
||||
case QMetaType::QRegularExpression:
|
||||
if (d->type != QMetaType::QCborValue || !v_cast<QCborValue>(d)->isRegularExpression())
|
||||
return false;
|
||||
*static_cast<QRegularExpression *>(result) = v_cast<QCborValue>(d)->toRegularExpression();
|
||||
break;
|
||||
case QMetaType::QJsonValue:
|
||||
switch (d->type) {
|
||||
case QMetaType::Nullptr:
|
||||
@ -1008,6 +1107,15 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
|
||||
*static_cast<QJsonValue *>(result) = doc.isArray() ? QJsonValue(doc.array()) : QJsonValue(doc.object());
|
||||
break;
|
||||
}
|
||||
case QMetaType::QCborValue:
|
||||
*static_cast<QJsonValue *>(result) = v_cast<QCborValue>(d)->toJsonValue();
|
||||
break;
|
||||
case QMetaType::QCborMap:
|
||||
*static_cast<QJsonValue *>(result) = v_cast<QCborMap>(d)->toJsonObject();
|
||||
break;
|
||||
case QMetaType::QCborArray:
|
||||
*static_cast<QJsonValue *>(result) = v_cast<QCborArray>(d)->toJsonArray();
|
||||
break;
|
||||
default:
|
||||
*static_cast<QJsonValue *>(result) = QJsonValue(QJsonValue::Undefined);
|
||||
return false;
|
||||
@ -1031,6 +1139,14 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
|
||||
return false;
|
||||
*static_cast<QJsonArray *>(result) = v_cast<QJsonDocument>(d)->array();
|
||||
break;
|
||||
case QMetaType::QCborValue:
|
||||
if (!v_cast<QCborValue>(d)->isArray())
|
||||
return false;
|
||||
*static_cast<QJsonArray *>(result) = v_cast<QCborValue>(d)->toArray().toJsonArray();
|
||||
break;
|
||||
case QMetaType::QCborArray:
|
||||
*static_cast<QJsonArray *>(result) = v_cast<QCborArray>(d)->toJsonArray();
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
@ -1053,11 +1169,177 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
|
||||
return false;
|
||||
*static_cast<QJsonObject *>(result) = v_cast<QJsonDocument>(d)->object();
|
||||
break;
|
||||
case QMetaType::QCborValue:
|
||||
if (!v_cast<QCborValue>(d)->isMap())
|
||||
return false;
|
||||
*static_cast<QJsonObject *>(result) = v_cast<QCborValue>(d)->toMap().toJsonObject();
|
||||
break;
|
||||
case QMetaType::QCborMap:
|
||||
*static_cast<QJsonObject *>(result) = v_cast<QCborMap>(d)->toJsonObject();
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case QMetaType::QCborSimpleType:
|
||||
if (d->type == QMetaType::QCborValue && v_cast<QCborValue>(d)->isSimpleType()) {
|
||||
*static_cast<QCborSimpleType *>(result) = v_cast<QCborValue>(d)->toSimpleType();
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
case QMetaType::QCborValue:
|
||||
switch (d->type) {
|
||||
case QMetaType::Nullptr:
|
||||
*static_cast<QCborValue *>(result) = QCborValue(QCborValue::Null);
|
||||
break;
|
||||
case QVariant::Bool:
|
||||
*static_cast<QCborValue *>(result) = QCborValue(d->data.b);
|
||||
break;
|
||||
case QMetaType::Int:
|
||||
case QMetaType::UInt:
|
||||
case QMetaType::ULong:
|
||||
case QMetaType::Long:
|
||||
case QMetaType::LongLong:
|
||||
case QMetaType::ULongLong:
|
||||
case QMetaType::UShort:
|
||||
case QMetaType::UChar:
|
||||
case QMetaType::Char:
|
||||
case QMetaType::SChar:
|
||||
case QMetaType::Short:
|
||||
*static_cast<QCborValue *>(result) = QCborValue(qConvertToNumber(d, ok));
|
||||
Q_ASSERT(ok);
|
||||
break;
|
||||
case QMetaType::Double:
|
||||
case QMetaType::Float:
|
||||
*static_cast<QCborValue *>(result) = QCborValue(qConvertToRealNumber(d, ok));
|
||||
Q_ASSERT(ok);
|
||||
break;
|
||||
case QVariant::String:
|
||||
*static_cast<QCborValue *>(result) = *v_cast<QString>(d);
|
||||
break;
|
||||
case QVariant::StringList:
|
||||
*static_cast<QCborValue *>(result) = QCborArray::fromStringList(*v_cast<QStringList>(d));
|
||||
break;
|
||||
case QVariant::ByteArray:
|
||||
*static_cast<QCborValue *>(result) = *v_cast<QByteArray>(d);
|
||||
break;
|
||||
case QVariant::Date:
|
||||
*static_cast<QCborValue *>(result) = QCborValue(QDateTime(*v_cast<QDate>(d)));
|
||||
break;
|
||||
case QVariant::DateTime:
|
||||
*static_cast<QCborValue *>(result) = QCborValue(*v_cast<QDateTime>(d));
|
||||
break;
|
||||
case QVariant::Url:
|
||||
*static_cast<QCborValue *>(result) = QCborValue(*v_cast<QUrl>(d));
|
||||
break;
|
||||
case QVariant::RegularExpression:
|
||||
*static_cast<QCborValue *>(result) = QCborValue(*v_cast<QRegularExpression>(d));
|
||||
break;
|
||||
case QVariant::Uuid:
|
||||
*static_cast<QCborValue *>(result) = QCborValue(*v_cast<QUuid>(d));
|
||||
break;
|
||||
case QVariant::List:
|
||||
*static_cast<QCborValue *>(result) = QCborArray::fromVariantList(*v_cast<QVariantList>(d));
|
||||
break;
|
||||
case QVariant::Map:
|
||||
*static_cast<QCborValue *>(result) = QCborMap::fromVariantMap(*v_cast<QVariantMap>(d));
|
||||
break;
|
||||
case QVariant::Hash:
|
||||
*static_cast<QCborValue *>(result) = QCborMap::fromVariantHash(*v_cast<QVariantHash>(d));
|
||||
break;
|
||||
case QMetaType::QJsonValue:
|
||||
*static_cast<QCborValue *>(result) = QCborValue::fromJsonValue(*v_cast<QJsonValue>(d));
|
||||
break;
|
||||
case QMetaType::QJsonObject:
|
||||
*static_cast<QCborValue *>(result) = QCborMap::fromJsonObject(*v_cast<QJsonObject>(d));
|
||||
break;
|
||||
case QMetaType::QJsonArray:
|
||||
*static_cast<QCborValue *>(result) = QCborArray::fromJsonArray(*v_cast<QJsonArray>(d));
|
||||
break;
|
||||
case QMetaType::QJsonDocument: {
|
||||
QJsonDocument doc = *v_cast<QJsonDocument>(d);
|
||||
if (doc.isArray())
|
||||
*static_cast<QCborValue *>(result) = QCborArray::fromJsonArray(doc.array());
|
||||
else
|
||||
*static_cast<QCborValue *>(result) = QCborMap::fromJsonObject(doc.object());
|
||||
break;
|
||||
}
|
||||
case QMetaType::QCborSimpleType:
|
||||
*static_cast<QCborValue *>(result) = *v_cast<QCborSimpleType>(d);
|
||||
break;
|
||||
case QMetaType::QCborMap:
|
||||
*static_cast<QCborValue *>(result) = *v_cast<QCborMap>(d);
|
||||
break;
|
||||
case QMetaType::QCborArray:
|
||||
*static_cast<QCborValue *>(result) = *v_cast<QCborArray>(d);
|
||||
break;
|
||||
default:
|
||||
*static_cast<QCborValue *>(result) = {};
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case QMetaType::QCborArray:
|
||||
switch (d->type) {
|
||||
case QVariant::StringList:
|
||||
*static_cast<QCborArray *>(result) = QCborArray::fromStringList(*v_cast<QStringList>(d));
|
||||
break;
|
||||
case QVariant::List:
|
||||
*static_cast<QCborArray *>(result) = QCborArray::fromVariantList(*v_cast<QVariantList>(d));
|
||||
break;
|
||||
case QMetaType::QCborValue:
|
||||
if (!v_cast<QCborValue>(d)->isArray())
|
||||
return false;
|
||||
*static_cast<QCborArray *>(result) = v_cast<QCborValue>(d)->toArray();
|
||||
break;
|
||||
case QMetaType::QJsonDocument:
|
||||
if (!v_cast<QJsonDocument>(d)->isArray())
|
||||
return false;
|
||||
*static_cast<QCborArray *>(result) = QCborArray::fromJsonArray(v_cast<QJsonDocument>(d)->array());
|
||||
break;
|
||||
case QMetaType::QJsonValue:
|
||||
if (!v_cast<QJsonValue>(d)->isArray())
|
||||
return false;
|
||||
*static_cast<QCborArray *>(result) = QCborArray::fromJsonArray(v_cast<QJsonValue>(d)->toArray());
|
||||
break;
|
||||
case QMetaType::QJsonArray:
|
||||
*static_cast<QCborArray *>(result) = QCborArray::fromJsonArray(*v_cast<QJsonArray>(d));
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case QMetaType::QCborMap:
|
||||
switch (d->type) {
|
||||
case QVariant::Map:
|
||||
*static_cast<QCborMap *>(result) = QCborMap::fromVariantMap(*v_cast<QVariantMap>(d));
|
||||
break;
|
||||
case QVariant::Hash:
|
||||
*static_cast<QCborMap *>(result) = QCborMap::fromVariantHash(*v_cast<QVariantHash>(d));
|
||||
break;
|
||||
case QMetaType::QCborValue:
|
||||
if (!v_cast<QCborValue>(d)->isMap())
|
||||
return false;
|
||||
*static_cast<QCborMap *>(result) = v_cast<QCborValue>(d)->toMap();
|
||||
break;
|
||||
case QMetaType::QJsonDocument:
|
||||
if (v_cast<QJsonDocument>(d)->isArray())
|
||||
return false;
|
||||
*static_cast<QCborMap *>(result) = QCborMap::fromJsonObject(v_cast<QJsonDocument>(d)->object());
|
||||
break;
|
||||
case QMetaType::QJsonValue:
|
||||
if (!v_cast<QJsonValue>(d)->isObject())
|
||||
return false;
|
||||
*static_cast<QCborMap *>(result) = QCborMap::fromJsonObject(v_cast<QJsonValue>(d)->toObject());
|
||||
break;
|
||||
case QMetaType::QJsonObject:
|
||||
*static_cast<QCborMap *>(result) = QCborMap::fromJsonObject(*v_cast<QJsonObject>(d));
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
|
||||
default:
|
||||
#ifndef QT_NO_QOBJECT
|
||||
if (d->type == QVariant::String || d->type == QVariant::ByteArray) {
|
||||
@ -1084,7 +1366,7 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if (QMetaType::typeFlags(t) & QMetaType::IsEnumeration) {
|
||||
if (QMetaType::typeFlags(t) & QMetaType::IsEnumeration || d->type == QMetaType::QCborSimpleType) {
|
||||
qlonglong value = qConvertToNumber(d, ok);
|
||||
if (*ok) {
|
||||
switch (QMetaType::sizeOf(t)) {
|
||||
@ -3282,15 +3564,66 @@ bool QVariant::canConvert(int targetTypeId) const
|
||||
case QMetaType::QVariantList:
|
||||
case QMetaType::QVariantMap:
|
||||
case QMetaType::QVariantHash:
|
||||
case QMetaType::QCborValue:
|
||||
case QMetaType::QCborArray:
|
||||
case QMetaType::QCborMap:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (currentType == QMetaType::QJsonArray)
|
||||
return targetTypeId == QMetaType::QVariantList;
|
||||
return targetTypeId == QMetaType::QVariantList || targetTypeId == QMetaType::QCborValue
|
||||
|| targetTypeId == QMetaType::QCborArray;
|
||||
if (currentType == QMetaType::QJsonObject)
|
||||
return targetTypeId == QMetaType::QVariantMap || targetTypeId == QMetaType::QVariantHash;
|
||||
return targetTypeId == QMetaType::QVariantMap || targetTypeId == QMetaType::QVariantHash
|
||||
|| targetTypeId == QMetaType::QCborValue || targetTypeId == QMetaType::QCborMap;
|
||||
|
||||
if (currentType == QMetaType::QCborValue || targetTypeId == QMetaType::QCborValue) {
|
||||
switch (currentType == QMetaType::QCborValue ? targetTypeId : currentType) {
|
||||
case QMetaType::UnknownType:
|
||||
case QMetaType::Nullptr:
|
||||
case QMetaType::Bool:
|
||||
case QMetaType::Int:
|
||||
case QMetaType::UInt:
|
||||
case QMetaType::Double:
|
||||
case QMetaType::Float:
|
||||
case QMetaType::ULong:
|
||||
case QMetaType::Long:
|
||||
case QMetaType::LongLong:
|
||||
case QMetaType::ULongLong:
|
||||
case QMetaType::UShort:
|
||||
case QMetaType::UChar:
|
||||
case QMetaType::Char:
|
||||
case QMetaType::SChar:
|
||||
case QMetaType::Short:
|
||||
case QMetaType::QString:
|
||||
case QMetaType::QByteArray:
|
||||
case QMetaType::QDateTime:
|
||||
case QMetaType::QUrl:
|
||||
case QMetaType::QRegularExpression:
|
||||
case QMetaType::QUuid:
|
||||
case QMetaType::QVariantList:
|
||||
case QMetaType::QVariantMap:
|
||||
case QMetaType::QVariantHash:
|
||||
case QMetaType::QJsonValue:
|
||||
case QMetaType::QJsonArray:
|
||||
case QMetaType::QJsonObject:
|
||||
case QMetaType::QJsonDocument:
|
||||
case QMetaType::QCborArray:
|
||||
case QMetaType::QCborMap:
|
||||
case QMetaType::QCborSimpleType:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (currentType == QMetaType::QCborArray)
|
||||
return targetTypeId == QMetaType::QVariantList || targetTypeId == QMetaType::QCborValue
|
||||
|| targetTypeId == QMetaType::QJsonArray;
|
||||
if (currentType == QMetaType::QCborMap)
|
||||
return targetTypeId == QMetaType::QVariantMap || targetTypeId == QMetaType::QVariantHash
|
||||
|| targetTypeId == QMetaType::QCborValue || targetTypeId == QMetaType::QJsonObject;
|
||||
|
||||
// FIXME It should be LastCoreType intead of Uuid
|
||||
if (currentType > int(QMetaType::QUuid) || targetTypeId > int(QMetaType::QUuid)) {
|
||||
@ -3390,7 +3723,7 @@ bool QVariant::convert(int targetTypeId)
|
||||
|
||||
create(targetTypeId, 0);
|
||||
// Fail if the value is not initialized or was forced null by a previous failed convert.
|
||||
if (oldValue.d.is_null)
|
||||
if (oldValue.d.is_null && oldValue.d.type != QMetaType::Nullptr)
|
||||
return false;
|
||||
|
||||
if ((QMetaType::typeFlags(oldValue.userType()) & QMetaType::PointerToQObject) && (QMetaType::typeFlags(targetTypeId) & QMetaType::PointerToQObject)) {
|
||||
|
@ -728,14 +728,19 @@ QCborValue QCborValue::fromVariant(const QVariant &variant)
|
||||
return QCborArray::fromJsonArray(doc.array());
|
||||
return QCborMap::fromJsonObject(doc.object());
|
||||
}
|
||||
case QMetaType::QCborValue:
|
||||
return variant.value<QCborValue>();
|
||||
case QMetaType::QCborArray:
|
||||
return variant.value<QCborArray>();
|
||||
case QMetaType::QCborMap:
|
||||
return variant.value<QCborMap>();
|
||||
case QMetaType::QCborSimpleType:
|
||||
return variant.value<QCborSimpleType>();
|
||||
#endif
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (variant.userType() == qMetaTypeId<QCborSimpleType>())
|
||||
return variant.value<QCborSimpleType>();
|
||||
|
||||
if (variant.isNull())
|
||||
return QCborValue(nullptr);
|
||||
|
||||
|
@ -46,6 +46,11 @@
|
||||
#include <qstringlist.h>
|
||||
#include <qdebug.h>
|
||||
|
||||
#ifndef QT_BOOTSTRAPPED
|
||||
# include <qcborarray.h>
|
||||
# include <qcbormap.h>
|
||||
#endif
|
||||
|
||||
#include "qjson_p.h"
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
@ -423,6 +428,25 @@ QJsonValue &QJsonValue::operator =(const QJsonValue &other)
|
||||
\li QMetaType::QUuid
|
||||
\endlist
|
||||
\li QJsonValue::String. Since Qt 5.11, the resulting string will not include braces
|
||||
\row
|
||||
\li
|
||||
\list
|
||||
\li QMetaType::QCborValue
|
||||
\endlist
|
||||
\li Whichever type QCborValue::toJsonValue() returns.
|
||||
\row
|
||||
\li
|
||||
\list
|
||||
\li QMetaType::QCborArray
|
||||
\endlist
|
||||
\li QJsonValue::Array. See QCborValue::toJsonValue() for conversion restrictions.
|
||||
\row
|
||||
\li
|
||||
\list
|
||||
\li QMetaType::QCborMap
|
||||
\endlist
|
||||
\li QJsonValue::Map. See QCborValue::toJsonValue() for conversion restrictions and the
|
||||
"stringification" of map keys.
|
||||
\endtable
|
||||
|
||||
For all other QVariant types a conversion to a QString will be attempted. If the returned string
|
||||
@ -469,6 +493,12 @@ QJsonValue QJsonValue::fromVariant(const QVariant &variant)
|
||||
QJsonDocument doc = variant.toJsonDocument();
|
||||
return doc.isArray() ? QJsonValue(doc.array()) : QJsonValue(doc.object());
|
||||
}
|
||||
case QMetaType::QCborValue:
|
||||
return variant.value<QCborValue>().toJsonValue();
|
||||
case QMetaType::QCborArray:
|
||||
return variant.value<QCborArray>().toJsonArray();
|
||||
case QMetaType::QCborMap:
|
||||
return variant.value<QCborMap>().toJsonObject();
|
||||
#endif
|
||||
default:
|
||||
break;
|
||||
|
@ -151,6 +151,12 @@ void tst_QCborValue_Json::toVariant()
|
||||
}
|
||||
|
||||
QCOMPARE(v.toVariant(), variant);
|
||||
if (variant.isValid()) {
|
||||
QVariant variant2 = QVariant::fromValue(v);
|
||||
QVERIFY(variant2.canConvert(variant.userType()));
|
||||
QVERIFY(variant2.convert(variant.userType()));
|
||||
QCOMPARE(variant2, variant);
|
||||
}
|
||||
|
||||
// tags get ignored:
|
||||
QCOMPARE(QCborValue(QCborKnownTags::Signature, v).toVariant(), variant);
|
||||
@ -174,6 +180,7 @@ void tst_QCborValue_Json::toJson()
|
||||
QFETCH(QJsonValue, json);
|
||||
|
||||
QCOMPARE(v.toJsonValue(), json);
|
||||
QCOMPARE(QVariant::fromValue(v).toJsonValue(), json);
|
||||
|
||||
// most tags get ignored:
|
||||
QCOMPARE(QCborValue(QCborKnownTags::Signature, v).toJsonValue(), json);
|
||||
@ -216,6 +223,7 @@ void tst_QCborValue_Json::fromVariant()
|
||||
QFETCH(QVariant, variant);
|
||||
|
||||
QCOMPARE(QCborValue::fromVariant(variant), v);
|
||||
QCOMPARE(variant.value<QCborValue>(), v);
|
||||
|
||||
// try arrays
|
||||
QCOMPARE(QCborArray::fromVariantList({variant}), QCborArray{v});
|
||||
@ -260,6 +268,7 @@ void tst_QCborValue_Json::fromJson()
|
||||
QFETCH(QJsonValue, json);
|
||||
|
||||
QCOMPARE(QCborValue::fromJsonValue(json), v);
|
||||
QCOMPARE(QVariant(json).value<QCborValue>(), v);
|
||||
QCOMPARE(QCborArray::fromJsonArray({json}), QCborArray({v}));
|
||||
QCOMPARE(QCborArray::fromJsonArray({json, json}), QCborArray({v, v}));
|
||||
QCOMPARE(QCborMap::fromJsonObject({{"foo", json}}), QCborMap({{"foo", v}}));
|
||||
|
Loading…
Reference in New Issue
Block a user