diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2012-01-09 10:09:47 +0100 |
---|---|---|
committer | Jamey Hicks <jamey.hicks@nokia.com> | 2012-01-09 12:30:47 +0100 |
commit | c9222f8c8c13d9c3bb64be8588add41a5c584488 (patch) | |
tree | 65fb51d78741e548b503497cc97d4197aa30af9f | |
parent | b35b1163b7bd3d48c6f23ed78ece5905868bcc3f (diff) |
Naming cleanups
Move private classes into Private namespace
Move ValueType into QJsonValue class
Change-Id: I122f2084037ed9dc6e508082af9280e39ed2f925
Sanity-Review: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Jamey Hicks <jamey.hicks@nokia.com>
-rw-r--r-- | src/qjson.cpp | 38 | ||||
-rw-r--r-- | src/qjson_p.h | 20 | ||||
-rw-r--r-- | src/qjsonarray.cpp | 24 | ||||
-rw-r--r-- | src/qjsonarray.h | 8 | ||||
-rw-r--r-- | src/qjsondocument.cpp | 52 | ||||
-rw-r--r-- | src/qjsondocument.h | 9 | ||||
-rw-r--r-- | src/qjsonglobal.h | 15 | ||||
-rw-r--r-- | src/qjsonobject.cpp | 42 | ||||
-rw-r--r-- | src/qjsonobject.h | 8 | ||||
-rw-r--r-- | src/qjsonparser.cpp | 54 | ||||
-rw-r--r-- | src/qjsonparser_p.h | 4 | ||||
-rw-r--r-- | src/qjsonvalue.cpp | 140 | ||||
-rw-r--r-- | src/qjsonvalue.h | 24 | ||||
-rw-r--r-- | src/qjsonwriter.cpp | 34 | ||||
-rw-r--r-- | src/qjsonwriter_p.h | 5 | ||||
-rw-r--r-- | tests/auto/tst_qtjson.cpp | 146 |
16 files changed, 318 insertions, 305 deletions
diff --git a/src/qjson.cpp b/src/qjson.cpp index 0b37d99..8206f88 100644 --- a/src/qjson.cpp +++ b/src/qjson.cpp @@ -41,7 +41,11 @@ #include <qjson_p.h> -using namespace QtJson; +namespace QtJson +{ + +namespace Private +{ void Data::compact() { @@ -220,12 +224,12 @@ int Value::usedStorage(const Base *b) const { int s = 0; switch (type) { - case NumberValue: + case JsonValue::Number: if (latinOrIntValue) break; s = sizeof(double); break; - case StringValue: { + case JsonValue::String: { char *d = data(b); if (latinOrIntValue) s = sizeof(ushort) + *(ushort *)d; @@ -233,12 +237,12 @@ int Value::usedStorage(const Base *b) const s = sizeof(int) + sizeof(ushort)*(*(int *)d); break; } - case ArrayValue: - case ObjectValue: + case JsonValue::Array: + case JsonValue::Object: s = objectOrArray(b)->size; break; - case NullValue: - case BooleanValue: + case JsonValue::Null: + case JsonValue::Boolean: default: break; } @@ -249,17 +253,17 @@ bool Value::isValid(const Base *b) const { int offset = 0; switch (type) { - case NumberValue: + case JsonValue::Number: if (latinOrIntValue) break; // fall through - case StringValue: - case ArrayValue: - case ObjectValue: + case JsonValue::String: + case JsonValue::Array: + case JsonValue::Object: offset = val; break; - case NullValue: - case BooleanValue: + case JsonValue::Null: + case JsonValue::Boolean: default: break; } @@ -274,9 +278,13 @@ bool Value::isValid(const Base *b) const return true; if (s < 0 || offset + s > (int)b->tableOffset) return false; - if (type == ArrayValue) + if (type == JsonValue::Array) return static_cast<Array *>(objectOrArray(b))->isValid(); - if (type == ObjectValue) + if (type == JsonValue::Object) return static_cast<Object *>(objectOrArray(b))->isValid(); return true; } + +} + +} diff --git a/src/qjson_p.h b/src/qjson_p.h index 86a6ba5..5c3256d 100644 --- a/src/qjson_p.h +++ b/src/qjson_p.h @@ -112,6 +112,8 @@ namespace QtJson { +namespace Private { + struct Array; struct Object; struct Value; @@ -522,13 +524,13 @@ struct Header { inline bool Value::toBoolean() const { - Q_ASSERT(type == BooleanValue); + Q_ASSERT(type == JsonValue::Boolean); return val != 0; } inline double Value::toNumber(const Base *b) const { - Q_ASSERT(type == NumberValue); + Q_ASSERT(type == JsonValue::Number); if (latinOrIntValue) return int_val; @@ -542,7 +544,7 @@ inline double Value::toNumber(const Base *b) const inline int Value::toInt() const { - Q_ASSERT(type == NumberValue && latinOrIntValue); + Q_ASSERT(type == JsonValue::Number && latinOrIntValue); return int_val; } @@ -560,19 +562,19 @@ inline QString Value::toString(const Base *b) const inline String Value::asString(const Base *b) const { - Q_ASSERT(type == StringValue && !latinOrIntValue); + Q_ASSERT(type == JsonValue::String && !latinOrIntValue); return String(data(b)); } inline Latin1String Value::asLatin1String(const Base *b) const { - Q_ASSERT(type == StringValue && latinOrIntValue); + Q_ASSERT(type == JsonValue::String && latinOrIntValue); return Latin1String(data(b)); } inline Base *Value::objectOrArray(const Base *b) const { - Q_ASSERT(type == ArrayValue || type == ObjectValue); + Q_ASSERT(type == JsonValue::Array || type == JsonValue::Object); return reinterpret_cast<Base *>(data(b)); } @@ -588,7 +590,7 @@ struct Data { { ref.store(0); } - inline Data(int reserved, ValueType valueType) + inline Data(int reserved, JsonValue::ValueType valueType) : compactionCounter(0), valid(Validated), rawData(0) { ref.store(0); @@ -599,7 +601,7 @@ struct Data { header->version = 1; Base *b = header->root(); b->size = sizeof(Base); - b->is_object = (valueType == ObjectValue); + b->is_object = (valueType == JsonValue::Object); b->tableOffset = sizeof(Base); b->length = 0; } @@ -652,4 +654,6 @@ struct Data { } +} + #endif // QJSON_P_H diff --git a/src/qjsonarray.cpp b/src/qjsonarray.cpp index 4d3a969..34298aa 100644 --- a/src/qjsonarray.cpp +++ b/src/qjsonarray.cpp @@ -55,7 +55,7 @@ JsonArray::JsonArray() { } -JsonArray::JsonArray(Data *data, Array *array) +JsonArray::JsonArray(Private::Data *data, Private::Array *array) : d(data), a(array) { d->ref.ref(); @@ -136,7 +136,7 @@ bool JsonArray::isEmpty() const JsonValue JsonArray::at(int i) const { if (!a || i < 0 || i >= (int)a->length) - return JsonValue(UndefinedValue); + return JsonValue(JsonValue::Undefined); return JsonValue(d, a, a->at(i)); } @@ -171,7 +171,7 @@ void JsonArray::removeAt(int i) JsonValue JsonArray::takeAt(int i) { if (!a || i < 0 || i >= (int)a->length) - return JsonValue(UndefinedValue); + return JsonValue(JsonValue::Undefined); detach(); @@ -190,14 +190,14 @@ void JsonArray::insert(int i, const JsonValue &value) bool compressed; int valueSize = value.requiredStorage(&compressed); - detach(valueSize + sizeof(Value)); + detach(valueSize + sizeof(Private::Value)); if (!a->length) - a->tableOffset = sizeof(Array); + a->tableOffset = sizeof(Private::Array); int valueOffset = a->reserveSpace(valueSize, i, 1); - Value &v = (*a)[i]; - v.type = (value.t == UndefinedValue ? NullValue : value.t); + Private::Value &v = (*a)[i]; + v.type = (value.t == JsonValue::Undefined ? JsonValue::Null : value.t); v.latinOrIntValue = compressed; v.latinKey = false; v.val = value.valueToStore(valueOffset); @@ -253,20 +253,20 @@ bool JsonArray::operator!=(const JsonArray &other) const void JsonArray::detach(uint reserve) { if (!d) { - d = new Data(reserve, ArrayValue); - a = static_cast<Array *>(d->header->root()); + d = new Private::Data(reserve, JsonValue::Array); + a = static_cast<Private::Array *>(d->header->root()); d->ref.ref(); return; } if (reserve == 0 && d->ref.load() == 1) return; - Data *x = d->detach(a, reserve); + Private::Data *x = d->detach(a, reserve); x->ref.ref(); if (!d->ref.deref()) delete d; d = x; - a = static_cast<Array *>(d->header->root()); + a = static_cast<Private::Array *>(d->header->root()); } void JsonArray::compact() const @@ -276,5 +276,5 @@ void JsonArray::compact() const const_cast<JsonArray *>(this)->detach(); d->compact(); - const_cast<JsonArray *>(this)->a = static_cast<Array *>(d->header->root()); + const_cast<JsonArray *>(this)->a = static_cast<Private::Array *>(d->header->root()); } diff --git a/src/qjsonarray.h b/src/qjsonarray.h index f0863c1..7155052 100644 --- a/src/qjsonarray.h +++ b/src/qjsonarray.h @@ -84,13 +84,13 @@ public: void detach(uint reserve = 0); private: - friend class Data; + friend class Private::Data; friend class JsonValue; friend class JsonDocument; - JsonArray(Data *data, Array *array); + JsonArray(Private::Data *data, Private::Array *array); void compact() const; - Data *d; - Array *a; + Private::Data *d; + Private::Array *a; }; } diff --git a/src/qjsondocument.cpp b/src/qjsondocument.cpp index a5c39ab..376770d 100644 --- a/src/qjsondocument.cpp +++ b/src/qjsondocument.cpp @@ -68,7 +68,7 @@ JsonDocument::JsonDocument(const JsonArray &array) setArray(array); } -JsonDocument::JsonDocument(Data *data) +JsonDocument::JsonDocument(Private::Data *data) : d(data) { Q_ASSERT(d); @@ -104,16 +104,16 @@ JsonDocument &JsonDocument::operator =(const JsonDocument &other) JsonDocument JsonDocument::fromBinaryData(const QByteArray &data) { - Header *h = (Header *) data.constData(); + Private::Header *h = (Private::Header *) data.constData(); - if (data.size() < (int)(sizeof(Header) + sizeof(Base)) || + if (data.size() < (int)(sizeof(Private::Header) + sizeof(Private::Base)) || h->tag != QBJS_Tag || h->version != 1u || - sizeof(Header) + h->root()->size > (uint)data.size()) + sizeof(Private::Header) + h->root()->size > (uint)data.size()) return JsonDocument(); char *raw = (char *)malloc(data.size()); memcpy(raw, data.constData(), data.size()); - Data *d = new Data(raw, data.size()); + Private::Data *d = new Private::Data(raw, data.size()); return JsonDocument(d); } @@ -137,9 +137,9 @@ QVariant JsonDocument::toVariant() const return QVariant(); if (d->header->root()->isArray()) - return JsonArray(d, static_cast<Array *>(d->header->root())).toVariantList(); + return JsonArray(d, static_cast<Private::Array *>(d->header->root())).toVariantList(); else - return JsonObject(d, static_cast<Object *>(d->header->root())).toVariantMap(); + return JsonObject(d, static_cast<Private::Object *>(d->header->root())).toVariantMap(); } QByteArray JsonDocument::toJson() const @@ -150,9 +150,9 @@ QByteArray JsonDocument::toJson() const QByteArray json; if (d->header->root()->isArray()) - QJsonWriter::arrayToJson(static_cast<Array *>(d->header->root()), json, 0); + QJsonWriter::arrayToJson(static_cast<Private::Array *>(d->header->root()), json, 0); else - QJsonWriter::objectToJson(static_cast<Object *>(d->header->root()), json, 0); + QJsonWriter::objectToJson(static_cast<Private::Object *>(d->header->root()), json, 0); return json; } @@ -176,24 +176,24 @@ QByteArray JsonDocument::toBinaryData() const if (!d || !d->rawData) return QByteArray(); - return QByteArray(d->rawData, d->header->root()->size + sizeof(Header)); + return QByteArray(d->rawData, d->header->root()->size + sizeof(Private::Header)); } -ValueType JsonDocument::type() const +JsonValue::ValueType JsonDocument::type() const { if (!d) - return NullValue; + return JsonValue::Null; - Header *h = (Header *)d->rawData; - return h->root()->isArray() ? ArrayValue : ObjectValue; + Private::Header *h = (Private::Header *)d->rawData; + return h->root()->isArray() ? JsonValue::Array : JsonValue::Object; } JsonObject JsonDocument::object() const { if (d) { - Base *b = d->header->root(); + Private::Base *b = d->header->root(); if (b->isObject()) - return JsonObject(d, static_cast<Object *>(b)); + return JsonObject(d, static_cast<Private::Object *>(b)); } return JsonObject(); } @@ -201,9 +201,9 @@ JsonObject JsonDocument::object() const JsonArray JsonDocument::array() const { if (d) { - Base *b = d->header->root(); + Private::Base *b = d->header->root(); if (b->isArray()) - return JsonArray(d, static_cast<Array *>(b)); + return JsonArray(d, static_cast<Private::Array *>(b)); } return JsonArray(); } @@ -215,7 +215,7 @@ void JsonDocument::setObject(const JsonObject &object) d = object.d; if (!d) { - d = new Data(0, ObjectValue); + d = new Private::Data(0, JsonValue::Object); } else if (d->compactionCounter) { object.compact(); d = object.d; @@ -237,7 +237,7 @@ void JsonDocument::setArray(const JsonArray &array) d = array.d; if (!d) { - d = new Data(0, ArrayValue); + d = new Private::Data(0, JsonValue::Array); } else if (d->compactionCounter) { array.compact(); d = array.d; @@ -264,11 +264,11 @@ bool JsonDocument::operator==(const JsonDocument &other) const return false; if (d->header->root()->isObject()) - return JsonObject(d, static_cast<Object *>(d->header->root())) - == JsonObject(other.d, static_cast<Object *>(other.d->header->root())); + return JsonObject(d, static_cast<Private::Object *>(d->header->root())) + == JsonObject(other.d, static_cast<Private::Object *>(other.d->header->root())); else - return JsonArray(d, static_cast<Array *>(d->header->root())) - == JsonArray(other.d, static_cast<Array *>(other.d->header->root())); + return JsonArray(d, static_cast<Private::Array *>(d->header->root())) + == JsonArray(other.d, static_cast<Private::Array *>(other.d->header->root())); } bool JsonDocument::operator!=(const JsonDocument &other) const @@ -281,11 +281,11 @@ bool JsonDocument::isValid() if (!d) return false; - if (d->valid == Data::Unchecked) + if (d->valid == Private::Data::Unchecked) // Unchecked, check for validity d->validate(); - if (d->valid != Data::Validated) { + if (d->valid != Private::Data::Validated) { if (!d->ref.deref()) delete d; d = 0; diff --git a/src/qjsondocument.h b/src/qjsondocument.h index c83054e..d24b7c2 100644 --- a/src/qjsondocument.h +++ b/src/qjsondocument.h @@ -43,6 +43,7 @@ #define QJSONDOCUMENT_H #include <qjsonglobal.h> +#include <qjsonvalue.h> #include <qvariant.h> namespace QtJson { @@ -69,7 +70,7 @@ public: bool isEmpty() const; - ValueType type() const; + JsonValue::ValueType type() const; JsonObject object() const; JsonArray array() const; @@ -83,12 +84,12 @@ public: bool isValid(); private: - friend class Data; + friend class Private::Data; friend class JsonValue; friend class QJsonParser; - JsonDocument(Data *data); + JsonDocument(Private::Data *data); - Data *d; + Private::Data *d; }; } diff --git a/src/qjsonglobal.h b/src/qjsonglobal.h index d35d706..3f7c15d 100644 --- a/src/qjsonglobal.h +++ b/src/qjsonglobal.h @@ -53,15 +53,14 @@ namespace QtJson { -// ### FIXME -// namespace Private { + namespace Private { struct Data; struct Base; struct Object; struct Header; struct Array; struct Value; -// }; + }; class JsonValue; class JsonObject; @@ -69,16 +68,6 @@ namespace QtJson class JsonDocument; class QJsonParser; - enum ValueType { - NullValue = 0x0, - BooleanValue = 0x1, - NumberValue = 0x2, - StringValue = 0x3, - ArrayValue = 0x4, - ObjectValue = 0x5, - UndefinedValue = 0x80 - }; - const uint QBJS_Tag = ('q') | ('b' << 8) | ('j' << 16) | ('s' << 24); } diff --git a/src/qjsonobject.cpp b/src/qjsonobject.cpp index 243b1f2..fb4f0e0 100644 --- a/src/qjsonobject.cpp +++ b/src/qjsonobject.cpp @@ -54,7 +54,7 @@ JsonObject::JsonObject() { } -JsonObject::JsonObject(Data *data, Object *object) +JsonObject::JsonObject(Private::Data *data, Private::Object *object) : d(data), o(object) { Q_ASSERT(d); @@ -105,7 +105,7 @@ QVariantMap JsonObject::toVariantMap() const { QVariantMap map; for (uint i = 0; i < o->length; ++i) { - Entry *e = o->entryAt(i); + Private::Entry *e = o->entryAt(i); map.insert(e->key(), JsonValue(d, o, e->value).toVariant()); } return map; @@ -127,7 +127,7 @@ QStringList JsonObject::keys() const QStringList keys; for (uint i = 0; i < o->length; ++i) { - Entry *e = o->entryAt(i); + Private::Entry *e = o->entryAt(i); keys.append(e->key()); } @@ -156,7 +156,7 @@ JsonValue JsonObject::value(const QString &key) const return JsonValue(); for (uint i = 0; i < o->length; ++i) { - Entry *e = o->entryAt(i); + Private::Entry *e = o->entryAt(i); if (e->value.latinKey) { if (e->shallowLatin1Key() == key) return JsonValue(d, o, e->value); @@ -165,12 +165,12 @@ JsonValue JsonObject::value(const QString &key) const return JsonValue(d, o, e->value); } } - return JsonValue(UndefinedValue); + return JsonValue(JsonValue::Undefined); } void JsonObject::insert(const QString &key, const JsonValue &value) { - if (value.t == UndefinedValue) { + if (value.t == JsonValue::Undefined) { remove(key); return; } @@ -178,14 +178,14 @@ void JsonObject::insert(const QString &key, const JsonValue &value) bool latinOrIntValue; int valueSize = value.requiredStorage(&latinOrIntValue); - bool latinKey = useCompressed(key); - int valueOffset = sizeof(Entry) + qStringSize(key, latinKey); + bool latinKey = Private::useCompressed(key); + int valueOffset = sizeof(Private::Entry) + Private::qStringSize(key, latinKey); int requiredSize = valueOffset + valueSize; - detach(requiredSize + sizeof(offset)); // offset for the new index entry + detach(requiredSize + sizeof(Private::offset)); // offset for the new index entry if (!o->length) - o->tableOffset = sizeof(Object); + o->tableOffset = sizeof(Private::Object); int pos = o->indexOf(key); @@ -198,12 +198,12 @@ void JsonObject::insert(const QString &key, const JsonValue &value) } o->reserveSpace(requiredSize, pos, newIndex); - Entry *e = o->entryAt(pos); + Private::Entry *e = o->entryAt(pos); e->value.type = value.t; e->value.latinKey = latinKey; e->value.latinOrIntValue = latinOrIntValue; e->value.val = value.valueToStore((char *)e - (char *)o + valueOffset); - copyString((char *)(e + 1), key, latinKey); + Private::copyString((char *)(e + 1), key, latinKey); if (valueSize) value.copyData((char *)e + valueOffset, latinOrIntValue); } @@ -227,13 +227,13 @@ void JsonObject::remove(const QString &key) JsonValue JsonObject::take(const QString &key) { if (!o) - return JsonValue(UndefinedValue); + return JsonValue(JsonValue::Undefined); int index = o->indexOf(key); if (index < 0) - return JsonValue(UndefinedValue); + return JsonValue(JsonValue::Undefined); - Entry *e = o->entryAt(index); + Private::Entry *e = o->entryAt(index); o->removeItems(index, 1); ++d->compactionCounter; if (d->compactionCounter > 32 && d->compactionCounter >= (int)o->length/2) @@ -263,7 +263,7 @@ bool JsonObject::operator==(const JsonObject &other) const return false; for (uint i = 0; i < o->length; ++i) { - Entry *e = o->entryAt(i); + Private::Entry *e = o->entryAt(i); JsonValue v(d, o, e->value); if (other.value(e->key()) != v) return false; @@ -280,20 +280,20 @@ bool JsonObject::operator!=(const JsonObject &other) const void JsonObject::detach(uint reserve) { if (!d) { - d = new Data(reserve, ObjectValue); - o = static_cast<Object *>(d->header->root()); + d = new Private::Data(reserve, JsonValue::Object); + o = static_cast<Private::Object *>(d->header->root()); d->ref.ref(); return; } if (reserve == 0 && d->ref.load() == 1) return; - Data *x = d->detach(o, reserve); + Private::Data *x = d->detach(o, reserve); x->ref.ref(); if (!d->ref.deref()) delete d; d = x; - o = static_cast<Object *>(d->header->root()); + o = static_cast<Private::Object *>(d->header->root()); } void JsonObject::compact() const @@ -303,5 +303,5 @@ void JsonObject::compact() const const_cast<JsonObject *>(this)->detach(); d->compact(); - const_cast<JsonObject *>(this)->o = static_cast<Object *>(d->header->root()); + const_cast<JsonObject *>(this)->o = static_cast<Private::Object *>(d->header->root()); } diff --git a/src/qjsonobject.h b/src/qjsonobject.h index e7d9261..3e17a42 100644 --- a/src/qjsonobject.h +++ b/src/qjsonobject.h @@ -82,15 +82,15 @@ public: void detach(uint reserve = 0); private: - friend class Data; + friend class Private::Data; friend class JsonValue; friend class JsonDocument; friend class QJsonParser; - JsonObject(Data *data, Object *object); + JsonObject(Private::Data *data, Private::Object *object); void compact() const; - Data *d; - Object *o; + Private::Data *d; + Private::Object *o; }; } diff --git a/src/qjsonparser.cpp b/src/qjsonparser.cpp index 9d33ff0..1163bb6 100644 --- a/src/qjsonparser.cpp +++ b/src/qjsonparser.cpp @@ -162,11 +162,11 @@ QtJson::JsonDocument QJsonParser::parse() data = (char *)malloc(dataLength); // fill in Header data - Header *h = (Header *)data; + Private::Header *h = (Private::Header *)data; h->tag = QBJS_Tag; h->version = 1u; - current = sizeof(Header); + current = sizeof(Private::Header); char token = nextToken(); DEBUG << token; @@ -182,7 +182,7 @@ QtJson::JsonDocument QJsonParser::parse() END; { - Data *d = new Data(data, current); + Private::Data *d = new Private::Data(data, current); return JsonDocument(d); } @@ -201,7 +201,7 @@ error: bool QJsonParser::parseObject() { - int objectOffset = reserveSpace(sizeof(Object)); + int objectOffset = reserveSpace(sizeof(Private::Object)); BEGIN << "parseObject pos=" << objectOffset << current << json; QVarLengthArray<uint> offsets; @@ -238,7 +238,7 @@ bool QJsonParser::parseObject() #endif } - Object *o = (Object *)(data + objectOffset); + Private::Object *o = (Private::Object *)(data + objectOffset); o->tableOffset = table - objectOffset; o->size = current - objectOffset; o->is_object = true; @@ -254,7 +254,7 @@ bool QJsonParser::parseObject() */ bool QJsonParser::parseMember(int baseOffset) { - int entryOffset = reserveSpace(sizeof(Entry)); + int entryOffset = reserveSpace(sizeof(Private::Entry)); BEGIN << "parseMember pos=" << entryOffset; bool latin1; @@ -263,12 +263,12 @@ bool QJsonParser::parseMember(int baseOffset) char token = nextToken(); if (token != NameSeparator) return false; - Value val; + Private::Value val; if (!parseValue(&val, baseOffset)) return false; // finalize the entry - Entry *e = (Entry *)(data + entryOffset); + Private::Entry *e = (Private::Entry *)(data + entryOffset); e->value = val; e->value.latinKey = latin1; @@ -282,9 +282,9 @@ bool QJsonParser::parseMember(int baseOffset) bool QJsonParser::parseArray() { BEGIN << "parseArray"; - int arrayOffset = reserveSpace(sizeof(Array)); + int arrayOffset = reserveSpace(sizeof(Private::Array)); - QVarLengthArray<Value> values; + QVarLengthArray<Private::Value> values; if (!eatSpace()) return false; @@ -292,7 +292,7 @@ bool QJsonParser::parseArray() nextToken(); } else { while (1) { - Value val; + Private::Value val; if (!parseValue(&val, arrayOffset)) return false; values.append(val); @@ -308,12 +308,12 @@ bool QJsonParser::parseArray() int table = arrayOffset; // finalize the object if (values.size()) { - int tableSize = values.size()*sizeof(Value); + int tableSize = values.size()*sizeof(Private::Value); table = reserveSpace(tableSize); memcpy(data + table, values.constData(), tableSize); } - Array *a = (Array *)(data + arrayOffset); + Private::Array *a = (Private::Array *)(data + arrayOffset); a->tableOffset = table - arrayOffset; a->size = current - arrayOffset; a->is_object = false; @@ -329,7 +329,7 @@ value = false / null / true / object / array / number / string */ -bool QJsonParser::parseValue(Value *val, int baseOffset) +bool QJsonParser::parseValue(Private::Value *val, int baseOffset) { BEGIN << "parse Value" << json; val->_dummy = 0; @@ -341,7 +341,7 @@ bool QJsonParser::parseValue(Value *val, int baseOffset) if (*json++ == 'u' && *json++ == 'l' && *json++ == 'l') { - val->type = NullValue; + val->type = JsonValue::Null; DEBUG << "value: null"; END; return true; @@ -353,7 +353,7 @@ bool QJsonParser::parseValue(Value *val, int baseOffset) if (*json++ == 'r' && *json++ == 'u' && *json++ == 'e') { - val->type = BooleanValue; + val->type = JsonValue::Boolean; val->val = true; DEBUG << "value: true"; END; @@ -367,7 +367,7 @@ bool QJsonParser::parseValue(Value *val, int baseOffset) *json++ == 'l' && *json++ == 's' && *json++ == 'e') { - val->type = BooleanValue; + val->type = JsonValue::Boolean; val->val = false; DEBUG << "value: false"; END; @@ -375,7 +375,7 @@ bool QJsonParser::parseValue(Value *val, int baseOffset) } return false; case Quote: { - val->type = StringValue; + val->type = JsonValue::String; val->val = current - baseOffset; bool latin1; if (!parseString(&latin1)) @@ -386,7 +386,7 @@ bool QJsonParser::parseValue(Value *val, int baseOffset) return true; } case BeginArray: - val->type = ArrayValue; + val->type = JsonValue::Array; val->val = current - baseOffset; if (!parseArray()) return false; @@ -394,7 +394,7 @@ bool QJsonParser::parseValue(Value *val, int baseOffset) END; return true; case BeginObject: - val->type = ObjectValue; + val->type = JsonValue::Object; val->val = current - baseOffset; if (!parseObject()) return false; @@ -430,10 +430,10 @@ bool QJsonParser::parseValue(Value *val, int baseOffset) */ -bool QJsonParser::parseNumber(Value *val, int baseOffset) +bool QJsonParser::parseNumber(Private::Value *val, int baseOffset) { BEGIN << "parseNumber" << json; - val->type = NumberValue; + val->type = JsonValue::Number; const char *start = json; bool isInt = true; @@ -679,7 +679,7 @@ bool QJsonParser::parseString(bool *latin1) // no unicode string, we are done if (*latin1) { // write string length - *(qle_ushort *)(data + stringPos) = current - outStart - sizeof(ushort); + *(Private::qle_ushort *)(data + stringPos) = current - outStart - sizeof(ushort); int pos = reserveSpace((4 - current) & 3); while (pos & 3) data[pos++] = 0; @@ -706,11 +706,11 @@ bool QJsonParser::parseString(bool *latin1) } if (ch > 0xffff) { int pos = reserveSpace(4); - *(qle_ushort *)(data + pos) = QChar::highSurrogate(ch); - *(qle_ushort *)(data + pos + 2) = QChar::lowSurrogate(ch); + *(Private::qle_ushort *)(data + pos) = QChar::highSurrogate(ch); + *(Private::qle_ushort *)(data + pos + 2) = QChar::lowSurrogate(ch); } else { int pos = reserveSpace(2); - *(qle_ushort *)(data + pos) = (ushort)ch; + *(Private::qle_ushort *)(data + pos) = (ushort)ch; } } ++json; @@ -719,7 +719,7 @@ bool QJsonParser::parseString(bool *latin1) return false; // write string length - *(qle_int *)(data + stringPos) = (current - outStart - sizeof(int))/2; + *(Private::qle_int *)(data + stringPos) = (current - outStart - sizeof(int))/2; int pos = reserveSpace((4 - current) & 3); while (pos & 3) data[pos++] = 0; diff --git a/src/qjsonparser_p.h b/src/qjsonparser_p.h index 0828676..98b5d8e 100644 --- a/src/qjsonparser_p.h +++ b/src/qjsonparser_p.h @@ -72,8 +72,8 @@ private: bool parseArray(); bool parseMember(int baseOffset); bool parseString(bool *latin1); - bool parseValue(Value *val, int baseOffset); - bool parseNumber(Value *val, int baseOffset); + bool parseValue(Private::Value *val, int baseOffset); + bool parseNumber(Private::Value *val, int baseOffset); const char *json; const char *end; diff --git a/src/qjsonvalue.cpp b/src/qjsonvalue.cpp index 07d043c..1df9edd 100644 --- a/src/qjsonvalue.cpp +++ b/src/qjsonvalue.cpp @@ -51,37 +51,37 @@ using namespace QtJson; -static const Base emptyArray = { { qToLittleEndian(sizeof(Base)) }, { 0 }, { 0 } }; -static const Base emptyObject = { { qToLittleEndian(sizeof(Base)) }, { 0 }, { 0 } }; +static const Private::Base emptyArray = { { qToLittleEndian(sizeof(Private::Base)) }, { 0 }, { 0 } }; +static const Private::Base emptyObject = { { qToLittleEndian(sizeof(Private::Base)) }, { 0 }, { 0 } }; JsonValue::JsonValue(ValueType type) : t(type), d(0), dbl(0.) { } -JsonValue::JsonValue(Data *data, Base *base, const Value &v) +JsonValue::JsonValue(Private::Data *data, Private::Base *base, const Private::Value &v) : d(0) { t = (ValueType)(uint)v.type; switch (t) { - case UndefinedValue: - case NullValue: + case Undefined: + case Null: dbl = 0; break; - case BooleanValue: + case Boolean: b = v.toBoolean(); break; - case NumberValue: + case Number: dbl = v.toNumber(base); break; - case StringValue: { + case String: { QString s = v.toString(base); stringData = *reinterpret_cast<QStringData **>(&s); stringData->ref.ref(); break; } - case ArrayValue: - case ObjectValue: + case Array: + case Object: d = data; this->base = v.objectOrArray(base); break; @@ -91,32 +91,32 @@ JsonValue::JsonValue(Data *data, Base *base, const Value &v) } JsonValue::JsonValue(bool b) - : t(BooleanValue), d(0) + : t(Boolean), d(0) { this->b = b; } JsonValue::JsonValue(double n) - : t(NumberValue), d(0) + : t(Number), d(0) { this->dbl = n; } JsonValue::JsonValue(int n) - : t(NumberValue), d(0) + : t(Number), d(0) { this->dbl = n; } JsonValue::JsonValue(const QString &s) - : t(StringValue), d(0) + : t(String), d(0) { stringData = *(QStringData **)(&s); stringData->ref.ref(); } JsonValue::JsonValue(const QLatin1String &s) - : t(StringValue), d(0) + : t(String), d(0) { // ### FIXME: Avoid creating the temp QString below QString str(s); @@ -125,7 +125,7 @@ JsonValue::JsonValue(const QLatin1String &s) } JsonValue::JsonValue(const JsonArray &a) - : t(ArrayValue), d(a.d) + : t(Array), d(a.d) { a.compact(); array = a.a; @@ -134,7 +134,7 @@ JsonValue::JsonValue(const JsonArray &a) } JsonValue::JsonValue(const JsonObject &o) - : t(ObjectValue), d(o.d) + : t(Object), d(o.d) { o.compact(); object = o.o; @@ -145,7 +145,7 @@ JsonValue::JsonValue(const JsonObject &o) JsonValue::~JsonValue() { - if (t == StringValue && stringData && !stringData->ref.deref()) + if (t == String && stringData && !stringData->ref.deref()) free(stringData); if (d && !d->ref.deref()) @@ -160,13 +160,13 @@ JsonValue::JsonValue(const JsonValue &other) if (d) d->ref.ref(); - if (t == StringValue && stringData) + if (t == String && stringData) stringData->ref.ref(); } JsonValue &JsonValue::operator =(const JsonValue &other) { - if (t == StringValue && stringData && !stringData->ref.deref()) + if (t == String && stringData && !stringData->ref.deref()) free(stringData); t = other.t; @@ -182,7 +182,7 @@ JsonValue &JsonValue::operator =(const JsonValue &other) } - if (t == StringValue && stringData) + if (t == String && stringData) stringData->ref.ref(); return *this; @@ -220,25 +220,25 @@ JsonValue JsonValue::fromVariant(const QVariant &variant) QVariant JsonValue::toVariant() const { switch (t) { - case BooleanValue: + case Boolean: return b; - case NumberValue: + case Number: return dbl; - case StringValue: + case String: return toString(); - case ArrayValue: + case Array: return JsonArray(d, array).toVariantList(); - case ObjectValue: + case Object: return JsonObject(d, object).toVariantMap(); - case NullValue: - case UndefinedValue: + case Null: + case Undefined: break; } return QVariant(); } -ValueType JsonValue::type() const +JsonValue::ValueType JsonValue::type() const { return t; } @@ -280,28 +280,28 @@ void JsonValue::setValue(const JsonObject &o) bool JsonValue::toBool() const { - if (t != BooleanValue) + if (t != Boolean) return false; return b; } double JsonValue::toNumber() const { - if (t != NumberValue) + if (t != Number) return 0; return dbl; } int JsonValue::toInt() const { - if (t != NumberValue) + if (t != Number) return 0; return (int)dbl; } QString JsonValue::toString() const { - if (t != StringValue) + if (t != String) return QString(); stringData->ref.ref(); // the constructor below doesn't add a ref. return QString(*(const QConstStringData<1> *)stringData); @@ -309,7 +309,7 @@ QString JsonValue::toString() const JsonArray JsonValue::toArray() const { - if (!d || t != ArrayValue) + if (!d || t != Array) return JsonArray(); return JsonArray(d, array); @@ -317,7 +317,7 @@ JsonArray JsonValue::toArray() const JsonObject JsonValue::toObject() const { - if (!d || t != ObjectValue) + if (!d || t != Object) return JsonObject(); return JsonObject(d, object); @@ -329,18 +329,18 @@ bool JsonValue::operator==(const JsonValue &other) const return false; switch (t) { - case UndefinedValue: - case NullValue: + case Undefined: + case Null: break; - case BooleanValue: + case Boolean: return b == other.b; - case NumberValue: + case Number: return dbl == other.dbl; - case StringValue: + case String: return toString() == other.toString(); - case ArrayValue: + case Array: return JsonArray(d, array) == JsonArray(other.d, other.array); - case ObjectValue: + case Object: return JsonObject(d, object) == JsonObject(other.d, other.object); } return true; @@ -356,35 +356,35 @@ void JsonValue::detach() if (!d) return; - Data *x = d->detach(object); + Private::Data *x = d->detach(object); x->ref.ref(); if (!d->ref.deref()) delete d; d = x; - object = static_cast<Object *>(d->header->root()); + object = static_cast<Private::Object *>(d->header->root()); } int JsonValue::requiredStorage(bool *compressed) const { *compressed = false; switch (t) { - case NumberValue: - if (compressedNumber(dbl) != INT_MAX) { + case Number: + if (Private::compressedNumber(dbl) != INT_MAX) { *compressed = true; return 0; } return sizeof(double); - case StringValue: { + case String: { QString s = toString(); - *compressed = useCompressed(s); - return qStringSize(s, *compressed); + *compressed = Private::useCompressed(s); + return Private::qStringSize(s, *compressed); } - case ArrayValue: - case ObjectValue: - return base ? base->size : sizeof(Base); - case UndefinedValue: - case NullValue: - case BooleanValue: + case Array: + case Object: + return base ? base->size : sizeof(Private::Base); + case Undefined: + case Null: + case Boolean: break; } return 0; @@ -393,19 +393,19 @@ int JsonValue::requiredStorage(bool *compressed) const uint JsonValue::valueToStore(uint offset) const { switch (t) { - case UndefinedValue: - case NullValue: + case Undefined: + case Null: break; - case BooleanValue: + case Boolean: return b; - case NumberValue: { - int c = compressedNumber(dbl); + case Number: { + int c = Private::compressedNumber(dbl); if (c != INT_MAX) return c; } - case StringValue: - case ArrayValue: - case ObjectValue: + case String: + case Array: + case Object: return offset; } return 0; @@ -414,21 +414,21 @@ uint JsonValue::valueToStore(uint offset) const void JsonValue::copyData(char *dest, bool compressed) const { switch (t) { - case NumberValue: + case Number: if (!compressed) { *((quint64 *)dest) = qToLittleEndian(ui); } break; - case StringValue: { + case String: { QString str = toString(); - copyString(dest, str, compressed); + Private::copyString(dest, str, compressed); break; } - case ArrayValue: - case ObjectValue: { - const Base *b = base; + case Array: + case Object: { + const Private::Base *b = base; if (!b) - b = (t == ArrayValue ? &emptyArray : &emptyObject); + b = (t == Array ? &emptyArray : &emptyObject); memcpy(dest, b, b->size); break; } diff --git a/src/qjsonvalue.h b/src/qjsonvalue.h index 611cf76..0a66216 100644 --- a/src/qjsonvalue.h +++ b/src/qjsonvalue.h @@ -50,7 +50,17 @@ namespace QtJson { class Q_JSON_EXPORT JsonValue { public: - JsonValue(ValueType = NullValue); + enum ValueType { + Null = 0x0, + Boolean = 0x1, + Number = 0x2, + String = 0x3, + Array = 0x4, + Object = 0x5, + Undefined = 0x80 + }; + + JsonValue(ValueType = Null); JsonValue(bool b); JsonValue(double n); JsonValue(int n); @@ -68,7 +78,7 @@ public: QVariant toVariant() const; ValueType type() const; - inline bool isUndefined() const { return type() == UndefinedValue; } + inline bool isUndefined() const { return type() == Undefined; } void setValue(bool); void setValue(double); @@ -97,22 +107,22 @@ private: friend class Value; friend class JsonArray; friend class JsonObject; - JsonValue(Data *d, Base *b, const Value& v); + JsonValue(Private::Data *d, Private::Base *b, const Private::Value& v); int requiredStorage(bool *compressed) const; uint valueToStore(uint offset) const; void copyData(char *dest, bool compressed) const; ValueType t; - Data *d; // needed for Objects and Arrays + Private::Data *d; // needed for Objects and Arrays union { char raw[sizeof(double)]; quint64 ui; bool b; double dbl; QStringData *stringData; - Base *base; - Object *object; - Array *array; + Private::Base *base; + Private::Object *object; + Private::Array *array; }; }; diff --git a/src/qjsonwriter.cpp b/src/qjsonwriter.cpp index b151490..bbb6773 100644 --- a/src/qjsonwriter.cpp +++ b/src/qjsonwriter.cpp @@ -44,8 +44,8 @@ using namespace QtJson; -static void objectContentToJson(const Object *o, QByteArray &json, int indent); -static void arrayContentToJson(const Array *a, QByteArray &json, int indent); +static void objectContentToJson(const Private::Object *o, QByteArray &json, int indent); +static void arrayContentToJson(const Private::Array *a, QByteArray &json, int indent); // some code from qutfcodec.cpp, inlined here for performance reasons // to allow fast escaping of strings @@ -175,40 +175,40 @@ static QByteArray escapedString(const QString &s) return ba; } -static void valueToJson(const Base *b, const Value &v, QByteArray &json, int indent) +static void valueToJson(const Private::Base *b, const Private::Value &v, QByteArray &json, int indent) { - ValueType type = (ValueType)(uint)v.type; + JsonValue::ValueType type = (JsonValue::ValueType)(uint)v.type; switch (type) { - case BooleanValue: + case JsonValue::Boolean: json += v.toBoolean() ? "true" : "false"; break; - case NumberValue: + case JsonValue::Number: json += QByteArray::number(v.toNumber(b)); break; - case StringValue: + case JsonValue::String: json += '"'; json += escapedString(v.toString(b)); json += '"'; break; - case ArrayValue: + case JsonValue::Array: json += "[\n"; - arrayContentToJson(static_cast<Array *>(v.objectOrArray(b)), json, indent + 1); + arrayContentToJson(static_cast<Private::Array *>(v.objectOrArray(b)), json, indent + 1); json += QByteArray(4*indent, ' '); json += "]"; break; - case ObjectValue: + case JsonValue::Object: json += "{\n"; - objectContentToJson(static_cast<Object *>(v.objectOrArray(b)), json, indent + 1); + objectContentToJson(static_cast<Private::Object *>(v.objectOrArray(b)), json, indent + 1); json += QByteArray(4*indent, ' '); json += "}"; break; - case NullValue: + case JsonValue::Null: default: json += "null"; } } -static void arrayContentToJson(const Array *a, QByteArray &json, int indent) +static void arrayContentToJson(const Private::Array *a, QByteArray &json, int indent) { if (!a || !a->length) return; @@ -230,7 +230,7 @@ static void arrayContentToJson(const Array *a, QByteArray &json, int indent) } -static void objectContentToJson(const Object *o, QByteArray &json, int indent) +static void objectContentToJson(const Private::Object *o, QByteArray &json, int indent) { if (!o || !o->length) return; @@ -239,7 +239,7 @@ static void objectContentToJson(const Object *o, QByteArray &json, int indent) uint i = 0; while (1) { - Entry *e = o->entryAt(i); + Private::Entry *e = o->entryAt(i); json += indentString; json += '"'; json += escapedString(e->key()); @@ -255,7 +255,7 @@ static void objectContentToJson(const Object *o, QByteArray &json, int indent) } } -void QJsonWriter::objectToJson(const Object *o, QByteArray &json, int indent) +void QJsonWriter::objectToJson(const Private::Object *o, QByteArray &json, int indent) { json.reserve(json.size() + (o ? o->size : 16)); json += "{\n"; @@ -264,7 +264,7 @@ void QJsonWriter::objectToJson(const Object *o, QByteArray &json, int indent) json += "}\n"; } -void QJsonWriter::arrayToJson(const Array *a, QByteArray &json, int indent) +void QJsonWriter::arrayToJson(const Private::Array *a, QByteArray &json, int indent) { json.reserve(json.size() + (a ? a->size : 16)); json += "[\n"; diff --git a/src/qjsonwriter_p.h b/src/qjsonwriter_p.h index 6d75f73..1098716 100644 --- a/src/qjsonwriter_p.h +++ b/src/qjsonwriter_p.h @@ -51,6 +51,7 @@ // // We mean it. // +#include <qjsonglobal.h> namespace QtJson { @@ -58,8 +59,8 @@ namespace QtJson class QJsonWriter { public: - static void objectToJson(const Object *o, QByteArray &json, int indent); - static void arrayToJson(const Array *a, QByteArray &json, int indent); + static void objectToJson(const Private::Object *o, QByteArray &json, int indent); + static void arrayToJson(const Private::Array *a, QByteArray &json, int indent); }; } diff --git a/tests/auto/tst_qtjson.cpp b/tests/auto/tst_qtjson.cpp index 0ab78a5..6d7485a 100644 --- a/tests/auto/tst_qtjson.cpp +++ b/tests/auto/tst_qtjson.cpp @@ -119,7 +119,7 @@ void TestQtJson::cleanup() void TestQtJson::testValueSimple() { JsonValue value(true); - QCOMPARE(value.type(), BooleanValue); + QCOMPARE(value.type(), JsonValue::Boolean); QCOMPARE(value.toNumber(), 0.); QCOMPARE(value.toInt(), 0); QCOMPARE(value.toString(), QString()); @@ -128,7 +128,7 @@ void TestQtJson::testValueSimple() QCOMPARE(value.toArray(), JsonArray()); value.setValue(999.); - QCOMPARE(value.type(), NumberValue); + QCOMPARE(value.type(), JsonValue::Number); QCOMPARE(value.toNumber(), 999.); QCOMPARE(value.toInt(), 999); QCOMPARE(value.toString(), QString()); @@ -188,7 +188,7 @@ void TestQtJson::testNumbers() for (int i = 0; i < n; ++i) array.append(numbers[i]); for (int i = 0; i < array.size(); ++i) { - QCOMPARE(array.at(i).type(), NumberValue); + QCOMPARE(array.at(i).type(), JsonValue::Number); QCOMPARE(array.at(i).toInt(), numbers[i]); QCOMPARE(array.at(i).toNumber(), (double)numbers[i]); } @@ -224,7 +224,7 @@ void TestQtJson::testNumbers() for (int i = 0; i < n; ++i) array.append(numbers[i]); for (int i = 0; i < array.size(); ++i) { - QCOMPARE(array.at(i).type(), NumberValue); + QCOMPARE(array.at(i).type(), JsonValue::Number); QCOMPARE(array.at(i).toNumber(), numbers[i]); } } @@ -235,10 +235,10 @@ void TestQtJson::testObjectSimple() { JsonObject object; object.insert("number", 999.); - QCOMPARE(object.value("number").type(), NumberValue); + QCOMPARE(object.value("number").type(), JsonValue::Number); QCOMPARE(object.value("number").toNumber(), 999.); object.insert("string", QString::fromLatin1("test")); - QCOMPARE(object.value("string").type(), StringValue); + QCOMPARE(object.value("string").type(), JsonValue::String); QCOMPARE(object.value("string").toString(), QString("test")); object.insert("boolean", true); QCOMPARE(object.value("boolean").toBool(), true); @@ -301,11 +301,11 @@ void TestQtJson::testArraySimple() array.append(JsonValue()); ++size; QCOMPARE(array.size(), size); - QCOMPARE(array.last().type(), NullValue); + QCOMPARE(array.last().type(), JsonValue::Null); QCOMPARE(array.last(), JsonValue()); - QCOMPARE(array.at(-1), JsonValue(UndefinedValue)); - QCOMPARE(array.at(array.size()), JsonValue(UndefinedValue)); + QCOMPARE(array.at(-1), JsonValue(JsonValue::Undefined)); + QCOMPARE(array.at(array.size()), JsonValue(JsonValue::Undefined)); } void TestQtJson::testValueObject() @@ -445,47 +445,47 @@ void TestQtJson::testObjectNestedEmpty() QCOMPARE(value.numKeys(), 0); object.insert("count", 0.); QCOMPARE(object.value("inner").toObject().numKeys(), 0); - QCOMPARE(object.value("inner").type(), ObjectValue); + QCOMPARE(object.value("inner").type(), JsonValue::Object); JsonDocument(object).toBinaryData(); QVERIFY(object.value("inner").toObject().isEmpty()); QVERIFY(object.value("inner2").toObject().isEmpty()); JsonObject reconstituted(JsonDocument::fromBinaryData(JsonDocument(object).toBinaryData()).object()); QCOMPARE(reconstituted.value("inner").toObject().numKeys(), 0); - QCOMPARE(reconstituted.value("inner").type(), ObjectValue); - QCOMPARE(reconstituted.value("inner2").type(), ObjectValue); + QCOMPARE(reconstituted.value("inner").type(), JsonValue::Object); + QCOMPARE(reconstituted.value("inner2").type(), JsonValue::Object); } void TestQtJson::testDocument() { JsonDocument doc; QCOMPARE(doc.isEmpty(), true); - QCOMPARE(doc.type(), NullValue); + QCOMPARE(doc.type(), JsonValue::Null); JsonObject object; doc.setObject(object); QCOMPARE(doc.isEmpty(), false); - QCOMPARE(doc.type(), ObjectValue); + QCOMPARE(doc.type(), JsonValue::Object); object.insert(QLatin1String("Key"), QLatin1String("Value")); doc.setObject(object); QCOMPARE(doc.isEmpty(), false); - QCOMPARE(doc.type(), ObjectValue); + QCOMPARE(doc.type(), JsonValue::Object); QVERIFY(doc.object() == object); QVERIFY(doc.array() == JsonArray()); doc = JsonDocument(); QCOMPARE(doc.isEmpty(), true); - QCOMPARE(doc.type(), NullValue); + QCOMPARE(doc.type(), JsonValue::Null); JsonArray array; doc.setArray(array); QCOMPARE(doc.isEmpty(), false); - QCOMPARE(doc.type(), ArrayValue); + QCOMPARE(doc.type(), JsonValue::Array); array.append(QLatin1String("Value")); doc.setArray(array); QCOMPARE(doc.isEmpty(), false); - QCOMPARE(doc.type(), ArrayValue); + QCOMPARE(doc.type(), JsonValue::Array); QVERIFY(doc.array() == array); QVERIFY(doc.object() == JsonObject()); @@ -504,18 +504,18 @@ void TestQtJson::testDocument() JsonDocument doc3; doc3.setObject(outer.value(QLatin1String("innter")).toObject()); - QCOMPARE(doc3.type(), ObjectValue); + QCOMPARE(doc3.type(), JsonValue::Object); QVERIFY(doc3.object().contains(QLatin1String("innerKey"))); QCOMPARE(doc3.object().value(QLatin1String("innerKey")), JsonValue(42)); JsonDocument doc4(outer.value(QLatin1String("innterArray")).toArray()); - QCOMPARE(doc4.type(), ArrayValue); + QCOMPARE(doc4.type(), JsonValue::Array); QCOMPARE(doc4.array().size(), 1); QCOMPARE(doc4.array().at(0), JsonValue(23)); JsonDocument doc5; doc5.setArray(outer.value(QLatin1String("innterArray")).toArray()); - QCOMPARE(doc5.type(), ArrayValue); + QCOMPARE(doc5.type(), JsonValue::Array); QCOMPARE(doc5.array().size(), 1); QCOMPARE(doc5.array().at(0), JsonValue(23)); } @@ -546,9 +546,9 @@ void TestQtJson::nullArrays() QVERIFY(nonNull != nullArray); QCOMPARE(nullArray.size(), 0); - QCOMPARE(nullArray.takeAt(0), JsonValue(UndefinedValue)); - QCOMPARE(nullArray.first(), JsonValue(UndefinedValue)); - QCOMPARE(nullArray.last(), JsonValue(UndefinedValue)); + QCOMPARE(nullArray.takeAt(0), JsonValue(JsonValue::Undefined)); + QCOMPARE(nullArray.first(), JsonValue(JsonValue::Undefined)); + QCOMPARE(nullArray.last(), JsonValue(JsonValue::Undefined)); nullArray.removeAt(0); nullArray.removeAt(-1); @@ -556,9 +556,9 @@ void TestQtJson::nullArrays() nullArray.removeAt(0); QCOMPARE(nullArray.size(), 0); - QCOMPARE(nullArray.takeAt(0), JsonValue(UndefinedValue)); - QCOMPARE(nullArray.first(), JsonValue(UndefinedValue)); - QCOMPARE(nullArray.last(), JsonValue(UndefinedValue)); + QCOMPARE(nullArray.takeAt(0), JsonValue(JsonValue::Undefined)); + QCOMPARE(nullArray.first(), JsonValue(JsonValue::Undefined)); + QCOMPARE(nullArray.last(), JsonValue(JsonValue::Undefined)); nullArray.removeAt(0); nullArray.removeAt(-1); @@ -580,7 +580,7 @@ void TestQtJson::nullObject() QCOMPARE(nullObject.keys(), QStringList()); nullObject.remove("foo"); QCOMPARE(nullObject, JsonObject()); - QCOMPARE(nullObject.take("foo"), JsonValue(UndefinedValue)); + QCOMPARE(nullObject.take("foo"), JsonValue(JsonValue::Undefined)); QCOMPARE(nullObject.contains("foo"), false); nullObject.detach(16); @@ -593,31 +593,31 @@ void TestQtJson::nullObject() QCOMPARE(nullObject.keys(), QStringList()); nullObject.remove("foo"); QCOMPARE(nullObject, JsonObject()); - QCOMPARE(nullObject.take("foo"), JsonValue(UndefinedValue)); + QCOMPARE(nullObject.take("foo"), JsonValue(JsonValue::Undefined)); QCOMPARE(nullObject.contains("foo"), false); } void TestQtJson::undefinedValues() { JsonObject object; - object.insert("Key", JsonValue(UndefinedValue)); + object.insert("Key", JsonValue(JsonValue::Undefined)); QCOMPARE(object.numKeys(), 0); object.insert("Key", QLatin1String("Value")); QCOMPARE(object.numKeys(), 1); - QCOMPARE(object.value("Key").type(), StringValue); - QCOMPARE(object.value("foo").type(), UndefinedValue); - object.insert("Key", JsonValue(UndefinedValue)); + QCOMPARE(object.value("Key").type(), JsonValue::String); + QCOMPARE(object.value("foo").type(), JsonValue::Undefined); + object.insert("Key", JsonValue(JsonValue::Undefined)); QCOMPARE(object.numKeys(), 0); - QCOMPARE(object.value("Key").type(), UndefinedValue); + QCOMPARE(object.value("Key").type(), JsonValue::Undefined); JsonArray array; - array.append(JsonValue(UndefinedValue)); + array.append(JsonValue(JsonValue::Undefined)); QCOMPARE(array.size(), 1); - QCOMPARE(array.at(0).type(), NullValue); + QCOMPARE(array.at(0).type(), JsonValue::Null); - QCOMPARE(array.at(1).type(), UndefinedValue); - QCOMPARE(array.at(-1).type(), UndefinedValue); + QCOMPARE(array.at(1).type(), JsonValue::Undefined); + QCOMPARE(array.at(-1).type(), JsonValue::Undefined); } @@ -641,15 +641,15 @@ void TestQtJson::fromVariantMap() 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); + QCOMPARE(object.value(QLatin1String("list")).type(), JsonValue::Array); JsonArray array = object.value(QLatin1String("list")).toArray(); QCOMPARE(array.size(), 4); - QCOMPARE(array.at(0).type(), BooleanValue); + QCOMPARE(array.at(0).type(), JsonValue::Boolean); QCOMPARE(array.at(0).toBool(), true); - QCOMPARE(array.at(1).type(), NullValue); - QCOMPARE(array.at(2).type(), NumberValue); + QCOMPARE(array.at(1).type(), JsonValue::Null); + QCOMPARE(array.at(2).type(), JsonValue::Number); QCOMPARE(array.at(2).toNumber(), 999.); - QCOMPARE(array.at(3).type(), StringValue); + QCOMPARE(array.at(3).type(), JsonValue::String); QCOMPARE(array.at(3).toString(), QLatin1String("foo")); } @@ -732,10 +732,10 @@ void TestQtJson::fromJson() QByteArray json = "[\n true\n]\n"; JsonDocument doc = JsonDocument::fromJson(json); QVERIFY(!doc.isEmpty()); - QCOMPARE(doc.type(), ArrayValue); + QCOMPARE(doc.type(), JsonValue::Array); JsonArray array = doc.array(); QCOMPARE(array.size(), 1); - QCOMPARE(array.at(0).type(), BooleanValue); + QCOMPARE(array.at(0).type(), JsonValue::Boolean); QCOMPARE(array.at(0).toBool(), true); QCOMPARE(doc.toJson(), json); } @@ -743,7 +743,7 @@ void TestQtJson::fromJson() QByteArray json = "[]"; JsonDocument doc = JsonDocument::fromJson(json); QVERIFY(!doc.isEmpty()); - QCOMPARE(doc.type(), ArrayValue); + QCOMPARE(doc.type(), JsonValue::Array); JsonArray array = doc.array(); QCOMPARE(array.size(), 0); } @@ -751,7 +751,7 @@ void TestQtJson::fromJson() QByteArray json = "{}"; JsonDocument doc = JsonDocument::fromJson(json); QVERIFY(!doc.isEmpty()); - QCOMPARE(doc.type(), ObjectValue); + QCOMPARE(doc.type(), JsonValue::Object); JsonObject object = doc.object(); QCOMPARE(object.numKeys(), 0); } @@ -759,7 +759,7 @@ void TestQtJson::fromJson() QByteArray json = "{\n \"Key\": true\n}\n"; JsonDocument doc = JsonDocument::fromJson(json); QVERIFY(!doc.isEmpty()); - QCOMPARE(doc.type(), ObjectValue); + QCOMPARE(doc.type(), JsonValue::Object); JsonObject object = doc.object(); QCOMPARE(object.numKeys(), 1); QCOMPARE(object.value("Key"), JsonValue(true)); @@ -769,42 +769,42 @@ void TestQtJson::fromJson() QByteArray json = "[ null, true, false, \"Foo\", 1, [], {} ]"; JsonDocument doc = JsonDocument::fromJson(json); QVERIFY(!doc.isEmpty()); - QCOMPARE(doc.type(), ArrayValue); + QCOMPARE(doc.type(), JsonValue::Array); JsonArray array = doc.array(); QCOMPARE(array.size(), 7); - QCOMPARE(array.at(0).type(), NullValue); - QCOMPARE(array.at(1).type(), BooleanValue); + QCOMPARE(array.at(0).type(), JsonValue::Null); + QCOMPARE(array.at(1).type(), JsonValue::Boolean); QCOMPARE(array.at(1).toBool(), true); - QCOMPARE(array.at(2).type(), BooleanValue); + QCOMPARE(array.at(2).type(), JsonValue::Boolean); QCOMPARE(array.at(2).toBool(), false); - QCOMPARE(array.at(3).type(), StringValue); + QCOMPARE(array.at(3).type(), JsonValue::String); QCOMPARE(array.at(3).toString(), QLatin1String("Foo")); - QCOMPARE(array.at(4).type(), NumberValue); + QCOMPARE(array.at(4).type(), JsonValue::Number); QCOMPARE(array.at(4).toInt(), 1); - QCOMPARE(array.at(5).type(), ArrayValue); + QCOMPARE(array.at(5).type(), JsonValue::Array); QCOMPARE(array.at(5).toArray().size(), 0); - QCOMPARE(array.at(6).type(), ObjectValue); + QCOMPARE(array.at(6).type(), JsonValue::Object); QCOMPARE(array.at(6).toObject().numKeys(), 0); } { QByteArray json = "{ \"0\": null, \"1\": true, \"2\": false, \"3\": \"Foo\", \"4\": 1, \"5\": [], \"6\": {} }"; JsonDocument doc = JsonDocument::fromJson(json); QVERIFY(!doc.isEmpty()); - QCOMPARE(doc.type(), ObjectValue); + QCOMPARE(doc.type(), JsonValue::Object); JsonObject object = doc.object(); QCOMPARE(object.numKeys(), 7); - QCOMPARE(object.value("0").type(), NullValue); - QCOMPARE(object.value("1").type(), BooleanValue); + QCOMPARE(object.value("0").type(), JsonValue::Null); + QCOMPARE(object.value("1").type(), JsonValue::Boolean); QCOMPARE(object.value("1").toBool(), true); - QCOMPARE(object.value("2").type(), BooleanValue); + QCOMPARE(object.value("2").type(), JsonValue::Boolean); QCOMPARE(object.value("2").toBool(), false); - QCOMPARE(object.value("3").type(), StringValue); + QCOMPARE(object.value("3").type(), JsonValue::String); QCOMPARE(object.value("3").toString(), QLatin1String("Foo")); - QCOMPARE(object.value("4").type(), NumberValue); + QCOMPARE(object.value("4").type(), JsonValue::Number); QCOMPARE(object.value("4").toInt(), 1); - QCOMPARE(object.value("5").type(), ArrayValue); + QCOMPARE(object.value("5").type(), JsonValue::Array); QCOMPARE(object.value("5").toArray().size(), 0); - QCOMPARE(object.value("6").type(), ObjectValue); + QCOMPARE(object.value("6").type(), JsonValue::Object); QCOMPARE(object.value("6").toObject().numKeys(), 0); } } @@ -850,11 +850,11 @@ void TestQtJson::parseNumbers() json += " ]"; JsonDocument doc = JsonDocument::fromJson(json); QVERIFY(!doc.isEmpty()); - QCOMPARE(doc.type(), ArrayValue); + QCOMPARE(doc.type(), JsonValue::Array); JsonArray array = doc.array(); QCOMPARE(array.size(), 1); JsonValue val = array.at(0); - QCOMPARE(val.type(), NumberValue); + QCOMPARE(val.type(), JsonValue::Number); QCOMPARE(val.toInt(), numbers[i].n); } } @@ -893,11 +893,11 @@ void TestQtJson::parseNumbers() json += " ]"; JsonDocument doc = JsonDocument::fromJson(json); QVERIFY(!doc.isEmpty()); - QCOMPARE(doc.type(), ArrayValue); + QCOMPARE(doc.type(), JsonValue::Array); JsonArray array = doc.array(); QCOMPARE(array.size(), 1); JsonValue val = array.at(0); - QCOMPARE(val.type(), NumberValue); + QCOMPARE(val.type(), JsonValue::Number); QCOMPARE(val.toNumber(), numbers[i].n); } } @@ -926,11 +926,11 @@ void TestQtJson::parseStrings() json += "\"\n]\n"; JsonDocument doc = JsonDocument::fromJson(json); QVERIFY(!doc.isEmpty()); - QCOMPARE(doc.type(), ArrayValue); + QCOMPARE(doc.type(), JsonValue::Array); JsonArray array = doc.array(); QCOMPARE(array.size(), 1); JsonValue val = array.at(0); - QCOMPARE(val.type(), StringValue); + QCOMPARE(val.type(), JsonValue::String); QCOMPARE(doc.toJson(), json); } @@ -955,11 +955,11 @@ void TestQtJson::parseStrings() out += "\"\n]\n"; JsonDocument doc = JsonDocument::fromJson(json); QVERIFY(!doc.isEmpty()); - QCOMPARE(doc.type(), ArrayValue); + QCOMPARE(doc.type(), JsonValue::Array); JsonArray array = doc.array(); QCOMPARE(array.size(), 1); JsonValue val = array.at(0); - QCOMPARE(val.type(), StringValue); + QCOMPARE(val.type(), JsonValue::String); QCOMPARE(doc.toJson(), out); } @@ -1122,7 +1122,7 @@ void TestQtJson::testCompaction() JsonDocument doc = JsonDocument::fromBinaryData(JsonDocument(obj).toBinaryData()); QVERIFY(doc.isValid()); QVERIFY(!doc.isEmpty()); - QVERIFY(doc.type() == ObjectValue); + QVERIFY(doc.type() == JsonValue::Object); QVERIFY(doc.object() == obj); } |