diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2011-11-26 14:07:19 +0100 |
---|---|---|
committer | Simons Kevin <kevin.simons@nokia.com> | 2011-12-05 14:32:41 +0100 |
commit | a04c36695a04c7184a8e178da59a66302e85a73c (patch) | |
tree | 45d96abf2b4203d1f13ada530d5f01895b2f9e42 | |
parent | c1723a3e35adb2d40d0d82696df528d192299a52 (diff) |
Added conversion to text
QByteArray JsonObject::toJson() returns
a utf8 encoded text representation of the
Json object.
Change-Id: I5f112e9bdfb8331eb31536eced765f3f5cb36c3f
Reviewed-by: Simons Kevin <kevin.simons@nokia.com>
-rw-r--r-- | src/qbinaryjson_p.h | 6 | ||||
-rw-r--r-- | src/qbinaryjsonobject.cpp | 87 | ||||
-rw-r--r-- | src/qbinaryjsonobject.h | 1 | ||||
-rw-r--r-- | tests/auto/tst_qtbinaryjson.cpp | 31 |
4 files changed, 122 insertions, 3 deletions
diff --git a/src/qbinaryjson_p.h b/src/qbinaryjson_p.h index e33372b..92d6896 100644 --- a/src/qbinaryjson_p.h +++ b/src/qbinaryjson_p.h @@ -143,7 +143,7 @@ struct Base offset tableOffset; // content follows here - inline offset *table() { return (offset *) (((char *) this) + tableOffset); } + inline offset *table() const { return (offset *) (((char *) this) + tableOffset); } void reserveSpace(uint dataSize, int posInTable, uint numItems) { @@ -171,7 +171,7 @@ struct Base struct Object : public Base { - Entry *entryAt(int i) { + Entry *entryAt(int i) const { return reinterpret_cast<Entry *>(((char *)this) + table()[i]); } int indexOf(const QString &key) { @@ -187,7 +187,7 @@ struct Object : public Base struct Array : public Base { - inline Value *at(int i) { + inline Value *at(int i) const { return reinterpret_cast<Value *>(((char *)this) + table()[i]); } }; diff --git a/src/qbinaryjsonobject.cpp b/src/qbinaryjsonobject.cpp index b423d79..491f360 100644 --- a/src/qbinaryjsonobject.cpp +++ b/src/qbinaryjsonobject.cpp @@ -84,6 +84,93 @@ QVariantMap JsonObject::toVariantMap() const return map; } +static void objectContentToJson(const Object *o, QByteArray &json, int indent); +static void arrayContentToJson(const Array *a, QByteArray &json, int indent); + +static void valueToJson(const Value *v, QByteArray &json, int indent) +{ + switch (v->type) { + case BooleanValue: + json += v->val ? "true" : "false"; + break; + case NumberValue: + json += QByteArray::number(v->toNumber()); + break; + case StringValue: + json += '"'; + json += v->shallowString().toUtf8(); + json += '"'; + break; + case ArrayValue: + json += "[\n"; + arrayContentToJson(v->array(), json, indent + 1); + json += QByteArray(4*indent, ' '); + json += "]"; + break; + case ObjectValue: + json += "{\n"; + objectContentToJson(v->object(), json, indent + 1); + json += QByteArray(4*indent, ' '); + json += "}"; + break; + case NullValue: + default: + json += "null"; + } +} + +static void arrayContentToJson(const Array *a, QByteArray &json, int indent) +{ + QByteArray indentString(4*indent, ' '); + + uint i = 0; + while (1) { + json += indentString; + valueToJson(a->at(i), json, indent); + + if (++i == a->length) { + json += '\n'; + break; + } + + json += ",\n"; + } +} + + +static void objectContentToJson(const Object *o, QByteArray &json, int indent) +{ + QByteArray indentString(4*indent, ' '); + + uint i = 0; + while (1) { + Entry *e = o->entryAt(i); + json += indentString; + json += '"'; + json += e->shallowKey().toUtf8(); + json += "\": "; + const Value *v = e->value(); + valueToJson(v, json, indent); + + if (++i == o->length) { + json += '\n'; + break; + } + + json += ",\n"; + } +} + +QByteArray JsonObject::toJson() const +{ + QByteArray json; + json.reserve(o ? o->size/2 : 0); + json += "{\n"; + objectContentToJson(o, json, 1); + json += "}\n"; + return json; +} + QStringList JsonObject::keys() { diff --git a/src/qbinaryjsonobject.h b/src/qbinaryjsonobject.h index 2150715..065c865 100644 --- a/src/qbinaryjsonobject.h +++ b/src/qbinaryjsonobject.h @@ -19,6 +19,7 @@ public: static JsonObject fromVariantMap(const QVariantMap &map); QVariantMap toVariantMap() const; + QByteArray toJson() const; QStringList keys(); int numKeys(); diff --git a/tests/auto/tst_qtbinaryjson.cpp b/tests/auto/tst_qtbinaryjson.cpp index c9a16bd..a4e5d1a 100644 --- a/tests/auto/tst_qtbinaryjson.cpp +++ b/tests/auto/tst_qtbinaryjson.cpp @@ -71,6 +71,8 @@ private Q_SLOTS: void fromVariantMap(); void toVariantMap(); + + void toJson(); }; TestQtBinaryJson::TestQtBinaryJson(QObject *parent) : QObject(parent) @@ -455,6 +457,35 @@ void TestQtBinaryJson::toVariantMap() QCOMPARE(list.at(3), QVariant()); } +void TestQtBinaryJson::toJson() +{ + JsonObject object; + object.insert("Key", QString("Value")); + object.insert("null", JsonValue()); + JsonArray array; + array.append(true); + array.append(999.); + array.append(QLatin1String("string")); + array.append(JsonValue()); + object.insert("Array", array); + + QByteArray json = object.toJson(); + + QByteArray expected = + "{\n" + " \"Key\": \"Value\",\n" + " \"null\": null,\n" + " \"Array\": [\n" + " true,\n" + " 999,\n" + " \"string\",\n" + " null\n" + " ]\n" + "}\n"; + QCOMPARE(json, expected); +} + + QTEST_MAIN(TestQtBinaryJson) #include "tst_qtbinaryjson.moc" |