summaryrefslogtreecommitdiffstats
path: root/src/corelib/json/qjsondocument.cpp
diff options
context:
space:
mode:
authorJean-Paul Delimat <jp.delimat@gmail.com>2012-12-28 01:23:16 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-01-26 03:09:27 +0100
commit4bb5566632ea6510c67fb8cf28cfe7dd4801488f (patch)
treeeea1eef7e3a4e0e5f250c84f1b84f16a5fefa667 /src/corelib/json/qjsondocument.cpp
parente335fb7254ad6c2ce08eda0d092ea524a302c2e0 (diff)
Add toJson() formatting argument to QJsonDocument interface
The writer delegate used by QJsonDocument to produce a Json QByteArray supports generating a human readable Json (with spaces and carriage returns that reflect the Json structure) and a less human readable (no spaces nor carriage returns) but more compact Json. The method toJson() was extended with a format argument to support the compact Json generation. Task-number: QTBUG-28815 Change-Id: I8d13849ab9ab6ed7c645011260251dc14a8629d2 Reviewed-by: Lars Knoll <lars.knoll@digia.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Debao Zhang <hello@debao.me>
Diffstat (limited to 'src/corelib/json/qjsondocument.cpp')
-rw-r--r--src/corelib/json/qjsondocument.cpp39
1 files changed, 37 insertions, 2 deletions
diff --git a/src/corelib/json/qjsondocument.cpp b/src/corelib/json/qjsondocument.cpp
index 550cc76b0d..bdb46528d3 100644
--- a/src/corelib/json/qjsondocument.cpp
+++ b/src/corelib/json/qjsondocument.cpp
@@ -303,15 +303,50 @@ QVariant QJsonDocument::toVariant() const
*/
QByteArray QJsonDocument::toJson() const
{
+ return toJson(Indented);
+}
+
+/*!
+ \enum QJsonDocument::JsonFormat
+
+ This value defines the format of the JSON byte array produced
+ when converting to a QJsonDocument using toJson().
+
+ \value Indented Defines human readable output as follows:
+ \code
+ {
+ "Array": [
+ true,
+ 999,
+ "string"
+ ],
+ "Key": "Value",
+ "null": null
+ }
+ \endcode
+
+ \value Compact Defines a compact output as follows:
+ \code
+ {"Array": [true,999,"string"],"Key": "Value","null": null}
+ \endcode
+ */
+
+/*!
+ Converts the QJsonDocument to a UTF-8 encoded JSON document in the provided \a format.
+
+ \sa fromJson(), JsonFormat
+ */
+QByteArray QJsonDocument::toJson(JsonFormat format) const
+{
if (!d)
return QByteArray();
QByteArray json;
if (d->header->root()->isArray())
- QJsonPrivate::Writer::arrayToJson(static_cast<QJsonPrivate::Array *>(d->header->root()), json, 0);
+ QJsonPrivate::Writer::arrayToJson(static_cast<QJsonPrivate::Array *>(d->header->root()), json, 0, (format == Compact));
else
- QJsonPrivate::Writer::objectToJson(static_cast<QJsonPrivate::Object *>(d->header->root()), json, 0);
+ QJsonPrivate::Writer::objectToJson(static_cast<QJsonPrivate::Object *>(d->header->root()), json, 0, (format == Compact));
return json;
}