summaryrefslogtreecommitdiffstats
path: root/src/corelib/serialization/qjsoncbor.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/serialization/qjsoncbor.cpp')
-rw-r--r--src/corelib/serialization/qjsoncbor.cpp187
1 files changed, 113 insertions, 74 deletions
diff --git a/src/corelib/serialization/qjsoncbor.cpp b/src/corelib/serialization/qjsoncbor.cpp
index 8bb04fa3bf..da07eca8a7 100644
--- a/src/corelib/serialization/qjsoncbor.cpp
+++ b/src/corelib/serialization/qjsoncbor.cpp
@@ -1,41 +1,5 @@
-/****************************************************************************
-**
-** Copyright (C) 2020 Intel Corporation.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtCore module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// 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"
@@ -48,6 +12,9 @@
#include "qjsondocument.h"
#include "qjson_p.h"
+#include <qmap.h>
+#include <qhash.h>
+
#include <private/qnumeric_p.h>
#include <quuid.h>
@@ -55,6 +22,8 @@ QT_BEGIN_NAMESPACE
using namespace QtCbor;
+enum class ConversionMode { FromRaw, FromVariantToJson };
+
static QJsonValue fpToJson(double v)
{
return qt_is_finite(v) ? QJsonValue(v) : QJsonValue();
@@ -89,13 +58,13 @@ static QString encodeByteArray(const QCborContainerPrivate *d, qsizetype idx, QC
return QString::fromLatin1(data, data.size());
}
-static QString makeString(const QCborContainerPrivate *d, qsizetype idx);
+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);
- const ByteData *b = d->byteData(e);
switch (tag) {
case qint64(QCborKnownTags::DateTimeString):
@@ -112,8 +81,12 @@ static QString maybeEncodeTag(const QCborContainerPrivate *d)
break;
case qint64(QCborKnownTags::Uuid):
- if (e.type == QCborValue::ByteArray && b->len == sizeof(QUuid))
+#ifndef QT_BOOTSTRAPPED
+ if (const ByteData *b = d->byteData(e); e.type == QCborValue::ByteArray && b
+ && b->len == sizeof(QUuid))
return QUuid::fromRfc4122(b->asByteArrayView()).toString(QUuid::WithoutBraces);
+#endif
+ break;
}
// don't know what to do, bail out
@@ -134,7 +107,8 @@ static QString encodeTag(const QCborContainerPrivate *d)
return s;
}
-static Q_NEVER_INLINE QString makeString(const QCborContainerPrivate *d, qsizetype idx)
+static Q_NEVER_INLINE QString makeString(const QCborContainerPrivate *d, qsizetype idx,
+ ConversionMode mode)
{
const auto &e = d->elements.at(idx);
@@ -146,7 +120,9 @@ static Q_NEVER_INLINE QString makeString(const QCborContainerPrivate *d, qsizety
return QString::number(e.fpvalue());
case QCborValue::ByteArray:
- return encodeByteArray(d, idx, QCborTag(QCborKnownTags::ExpectedBase64url));
+ return mode == ConversionMode::FromVariantToJson
+ ? d->stringAt(idx)
+ : encodeByteArray(d, idx, QCborTag(QCborKnownTags::ExpectedBase64url));
case QCborValue::String:
return d->stringAt(idx);
@@ -190,19 +166,23 @@ static Q_NEVER_INLINE QString makeString(const QCborContainerPrivate *d, qsizety
return simpleTypeString(e.type);
}
-QJsonValue qt_convertToJson(QCborContainerPrivate *d, qsizetype idx);
+QJsonValue qt_convertToJson(QCborContainerPrivate *d, qsizetype idx,
+ ConversionMode mode = ConversionMode::FromRaw);
static QJsonValue convertExtendedTypeToJson(QCborContainerPrivate *d)
{
-#ifndef QT_BUILD_QMAKE
qint64 tag = d->elements.at(0).value;
switch (tag) {
case qint64(QCborKnownTags::Url):
- // use the fullly-encoded URL form
+#ifdef QT_BOOTSTRAPPED
+ break;
+#else
+ // use the fully-encoded URL form
if (d->elements.at(1).type == QCborValue::String)
return QUrl::fromEncoded(d->byteData(1)->asByteArrayView()).toString(QUrl::FullyEncoded);
Q_FALLTHROUGH();
+#endif
case qint64(QCborKnownTags::DateTimeString):
case qint64(QCborKnownTags::ExpectedBase64url):
@@ -215,42 +195,43 @@ static QJsonValue convertExtendedTypeToJson(QCborContainerPrivate *d)
return s;
}
}
-#endif
// for all other tags, ignore it and return the converted tagged item
return qt_convertToJson(d, 1);
}
// We need to do this because sub-objects may need conversion.
-static QJsonArray convertToJsonArray(QCborContainerPrivate *d)
+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));
+ 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)
+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));
+ o.insert(makeString(d, idx), qt_convertToJson(d, idx + 1, mode));
}
return o;
}
-QJsonValue qt_convertToJson(QCborContainerPrivate *d, qsizetype idx)
+QJsonValue qt_convertToJson(QCborContainerPrivate *d, qsizetype idx, ConversionMode mode)
{
// encoding the container itself
if (idx == -QCborValue::Array)
- return convertToJsonArray(d);
+ return convertToJsonArray(d, mode);
if (idx == -QCborValue::Map)
- return convertToJsonObject(d);
+ return convertToJsonObject(d, mode);
if (idx < 0) {
// tag-like type
if (!d || d->elements.size() != 2)
@@ -264,6 +245,15 @@ QJsonValue qt_convertToJson(QCborContainerPrivate *d, qsizetype idx)
case QCborValue::Integer:
return QJsonValue(e.value);
case QCborValue::ByteArray:
+ if (mode == ConversionMode::FromVariantToJson) {
+ const auto value = makeString(d, idx, mode);
+ return value.isEmpty() ? QJsonValue() : QJsonPrivate::Value::fromTrustedCbor(value);
+ }
+ break;
+ case QCborValue::RegularExpression:
+ if (mode == ConversionMode::FromVariantToJson)
+ return QJsonValue();
+ break;
case QCborValue::String:
case QCborValue::SimpleType:
// make string
@@ -274,10 +264,10 @@ QJsonValue qt_convertToJson(QCborContainerPrivate *d, qsizetype idx)
case QCborValue::Tag:
case QCborValue::DateTime:
case QCborValue::Url:
- case QCborValue::RegularExpression:
case QCborValue::Uuid:
// recurse
- return qt_convertToJson(e.flags & Element::IsContainer ? e.container : nullptr, -e.type);
+ return qt_convertToJson(e.flags & Element::IsContainer ? e.container : nullptr, -e.type,
+ mode);
case QCborValue::Null:
case QCborValue::Undefined:
@@ -294,7 +284,7 @@ QJsonValue qt_convertToJson(QCborContainerPrivate *d, qsizetype idx)
return fpToJson(e.fpvalue());
}
- return QJsonPrivate::Value::fromTrustedCbor(makeString(d, idx));
+ return QJsonPrivate::Value::fromTrustedCbor(makeString(d, idx, mode));
}
/*!
@@ -408,10 +398,12 @@ QJsonValue QCborValue::toJsonValue() const
return QJsonPrivate::Value::fromTrustedCbor(simpleTypeString(type()));
}
+#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED)
QJsonValue QCborValueRef::toJsonValue() const
{
return qt_convertToJson(d, i);
}
+#endif
/*!
Recursively converts every \l QCborValue element in this array to JSON
@@ -429,6 +421,14 @@ QJsonArray QCborArray::toJsonArray() const
return convertToJsonArray(d.data());
}
+#ifndef QT_NO_VARIANT
+QJsonArray QJsonPrivate::Variant::toJsonArray(const QVariantList &list)
+{
+ const auto cborArray = QCborArray::fromVariantList(list);
+ return convertToJsonArray(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
@@ -471,6 +471,13 @@ QJsonObject QCborMap::toJsonObject() const
return convertToJsonObject(d.data());
}
+#ifndef QT_NO_VARIANT
+QJsonObject QJsonPrivate::Variant::toJsonObject(const QVariantMap &map)
+{
+ const auto cborMap = QCborMap::fromVariantMap(map);
+ return convertToJsonObject(cborMap.d.data(), ConversionMode::FromVariantToJson);
+}
+
/*!
Converts this value to a native Qt type and returns the corresponding QVariant.
@@ -552,15 +559,15 @@ QVariant QCborValue::toVariant() const
#ifndef QT_BOOTSTRAPPED
case Url:
return toUrl();
-#endif
-#if QT_CONFIG(regularexpression)
+# if QT_CONFIG(regularexpression)
case RegularExpression:
return toRegularExpression();
-#endif
+# endif
case Uuid:
return toUuid();
+#endif
case Invalid:
return QVariant();
@@ -572,9 +579,9 @@ QVariant QCborValue::toVariant() const
if (isSimpleType())
return QVariant::fromValue(toSimpleType());
- Q_UNREACHABLE();
- return QVariant();
+ Q_UNREACHABLE_RETURN(QVariant());
}
+#endif // !QT_NO_VARIANT
/*!
Converts the JSON value contained in \a v into its corresponding CBOR value
@@ -610,7 +617,7 @@ QCborValue QCborValue::fromJsonValue(const QJsonValue &v)
case QJsonValue::Bool:
return v.toBool();
case QJsonValue::Double: {
- if (v.t == Integer)
+ if (v.value.t == Integer)
return v.toInteger();
return v.toDouble();
}
@@ -628,11 +635,12 @@ QCborValue QCborValue::fromJsonValue(const QJsonValue &v)
return QCborValue();
}
+#ifndef QT_NO_VARIANT
static void appendVariant(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.userType();
+ int type = variant.metaType().id();
if (type == QMetaType::QString) {
d->append(variant.toString());
} else if (type == QMetaType::QByteArray) {
@@ -679,21 +687,26 @@ static void appendVariant(QCborContainerPrivate *d, const QVariant &variant)
\row \li \l QUuid \li Uuid
\endtable
- For any other types, this function will return Null if the QVariant itself
- is null, and otherwise will try to convert to string using
- QVariant::toString(). If the conversion to string fails, this function
- returns Undefined.
+ 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. QCborValue may be extended in the future to support
- more types, which will result in a change in how this function performs
- conversions.
+ 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()
+ \sa toVariant(), fromJsonValue(), QCborArray::toVariantList(), QCborMap::toVariantMap(), QJsonValue::fromVariant()
*/
QCborValue QCborValue::fromVariant(const QVariant &variant)
{
- switch (variant.userType()) {
+ switch (variant.metaType().id()) {
case QMetaType::UnknownType:
return {};
case QMetaType::Nullptr:
@@ -724,9 +737,9 @@ QCborValue QCborValue::fromVariant(const QVariant &variant)
#ifndef QT_BOOTSTRAPPED
case QMetaType::QUrl:
return QCborValue(variant.toUrl());
-#endif
case QMetaType::QUuid:
return QCborValue(variant.toUuid());
+#endif
case QMetaType::QVariantList:
return QCborArray::fromVariantList(variant.toList());
case QMetaType::QVariantMap:
@@ -823,6 +836,7 @@ QCborArray QCborArray::fromVariantList(const QVariantList &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
@@ -843,6 +857,19 @@ QCborArray QCborArray::fromJsonArray(const QJsonArray &array)
}
/*!
+ \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.
@@ -938,6 +965,7 @@ QCborMap QCborMap::fromVariantHash(const QVariantHash &hash)
}
return m;
}
+#endif // !QT_NO_VARIANT
/*!
Converts all JSON items found in the \a obj object to CBOR using
@@ -956,4 +984,15 @@ QCborMap QCborMap::fromJsonObject(const QJsonObject &obj)
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