diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2012-01-09 12:17:48 +0100 |
---|---|---|
committer | Lars Knoll <lars.knoll@nokia.com> | 2012-01-09 13:29:08 +0100 |
commit | bbf09d4de0413ab5ea173639511c95da00aca2ab (patch) | |
tree | ef06b4500261419e7f99abec29969750bd94f662 | |
parent | 12b74f21879ba9c13542640df8df59fd226356ee (diff) |
Changes after API review round.
* Added:
QJsonArray/Object::count()
QJsonArray::prepend()
* Renamed:
QJsonDocument::Tag to BinaryFormatTag
QJsonObject::numKeys() to size()
QJsonValue::Boolean to Bool
* Replaced QJsonDocument::type() with
isArray() and isObject()
* Made some constructors explicit.
* Removed QJsonObject::toJson()
* Added isType() methods to QJsonValue
* added some ### for further changes
Change-Id: Id88f24f2921b53d6781def3f808e146c9f61cb31
Sanity-Review: Lars Knoll <lars.knoll@nokia.com>
Reviewed-by: Simon Hausmann <simon.hausmann@nokia.com>
-rw-r--r-- | src/qjson.cpp | 8 | ||||
-rw-r--r-- | src/qjson_p.h | 6 | ||||
-rw-r--r-- | src/qjsonarray.cpp | 5 | ||||
-rw-r--r-- | src/qjsonarray.h | 8 | ||||
-rw-r--r-- | src/qjsondocument.cpp | 17 | ||||
-rw-r--r-- | src/qjsondocument.h | 10 | ||||
-rw-r--r-- | src/qjsonobject.cpp | 10 | ||||
-rw-r--r-- | src/qjsonobject.h | 8 | ||||
-rw-r--r-- | src/qjsonparser.cpp | 6 | ||||
-rw-r--r-- | src/qjsonvalue.cpp | 14 | ||||
-rw-r--r-- | src/qjsonvalue.h | 16 | ||||
-rw-r--r-- | src/qjsonwriter.cpp | 2 | ||||
-rw-r--r-- | tests/auto/tst_qtjson.cpp | 125 |
13 files changed, 144 insertions, 91 deletions
diff --git a/src/qjson.cpp b/src/qjson.cpp index 1148db5..5936949 100644 --- a/src/qjson.cpp +++ b/src/qjson.cpp @@ -69,7 +69,7 @@ void Data::compact() int size = sizeof(Base) + reserve + base->length*sizeof(offset); int alloc = sizeof(Header) + size; Header *h = (Header *) malloc(alloc); - h->tag = QJsonDocument::Tag; + h->tag = QJsonDocument::BinaryFormatTag; h->version = 1; Base *b = h->root(); b->size = size; @@ -126,7 +126,7 @@ void Data::validate() if (valid != Unchecked) return; - if (header->tag != QJsonDocument::Tag || header->version != 1u) { + if (header->tag != QJsonDocument::BinaryFormatTag || header->version != 1u) { valid = Invalid; return; } @@ -242,7 +242,7 @@ int Value::usedStorage(const Base *b) const s = objectOrArray(b)->size; break; case QJsonValue::Null: - case QJsonValue::Boolean: + case QJsonValue::Bool: default: break; } @@ -263,7 +263,7 @@ bool Value::isValid(const Base *b) const offset = val; break; case QJsonValue::Null: - case QJsonValue::Boolean: + case QJsonValue::Bool: default: break; } diff --git a/src/qjson_p.h b/src/qjson_p.h index 576ae41..4e88f1f 100644 --- a/src/qjson_p.h +++ b/src/qjson_p.h @@ -525,7 +525,7 @@ struct Header { inline bool Value::toBoolean() const { - Q_ASSERT(type == QJsonValue::Boolean); + Q_ASSERT(type == QJsonValue::Bool); return val != 0; } @@ -598,7 +598,7 @@ struct Data { alloc = sizeof(Header) + sizeof(Base) + reserved + sizeof(offset); header = (Header *)malloc(alloc); - header->tag = QJsonDocument::Tag; + header->tag = QJsonDocument::BinaryFormatTag; header->version = 1; Base *b = header->root(); b->size = sizeof(Base); @@ -642,7 +642,7 @@ struct Data { char *raw = (char *)malloc(size); memcpy(raw + sizeof(Header), b, b->size); Header *h = (Header *)raw; - h->tag = QJsonDocument::Tag; + h->tag = QJsonDocument::BinaryFormatTag; h->version = 1; Data *d = new Data(raw, size); d->compactionCounter = compactionCounter; diff --git a/src/qjsonarray.cpp b/src/qjsonarray.cpp index ae1d68b..9e25107 100644 --- a/src/qjsonarray.cpp +++ b/src/qjsonarray.cpp @@ -151,6 +151,11 @@ QJsonValue QJsonArray::last() const return at(a ? (a->length - 1) : 0); } +void QJsonArray::prepend(const QJsonValue &value) +{ + insert(0, value); +} + void QJsonArray::append(const QJsonValue &value) { insert(a ? a->length : 0, value); diff --git a/src/qjsonarray.h b/src/qjsonarray.h index 931ed51..0ad2779 100644 --- a/src/qjsonarray.h +++ b/src/qjsonarray.h @@ -63,24 +63,30 @@ public: QVariantList toVariantList() const; int size() const; + inline int count() const { return size(); } + bool isEmpty() const; QJsonValue at(int i) const; QJsonValue first() const; QJsonValue last() const; + void prepend(const QJsonValue &value); void append(const QJsonValue &value); void removeAt(int i); QJsonValue takeAt(int i); void insert(int i, const QJsonValue &value); + // ### void replace(int i, const QJsonValue &value); + bool contains(const QJsonValue &element) const; -// JsonValue &operator[](int i); + // ### JsonValueRef &operator[](int i); QJsonValue operator[](int i) const; bool operator==(const QJsonArray &other) const; bool operator!=(const QJsonArray &other) const; + // ### make private void detach(uint reserve = 0); private: diff --git a/src/qjsondocument.cpp b/src/qjsondocument.cpp index 312f71e..4702428 100644 --- a/src/qjsondocument.cpp +++ b/src/qjsondocument.cpp @@ -107,7 +107,7 @@ QJsonDocument QJsonDocument::fromBinaryData(const QByteArray &data) Private::Header *h = (Private::Header *) data.constData(); if (data.size() < (int)(sizeof(Private::Header) + sizeof(Private::Base)) || - h->tag != QJsonDocument::Tag || h->version != 1u || + h->tag != QJsonDocument::BinaryFormatTag || h->version != 1u || sizeof(Private::Header) + h->root()->size > (uint)data.size()) return QJsonDocument(); @@ -179,13 +179,22 @@ QByteArray QJsonDocument::toBinaryData() const return QByteArray(d->rawData, d->header->root()->size + sizeof(Private::Header)); } -QJsonValue::Type QJsonDocument::type() const +bool QJsonDocument::isArray() const { if (!d) - return QJsonValue::Null; + return false; + + Private::Header *h = (Private::Header *)d->rawData; + return h->root()->isArray(); +} + +bool QJsonDocument::isObject() const +{ + if (!d) + return false; Private::Header *h = (Private::Header *)d->rawData; - return h->root()->isArray() ? QJsonValue::Array : QJsonValue::Object; + return h->root()->isObject(); } QJsonObject QJsonDocument::object() const diff --git a/src/qjsondocument.h b/src/qjsondocument.h index 02da9f3..ba94729 100644 --- a/src/qjsondocument.h +++ b/src/qjsondocument.h @@ -51,11 +51,11 @@ namespace QtJson { class Q_JSON_EXPORT QJsonDocument { public: - static const uint Tag = ('q') | ('b' << 8) | ('j' << 16) | ('s' << 24); + static const uint BinaryFormatTag = ('q') | ('b' << 8) | ('j' << 16) | ('s' << 24); QJsonDocument(); - QJsonDocument(const QJsonObject &object); - QJsonDocument(const QJsonArray &array); + explicit QJsonDocument(const QJsonObject &object); + explicit QJsonDocument(const QJsonArray &array); ~QJsonDocument(); QJsonDocument(const QJsonDocument &other); @@ -71,8 +71,8 @@ public: static QJsonDocument fromJson(const QByteArray &json); bool isEmpty() const; - - QJsonValue::Type type() const; + bool isArray() const; + bool isObject() const; QJsonObject object() const; QJsonArray array() const; diff --git a/src/qjsonobject.cpp b/src/qjsonobject.cpp index c2259d9..bbe9b80 100644 --- a/src/qjsonobject.cpp +++ b/src/qjsonobject.cpp @@ -111,14 +111,6 @@ QVariantMap QJsonObject::toVariantMap() const return map; } -QByteArray QJsonObject::toJson() const -{ - QByteArray json; - QJsonWriter::objectToJson(o, json, 0); - return json; -} - - QStringList QJsonObject::keys() const { if (!d) @@ -134,7 +126,7 @@ QStringList QJsonObject::keys() const return keys; } -int QJsonObject::numKeys() const +int QJsonObject::size() const { if (!d) return 0; diff --git a/src/qjsonobject.h b/src/qjsonobject.h index 397131d..de46cdd 100644 --- a/src/qjsonobject.h +++ b/src/qjsonobject.h @@ -59,17 +59,16 @@ public: static QJsonObject fromVariantMap(const QVariantMap &map); QVariantMap toVariantMap() const; - QByteArray toJson() const; - QStringList keys() const; - int numKeys() const; + int size() const; + inline int count() const { return size(); } bool isEmpty() const; // ### rather use an iterator? // QString keyAt(int); // QSonValue valueAt(int) - QJsonValue value(const QString & key) const; + QJsonValue value(const QString &key) const; void insert(const QString &key, const QJsonValue &value); void remove(const QString &key); @@ -79,6 +78,7 @@ public: bool operator==(const QJsonObject &other) const; bool operator!=(const QJsonObject &other) const; + // mkae private void detach(uint reserve = 0); private: diff --git a/src/qjsonparser.cpp b/src/qjsonparser.cpp index df23ae3..2e70f86 100644 --- a/src/qjsonparser.cpp +++ b/src/qjsonparser.cpp @@ -163,7 +163,7 @@ QtJson::QJsonDocument QJsonParser::parse() // fill in Header data Private::Header *h = (Private::Header *)data; - h->tag = QJsonDocument::Tag; + h->tag = QJsonDocument::BinaryFormatTag; h->version = 1u; current = sizeof(Private::Header); @@ -353,7 +353,7 @@ bool QJsonParser::parseValue(Private::Value *val, int baseOffset) if (*json++ == 'r' && *json++ == 'u' && *json++ == 'e') { - val->type = QJsonValue::Boolean; + val->type = QJsonValue::Bool; val->val = true; DEBUG << "value: true"; END; @@ -367,7 +367,7 @@ bool QJsonParser::parseValue(Private::Value *val, int baseOffset) *json++ == 'l' && *json++ == 's' && *json++ == 'e') { - val->type = QJsonValue::Boolean; + val->type = QJsonValue::Bool; val->val = false; DEBUG << "value: false"; END; diff --git a/src/qjsonvalue.cpp b/src/qjsonvalue.cpp index f5b29f8..25dfebf 100644 --- a/src/qjsonvalue.cpp +++ b/src/qjsonvalue.cpp @@ -68,7 +68,7 @@ QJsonValue::QJsonValue(Private::Data *data, Private::Base *base, const Private:: case Null: dbl = 0; break; - case Boolean: + case Bool: b = v.toBoolean(); break; case Number: @@ -91,7 +91,7 @@ QJsonValue::QJsonValue(Private::Data *data, Private::Base *base, const Private:: } QJsonValue::QJsonValue(bool b) - : t(Boolean), d(0) + : t(Bool), d(0) { this->b = b; } @@ -220,7 +220,7 @@ QJsonValue QJsonValue::fromVariant(const QVariant &variant) QVariant QJsonValue::toVariant() const { switch (t) { - case Boolean: + case Bool: return b; case Number: return dbl; @@ -280,7 +280,7 @@ void QJsonValue::setValue(const QJsonObject &o) bool QJsonValue::toBool() const { - if (t != Boolean) + if (t != Bool) return false; return b; } @@ -332,7 +332,7 @@ bool QJsonValue::operator==(const QJsonValue &other) const case Undefined: case Null: break; - case Boolean: + case Bool: return b == other.b; case Number: return dbl == other.dbl; @@ -384,7 +384,7 @@ int QJsonValue::requiredStorage(bool *compressed) const return base ? base->size : sizeof(Private::Base); case Undefined: case Null: - case Boolean: + case Bool: break; } return 0; @@ -396,7 +396,7 @@ uint QJsonValue::valueToStore(uint offset) const case Undefined: case Null: break; - case Boolean: + case Bool: return b; case Number: { int c = Private::compressedNumber(dbl); diff --git a/src/qjsonvalue.h b/src/qjsonvalue.h index 9028c59..6b677b4 100644 --- a/src/qjsonvalue.h +++ b/src/qjsonvalue.h @@ -52,7 +52,7 @@ class Q_JSON_EXPORT QJsonValue { public: enum Type { Null = 0x0, - Boolean = 0x1, + Bool = 0x1, Number = 0x2, String = 0x3, Array = 0x4, @@ -78,8 +78,15 @@ public: QVariant toVariant() const; Type type() const; + inline bool isNull() const { return type() == Null; } + inline bool isBool() const { return type() == Bool; } + inline bool isNumber() const { return type() == Number; } + inline bool isString() const { return type() == String; } + inline bool isArray() const { return type() == Array; } + inline bool isObject() const { return type() == Object; } inline bool isUndefined() const { return type() == Undefined; } + // ### remove! void setValue(bool); void setValue(double); void setValue(int); @@ -98,6 +105,7 @@ public: bool operator==(const QJsonValue &other) const; bool operator!=(const QJsonValue &other) const; + // ### make private void detach(); private: @@ -108,10 +116,16 @@ private: friend class QJsonArray; friend class QJsonObject; QJsonValue(Private::Data *d, Private::Base *b, const Private::Value& v); + + // ### These should move to Private::Value int requiredStorage(bool *compressed) const; uint valueToStore(uint offset) const; void copyData(char *dest, bool compressed) const; + // ### This should be: + // Private::Data *d; + // Private::Base *b; + // Private::Value *v; Type t; Private::Data *d; // needed for Objects and Arrays union { diff --git a/src/qjsonwriter.cpp b/src/qjsonwriter.cpp index cea366f..b678cf3 100644 --- a/src/qjsonwriter.cpp +++ b/src/qjsonwriter.cpp @@ -179,7 +179,7 @@ static void valueToJson(const Private::Base *b, const Private::Value &v, QByteAr { QJsonValue::Type type = (QJsonValue::Type)(uint)v.type; switch (type) { - case QJsonValue::Boolean: + case QJsonValue::Bool: json += v.toBoolean() ? "true" : "false"; break; case QJsonValue::Number: diff --git a/tests/auto/tst_qtjson.cpp b/tests/auto/tst_qtjson.cpp index 8c725db..e2070a5 100644 --- a/tests/auto/tst_qtjson.cpp +++ b/tests/auto/tst_qtjson.cpp @@ -119,7 +119,7 @@ void TestQtJson::cleanup() void TestQtJson::testValueSimple() { QJsonValue value(true); - QCOMPARE(value.type(), QJsonValue::Boolean); + QCOMPARE(value.type(), QJsonValue::Bool); QCOMPARE(value.toNumber(), 0.); QCOMPARE(value.toInt(), 0); QCOMPARE(value.toString(), QString()); @@ -254,9 +254,9 @@ void TestQtJson::testObjectSimple() object.insert("value", value); QCOMPARE(object.value("value"), value); - int size = object.numKeys(); + int size = object.size(); object.remove("boolean"); - QCOMPARE(object.numKeys(), size - 1); + QCOMPARE(object.size(), size - 1); QVERIFY2(!object.contains("boolean"), "key boolean should have been removed"); QJsonValue taken = object.take("value"); @@ -304,6 +304,13 @@ void TestQtJson::testArraySimple() QCOMPARE(array.last().type(), QJsonValue::Null); QCOMPARE(array.last(), QJsonValue()); + QCOMPARE(array.first().type(), QJsonValue::String); + QCOMPARE(array.first(), QJsonValue(QLatin1String("test"))); + + array.prepend(false); + QCOMPARE(array.first().type(), QJsonValue::Bool); + QCOMPARE(array.first(), QJsonValue(false)); + QCOMPARE(array.at(-1), QJsonValue(QJsonValue::Undefined)); QCOMPARE(array.at(array.size()), QJsonValue(QJsonValue::Undefined)); } @@ -440,17 +447,17 @@ void TestQtJson::testObjectNestedEmpty() object.insert("inner", inner); object.insert("inner2", inner2); QJsonObject value = object.value("inner").toObject(); - QCOMPARE(value.numKeys(), 0); + QCOMPARE(value.size(), 0); QCOMPARE(value, inner); - QCOMPARE(value.numKeys(), 0); + QCOMPARE(value.size(), 0); object.insert("count", 0.); - QCOMPARE(object.value("inner").toObject().numKeys(), 0); + QCOMPARE(object.value("inner").toObject().size(), 0); QCOMPARE(object.value("inner").type(), QJsonValue::Object); QJsonDocument(object).toBinaryData(); QVERIFY(object.value("inner").toObject().isEmpty()); QVERIFY(object.value("inner2").toObject().isEmpty()); QJsonObject reconstituted(QJsonDocument::fromBinaryData(QJsonDocument(object).toBinaryData()).object()); - QCOMPARE(reconstituted.value("inner").toObject().numKeys(), 0); + QCOMPARE(reconstituted.value("inner").toObject().size(), 0); QCOMPARE(reconstituted.value("inner").type(), QJsonValue::Object); QCOMPARE(reconstituted.value("inner2").type(), QJsonValue::Object); } @@ -459,33 +466,39 @@ void TestQtJson::testDocument() { QJsonDocument doc; QCOMPARE(doc.isEmpty(), true); - QCOMPARE(doc.type(), QJsonValue::Null); + QCOMPARE(doc.isArray(), false); + QCOMPARE(doc.isObject(), false); QJsonObject object; doc.setObject(object); QCOMPARE(doc.isEmpty(), false); - QCOMPARE(doc.type(), QJsonValue::Object); + QCOMPARE(doc.isArray(), false); + QCOMPARE(doc.isObject(), true); object.insert(QLatin1String("Key"), QLatin1String("Value")); doc.setObject(object); QCOMPARE(doc.isEmpty(), false); - QCOMPARE(doc.type(), QJsonValue::Object); + QCOMPARE(doc.isArray(), false); + QCOMPARE(doc.isObject(), true); QVERIFY(doc.object() == object); QVERIFY(doc.array() == QJsonArray()); doc = QJsonDocument(); QCOMPARE(doc.isEmpty(), true); - QCOMPARE(doc.type(), QJsonValue::Null); + QCOMPARE(doc.isArray(), false); + QCOMPARE(doc.isObject(), false); QJsonArray array; doc.setArray(array); QCOMPARE(doc.isEmpty(), false); - QCOMPARE(doc.type(), QJsonValue::Array); + QCOMPARE(doc.isArray(), true); + QCOMPARE(doc.isObject(), false); array.append(QLatin1String("Value")); doc.setArray(array); QCOMPARE(doc.isEmpty(), false); - QCOMPARE(doc.type(), QJsonValue::Array); + QCOMPARE(doc.isArray(), true); + QCOMPARE(doc.isObject(), false); QVERIFY(doc.array() == array); QVERIFY(doc.object() == QJsonObject()); @@ -504,18 +517,21 @@ void TestQtJson::testDocument() QJsonDocument doc3; doc3.setObject(outer.value(QLatin1String("innter")).toObject()); - QCOMPARE(doc3.type(), QJsonValue::Object); + QCOMPARE(doc3.isArray(), false); + QCOMPARE(doc3.isObject(), true); QVERIFY(doc3.object().contains(QLatin1String("innerKey"))); QCOMPARE(doc3.object().value(QLatin1String("innerKey")), QJsonValue(42)); QJsonDocument doc4(outer.value(QLatin1String("innterArray")).toArray()); - QCOMPARE(doc4.type(), QJsonValue::Array); + QCOMPARE(doc4.isArray(), true); + QCOMPARE(doc4.isObject(), false); QCOMPARE(doc4.array().size(), 1); QCOMPARE(doc4.array().at(0), QJsonValue(23)); QJsonDocument doc5; doc5.setArray(outer.value(QLatin1String("innterArray")).toArray()); - QCOMPARE(doc5.type(), QJsonValue::Array); + QCOMPARE(doc5.isArray(), true); + QCOMPARE(doc5.isObject(), false); QCOMPARE(doc5.array().size(), 1); QCOMPARE(doc5.array().at(0), QJsonValue(23)); } @@ -531,7 +547,7 @@ void TestQtJson::nullValues() QJsonObject object; object.insert(QString("key"), QJsonValue()); QCOMPARE(object.contains("key"), true); - QCOMPARE(object.numKeys(), 1); + QCOMPARE(object.size(), 1); QCOMPARE(object.value("key"), QJsonValue()); } @@ -576,7 +592,7 @@ void TestQtJson::nullObject() QVERIFY(nullObject != nonNull); QVERIFY(nonNull != nullObject); - QCOMPARE(nullObject.numKeys(), 0); + QCOMPARE(nullObject.size(), 0); QCOMPARE(nullObject.keys(), QStringList()); nullObject.remove("foo"); QCOMPARE(nullObject, QJsonObject()); @@ -589,7 +605,7 @@ void TestQtJson::nullObject() nullObject.insert("foo", QString("bar")); nullObject.remove("foo"); - QCOMPARE(nullObject.numKeys(), 0); + QCOMPARE(nullObject.size(), 0); QCOMPARE(nullObject.keys(), QStringList()); nullObject.remove("foo"); QCOMPARE(nullObject, QJsonObject()); @@ -601,14 +617,14 @@ void TestQtJson::undefinedValues() { QJsonObject object; object.insert("Key", QJsonValue(QJsonValue::Undefined)); - QCOMPARE(object.numKeys(), 0); + QCOMPARE(object.size(), 0); object.insert("Key", QLatin1String("Value")); - QCOMPARE(object.numKeys(), 1); + QCOMPARE(object.size(), 1); QCOMPARE(object.value("Key").type(), QJsonValue::String); QCOMPARE(object.value("foo").type(), QJsonValue::Undefined); object.insert("Key", QJsonValue(QJsonValue::Undefined)); - QCOMPARE(object.numKeys(), 0); + QCOMPARE(object.size(), 0); QCOMPARE(object.value("Key").type(), QJsonValue::Undefined); QJsonArray array; @@ -627,7 +643,7 @@ void TestQtJson::fromVariantMap() map.insert(QLatin1String("key1"), QLatin1String("value1")); map.insert(QLatin1String("key2"), QLatin1String("value2")); QJsonObject object = QJsonObject::fromVariantMap(map); - QCOMPARE(object.numKeys(), 2); + QCOMPARE(object.size(), 2); QCOMPARE(object.value(QLatin1String("key1")), QJsonValue(QLatin1String("value1"))); QCOMPARE(object.value(QLatin1String("key2")), QJsonValue(QLatin1String("value2"))); @@ -638,13 +654,13 @@ void TestQtJson::fromVariantMap() list.append(QLatin1String("foo")); map.insert("list", list); object = QJsonObject::fromVariantMap(map); - QCOMPARE(object.numKeys(), 3); + QCOMPARE(object.size(), 3); QCOMPARE(object.value(QLatin1String("key1")), QJsonValue(QLatin1String("value1"))); QCOMPARE(object.value(QLatin1String("key2")), QJsonValue(QLatin1String("value2"))); QCOMPARE(object.value(QLatin1String("list")).type(), QJsonValue::Array); QJsonArray array = object.value(QLatin1String("list")).toArray(); QCOMPARE(array.size(), 4); - QCOMPARE(array.at(0).type(), QJsonValue::Boolean); + QCOMPARE(array.at(0).type(), QJsonValue::Bool); QCOMPARE(array.at(0).toBool(), true); QCOMPARE(array.at(1).type(), QJsonValue::Null); QCOMPARE(array.at(2).type(), QJsonValue::Number); @@ -692,7 +708,7 @@ void TestQtJson::toJson() array.append(QLatin1String("\\\a\n\r\b\tabcABC\"")); object.insert("Array", array); - QByteArray json = object.toJson(); + QByteArray json = QJsonDocument(object).toJson(); QByteArray expected = "{\n" @@ -732,10 +748,11 @@ void TestQtJson::fromJson() QByteArray json = "[\n true\n]\n"; QJsonDocument doc = QJsonDocument::fromJson(json); QVERIFY(!doc.isEmpty()); - QCOMPARE(doc.type(), QJsonValue::Array); + QCOMPARE(doc.isArray(), true); + QCOMPARE(doc.isObject(), false); QJsonArray array = doc.array(); QCOMPARE(array.size(), 1); - QCOMPARE(array.at(0).type(), QJsonValue::Boolean); + QCOMPARE(array.at(0).type(), QJsonValue::Bool); QCOMPARE(array.at(0).toBool(), true); QCOMPARE(doc.toJson(), json); } @@ -743,7 +760,8 @@ void TestQtJson::fromJson() QByteArray json = "[]"; QJsonDocument doc = QJsonDocument::fromJson(json); QVERIFY(!doc.isEmpty()); - QCOMPARE(doc.type(), QJsonValue::Array); + QCOMPARE(doc.isArray(), true); + QCOMPARE(doc.isObject(), false); QJsonArray array = doc.array(); QCOMPARE(array.size(), 0); } @@ -751,17 +769,19 @@ void TestQtJson::fromJson() QByteArray json = "{}"; QJsonDocument doc = QJsonDocument::fromJson(json); QVERIFY(!doc.isEmpty()); - QCOMPARE(doc.type(), QJsonValue::Object); + QCOMPARE(doc.isArray(), false); + QCOMPARE(doc.isObject(), true); QJsonObject object = doc.object(); - QCOMPARE(object.numKeys(), 0); + QCOMPARE(object.size(), 0); } { QByteArray json = "{\n \"Key\": true\n}\n"; QJsonDocument doc = QJsonDocument::fromJson(json); QVERIFY(!doc.isEmpty()); - QCOMPARE(doc.type(), QJsonValue::Object); + QCOMPARE(doc.isArray(), false); + QCOMPARE(doc.isObject(), true); QJsonObject object = doc.object(); - QCOMPARE(object.numKeys(), 1); + QCOMPARE(object.size(), 1); QCOMPARE(object.value("Key"), QJsonValue(true)); QCOMPARE(doc.toJson(), json); } @@ -769,13 +789,14 @@ void TestQtJson::fromJson() QByteArray json = "[ null, true, false, \"Foo\", 1, [], {} ]"; QJsonDocument doc = QJsonDocument::fromJson(json); QVERIFY(!doc.isEmpty()); - QCOMPARE(doc.type(), QJsonValue::Array); + QCOMPARE(doc.isArray(), true); + QCOMPARE(doc.isObject(), false); QJsonArray array = doc.array(); QCOMPARE(array.size(), 7); QCOMPARE(array.at(0).type(), QJsonValue::Null); - QCOMPARE(array.at(1).type(), QJsonValue::Boolean); + QCOMPARE(array.at(1).type(), QJsonValue::Bool); QCOMPARE(array.at(1).toBool(), true); - QCOMPARE(array.at(2).type(), QJsonValue::Boolean); + QCOMPARE(array.at(2).type(), QJsonValue::Bool); QCOMPARE(array.at(2).toBool(), false); QCOMPARE(array.at(3).type(), QJsonValue::String); QCOMPARE(array.at(3).toString(), QLatin1String("Foo")); @@ -784,19 +805,20 @@ void TestQtJson::fromJson() QCOMPARE(array.at(5).type(), QJsonValue::Array); QCOMPARE(array.at(5).toArray().size(), 0); QCOMPARE(array.at(6).type(), QJsonValue::Object); - QCOMPARE(array.at(6).toObject().numKeys(), 0); + QCOMPARE(array.at(6).toObject().size(), 0); } { QByteArray json = "{ \"0\": null, \"1\": true, \"2\": false, \"3\": \"Foo\", \"4\": 1, \"5\": [], \"6\": {} }"; QJsonDocument doc = QJsonDocument::fromJson(json); QVERIFY(!doc.isEmpty()); - QCOMPARE(doc.type(), QJsonValue::Object); + QCOMPARE(doc.isArray(), false); + QCOMPARE(doc.isObject(), true); QJsonObject object = doc.object(); - QCOMPARE(object.numKeys(), 7); + QCOMPARE(object.size(), 7); QCOMPARE(object.value("0").type(), QJsonValue::Null); - QCOMPARE(object.value("1").type(), QJsonValue::Boolean); + QCOMPARE(object.value("1").type(), QJsonValue::Bool); QCOMPARE(object.value("1").toBool(), true); - QCOMPARE(object.value("2").type(), QJsonValue::Boolean); + QCOMPARE(object.value("2").type(), QJsonValue::Bool); QCOMPARE(object.value("2").toBool(), false); QCOMPARE(object.value("3").type(), QJsonValue::String); QCOMPARE(object.value("3").toString(), QLatin1String("Foo")); @@ -805,7 +827,7 @@ void TestQtJson::fromJson() QCOMPARE(object.value("5").type(), QJsonValue::Array); QCOMPARE(object.value("5").toArray().size(), 0); QCOMPARE(object.value("6").type(), QJsonValue::Object); - QCOMPARE(object.value("6").toObject().numKeys(), 0); + QCOMPARE(object.value("6").toObject().size(), 0); } } @@ -850,7 +872,8 @@ void TestQtJson::parseNumbers() json += " ]"; QJsonDocument doc = QJsonDocument::fromJson(json); QVERIFY(!doc.isEmpty()); - QCOMPARE(doc.type(), QJsonValue::Array); + QCOMPARE(doc.isArray(), true); + QCOMPARE(doc.isObject(), false); QJsonArray array = doc.array(); QCOMPARE(array.size(), 1); QJsonValue val = array.at(0); @@ -893,7 +916,8 @@ void TestQtJson::parseNumbers() json += " ]"; QJsonDocument doc = QJsonDocument::fromJson(json); QVERIFY(!doc.isEmpty()); - QCOMPARE(doc.type(), QJsonValue::Array); + QCOMPARE(doc.isArray(), true); + QCOMPARE(doc.isObject(), false); QJsonArray array = doc.array(); QCOMPARE(array.size(), 1); QJsonValue val = array.at(0); @@ -926,7 +950,8 @@ void TestQtJson::parseStrings() json += "\"\n]\n"; QJsonDocument doc = QJsonDocument::fromJson(json); QVERIFY(!doc.isEmpty()); - QCOMPARE(doc.type(), QJsonValue::Array); + QCOMPARE(doc.isArray(), true); + QCOMPARE(doc.isObject(), false); QJsonArray array = doc.array(); QCOMPARE(array.size(), 1); QJsonValue val = array.at(0); @@ -955,7 +980,8 @@ void TestQtJson::parseStrings() out += "\"\n]\n"; QJsonDocument doc = QJsonDocument::fromJson(json); QVERIFY(!doc.isEmpty()); - QCOMPARE(doc.type(), QJsonValue::Array); + QCOMPARE(doc.isArray(), true); + QCOMPARE(doc.isObject(), false); QJsonArray array = doc.array(); QCOMPARE(array.size(), 1); QJsonValue val = array.at(0); @@ -1103,7 +1129,7 @@ void TestQtJson::testDuplicateKeys() QJsonObject obj; obj.insert(QLatin1String("foo"), QLatin1String("bar")); obj.insert(QLatin1String("foo"), QLatin1String("zap")); - QCOMPARE(obj.numKeys(), 1); + QCOMPARE(obj.size(), 1); QCOMPARE(obj.value(QLatin1String("foo")).toString(), QLatin1String("zap")); } @@ -1116,13 +1142,14 @@ void TestQtJson::testCompaction() obj.remove(QLatin1String("foo")); obj.insert(QLatin1String("foo"), QLatin1String("bar")); } - QCOMPARE(obj.numKeys(), 1); + QCOMPARE(obj.size(), 1); QCOMPARE(obj.value(QLatin1String("foo")).toString(), QLatin1String("bar")); QJsonDocument doc = QJsonDocument::fromBinaryData(QJsonDocument(obj).toBinaryData()); QVERIFY(doc.isValid()); QVERIFY(!doc.isEmpty()); - QVERIFY(doc.type() == QJsonValue::Object); + QCOMPARE(doc.isArray(), false); + QCOMPARE(doc.isObject(), true); QVERIFY(doc.object() == obj); } |