summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@nokia.com>2011-11-24 14:26:41 +0100
committerKnoll Lars <lars.knoll@nokia.com>2011-11-25 12:53:56 +0100
commit72b3fda971684ce59c6b02fd2eb61161411584d2 (patch)
tree714be5aa664ee277effdbb8242427ec0f43e1733
parentb28e7ecec0ec1619f8004b34475fd862dc73be39 (diff)
Convert from Variants and VariantMaps
Added conversions from * VariantMap to a JsonObject. * Variant to JsonValue * VariantList and StringList to JsonArray Also added checks for null d pointer quite a few places so that null values work. Change-Id: I689bda259817148a53fc9f08c2537ab55d3c906b Reviewed-by: Knoll Lars <lars.knoll@nokia.com>
-rw-r--r--src/qbinaryjsonarray.cpp57
-rw-r--r--src/qbinaryjsonarray.h5
-rw-r--r--src/qbinaryjsonobject.cpp18
-rw-r--r--src/qbinaryjsonobject.h4
-rw-r--r--src/qbinaryjsonvalue.cpp42
-rw-r--r--src/qbinaryjsonvalue.h4
-rw-r--r--tests/auto/tst_qtbinaryjson.cpp53
7 files changed, 158 insertions, 25 deletions
diff --git a/src/qbinaryjsonarray.cpp b/src/qbinaryjsonarray.cpp
index 6b3f9ae..2bd7237 100644
--- a/src/qbinaryjsonarray.cpp
+++ b/src/qbinaryjsonarray.cpp
@@ -2,6 +2,9 @@
#include <qbinaryjsonvalue.h>
#include <qbinaryjsonarray.h>
#include <qbinaryjson_p.h>
+#include <qbinaryjsonvalue.h>
+
+#include <qstringlist.h>
using namespace QtBinaryJson;
@@ -44,6 +47,22 @@ JsonArray &JsonArray::operator =(const JsonArray &other)
return *this;
}
+JsonArray JsonArray::fromStringList(const QStringList &list)
+{
+ JsonArray array;
+ for (QStringList::const_iterator it = list.constBegin(); it != list.constEnd(); ++it)
+ array.append(JsonValue(*it));
+ return array;
+}
+
+JsonArray JsonArray::fromVariantList(const QVariantList &list)
+{
+ JsonArray array;
+ for (QVariantList::const_iterator it = list.constBegin(); it != list.constEnd(); ++it)
+ array.append(JsonValue::fromVariant(*it));
+ return array;
+}
+
int JsonArray::size() const
{
@@ -55,42 +74,30 @@ int JsonArray::size() const
JsonValue JsonArray::at(int i) const
{
- if (!d)
+ if (!a || i < 0 || i >= (int)a->length)
return JsonValue();
- Q_ASSERT(i >= 0 && i < (int)a->length);
-
return d->toValue(a->at(i));
}
JsonValue JsonArray::first() const
{
- Q_ASSERT(a && a->length);
return at(0);
}
JsonValue JsonArray::last() const
{
- Q_ASSERT(a && a->length);
return at(a->length - 1);
}
void JsonArray::append(const JsonValue &value)
{
- Value *v = value.v;
- detach(v->size + sizeof(offset)); // offset for the new index entry
-
- if (!a->length)
- a->tableOffset = sizeof(Array);
-
- int pos = a->length;
- a->reserveSpace(v->size, pos, 1);
- memcpy(a->at(pos), v, v->size);
+ insert(a ? a->length : 0, value);
}
void JsonArray::removeAt(int i)
{
- if (i < 0 || i >= (int)a->length)
+ if (!a || i < 0 || i >= (int)a->length)
return;
detach();
@@ -100,7 +107,7 @@ void JsonArray::removeAt(int i)
JsonValue JsonArray::takeAt(int i)
{
- if (i < 0 || i >= (int)a->length)
+ if (!a || i < 0 || i >= (int)a->length)
return JsonValue();
detach();
@@ -115,17 +122,25 @@ JsonValue JsonArray::takeAt(int i)
void JsonArray::insert(int i, const JsonValue &value)
{
- Q_ASSERT (i < 0 || i > (int)a->length);
+ Q_ASSERT (i >= 0 && i <= (int)(a ? a->length : 0));
Value *v = value.v;
+ int valueSize = v ? v->size : sizeof(Value);
- detach(v->size + sizeof(offset)); // offset for the new index entry
+ detach(valueSize + sizeof(offset)); // offset for the new index entry
if (!a->length)
a->tableOffset = sizeof(Array);
- a->reserveSpace(v->size, i, 1);
- memcpy(a->at(i), v, v->size);
+ a->reserveSpace(valueSize, i, 1);
+ if (v) {
+ memcpy(a->at(i), v, v->size);
+ } else {
+ Value *n = a->at(i);
+ n->type = NullValue;
+ n->val = 0;
+ n->size = sizeof(Value);
+ }
}
//JsonValue &JsonArray::operator[](int i)
@@ -145,7 +160,7 @@ bool JsonArray::operator==(const JsonArray &other) const
if (a == other.a)
return true;
- if (a->length != other.a->length)
+ if (!a || !other.a || a->length != other.a->length)
return false;
diff --git a/src/qbinaryjsonarray.h b/src/qbinaryjsonarray.h
index 0540139..3c1342c 100644
--- a/src/qbinaryjsonarray.h
+++ b/src/qbinaryjsonarray.h
@@ -2,6 +2,8 @@
#define QBINARYJSONARRAY_H
#include <qbinaryjsonglobal.h>
+#include <qvariant.h>
+class QStringList;
namespace QtBinaryJson
{
@@ -15,6 +17,9 @@ public:
JsonArray(const JsonArray &other);
JsonArray &operator =(const JsonArray &other);
+ static JsonArray fromStringList(const QStringList &list);
+ static JsonArray fromVariantList(const QVariantList &list);
+
int size() const;
JsonValue at(int i) const;
JsonValue first() const;
diff --git a/src/qbinaryjsonobject.cpp b/src/qbinaryjsonobject.cpp
index e57c6af..93bd237 100644
--- a/src/qbinaryjsonobject.cpp
+++ b/src/qbinaryjsonobject.cpp
@@ -62,6 +62,22 @@ JsonObject JsonObject::fromData(const QByteArray &data)
return d->toObject(&h->root);
}
+JsonObject JsonObject::fromVariantMap(const QVariantMap &map)
+{
+ // ### this is implemented the trivial way, not the most effiecent way
+
+ JsonObject object;
+ for (QVariantMap::const_iterator it = map.constBegin(); it != map.constEnd(); ++it)
+ object.insert(it.key(), JsonValue::fromVariant(it.value()));
+ return object;
+}
+
+QVariantMap JsonObject::toVariantMap() const
+{
+
+}
+
+
QStringList JsonObject::keys()
{
if (!d)
@@ -170,7 +186,7 @@ bool JsonObject::operator==(const JsonObject &other) const
if (o == other.o)
return true;
- if (o->length != other.o->length)
+ if (!o || !other.o || o->length != other.o->length)
return false;
for (uint i = 0; i < o->length; ++i) {
diff --git a/src/qbinaryjsonobject.h b/src/qbinaryjsonobject.h
index f1378e9..2150715 100644
--- a/src/qbinaryjsonobject.h
+++ b/src/qbinaryjsonobject.h
@@ -2,6 +2,7 @@
#define QBINARYSJONOBJECT_H
#include <qbinaryjsonglobal.h>
+#include <qvariant.h>
namespace QtBinaryJson {
@@ -15,6 +16,9 @@ public:
JsonObject &operator =(const JsonObject &other);
static JsonObject fromData(const QByteArray &data);
+ static JsonObject fromVariantMap(const QVariantMap &map);
+
+ QVariantMap toVariantMap() const;
QStringList keys();
int numKeys();
diff --git a/src/qbinaryjsonvalue.cpp b/src/qbinaryjsonvalue.cpp
index dc48707..5288909 100644
--- a/src/qbinaryjsonvalue.cpp
+++ b/src/qbinaryjsonvalue.cpp
@@ -2,6 +2,10 @@
#include <qbinaryjsonvalue.h>
#include <qbinaryjsonarray.h>
#include <qbinaryjson_p.h>
+#include <qbinaryjsonarray.h>
+
+#include <qvariant.h>
+#include <qstringlist.h>
using namespace QtBinaryJson;
@@ -85,6 +89,35 @@ JsonValue &JsonValue::operator =(const JsonValue &other)
return *this;
}
+JsonValue JsonValue::fromVariant(const QVariant &variant)
+{
+ switch (variant.type()) {
+ case QVariant::Bool:
+ return JsonValue(variant.toBool());
+ case QVariant::Double:
+ case QVariant::Int:
+ case QVariant::LongLong:
+ case QVariant::ULongLong:
+ case QVariant::UInt:
+ return JsonValue(variant.toDouble());
+ case QVariant::String:
+ return JsonValue(variant.toString());
+ case QVariant::StringList:
+ return JsonValue(JsonArray::fromStringList(variant.toStringList()));
+ case QVariant::List:
+ return JsonValue(JsonArray::fromVariantList(variant.toList()));
+ case QVariant::Map:
+ return JsonValue(JsonObject::fromVariantMap(variant.toMap()));
+ default:
+ break;
+ }
+ QString string = variant.toString();
+ if (string.isEmpty())
+ return JsonValue();
+ return JsonValue(string);
+}
+
+
ValueType JsonValue::type()
{
if (!d)
@@ -175,12 +208,14 @@ bool JsonValue::operator==(const JsonValue &other) const
if (v == other.v)
return true;
+ if (!v)
+ return other.v->type == NullValue;
+ if (!other.v)
+ return v->type == NullValue;
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:
@@ -191,7 +226,10 @@ bool JsonValue::operator==(const JsonValue &other) const
return d->toArray(v->array()) == other.d->toArray(other.v->array());
case ObjectValue:
return d->toObject(v->object()) == other.d->toObject(other.v->object());
+ case NullValue:
+ break;
}
+ return true;
}
bool JsonValue::operator!=(const JsonValue &other) const
diff --git a/src/qbinaryjsonvalue.h b/src/qbinaryjsonvalue.h
index 535020d..da47b55 100644
--- a/src/qbinaryjsonvalue.h
+++ b/src/qbinaryjsonvalue.h
@@ -3,6 +3,8 @@
#include <qbinaryjsonglobal.h>
+class QVariant;
+
namespace QtBinaryJson {
class JsonValue {
@@ -19,6 +21,8 @@ public:
JsonValue(const JsonValue &other);
JsonValue &operator =(const JsonValue &other);
+ static JsonValue fromVariant(const QVariant &variant);
+
ValueType type();
// template <> value() const;
diff --git a/tests/auto/tst_qtbinaryjson.cpp b/tests/auto/tst_qtbinaryjson.cpp
index da0487e..9dd0c37 100644
--- a/tests/auto/tst_qtbinaryjson.cpp
+++ b/tests/auto/tst_qtbinaryjson.cpp
@@ -63,6 +63,9 @@ private Q_SLOTS:
void testValueArray();
void testObjectNested();
void testArrayNested();
+
+ void fromVariantMap();
+ void toVariantMap();
};
TestQtBinaryJson::TestQtBinaryJson(QObject *parent) : QObject(parent)
@@ -171,10 +174,20 @@ void TestQtBinaryJson::testArraySimple()
int size = array.size();
array.removeAt(2);
- QCOMPARE(array.size(), size - 1);
+ --size;
+ QCOMPARE(array.size(), size);
JsonValue taken = array.takeAt(0);
+ --size;
QCOMPARE(taken.toNumber(), 999.);
+ QCOMPARE(array.size(), size);
+
+ // check whether null values work
+ array.append(JsonValue());
+ ++size;
+ QCOMPARE(array.size(), size);
+ QCOMPARE(array.last().type(), NullValue);
+ QCOMPARE(array.last(), JsonValue());
}
void TestQtBinaryJson::testValueObject()
@@ -284,6 +297,44 @@ void TestQtBinaryJson::testArrayNested()
QCOMPARE(outer.last().toArray().last().toArray().at(0).toString(), QString("nested"));
}
+void TestQtBinaryJson::fromVariantMap()
+{
+ QVariantMap map;
+ map.insert(QLatin1String("key1"), QLatin1String("value1"));
+ map.insert(QLatin1String("key2"), QLatin1String("value2"));
+ JsonObject object = JsonObject::fromVariantMap(map);
+ QCOMPARE(object.numKeys(), 2);
+ QCOMPARE(object.value(QLatin1String("key1")), JsonValue(QLatin1String("value1")));
+ QCOMPARE(object.value(QLatin1String("key2")), JsonValue(QLatin1String("value2")));
+
+ QVariantList list;
+ list.append(true);
+ list.append(QVariant());
+ list.append(999.);
+ list.append(QLatin1String("foo"));
+ map.insert("list", list);
+ object = JsonObject::fromVariantMap(map);
+ QCOMPARE(object.numKeys(), 3);
+ QCOMPARE(object.value(QLatin1String("key1")), JsonValue(QLatin1String("value1")));
+ QCOMPARE(object.value(QLatin1String("key2")), JsonValue(QLatin1String("value2")));
+ QCOMPARE(object.value(QLatin1String("list")).type(), ArrayValue);
+ JsonArray array = object.value(QLatin1String("list")).toArray();
+ QCOMPARE(array.size(), 4);
+ QCOMPARE(array.at(0).type(), BooleanValue);
+ QCOMPARE(array.at(0).toBool(), true);
+ QCOMPARE(array.at(1).type(), NullValue);
+ QCOMPARE(array.at(2).type(), NumberValue);
+ QCOMPARE(array.at(2).toNumber(), 999.);
+ QCOMPARE(array.at(3).type(), StringValue);
+ QCOMPARE(array.at(3).toString(), QLatin1String("foo"));
+}
+
+void TestQtBinaryJson::toVariantMap()
+{
+
+}
+
+
QTEST_MAIN(TestQtBinaryJson)
#include "tst_qtbinaryjson.moc"