Commit 4405e749 authored by Volker Krause's avatar Volker Krause
Browse files

Adapt to QVariant::isNull semantics change in Qt6

We rely on a "deep" isNull check on values inside variants in a few places,
to add our own and make that explicit.
parent 84aef37a
Pipeline #182309 passed with stage
in 4 minutes and 11 seconds
......@@ -348,7 +348,7 @@ QVariant JsonLdDocument::fromJsonSingular(const QJsonObject &obj)
return createInstance(normalized.at(0).toObject());
}
static bool valueIsNull(const QVariant &v)
bool JsonLd::valueIsNull(const QVariant &v)
{
if (v.type() == QVariant::Url) {
return !v.toUrl().isValid();
......@@ -356,6 +356,16 @@ static bool valueIsNull(const QVariant &v)
if (v.type() == qMetaTypeId<float>()) {
return std::isnan(v.toFloat());
}
// starting with Qt6, QVariant::isNull is "shallow" and would miss the following as well
if (v.type() == QVariant::String) {
return v.toString().isNull();
}
if (v.type() == QVariant::DateTime) {
return v.toDateTime().isNull();
}
if (v.type() == QVariant::Date) {
return v.toDate().isNull();
}
return v.isNull();
}
......@@ -451,7 +461,7 @@ static QJsonValue toJsonValue(const QVariant &v)
}
const auto value = prop.readOnGadget(v.constData());
if (!valueIsNull(value)) {
if (!JsonLd::valueIsNull(value)) {
const auto jsVal = toJsonValue(value);
if (jsVal.type() != QJsonValue::Null) {
obj.insert(QString::fromUtf8(prop.name()), jsVal);
......@@ -593,7 +603,7 @@ QVariant JsonLdDocument::apply(const QVariant& lhs, const QVariant& rhs)
if ((QMetaType::typeFlags(pv.userType()) & QMetaType::IsGadget) && QMetaType(pv.userType()).metaObject()) {
pv = apply(prop.readOnGadget(lhs.constData()), pv);
}
if (!pv.isNull()) {
if (!JsonLd::valueIsNull(pv)) {
prop.writeOnGadget(res.data(), pv);
}
}
......
......@@ -17,6 +17,17 @@ struct QMetaObject;
namespace KItinerary {
namespace JsonLd {
/** Checks whether @p v holds a null-like value.
* This is similar to QVariant::isNull in Qt5, but differs
* from the "shallow" QVariant::isNull in Qt6 which doesn't
* check the content at all.
*/
KITINERARY_EXPORT bool valueIsNull(const QVariant &v);
}
/** Serialization/deserialization code for JSON-LD data.
* @see https://www.w3.org/TR/json-ld/
*/
......
......@@ -492,14 +492,6 @@ static Ticket mergeValue(const Ticket &lhs, const Ticket &rhs)
return t;
}
static bool checkValueIsNull(const QVariant &v)
{
if (v.type() == qMetaTypeId<float>()) {
return std::isnan(v.toFloat());
}
return v.isNull();
}
QVariant MergeUtil::merge(const QVariant &lhs, const QVariant &rhs)
{
if (rhs.isNull()) {
......@@ -547,7 +539,7 @@ QVariant MergeUtil::merge(const QVariant &lhs, const QVariant &rhs)
rv = merge(prop.readOnGadget(lhs.constData()), rv);
}
if (!checkValueIsNull(rv)) {
if (!JsonLd::valueIsNull(rv)) {
prop.writeOnGadget(res.data(), rv);
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment