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>
This commit is contained in:
Jean-Paul Delimat 2012-12-28 01:23:16 +01:00 committed by The Qt Project
parent e335fb7254
commit 4bb5566632
3 changed files with 144 additions and 42 deletions

View File

@ -302,6 +302,41 @@ QVariant QJsonDocument::toVariant() const
\sa fromJson()
*/
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();
@ -309,9 +344,9 @@ QByteArray QJsonDocument::toJson() const
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;
}

View File

@ -109,8 +109,19 @@ public:
static QJsonDocument fromVariant(const QVariant &variant);
QVariant toVariant() const;
enum JsonFormat {
Indented,
Compact
};
static QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error = 0);
QByteArray toJson() const;
#ifdef Q_QDOC
QByteArray toJson(JsonFormat format = Indented) const;
#else
QByteArray toJson() const; //### Merge in Qt6
QByteArray toJson(JsonFormat format) const;
#endif
bool isEmpty() const;
bool isArray() const;

View File

@ -1002,6 +1002,8 @@ void tst_QtJson::toVariantList()
}
void tst_QtJson::toJson()
{
// Test QJsonDocument::Indented format
{
QJsonObject object;
object.insert("\\Key\n", QString("Value"));
@ -1048,6 +1050,36 @@ void tst_QtJson::toJson()
QCOMPARE(json, expected);
}
// Test QJsonDocument::Compact format
{
QJsonObject object;
object.insert("\\Key\n", QString("Value"));
object.insert("null", QJsonValue());
QJsonArray array;
array.append(true);
array.append(999.);
array.append(QLatin1String("string"));
array.append(QJsonValue());
array.append(QLatin1String("\\\a\n\r\b\tabcABC\""));
object.insert("Array", array);
QByteArray json = QJsonDocument(object).toJson(QJsonDocument::Compact);
QByteArray expected =
"{\"Array\": [true,999,\"string\",null,\"\\\\\\u0007\\n\\r\\b\\tabcABC\\\"\"],\"\\\\Key\\n\": \"Value\",\"null\": null}";
QCOMPARE(json, expected);
QJsonDocument doc;
doc.setObject(object);
json = doc.toJson(QJsonDocument::Compact);
QCOMPARE(json, expected);
doc.setArray(array);
json = doc.toJson(QJsonDocument::Compact);
expected = "[true,999,\"string\",null,\"\\\\\\u0007\\n\\r\\b\\tabcABC\\\"\"]";
QCOMPARE(json, expected);
}
}
void tst_QtJson::fromJson()
{
{
@ -1135,6 +1167,30 @@ void tst_QtJson::fromJson()
QCOMPARE(object.value("6").type(), QJsonValue::Object);
QCOMPARE(object.value("6").toObject().size(), 0);
}
{
QByteArray compactJson = "{\"Array\": [true,999,\"string\",null,\"\\\\\\u0007\\n\\r\\b\\tabcABC\\\"\"],\"\\\\Key\\n\": \"Value\",\"null\": null}";
QJsonDocument doc = QJsonDocument::fromJson(compactJson);
QVERIFY(!doc.isEmpty());
QCOMPARE(doc.isArray(), false);
QCOMPARE(doc.isObject(), true);
QJsonObject object = doc.object();
QCOMPARE(object.size(), 3);
QCOMPARE(object.value("\\Key\n").isString(), true);
QCOMPARE(object.value("\\Key\n").toString(), QString("Value"));
QCOMPARE(object.value("null").isNull(), true);
QCOMPARE(object.value("Array").isArray(), true);
QJsonArray array = object.value("Array").toArray();
QCOMPARE(array.size(), 5);
QCOMPARE(array.at(0).isBool(), true);
QCOMPARE(array.at(0).toBool(), true);
QCOMPARE(array.at(1).isDouble(), true);
QCOMPARE(array.at(1).toDouble(), 999.);
QCOMPARE(array.at(2).isString(), true);
QCOMPARE(array.at(2).toString(), QLatin1String("string"));
QCOMPARE(array.at(3).isNull(), true);
QCOMPARE(array.at(4).isString(), true);
QCOMPARE(array.at(4).toString(), QLatin1String("\\\a\n\r\b\tabcABC\""));
}
}
void tst_QtJson::fromJsonErrors()