diff options
author | Jamey Hicks <jamey.hicks@nokia.com> | 2011-12-02 16:49:09 -0500 |
---|---|---|
committer | Knoll Lars <lars.knoll@nokia.com> | 2011-12-06 15:27:40 +0100 |
commit | aa4c52c475d42023da378abba52d695d4c7e5c46 (patch) | |
tree | 66bcb70b07f3a3ff6928fdad448fbd73ce969857 | |
parent | da1bb989f409431181fc5e7b58fde2af4986869f (diff) |
Add support for int based values and make some methods const
Change-Id: Iaec08e5a4031c3fb101e661665b84f5dcd6df8a5
Reviewed-by: Knoll Lars <lars.knoll@nokia.com>
-rw-r--r-- | src/qbinaryjson_p.h | 7 | ||||
-rw-r--r-- | src/qbinaryjsonarray.cpp | 8 | ||||
-rw-r--r-- | src/qbinaryjsonarray.h | 1 | ||||
-rw-r--r-- | src/qbinaryjsonobject.cpp | 14 | ||||
-rw-r--r-- | src/qbinaryjsonobject.h | 7 | ||||
-rw-r--r-- | src/qbinaryjsonvalue.cpp | 30 | ||||
-rw-r--r-- | src/qbinaryjsonvalue.h | 6 | ||||
-rw-r--r-- | tests/auto/tst_qtbinaryjson.cpp | 13 |
8 files changed, 78 insertions, 8 deletions
diff --git a/src/qbinaryjson_p.h b/src/qbinaryjson_p.h index 2216231..ef62d4d 100644 --- a/src/qbinaryjson_p.h +++ b/src/qbinaryjson_p.h @@ -88,6 +88,7 @@ struct Value bool toBoolean() const; double toNumber() const; + int toInt() const; QString shallowString() const; QString string() const; Array *array() const; @@ -213,6 +214,12 @@ inline double Value::toNumber() const return *(double *)((char *)(this+1)); } +inline int Value::toInt() const +{ + Q_ASSERT(type == NumberValue); + return (int)*(double *)((char *)(this+1)); +} + inline QString Value::shallowString() const { Q_ASSERT(type == StringValue); diff --git a/src/qbinaryjsonarray.cpp b/src/qbinaryjsonarray.cpp index 3e711f8..bd391e6 100644 --- a/src/qbinaryjsonarray.cpp +++ b/src/qbinaryjsonarray.cpp @@ -83,6 +83,14 @@ int JsonArray::size() const return (int)a->length; } +bool JsonArray::isEmpty() const +{ + if (!d) + return true; + + return a->length == 0; +} + JsonValue JsonArray::at(int i) const { if (!a || i < 0 || i >= (int)a->length) diff --git a/src/qbinaryjsonarray.h b/src/qbinaryjsonarray.h index 23a8407..a379436 100644 --- a/src/qbinaryjsonarray.h +++ b/src/qbinaryjsonarray.h @@ -22,6 +22,7 @@ public: QVariantList toVariantList() const; int size() const; + bool isEmpty() const; JsonValue at(int i) const; JsonValue first() const; JsonValue last() const; diff --git a/src/qbinaryjsonobject.cpp b/src/qbinaryjsonobject.cpp index 4243116..5bbe0ac 100644 --- a/src/qbinaryjsonobject.cpp +++ b/src/qbinaryjsonobject.cpp @@ -307,7 +307,7 @@ QByteArray JsonObject::toJson() const } -QStringList JsonObject::keys() +QStringList JsonObject::keys() const { if (!d) return QStringList(); @@ -322,7 +322,7 @@ QStringList JsonObject::keys() return keys; } -int JsonObject::numKeys() +int JsonObject::numKeys() const { if (!d) return 0; @@ -330,6 +330,14 @@ int JsonObject::numKeys() return o->length; } +bool JsonObject::isEmpty() const +{ + if (!d) + return true; + + return o->length == 0; +} + JsonValue JsonObject::value(const QString &key) const { if (!d) @@ -404,7 +412,7 @@ JsonValue JsonObject::take(const QString &key) return d->toValue(e->value()); } -bool JsonObject::contains(const QString &key) +bool JsonObject::contains(const QString &key) const { if (!o) return false; diff --git a/src/qbinaryjsonobject.h b/src/qbinaryjsonobject.h index 065c865..b8b5fc1 100644 --- a/src/qbinaryjsonobject.h +++ b/src/qbinaryjsonobject.h @@ -21,8 +21,9 @@ public: QVariantMap toVariantMap() const; QByteArray toJson() const; - QStringList keys(); - int numKeys(); + QStringList keys() const; + int numKeys() const; + bool isEmpty() const; // ### rather use an iterator? // QString keyAt(int); @@ -33,7 +34,7 @@ public: void insert(const QString &key, const JsonValue &value); void remove(const QString &key); JsonValue take(const QString &key); - bool contains(const QString &key); + bool contains(const QString &key) const; QByteArray data() const; diff --git a/src/qbinaryjsonvalue.cpp b/src/qbinaryjsonvalue.cpp index 3483596..6ca2dfe 100644 --- a/src/qbinaryjsonvalue.cpp +++ b/src/qbinaryjsonvalue.cpp @@ -36,6 +36,14 @@ JsonValue::JsonValue(double n) d->ref.ref(); } +JsonValue::JsonValue(int n) + : d(0), v(0) +{ + v = Value::fromNumber((double)n); + d = new Data((char *)v, v->size); + d->ref.ref(); +} + JsonValue::JsonValue(const QString &s) : d(0), v(0) { @@ -148,7 +156,7 @@ QVariant JsonValue::toVariant() const } -ValueType JsonValue::type() +ValueType JsonValue::type() const { if (!d) return NullValue; @@ -166,11 +174,21 @@ void JsonValue::setValue(double d) *this = JsonValue(d); } +void JsonValue::setValue(int i) +{ + *this = JsonValue(i); +} + void JsonValue::setValue(const QString &s) { *this = JsonValue(s); } +void JsonValue::setValue(const QLatin1String &s) +{ + *this = JsonValue(s); +} + void JsonValue::setValue(const JsonArray &a) { *this = JsonValue(a); @@ -201,6 +219,16 @@ double JsonValue::toNumber() const return v->toNumber(); } +int JsonValue::toInt() const +{ + if (!d) + return 0; + + if (v->type != NumberValue) + return 0; + return v->toInt(); +} + QString JsonValue::toString() const { if (!d) diff --git a/src/qbinaryjsonvalue.h b/src/qbinaryjsonvalue.h index 3463286..630419e 100644 --- a/src/qbinaryjsonvalue.h +++ b/src/qbinaryjsonvalue.h @@ -12,6 +12,7 @@ public: JsonValue(); JsonValue(bool b); JsonValue(double n); + JsonValue(int n); JsonValue(const QString &s); JsonValue(const QLatin1String &s); JsonValue(const JsonArray &a); @@ -25,18 +26,21 @@ public: static JsonValue fromVariant(const QVariant &variant); QVariant toVariant() const; - ValueType type(); + ValueType type() const; // template <> value() const; void setValue(bool); void setValue(double); + void setValue(int); void setValue(const QString &); + void setValue(const QLatin1String &); void setValue(const JsonArray &); void setValue(const JsonObject &); bool toBool() const; double toNumber() const; + int toInt() const; QString toString() const; JsonArray toArray() const; JsonObject toObject() const; diff --git a/tests/auto/tst_qtbinaryjson.cpp b/tests/auto/tst_qtbinaryjson.cpp index 9063025..ad064bf 100644 --- a/tests/auto/tst_qtbinaryjson.cpp +++ b/tests/auto/tst_qtbinaryjson.cpp @@ -100,6 +100,7 @@ void TestQtBinaryJson::testValueSimple() { JsonValue value(true); QCOMPARE(value.toNumber(), 0.); + QCOMPARE(value.toInt(), 0); QCOMPARE(value.toString(), QString()); QCOMPARE(value.toBool(), true); QCOMPARE(value.toObject(), JsonObject()); @@ -107,6 +108,7 @@ void TestQtBinaryJson::testValueSimple() value.setValue(999.); QCOMPARE(value.toNumber(), 999.); + QCOMPARE(value.toInt(), 999); QCOMPARE(value.toString(), QString()); QCOMPARE(value.toBool(), false); QCOMPARE(value.toObject(), JsonObject()); @@ -114,6 +116,7 @@ void TestQtBinaryJson::testValueSimple() value.setValue(QLatin1String("test")); QCOMPARE(value.toNumber(), 0.); + QCOMPARE(value.toInt(), 0); QCOMPARE(value.toString(), QLatin1String("test")); QCOMPARE(value.toBool(), false); QCOMPARE(value.toObject(), JsonObject()); @@ -121,10 +124,20 @@ void TestQtBinaryJson::testValueSimple() value.setValue(true); QCOMPARE(value.toNumber(), 0.); + QCOMPARE(value.toInt(), 0); QCOMPARE(value.toString(), QString()); QCOMPARE(value.toBool(), true); QCOMPARE(value.toObject(), JsonObject()); QCOMPARE(value.toArray(), JsonArray()); + + value.setValue(999); + QCOMPARE(value.toNumber(), 999.); + QCOMPARE(value.toInt(), 999); + QCOMPARE(value.toString(), QString()); + QCOMPARE(value.toBool(), false); + QCOMPARE(value.toObject(), JsonObject()); + QCOMPARE(value.toArray(), JsonArray()); + } void TestQtBinaryJson::testObjectSimple() |