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:
parent
e335fb7254
commit
4bb5566632
@ -302,6 +302,41 @@ QVariant QJsonDocument::toVariant() const
|
|||||||
\sa fromJson()
|
\sa fromJson()
|
||||||
*/
|
*/
|
||||||
QByteArray QJsonDocument::toJson() 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)
|
if (!d)
|
||||||
return QByteArray();
|
return QByteArray();
|
||||||
@ -309,9 +344,9 @@ QByteArray QJsonDocument::toJson() const
|
|||||||
QByteArray json;
|
QByteArray json;
|
||||||
|
|
||||||
if (d->header->root()->isArray())
|
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
|
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;
|
return json;
|
||||||
}
|
}
|
||||||
|
@ -109,8 +109,19 @@ public:
|
|||||||
static QJsonDocument fromVariant(const QVariant &variant);
|
static QJsonDocument fromVariant(const QVariant &variant);
|
||||||
QVariant toVariant() const;
|
QVariant toVariant() const;
|
||||||
|
|
||||||
|
enum JsonFormat {
|
||||||
|
Indented,
|
||||||
|
Compact
|
||||||
|
};
|
||||||
|
|
||||||
static QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error = 0);
|
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 isEmpty() const;
|
||||||
bool isArray() const;
|
bool isArray() const;
|
||||||
|
@ -1003,49 +1003,81 @@ void tst_QtJson::toVariantList()
|
|||||||
|
|
||||||
void tst_QtJson::toJson()
|
void tst_QtJson::toJson()
|
||||||
{
|
{
|
||||||
QJsonObject object;
|
// Test QJsonDocument::Indented format
|
||||||
object.insert("\\Key\n", QString("Value"));
|
{
|
||||||
object.insert("null", QJsonValue());
|
QJsonObject object;
|
||||||
QJsonArray array;
|
object.insert("\\Key\n", QString("Value"));
|
||||||
array.append(true);
|
object.insert("null", QJsonValue());
|
||||||
array.append(999.);
|
QJsonArray array;
|
||||||
array.append(QLatin1String("string"));
|
array.append(true);
|
||||||
array.append(QJsonValue());
|
array.append(999.);
|
||||||
array.append(QLatin1String("\\\a\n\r\b\tabcABC\""));
|
array.append(QLatin1String("string"));
|
||||||
object.insert("Array", array);
|
array.append(QJsonValue());
|
||||||
|
array.append(QLatin1String("\\\a\n\r\b\tabcABC\""));
|
||||||
|
object.insert("Array", array);
|
||||||
|
|
||||||
QByteArray json = QJsonDocument(object).toJson();
|
QByteArray json = QJsonDocument(object).toJson();
|
||||||
|
|
||||||
QByteArray expected =
|
QByteArray expected =
|
||||||
"{\n"
|
"{\n"
|
||||||
" \"Array\": [\n"
|
" \"Array\": [\n"
|
||||||
" true,\n"
|
" true,\n"
|
||||||
" 999,\n"
|
" 999,\n"
|
||||||
" \"string\",\n"
|
" \"string\",\n"
|
||||||
" null,\n"
|
" null,\n"
|
||||||
" \"\\\\\\u0007\\n\\r\\b\\tabcABC\\\"\"\n"
|
" \"\\\\\\u0007\\n\\r\\b\\tabcABC\\\"\"\n"
|
||||||
" ],\n"
|
" ],\n"
|
||||||
" \"\\\\Key\\n\": \"Value\",\n"
|
" \"\\\\Key\\n\": \"Value\",\n"
|
||||||
" \"null\": null\n"
|
" \"null\": null\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
QCOMPARE(json, expected);
|
QCOMPARE(json, expected);
|
||||||
|
|
||||||
QJsonDocument doc;
|
QJsonDocument doc;
|
||||||
doc.setObject(object);
|
doc.setObject(object);
|
||||||
json = doc.toJson();
|
json = doc.toJson();
|
||||||
QCOMPARE(json, expected);
|
QCOMPARE(json, expected);
|
||||||
|
|
||||||
doc.setArray(array);
|
doc.setArray(array);
|
||||||
json = doc.toJson();
|
json = doc.toJson();
|
||||||
expected =
|
expected =
|
||||||
"[\n"
|
"[\n"
|
||||||
" true,\n"
|
" true,\n"
|
||||||
" 999,\n"
|
" 999,\n"
|
||||||
" \"string\",\n"
|
" \"string\",\n"
|
||||||
" null,\n"
|
" null,\n"
|
||||||
" \"\\\\\\u0007\\n\\r\\b\\tabcABC\\\"\"\n"
|
" \"\\\\\\u0007\\n\\r\\b\\tabcABC\\\"\"\n"
|
||||||
"]\n";
|
"]\n";
|
||||||
QCOMPARE(json, expected);
|
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()
|
void tst_QtJson::fromJson()
|
||||||
@ -1135,6 +1167,30 @@ void tst_QtJson::fromJson()
|
|||||||
QCOMPARE(object.value("6").type(), QJsonValue::Object);
|
QCOMPARE(object.value("6").type(), QJsonValue::Object);
|
||||||
QCOMPARE(object.value("6").toObject().size(), 0);
|
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()
|
void tst_QtJson::fromJsonErrors()
|
||||||
|
Loading…
Reference in New Issue
Block a user