Skip to content

Commit

Permalink
QJsonDocument: use new comparison helper macros
Browse files Browse the repository at this point in the history
Replace public operators operator==(), operator!=() of
QJsonDocument to friend methods comparesEqual().

Use QT_CORE_REMOVED_SINCE and removed_api.cpp to get rid of current
comparison methods and replace them with a friend.

Task-number: QTBUG-120300
Change-Id: I7b61765c34406b7a9fb7dd8b1fc554c87af6a3f3
Reviewed-by: Ivan Solovev <[email protected]>
Reviewed-by: Thiago Macieira <[email protected]>
  • Loading branch information
qt-tatiana committed Mar 22, 2024
1 parent 043ceca commit e2bd247
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
7 changes: 7 additions & 0 deletions src/corelib/compat/removed_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,13 @@ bool QJsonArray::operator!=(const QJsonArray &other) const
return !comparesEqual(*this, other);
}

#include "qjsondocument.h"

bool QJsonDocument::operator==(const QJsonDocument &other) const
{
return comparesEqual(*this, other);
}

#if QT_CONFIG(processenvironment)
#include "qprocess.h" // inlined API

Expand Down
22 changes: 13 additions & 9 deletions src/corelib/serialization/qjsondocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ QT_BEGIN_NAMESPACE
\brief The QJsonDocument class provides a way to read and write JSON documents.
\compares equality
QJsonDocument is a class that wraps a complete JSON document and can read
this document from, and write it to, a UTF-8 encoded text-based
representation.
Expand Down Expand Up @@ -456,20 +458,22 @@ const QJsonValue QJsonDocument::operator[](qsizetype i) const
}

/*!
Returns \c true if the \a other document is equal to this document.
*/
bool QJsonDocument::operator==(const QJsonDocument &other) const
\fn bool QJsonDocument::operator==(const QJsonDocument &lhs, const QJsonDocument &rhs)
Returns \c true if the \a lhs document is equal to \a rhs document, \c false otherwise.
*/
bool comparesEqual(const QJsonDocument &lhs, const QJsonDocument &rhs) noexcept
{
if (d && other.d)
return d->value == other.d->value;
return !d == !other.d;
if (lhs.d && rhs.d)
return lhs.d->value == rhs.d->value;
return !lhs.d == !rhs.d;
}

/*!
\fn bool QJsonDocument::operator!=(const QJsonDocument &other) const
\fn bool QJsonDocument::operator!=(const QJsonDocument &lhs, const QJsonDocument &rhs)
returns \c true if \a other is not equal to this document
*/
Returns \c true if the \a lhs document is not equal to \a rhs document, \c false otherwise.
*/

/*!
returns \c true if this document is null.
Expand Down
10 changes: 7 additions & 3 deletions src/corelib/serialization/qjsondocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#ifndef QJSONDOCUMENT_H
#define QJSONDOCUMENT_H

#include <QtCore/qcompare.h>
#include <QtCore/qjsonvalue.h>
#include <QtCore/qscopedpointer.h>

Expand Down Expand Up @@ -98,16 +99,19 @@ class Q_CORE_EXPORT QJsonDocument
const QJsonValue operator[](QStringView key) const;
const QJsonValue operator[](QLatin1StringView key) const;
const QJsonValue operator[](qsizetype i) const;

#if QT_CORE_REMOVED_SINCE(6, 8)
bool operator==(const QJsonDocument &other) const;
bool operator!=(const QJsonDocument &other) const { return !(*this == other); }

bool operator!=(const QJsonDocument &other) const { return !operator==(other); }
#endif
bool isNull() const;

private:
friend class QJsonValue;
friend class QJsonPrivate::Parser;
friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonDocument &);
friend Q_CORE_EXPORT bool comparesEqual(const QJsonDocument &lhs,
const QJsonDocument &rhs) noexcept;
Q_DECLARE_EQUALITY_COMPARABLE(QJsonDocument)

QJsonDocument(const QCborValue &data);

Expand Down
23 changes: 20 additions & 3 deletions tests/auto/corelib/serialization/json/tst_qtjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ void tst_QtJson::initTestCase()
void tst_QtJson::compareCompiles()
{
QTestPrivate::testEqualityOperatorsCompile<QJsonArray>();
QTestPrivate::testEqualityOperatorsCompile<QJsonDocument>();
QTestPrivate::testEqualityOperatorsCompile<QJsonArray, QJsonValue>();
}

Expand Down Expand Up @@ -381,6 +382,7 @@ void tst_QtJson::testNumbers_2()
QVERIFY2(floatValues[power] == floatValues_1[power], QString("floatValues[%1] != floatValues_1[%1]").arg(power).toLatin1());
}

QT_TEST_EQUALITY_OPS(jDocument1, jDocument2, true);
// The last value is below min denorm and should round to 0, everything else should contain a value
QVERIFY2(floatValues_1[1075] == 0, "Value after min denorm should round to 0");

Expand Down Expand Up @@ -412,6 +414,10 @@ void tst_QtJson::testNumbers_3()

QJsonDocument jDocument2(QJsonDocument::fromJson(ba));

QT_TEST_EQUALITY_OPS(jDocument1, jDocument2, true);
QT_TEST_EQUALITY_OPS(jDocument1, QJsonDocument(), false);
QT_TEST_EQUALITY_OPS(QJsonDocument(), QJsonDocument(), true);

double d1_1(jDocument2.object().value("d1").toDouble());
double d2_1(jDocument2.object().value("d2").toDouble());
QVERIFY(d1_1 != d2_1);
Expand All @@ -429,7 +435,8 @@ void tst_QtJson::testNumbers_4()
array << QJsonValue(-9223372036854775808.0);
array << QJsonValue(+18446744073709551616.0);
array << QJsonValue(-18446744073709551616.0);
const QByteArray json(QJsonDocument(array).toJson());
QJsonDocument doc1 = QJsonDocument(array);
const QByteArray json(doc1.toJson());
const QByteArray expected =
"[\n"
" 1000000000000000,\n"
Expand All @@ -450,7 +457,8 @@ void tst_QtJson::testNumbers_4()
array2 << QJsonValue(Q_INT64_C(-9007199254740992));
array2 << QJsonValue(Q_INT64_C(+9223372036854775807));
array2 << QJsonValue(Q_INT64_C(-9223372036854775807));
const QByteArray json2(QJsonDocument(array2).toJson());
QJsonDocument doc2 = QJsonDocument(array2);
const QByteArray json2(doc2.toJson());
const QByteArray expected2 =
"[\n"
" 1000000000000000,\n"
Expand All @@ -461,6 +469,8 @@ void tst_QtJson::testNumbers_4()
" -9223372036854775807\n"
"]\n";
QCOMPARE(json2, expected2);

QT_TEST_EQUALITY_OPS(doc1, doc2, false);
}

void tst_QtJson::testNumberComparisons()
Expand Down Expand Up @@ -886,6 +896,7 @@ void tst_QtJson::testArrayNestedEmpty()
QJsonValue val = object.value("inner");
QJsonArray value = object.value("inner").toArray();
QVERIFY(QJsonDocument(value).isArray());
QT_TEST_EQUALITY_OPS(QJsonDocument(), QJsonDocument(value), false);
QCOMPARE(value.size(), 0);
QCOMPARE(value, inner);
QCOMPARE(value.size(), 0);
Expand All @@ -903,6 +914,7 @@ void tst_QtJson::testObjectNestedEmpty()
object.insert("inner2", inner2);
QJsonObject value = object.value("inner").toObject();
QVERIFY(QJsonDocument(value).isObject());
QT_TEST_EQUALITY_OPS(QJsonDocument(), QJsonDocument(value), false);
QCOMPARE(value.size(), 0);
QCOMPARE(value, inner);
QCOMPARE(value.size(), 0);
Expand Down Expand Up @@ -1338,6 +1350,8 @@ void tst_QtJson::testDocument()
QCOMPARE(doc5.isObject(), false);
QCOMPARE(doc5.array().size(), 1);
QCOMPARE(doc5.array().at(0), QJsonValue(23));

QT_TEST_EQUALITY_OPS(doc2, doc3, true);
}

void tst_QtJson::nullValues()
Expand Down Expand Up @@ -3374,7 +3388,7 @@ void tst_QtJson::documentFromVariant()

// As JSON arrays they should be equal.
QCOMPARE(da1.array(), da2.array());

QT_TEST_EQUALITY_OPS(da1, da2, true);

QMap <QString, QVariant> map;
map["key"] = string;
Expand All @@ -3390,6 +3404,7 @@ void tst_QtJson::documentFromVariant()

// As JSON objects they should be equal.
QCOMPARE(do1.object(), do2.object());
QT_TEST_EQUALITY_OPS(do1, do2, true);
}

void tst_QtJson::parseErrorOffset_data()
Expand Down Expand Up @@ -3488,6 +3503,7 @@ void tst_QtJson::streamSerializationQJsonDocument()
QDataStream load(buffer);
load >> output;
QCOMPARE(output, document);
QT_TEST_EQUALITY_OPS(output, document, true);
}

void tst_QtJson::streamSerializationQJsonArray_data()
Expand Down Expand Up @@ -3877,6 +3893,7 @@ void tst_QtJson::noLeakOnNameClash()
QVERIFY2(!expected.isNull(), qPrintable(error.errorString()));

QCOMPARE(doc, expected);
QT_TEST_EQUALITY_OPS(doc, expected, true);

// It should not leak.
// In particular it should not forget to deref the container for the inner objects.
Expand Down

0 comments on commit e2bd247

Please sign in to comment.