[Interest] QJsonObject::operator== bug

Carel Combrink carel.combrink at gmail.com
Thu Nov 3 18:09:27 CET 2016


Hi,

QJsonObject::operator==() does not compare QJsonObjects correctly.
I am using Qt 5.6.2 on Ubuntu 16.06
(I can't test on other versions at this moment)

This is probably related to QTBUG-56843
<https://bugreports.qt.io/browse/QTBUG-56843> but that bug states "Complex
QJsonObject", but the following shows it is an issue with basic
QJsonObjects as well:

Code:


#include <QJsonDocument>

#include <QJsonObject>

#include <QDebug>

int main(int argc, char *argv[])

{

    QJsonObject obj1;

    obj1["enabled"] = true;

    obj1["radius" ] = 2;

    obj1["width"  ] = 5;

    QJsonObject obj2; // Same as obj1 but width and radius swapped but
still same values.

    obj2["enabled"] = true;

    obj2["width"  ] = 5;

    obj2["radius" ] = 2;

    QJsonObject obj3; // Same order as obj2

    obj3["enabled"] = true;

    obj3["width"  ] = 5;

    obj3["radius" ] = 2;


    qDebug() << "obj1: " << obj1;

    qDebug() << "obj2: " << obj2;

    qDebug() << "obj3: " << obj3;

    qDebug() << "SAME QJsonObjects (1==2): " << (obj1 == obj2);

    qDebug() << "SAME QJsonObjects (2==3): " << (obj2 == obj3);

    qDebug() << "SAME QJsonObjects (1==3): " << (obj1 == obj3);

    qDebug() << "SAME QJsonDocument (1==2): " << (QJsonDocument(obj1)
== QJsonDocument(obj2));

    qDebug() << "SAME QJsonDocument (2==3): " << (QJsonDocument(obj2)
== QJsonDocument(obj3));

    qDebug() << "SAME QJsonDocument (1==3): " << (QJsonDocument(obj1)
== QJsonDocument(obj3));

}


The output that I get is:

obj1: QJsonObject({"enabled":true,"radius":2,"width":5})

obj2: QJsonObject({"enabled":true,"radius":2,"width":5})

obj3: QJsonObject({"enabled":true,"radius":2,"width":5})

SAME QJsonObjects (1==2): *false*

SAME QJsonObjects (2==3): true

SAME QJsonObjects (1==3): *false*

SAME QJsonDocument (1==2): true

SAME QJsonDocument (2==3): true

SAME QJsonDocument (1==3): true

I expect:

obj1: QJsonObject({"enabled":true,"radius":2,"width":5})

obj2: QJsonObject({"enabled":true,"radius":2,"width":5})

obj3: QJsonObject({"enabled":true,"radius":2,"width":5})

SAME QJsonObjects (1==2): *true*

SAME QJsonObjects (2==3): true

SAME QJsonObjects (1==3): *true*

SAME QJsonDocument (1==2): true

SAME QJsonDocument (2==3): true

SAME QJsonDocument (1==3): true


As one can see, the order of how the elements are defined are different for
obj1 and ob2, but the same for obj2 and obj3. The values are always the
same for the given field. Obj1 is not considered equal to obj2 but obj2 and
obj3 are considered equal.
The QJsonDocuments are considered equal for all 3.

If this bug is not the same as the mentioned bug, please let me know and I
will create a new bug report.
If this is intended behaviour that 2 QJsonObjects are not equal if the
order of the elements differ, then this is a documentation issue that
should be made clear.

Regards,
Carel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20161103/66af8dbf/attachment.html>


More information about the Interest mailing list