Resolve the remaining open ### Qt6 comments of QJsonValue
Changed QJsonValue to use QCborValue for data storage. Removed unused internal methods. Task-number: QTBUG-85700 Change-Id: I784fc7c0c4407f79eb7ca87a1f5116f00c26155d Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
a41c61fb2d
commit
35d90cb898
@ -204,13 +204,12 @@ class Value
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static QCborContainerPrivate *container(const QCborValue &v) { return v.container; }
|
static QCborContainerPrivate *container(const QCborValue &v) { return v.container; }
|
||||||
|
static qint64 valueHelper(const QCborValue &v) { return v.n; }
|
||||||
|
|
||||||
static QJsonValue fromTrustedCbor(const QCborValue &v)
|
static QJsonValue fromTrustedCbor(const QCborValue &v)
|
||||||
{
|
{
|
||||||
QJsonValue result;
|
QJsonValue result;
|
||||||
result.d = v.container;
|
result.value = v;
|
||||||
result.n = v.n;
|
|
||||||
result.t = v.t;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -640,7 +640,7 @@ QCborValue QCborValue::fromJsonValue(const QJsonValue &v)
|
|||||||
case QJsonValue::Bool:
|
case QJsonValue::Bool:
|
||||||
return v.toBool();
|
return v.toBool();
|
||||||
case QJsonValue::Double: {
|
case QJsonValue::Double: {
|
||||||
if (v.t == Integer)
|
if (v.value.t == Integer)
|
||||||
return v.toInteger();
|
return v.toInteger();
|
||||||
return v.toDouble();
|
return v.toDouble();
|
||||||
}
|
}
|
||||||
|
@ -114,26 +114,25 @@ QT_BEGIN_NAMESPACE
|
|||||||
The default is to create a Null value.
|
The default is to create a Null value.
|
||||||
*/
|
*/
|
||||||
QJsonValue::QJsonValue(Type type)
|
QJsonValue::QJsonValue(Type type)
|
||||||
: d(nullptr), t(QCborValue::Undefined)
|
|
||||||
{
|
{
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case Null:
|
case Null:
|
||||||
t = QCborValue::Null;
|
value = QCborValue::Null;
|
||||||
break;
|
break;
|
||||||
case Bool:
|
case Bool:
|
||||||
t = QCborValue::False;
|
value = QCborValue::False;
|
||||||
break;
|
break;
|
||||||
case Double:
|
case Double:
|
||||||
t = QCborValue::Double;
|
value = QCborValue::Double;
|
||||||
break;
|
break;
|
||||||
case String:
|
case String:
|
||||||
t = QCborValue::String;
|
value = QCborValue::String;
|
||||||
break;
|
break;
|
||||||
case Array:
|
case Array:
|
||||||
t = QCborValue::Array;
|
value = QCborValue::Array;
|
||||||
break;
|
break;
|
||||||
case Object:
|
case Object:
|
||||||
t = QCborValue::Map;
|
value = QCborValue::Map;
|
||||||
break;
|
break;
|
||||||
case Undefined:
|
case Undefined:
|
||||||
break;
|
break;
|
||||||
@ -144,24 +143,27 @@ QJsonValue::QJsonValue(Type type)
|
|||||||
Creates a value of type Bool, with value \a b.
|
Creates a value of type Bool, with value \a b.
|
||||||
*/
|
*/
|
||||||
QJsonValue::QJsonValue(bool b)
|
QJsonValue::QJsonValue(bool b)
|
||||||
: t(b ? QCborValue::True : QCborValue::False)
|
: value(b)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline QCborValue doubleValueHelper(double v)
|
||||||
|
{
|
||||||
|
qint64 n = 0;
|
||||||
|
// Convert to integer if the number is an integer and changing wouldn't
|
||||||
|
// introduce additional digit precision not present in the double.
|
||||||
|
if (convertDoubleTo<qint64>(v, &n, false /* allow_precision_upgrade */))
|
||||||
|
return n;
|
||||||
|
else
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
Creates a value of type Double, with value \a v.
|
Creates a value of type Double, with value \a v.
|
||||||
*/
|
*/
|
||||||
QJsonValue::QJsonValue(double v)
|
QJsonValue::QJsonValue(double v)
|
||||||
: d(nullptr)
|
: value(doubleValueHelper(v))
|
||||||
{
|
{
|
||||||
// Convert to integer if the number is an integer and changing wouldn't
|
|
||||||
// introduce additional digit precision not present in the double.
|
|
||||||
if (convertDoubleTo<qint64>(v, &n, false /* allow_precision_upgrade */)) {
|
|
||||||
t = QCborValue::Integer;
|
|
||||||
} else {
|
|
||||||
memcpy(&n, &v, sizeof(n));
|
|
||||||
t = QCborValue::Double;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -169,7 +171,7 @@ QJsonValue::QJsonValue(double v)
|
|||||||
Creates a value of type Double, with value \a v.
|
Creates a value of type Double, with value \a v.
|
||||||
*/
|
*/
|
||||||
QJsonValue::QJsonValue(int v)
|
QJsonValue::QJsonValue(int v)
|
||||||
: n(v), t(QCborValue::Integer)
|
: value(v)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -180,7 +182,7 @@ QJsonValue::QJsonValue(int v)
|
|||||||
If you pass in values outside this range expect a loss of precision to occur.
|
If you pass in values outside this range expect a loss of precision to occur.
|
||||||
*/
|
*/
|
||||||
QJsonValue::QJsonValue(qint64 v)
|
QJsonValue::QJsonValue(qint64 v)
|
||||||
: n(v), t(QCborValue::Integer)
|
: value(v)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -188,7 +190,7 @@ QJsonValue::QJsonValue(qint64 v)
|
|||||||
Creates a value of type String, with value \a s.
|
Creates a value of type String, with value \a s.
|
||||||
*/
|
*/
|
||||||
QJsonValue::QJsonValue(const QString &s)
|
QJsonValue::QJsonValue(const QString &s)
|
||||||
: QJsonValue(QJsonPrivate::Value::fromTrustedCbor(s))
|
: value(s)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -204,17 +206,11 @@ QJsonValue::QJsonValue(const QString &s)
|
|||||||
\since 5.3
|
\since 5.3
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// ### Qt6: remove
|
|
||||||
void QJsonValue::stringDataFromQStringHelper(const QString &string)
|
|
||||||
{
|
|
||||||
*this = QJsonValue(string);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
Creates a value of type String, with value \a s.
|
Creates a value of type String, with value \a s.
|
||||||
*/
|
*/
|
||||||
QJsonValue::QJsonValue(QLatin1String s)
|
QJsonValue::QJsonValue(QLatin1String s)
|
||||||
: QJsonValue(QJsonPrivate::Value::fromTrustedCbor(s))
|
: value(s)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -222,7 +218,7 @@ QJsonValue::QJsonValue(QLatin1String s)
|
|||||||
Creates a value of type Array, with value \a a.
|
Creates a value of type Array, with value \a a.
|
||||||
*/
|
*/
|
||||||
QJsonValue::QJsonValue(const QJsonArray &a)
|
QJsonValue::QJsonValue(const QJsonArray &a)
|
||||||
: n(-1), d(a.a), t(QCborValue::Array)
|
: value(QCborArray::fromJsonArray(a))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -230,7 +226,7 @@ QJsonValue::QJsonValue(const QJsonArray &a)
|
|||||||
Creates a value of type Object, with value \a o.
|
Creates a value of type Object, with value \a o.
|
||||||
*/
|
*/
|
||||||
QJsonValue::QJsonValue(const QJsonObject &o)
|
QJsonValue::QJsonValue(const QJsonObject &o)
|
||||||
: n(-1), d(o.o), t(QCborValue::Map)
|
: value(QCborMap::fromJsonObject(o))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -244,10 +240,8 @@ QJsonValue::~QJsonValue() = default;
|
|||||||
Creates a copy of \a other.
|
Creates a copy of \a other.
|
||||||
*/
|
*/
|
||||||
QJsonValue::QJsonValue(const QJsonValue &other)
|
QJsonValue::QJsonValue(const QJsonValue &other)
|
||||||
|
: value(other.value)
|
||||||
{
|
{
|
||||||
n = other.n;
|
|
||||||
t = other.t;
|
|
||||||
d = other.d;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -260,21 +254,15 @@ QJsonValue &QJsonValue::operator =(const QJsonValue &other)
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
QJsonValue::QJsonValue(QJsonValue &&other) noexcept :
|
QJsonValue::QJsonValue(QJsonValue &&other) noexcept
|
||||||
n(other.n),
|
: value(std::move(other.value))
|
||||||
d(other.d),
|
|
||||||
t(other.t)
|
|
||||||
{
|
{
|
||||||
other.n = 0;
|
other.value = QCborValue(nullptr);
|
||||||
other.d = nullptr;
|
|
||||||
other.t = QCborValue::Null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void QJsonValue::swap(QJsonValue &other) noexcept
|
void QJsonValue::swap(QJsonValue &other) noexcept
|
||||||
{
|
{
|
||||||
qSwap(n, other.n);
|
value.swap(other.value);
|
||||||
qSwap(d, other.d);
|
|
||||||
qSwap(t, other.t);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -553,7 +541,7 @@ QJsonValue QJsonValue::fromVariant(const QVariant &variant)
|
|||||||
*/
|
*/
|
||||||
QVariant QJsonValue::toVariant() const
|
QVariant QJsonValue::toVariant() const
|
||||||
{
|
{
|
||||||
switch (t) {
|
switch (value.type()) {
|
||||||
case QCborValue::True:
|
case QCborValue::True:
|
||||||
return true;
|
return true;
|
||||||
case QCborValue::False:
|
case QCborValue::False:
|
||||||
@ -565,13 +553,9 @@ QVariant QJsonValue::toVariant() const
|
|||||||
case QCborValue::String:
|
case QCborValue::String:
|
||||||
return toString();
|
return toString();
|
||||||
case QCborValue::Array:
|
case QCborValue::Array:
|
||||||
return d ?
|
return toArray().toVariantList();
|
||||||
QJsonArray(d.data()).toVariantList() :
|
|
||||||
QVariantList();
|
|
||||||
case QCborValue::Map:
|
case QCborValue::Map:
|
||||||
return d ?
|
return toObject().toVariantMap();
|
||||||
QJsonObject(d.data()).toVariantMap() :
|
|
||||||
QVariantMap();
|
|
||||||
case QCborValue::Null:
|
case QCborValue::Null:
|
||||||
return QVariant::fromValue(nullptr);
|
return QVariant::fromValue(nullptr);
|
||||||
case QCborValue::Undefined:
|
case QCborValue::Undefined:
|
||||||
@ -605,7 +589,7 @@ QVariant QJsonValue::toVariant() const
|
|||||||
*/
|
*/
|
||||||
QJsonValue::Type QJsonValue::type() const
|
QJsonValue::Type QJsonValue::type() const
|
||||||
{
|
{
|
||||||
switch (t) {
|
switch (value.type()) {
|
||||||
case QCborValue::Null:
|
case QCborValue::Null:
|
||||||
return QJsonValue::Null;
|
return QJsonValue::Null;
|
||||||
case QCborValue::True:
|
case QCborValue::True:
|
||||||
@ -633,7 +617,7 @@ QJsonValue::Type QJsonValue::type() const
|
|||||||
*/
|
*/
|
||||||
bool QJsonValue::toBool(bool defaultValue) const
|
bool QJsonValue::toBool(bool defaultValue) const
|
||||||
{
|
{
|
||||||
switch (t) {
|
switch (value.type()) {
|
||||||
case QCborValue::True:
|
case QCborValue::True:
|
||||||
return true;
|
return true;
|
||||||
case QCborValue::False:
|
case QCborValue::False:
|
||||||
@ -652,17 +636,19 @@ bool QJsonValue::toBool(bool defaultValue) const
|
|||||||
*/
|
*/
|
||||||
int QJsonValue::toInt(int defaultValue) const
|
int QJsonValue::toInt(int defaultValue) const
|
||||||
{
|
{
|
||||||
switch (t) {
|
switch (value.type()) {
|
||||||
case QCborValue::Double: {
|
case QCborValue::Double: {
|
||||||
int dblInt;
|
int dblInt;
|
||||||
if (convertDoubleTo<int>(toDouble(), &dblInt))
|
if (convertDoubleTo<int>(toDouble(), &dblInt))
|
||||||
return dblInt;
|
return dblInt;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case QCborValue::Integer:
|
case QCborValue::Integer: {
|
||||||
|
const auto n = value.toInteger();
|
||||||
if (qint64(int(n)) == n)
|
if (qint64(int(n)) == n)
|
||||||
return int(n);
|
return int(n);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -678,9 +664,9 @@ int QJsonValue::toInt(int defaultValue) const
|
|||||||
*/
|
*/
|
||||||
qint64 QJsonValue::toInteger(qint64 defaultValue) const
|
qint64 QJsonValue::toInteger(qint64 defaultValue) const
|
||||||
{
|
{
|
||||||
switch (t) {
|
switch (value.type()) {
|
||||||
case QCborValue::Integer:
|
case QCborValue::Integer:
|
||||||
return n;
|
return value.toInteger();
|
||||||
case QCborValue::Double: {
|
case QCborValue::Double: {
|
||||||
qint64 dblInt;
|
qint64 dblInt;
|
||||||
if (convertDoubleTo<qint64>(toDouble(), &dblInt))
|
if (convertDoubleTo<qint64>(toDouble(), &dblInt))
|
||||||
@ -700,17 +686,7 @@ qint64 QJsonValue::toInteger(qint64 defaultValue) const
|
|||||||
*/
|
*/
|
||||||
double QJsonValue::toDouble(double defaultValue) const
|
double QJsonValue::toDouble(double defaultValue) const
|
||||||
{
|
{
|
||||||
switch (t) {
|
return value.toDouble(defaultValue);
|
||||||
case QCborValue::Double: {
|
|
||||||
double d;
|
|
||||||
memcpy(&d, &n, sizeof(d));
|
|
||||||
return d;
|
|
||||||
}
|
|
||||||
case QCborValue::Integer:
|
|
||||||
return double(n);
|
|
||||||
default:
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -720,7 +696,7 @@ double QJsonValue::toDouble(double defaultValue) const
|
|||||||
*/
|
*/
|
||||||
QString QJsonValue::toString(const QString &defaultValue) const
|
QString QJsonValue::toString(const QString &defaultValue) const
|
||||||
{
|
{
|
||||||
return (t == QCborValue::String && d) ? d->stringAt(n) : defaultValue;
|
return value.toString(defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -732,7 +708,7 @@ QString QJsonValue::toString(const QString &defaultValue) const
|
|||||||
*/
|
*/
|
||||||
QString QJsonValue::toString() const
|
QString QJsonValue::toString() const
|
||||||
{
|
{
|
||||||
return (t == QCborValue::String && d) ? d->stringAt(n) : QString();
|
return value.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -742,10 +718,12 @@ QString QJsonValue::toString() const
|
|||||||
*/
|
*/
|
||||||
QJsonArray QJsonValue::toArray(const QJsonArray &defaultValue) const
|
QJsonArray QJsonValue::toArray(const QJsonArray &defaultValue) const
|
||||||
{
|
{
|
||||||
if (t != QCborValue::Array || n >= 0 || !d)
|
const auto dd = QJsonPrivate::Value::container(value);
|
||||||
|
const auto n = QJsonPrivate::Value::valueHelper(value);
|
||||||
|
if (value.type() != QCborValue::Array || n >= 0 || !dd)
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
|
|
||||||
return QJsonArray(d.data());
|
return QJsonArray(dd);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -767,10 +745,12 @@ QJsonArray QJsonValue::toArray() const
|
|||||||
*/
|
*/
|
||||||
QJsonObject QJsonValue::toObject(const QJsonObject &defaultValue) const
|
QJsonObject QJsonValue::toObject(const QJsonObject &defaultValue) const
|
||||||
{
|
{
|
||||||
if (t != QCborValue::Map || n >= 0 || !d)
|
const auto dd = QJsonPrivate::Value::container(value);
|
||||||
|
const auto n = QJsonPrivate::Value::valueHelper(value);
|
||||||
|
if (value.type() != QCborValue::Map || n >= 0 || !dd)
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
|
|
||||||
return QJsonObject(d.data());
|
return QJsonObject(dd);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -853,7 +833,7 @@ const QJsonValue QJsonValue::operator[](qsizetype i) const
|
|||||||
*/
|
*/
|
||||||
bool QJsonValue::operator==(const QJsonValue &other) const
|
bool QJsonValue::operator==(const QJsonValue &other) const
|
||||||
{
|
{
|
||||||
if (t != other.t) {
|
if (value.type() != other.value.type()) {
|
||||||
if (isDouble() && other.isDouble()) {
|
if (isDouble() && other.isDouble()) {
|
||||||
// One value Cbor integer, one Cbor double, should interact as doubles.
|
// One value Cbor integer, one Cbor double, should interact as doubles.
|
||||||
return toDouble() == other.toDouble();
|
return toDouble() == other.toDouble();
|
||||||
@ -861,7 +841,7 @@ bool QJsonValue::operator==(const QJsonValue &other) const
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (t) {
|
switch (value.type()) {
|
||||||
case QCborValue::Undefined:
|
case QCborValue::Undefined:
|
||||||
case QCborValue::Null:
|
case QCborValue::Null:
|
||||||
case QCborValue::True:
|
case QCborValue::True:
|
||||||
@ -870,21 +850,14 @@ bool QJsonValue::operator==(const QJsonValue &other) const
|
|||||||
case QCborValue::Double:
|
case QCborValue::Double:
|
||||||
return toDouble() == other.toDouble();
|
return toDouble() == other.toDouble();
|
||||||
case QCborValue::Integer:
|
case QCborValue::Integer:
|
||||||
return n == other.n;
|
return QJsonPrivate::Value::valueHelper(value)
|
||||||
|
== QJsonPrivate::Value::valueHelper(other.value);
|
||||||
case QCborValue::String:
|
case QCborValue::String:
|
||||||
return toString() == other.toString();
|
return toString() == other.toString();
|
||||||
case QCborValue::Array:
|
case QCborValue::Array:
|
||||||
if (!d)
|
return toArray() == other.toArray();
|
||||||
return !other.d || other.d->elements.length() == 0;
|
|
||||||
if (!other.d)
|
|
||||||
return d->elements.length() == 0;
|
|
||||||
return QJsonArray(d.data()) == QJsonArray(other.d.data());
|
|
||||||
case QCborValue::Map:
|
case QCborValue::Map:
|
||||||
if (!d)
|
return toObject() == other.toObject();
|
||||||
return !other.d || other.d->elements.length() == 0;
|
|
||||||
if (!other.d)
|
|
||||||
return d->elements.length() == 0;
|
|
||||||
return QJsonObject(d.data()) == QJsonObject(other.d.data());
|
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -899,15 +872,6 @@ bool QJsonValue::operator!=(const QJsonValue &other) const
|
|||||||
return !(*this == other);
|
return !(*this == other);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
\internal
|
|
||||||
*/
|
|
||||||
void QJsonValue::detach()
|
|
||||||
{
|
|
||||||
d.detach();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\class QJsonValueRef
|
\class QJsonValueRef
|
||||||
\inmodule QtCore
|
\inmodule QtCore
|
||||||
@ -1000,7 +964,7 @@ size_t qHash(const QJsonValue &value, size_t seed)
|
|||||||
QDebug operator<<(QDebug dbg, const QJsonValue &o)
|
QDebug operator<<(QDebug dbg, const QJsonValue &o)
|
||||||
{
|
{
|
||||||
QDebugStateSaver saver(dbg);
|
QDebugStateSaver saver(dbg);
|
||||||
switch (o.t) {
|
switch (o.value.type()) {
|
||||||
case QCborValue::Undefined:
|
case QCborValue::Undefined:
|
||||||
dbg << "QJsonValue(undefined)";
|
dbg << "QJsonValue(undefined)";
|
||||||
break;
|
break;
|
||||||
|
@ -141,15 +141,7 @@ private:
|
|||||||
friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &);
|
friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &);
|
||||||
friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonValue &);
|
friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonValue &);
|
||||||
|
|
||||||
// ### Qt6: Remove this.
|
QCborValue value;
|
||||||
void stringDataFromQStringHelper(const QString &string);
|
|
||||||
|
|
||||||
void detach();
|
|
||||||
|
|
||||||
// ### Qt6: change to an actual QCborValue
|
|
||||||
qint64 n = 0;
|
|
||||||
QExplicitlySharedDataPointer<QCborContainerPrivate> d; // needed for Objects, Arrays, Strings
|
|
||||||
QCborValue::Type t;
|
|
||||||
|
|
||||||
// Assert binary compatibility with pre-5.15 QJsonValue
|
// Assert binary compatibility with pre-5.15 QJsonValue
|
||||||
static_assert(sizeof(QExplicitlySharedDataPointer<QCborContainerPrivate>) == sizeof(void *));
|
static_assert(sizeof(QExplicitlySharedDataPointer<QCborContainerPrivate>) == sizeof(void *));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user