summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@nokia.com>2011-11-23 16:38:26 +0100
committerKnoll Lars <lars.knoll@nokia.com>2011-11-23 16:40:10 +0100
commitbf402bfaedaffae08fb1d9ad5af91306b89b1daa (patch)
treed8d1be6e3de980713cef959b1e61dea2b0a6d3cc
parentac3564d4d014705f34f5fa533ed2eac500c4d3b8 (diff)
Properly implement comparison operators
Add correct comparison operators for JsonValue, JsonObject and JsonArray. This makes 8 out of 9 autotests pass. Change-Id: I14289f6e8e684b21280b687a3eab12e585cfca60 Reviewed-by: Knoll Lars <lars.knoll@nokia.com>
-rw-r--r--src/qbinaryjsonarray.cpp13
-rw-r--r--src/qbinaryjsonobject.cpp17
-rw-r--r--src/qbinaryjsonobject.h2
-rw-r--r--src/qbinaryjsonvalue.cpp21
-rw-r--r--tests/auto/tst_qtbinaryjson.cpp10
5 files changed, 52 insertions, 11 deletions
diff --git a/src/qbinaryjsonarray.cpp b/src/qbinaryjsonarray.cpp
index e3eb347..6b3f9ae 100644
--- a/src/qbinaryjsonarray.cpp
+++ b/src/qbinaryjsonarray.cpp
@@ -142,7 +142,18 @@ JsonValue JsonArray::operator[](int i) const
bool JsonArray::operator==(const JsonArray &other) const
{
- return d == other.d && a == other.a;
+ if (a == other.a)
+ return true;
+
+ if (a->length != other.a->length)
+ return false;
+
+
+ for (int i = 0; i < a->length; ++i) {
+ if (d->toValue(a->at(i)) != other.d->toValue(other.a->at(i)))
+ return false;
+ }
+ return true;
}
bool JsonArray::operator!=(const JsonArray &other) const
diff --git a/src/qbinaryjsonobject.cpp b/src/qbinaryjsonobject.cpp
index 3fa0c71..e57c6af 100644
--- a/src/qbinaryjsonobject.cpp
+++ b/src/qbinaryjsonobject.cpp
@@ -85,7 +85,7 @@ int JsonObject::numKeys()
return o->length;
}
-JsonValue JsonObject::value(const QString &key)
+JsonValue JsonObject::value(const QString &key) const
{
if (!d)
return JsonValue();
@@ -167,7 +167,20 @@ QByteArray JsonObject::data() const
bool JsonObject::operator==(const JsonObject &other) const
{
- return d == other.d && o == other.o;
+ if (o == other.o)
+ return true;
+
+ if (o->length != other.o->length)
+ return false;
+
+ for (uint i = 0; i < o->length; ++i) {
+ Entry *e = o->entryAt(i);
+ JsonValue v = d->toValue(e->value());
+ if (other.value(e->key()) != v)
+ return false;
+ }
+
+ return true;
}
bool JsonObject::operator!=(const JsonObject &other) const
diff --git a/src/qbinaryjsonobject.h b/src/qbinaryjsonobject.h
index e34ceb4..f1378e9 100644
--- a/src/qbinaryjsonobject.h
+++ b/src/qbinaryjsonobject.h
@@ -23,7 +23,7 @@ public:
// QString keyAt(int);
// QSonValue valueAt(int)
- JsonValue value(const QString & key);
+ JsonValue value(const QString & key) const;
void insert(const QString &key, const JsonValue &value);
void remove(const QString &key);
diff --git a/src/qbinaryjsonvalue.cpp b/src/qbinaryjsonvalue.cpp
index 2c7d1f3..dc48707 100644
--- a/src/qbinaryjsonvalue.cpp
+++ b/src/qbinaryjsonvalue.cpp
@@ -172,7 +172,26 @@ JsonObject JsonValue::toObject() const
bool JsonValue::operator==(const JsonValue &other) const
{
- return d == other.d && v == other.v;
+ if (v == other.v)
+ return true;
+
+ if (v->type != other.v->type)
+ return false;
+
+ switch ((ValueType)v->type) {
+ case NullValue:
+ return true;
+ case NumberValue:
+ return v->toNumber() == other.v->toNumber();
+ case BooleanValue:
+ return v->toBoolean() == other.v->toBoolean();
+ case StringValue:
+ return v->shallowString() == other.v->shallowString();
+ case ArrayValue:
+ return d->toArray(v->array()) == other.d->toArray(other.v->array());
+ case ObjectValue:
+ return d->toObject(v->object()) == other.d->toObject(other.v->object());
+ }
}
bool JsonValue::operator!=(const JsonValue &other) const
diff --git a/tests/auto/tst_qtbinaryjson.cpp b/tests/auto/tst_qtbinaryjson.cpp
index ab104af..b9f43f4 100644
--- a/tests/auto/tst_qtbinaryjson.cpp
+++ b/tests/auto/tst_qtbinaryjson.cpp
@@ -132,11 +132,10 @@ void TestQtBinaryJson::testObjectSimple()
QVERIFY2(keys.contains("boolean"), "key boolean not found");
// if we put a JsonValue into the JsonObject and retreive
- // it, it should be identical (non-detached).
+ // it, it should be identical.
JsonValue value("foo");
object.insert("value", value);
- // not true, we detach when inserting
-// QCOMPARE(object.value("value"), value);
+ QCOMPARE(object.value("value"), value);
int size = object.numKeys();
object.remove("boolean");
@@ -165,11 +164,10 @@ void TestQtBinaryJson::testArraySimple()
QCOMPARE(array.size(), 3);
// if we put a JsonValue into the JsonArray and retreive
- // it, it should be identical (non-detached).
+ // it, it should be identical.
JsonValue value("foo");
array.append(value);
- // This doesn't work as the inserting creates a copy
-// QCOMPARE(array.at(3), value);
+ QCOMPARE(array.at(3), value);
int size = array.size();
array.removeAt(2);