summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@nokia.com>2011-12-08 12:27:33 +0100
committerHicks James <jamey.hicks@nokia.com>2011-12-08 22:42:42 +0100
commit96f70bcffdeaf306a74ae3bdbae71628834352c0 (patch)
tree7c6f23ee846a7d40a6bb650c052ff7233ec78881
parenta9d3828eebe4c11473b8933948c4e711dab25659 (diff)
The root object shouldn't be part of the header
This complicates the logic in some places. Rather add a getter to the header that'll retrieve the root object. Change-Id: Ib0d98734e71e065cc0bbf99f25fa3098e9fa4c62 Reviewed-by: Hicks James <jamey.hicks@nokia.com>
-rw-r--r--src/qjson_p.h4
-rw-r--r--src/qjsondocument.cpp49
-rw-r--r--src/qjsondocument.h1
3 files changed, 29 insertions, 25 deletions
diff --git a/src/qjson_p.h b/src/qjson_p.h
index a832d97..57e108c 100644
--- a/src/qjson_p.h
+++ b/src/qjson_p.h
@@ -65,6 +65,8 @@ struct Object;
struct Value;
struct Entry;
+const uint QBJS_Tag = ('q' << 24) | ('b' << 16) | ('j' << 8) | 's';
+
static inline int alignedSize(int size) { return (size + 3) & ~3; }
static inline int qStringSize(const QString &string, bool compress)
@@ -389,7 +391,7 @@ struct Header {
uint tag; // 'qbjs'
uint type;
uint unused;
- Base root;
+ Base *root() { return (Base *)(this + 1); }
};
diff --git a/src/qjsondocument.cpp b/src/qjsondocument.cpp
index 415b315..23aef2a 100644
--- a/src/qjsondocument.cpp
+++ b/src/qjsondocument.cpp
@@ -8,7 +8,6 @@
using namespace QtJson;
-const uint QBJS_Tag = ('q' << 24) | ('b' << 16) | ('j' << 8) | 's';
JsonDocument::JsonDocument()
: d(0)
@@ -84,10 +83,10 @@ QVariant JsonDocument::toVariant() const
ValueType type = d ? (ValueType)d->_header->type : NullValue;
switch (type) {
case ArrayValue:
- return JsonArray(d, static_cast<Array *>(&d->_header->root)).toVariantList();
+ return JsonArray(d, static_cast<Array *>(d->_header->root())).toVariantList();
break;
case ObjectValue:
- return JsonObject(d, static_cast<Object *>(&d->_header->root)).toVariantMap();
+ return JsonObject(d, static_cast<Object *>(d->_header->root())).toVariantMap();
break;
default:
break;
@@ -101,10 +100,10 @@ QByteArray JsonDocument::toJson() const
ValueType type = d ? (ValueType)d->_header->type : NullValue;
switch (type) {
case ArrayValue:
- QJsonWriter::arrayToJson(static_cast<Array *>(&d->_header->root), json, 0);
+ QJsonWriter::arrayToJson(static_cast<Array *>(d->_header->root()), json, 0);
break;
case ObjectValue:
- QJsonWriter::objectToJson(static_cast<Object *>(&d->_header->root), json, 0);
+ QJsonWriter::objectToJson(static_cast<Object *>(d->_header->root()), json, 0);
break;
default:
break;
@@ -142,7 +141,7 @@ JsonObject JsonDocument::object() const
if (d) {
Header *h = (Header *)d->rawData;
if (h->type == ObjectValue)
- return JsonObject(d, static_cast<Object *>(&h->root));
+ return JsonObject(d, static_cast<Object *>(h->root()));
}
return JsonObject();
}
@@ -152,7 +151,7 @@ JsonArray JsonDocument::array() const
if (d) {
Header *h = (Header *)d->rawData;
if (h->type == ArrayValue)
- return JsonArray(d, static_cast<Array *>(&h->root));
+ return JsonArray(d, static_cast<Array *>(h->root()));
}
return JsonArray();
}
@@ -161,19 +160,20 @@ void JsonDocument::setObject(const JsonObject &object)
{
Object *o = object.o;
int objectSize = o ? o->size : sizeof(Object);
- int alloc = sizeof(Header) - sizeof(Base) + objectSize;
+ int alloc = sizeof(Header) + objectSize;
Header *h = (Header *)malloc(alloc);
- h->size = sizeof(Header);
+ h->size = alloc;
h->tag = QBJS_Tag;
h->type = ObjectValue;
h->unused = 0;
if (o) {
- memcpy(&h->root, o, o->size);
+ memcpy(h->root(), o, o->size);
} else {
- h->root.size = sizeof(Base);
- h->root.length = 0;
- h->root.tableOffset = 0;
+ Base *b = h->root();
+ b->size = sizeof(Base);
+ b->length = 0;
+ b->tableOffset = 0;
}
*this = JsonDocument(new Data((char *)h, alloc));
@@ -183,19 +183,20 @@ void JsonDocument::setArray(const JsonArray &array)
{
Array *a = array.a;
int arraySize = a ? a->size : sizeof(Array);
- int alloc = sizeof(Header) - sizeof(Base) + arraySize;
+ int alloc = sizeof(Header) + arraySize;
Header *h = (Header *)malloc(alloc);
- h->size = sizeof(Header);
- h->tag = ('q' << 24) | ('b' << 16) | ('j' << 8) | 's';
+ h->size = alloc;
+ h->tag = QBJS_Tag;
h->type = ArrayValue;
h->unused = 0;
if (a) {
- memcpy(&h->root, a, a->size);
+ memcpy(h->root(), a, a->size);
} else {
- h->root.size = sizeof(Base);
- h->root.length = 0;
- h->root.tableOffset = 0;
+ Base *b = h->root();
+ b->size = sizeof(Base);
+ b->length = 0;
+ b->tableOffset = 0;
}
*this = JsonDocument(new Data((char *)h, alloc));
@@ -218,11 +219,11 @@ bool JsonDocument::operator==(const JsonDocument &other) const
case NullValue:
return true;
case ObjectValue:
- return JsonObject(d, static_cast<Object *>(&d->_header->root))
- == JsonObject(other.d, static_cast<Object *>(&other.d->_header->root));
+ return JsonObject(d, static_cast<Object *>(d->_header->root()))
+ == JsonObject(other.d, static_cast<Object *>(other.d->_header->root()));
case ArrayValue:
- return JsonArray(d, static_cast<Array *>(&d->_header->root))
- == JsonArray(other.d, static_cast<Array *>(&other.d->_header->root));
+ return JsonArray(d, static_cast<Array *>(d->_header->root()))
+ == JsonArray(other.d, static_cast<Array *>(other.d->_header->root()));
default:
break;
}
diff --git a/src/qjsondocument.h b/src/qjsondocument.h
index 6f2ef80..deb783f 100644
--- a/src/qjsondocument.h
+++ b/src/qjsondocument.h
@@ -39,6 +39,7 @@ public:
private:
friend class Data;
friend class JsonValue;
+ friend class QJsonParser;
JsonDocument(Data *data);
Data *d;