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 @@ -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(d->header->root()), json, 0); + QJsonPrivate::Writer::arrayToJson(static_cast(d->header->root()), json, 0, (format == Compact)); else - QJsonPrivate::Writer::objectToJson(static_cast(d->header->root()), json, 0); + QJsonPrivate::Writer::objectToJson(static_cast(d->header->root()), json, 0, (format == Compact)); return json; } diff --git a/src/corelib/json/qjsondocument.h b/src/corelib/json/qjsondocument.h index 675ee75dbf..0bb21d89cf 100644 --- a/src/corelib/json/qjsondocument.h +++ b/src/corelib/json/qjsondocument.h @@ -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; diff --git a/tests/auto/corelib/json/tst_qtjson.cpp b/tests/auto/corelib/json/tst_qtjson.cpp index a692b79fe9..6811551769 100644 --- a/tests/auto/corelib/json/tst_qtjson.cpp +++ b/tests/auto/corelib/json/tst_qtjson.cpp @@ -1003,49 +1003,81 @@ void tst_QtJson::toVariantList() void tst_QtJson::toJson() { - 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); + // Test QJsonDocument::Indented 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(); + QByteArray json = QJsonDocument(object).toJson(); - QByteArray expected = - "{\n" - " \"Array\": [\n" - " true,\n" - " 999,\n" - " \"string\",\n" - " null,\n" - " \"\\\\\\u0007\\n\\r\\b\\tabcABC\\\"\"\n" - " ],\n" - " \"\\\\Key\\n\": \"Value\",\n" - " \"null\": null\n" - "}\n"; - QCOMPARE(json, expected); + QByteArray expected = + "{\n" + " \"Array\": [\n" + " true,\n" + " 999,\n" + " \"string\",\n" + " null,\n" + " \"\\\\\\u0007\\n\\r\\b\\tabcABC\\\"\"\n" + " ],\n" + " \"\\\\Key\\n\": \"Value\",\n" + " \"null\": null\n" + "}\n"; + QCOMPARE(json, expected); - QJsonDocument doc; - doc.setObject(object); - json = doc.toJson(); - QCOMPARE(json, expected); + QJsonDocument doc; + doc.setObject(object); + json = doc.toJson(); + QCOMPARE(json, expected); - doc.setArray(array); - json = doc.toJson(); - expected = - "[\n" - " true,\n" - " 999,\n" - " \"string\",\n" - " null,\n" - " \"\\\\\\u0007\\n\\r\\b\\tabcABC\\\"\"\n" - "]\n"; - QCOMPARE(json, expected); + doc.setArray(array); + json = doc.toJson(); + expected = + "[\n" + " true,\n" + " 999,\n" + " \"string\",\n" + " null,\n" + " \"\\\\\\u0007\\n\\r\\b\\tabcABC\\\"\"\n" + "]\n"; + 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()