diff options
Diffstat (limited to 'src/qjsonwriter.cpp')
-rw-r--r-- | src/qjsonwriter.cpp | 48 |
1 files changed, 25 insertions, 23 deletions
diff --git a/src/qjsonwriter.cpp b/src/qjsonwriter.cpp index b678cf3..7f98886 100644 --- a/src/qjsonwriter.cpp +++ b/src/qjsonwriter.cpp @@ -44,8 +44,8 @@ using namespace QtJson; -static void objectContentToJson(const Private::Object *o, QByteArray &json, int indent); -static void arrayContentToJson(const Private::Array *a, QByteArray &json, int indent); +static void objectContentToJson(const Private::Object *o, QByteArray &json, int indent, bool compact); +static void arrayContentToJson(const Private::Array *a, QByteArray &json, int indent, bool compact); // some code from qutfcodec.cpp, inlined here for performance reasons // to allow fast escaping of strings @@ -175,7 +175,7 @@ static QByteArray escapedString(const QString &s) return ba; } -static void valueToJson(const Private::Base *b, const Private::Value &v, QByteArray &json, int indent) +static void valueToJson(const Private::Base *b, const Private::Value &v, QByteArray &json, int indent, bool compact) { QJsonValue::Type type = (QJsonValue::Type)(uint)v.type; switch (type) { @@ -191,14 +191,14 @@ static void valueToJson(const Private::Base *b, const Private::Value &v, QByteAr json += '"'; break; case QJsonValue::Array: - json += "[\n"; - arrayContentToJson(static_cast<Private::Array *>(v.objectOrArray(b)), json, indent + 1); + json += compact ? "[" : "[\n"; + arrayContentToJson(static_cast<Private::Array *>(v.objectOrArray(b)), json, indent + (compact ? 0 : 1), compact); json += QByteArray(4*indent, ' '); json += "]"; break; case QJsonValue::Object: - json += "{\n"; - objectContentToJson(static_cast<Private::Object *>(v.objectOrArray(b)), json, indent + 1); + json += compact ? "{" : "{\n"; + objectContentToJson(static_cast<Private::Object *>(v.objectOrArray(b)), json, indent + (compact ? 0 : 1), compact); json += QByteArray(4*indent, ' '); json += "}"; break; @@ -208,7 +208,7 @@ static void valueToJson(const Private::Base *b, const Private::Value &v, QByteAr } } -static void arrayContentToJson(const Private::Array *a, QByteArray &json, int indent) +static void arrayContentToJson(const Private::Array *a, QByteArray &json, int indent, bool compact) { if (!a || !a->length) return; @@ -218,19 +218,20 @@ static void arrayContentToJson(const Private::Array *a, QByteArray &json, int in uint i = 0; while (1) { json += indentString; - valueToJson(a, a->at(i), json, indent); + valueToJson(a, a->at(i), json, indent, compact); if (++i == a->length) { - json += '\n'; + if (!compact) + json += '\n'; break; } - json += ",\n"; + json += compact ? "," : ",\n"; } } -static void objectContentToJson(const Private::Object *o, QByteArray &json, int indent) +static void objectContentToJson(const Private::Object *o, QByteArray &json, int indent, bool compact) { if (!o || !o->length) return; @@ -244,31 +245,32 @@ static void objectContentToJson(const Private::Object *o, QByteArray &json, int json += '"'; json += escapedString(e->key()); json += "\": "; - valueToJson(o, e->value, json, indent); + valueToJson(o, e->value, json, indent, compact); if (++i == o->length) { - json += '\n'; + if (!compact) + json += '\n'; break; } - json += ",\n"; + json += compact ? "," : ",\n"; } } -void QJsonWriter::objectToJson(const Private::Object *o, QByteArray &json, int indent) +void QJsonWriter::objectToJson(const Private::Object *o, QByteArray &json, int indent, bool compact) { json.reserve(json.size() + (o ? o->size : 16)); - json += "{\n"; - objectContentToJson(o, json, indent + 1); + json += compact ? "{" : "{\n"; + objectContentToJson(o, json, indent + (compact ? 0 : 1), compact); json += QByteArray(4*indent, ' '); - json += "}\n"; + json += compact ? "}" : "}\n"; } -void QJsonWriter::arrayToJson(const Private::Array *a, QByteArray &json, int indent) +void QJsonWriter::arrayToJson(const Private::Array *a, QByteArray &json, int indent, bool compact) { json.reserve(json.size() + (a ? a->size : 16)); - json += "[\n"; - arrayContentToJson(a, json, indent + 1); + json += compact ? "[" : "[\n"; + arrayContentToJson(a, json, indent + (compact ? 0 : 1), compact); json += QByteArray(4*indent, ' '); - json += "]\n"; + json += compact ? "]" : "]\n"; } |