From 73b8c83ec727b61a4b99d444ac84d540d0842730 Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Wed, 18 Dec 2024 15:32:02 +0100 Subject: [PATCH] Do not duplicate JsonFormat enum The enum already exists in QJsonDocument, so there is no reason to introduce a new one in QJsonValue. Instead, use the fact that we only need to forward-declare QJsonValue in QJsonDocument's header, include the latter into qjsonvalue.h, and use a type alias. For Qt 7, pre-program moving of the enum into QJsonValue and using an alias in QJsonDocument. Amends ac73079dee5f0260528a5c217a82cb0beafb0a56. Found in Qt 6.9 API review. [ChangeLog][QtCore][Potentially Source-Incompatible Changes] The QJsonDocument header no longer includes QJsonValue. The backward-compatible fix is to include all needed headers explicitly and to not rely on the transitive includes. Change-Id: I7c5219a239149e4a87d4780c4277d111983ad0f4 Reviewed-by: Thiago Macieira (cherry picked from commit 3cb87d891bb040f73fb68b6c5e7e82518f603c59) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/serialization/qjsondocument.cpp | 13 ++++++++++-- src/corelib/serialization/qjsondocument.h | 20 ++++++++++++++++++- src/corelib/serialization/qjsonvalue.cpp | 11 ++++++++-- src/corelib/serialization/qjsonvalue.h | 9 ++++++++- .../corelib/serialization/json/tst_qtjson.cpp | 8 ++++---- 5 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/corelib/serialization/qjsondocument.cpp b/src/corelib/serialization/qjsondocument.cpp index 1c8b6a69798..9b1439c66a6 100644 --- a/src/corelib/serialization/qjsondocument.cpp +++ b/src/corelib/serialization/qjsondocument.cpp @@ -216,6 +216,7 @@ QVariant QJsonDocument::toVariant() const #endif // !QT_NO_VARIANT /*! +\if !defined(qt7) \enum QJsonDocument::JsonFormat \since 5.1 @@ -227,6 +228,12 @@ QVariant QJsonDocument::toVariant() const \value Compact Defines a compact output as follows: \snippet code/src_corelib_serialization_qjsondocument.cpp 1 +\else + \typealias QJsonDocument::JsonFormat + \since 5.1 + + Same as \l QJsonValue::JsonFormat. +\endif */ /*! @@ -243,7 +250,8 @@ QByteArray QJsonDocument::toJson(JsonFormat format) const return json; return QJsonPrivate::Value::fromTrustedCbor(d->value).toJson( - format == JsonFormat::Compact ? QJsonValue::Compact : QJsonValue::Indented); + format == JsonFormat::Compact ? QJsonValue::JsonFormat::Compact + : QJsonValue::JsonFormat::Indented); } #endif @@ -472,7 +480,8 @@ QDebug operator<<(QDebug dbg, const QJsonDocument &o) dbg << "QJsonDocument()"; return dbg; } - QByteArray json = QJsonPrivate::Value::fromTrustedCbor(o.d->value).toJson(QJsonValue::Compact); + QByteArray json = + QJsonPrivate::Value::fromTrustedCbor(o.d->value).toJson(QJsonValue::JsonFormat::Compact); dbg.nospace() << "QJsonDocument(" << json.constData() // print as utf-8 string without extra quotation marks << ')'; diff --git a/src/corelib/serialization/qjsondocument.h b/src/corelib/serialization/qjsondocument.h index a52bc6928cd..237445b04aa 100644 --- a/src/corelib/serialization/qjsondocument.h +++ b/src/corelib/serialization/qjsondocument.h @@ -6,8 +6,12 @@ #include #include +#if (QT_VERSION >= QT_VERSION_CHECK(7, 0, 0)) || defined(QT_BOOTSTRAPPED) #include +#endif +#include #include +#include #include @@ -15,6 +19,9 @@ QT_BEGIN_NAMESPACE class QDebug; class QCborValue; +class QJsonArray; +class QJsonObject; +class QJsonValue; namespace QJsonPrivate { class Parser; } @@ -49,15 +56,26 @@ public: static QJsonDocument fromVariant(const QVariant &variant); QVariant toVariant() const; +#if (QT_VERSION < QT_VERSION_CHECK(7, 0, 0)) && !defined(QT_BOOTSTRAPPED) enum JsonFormat { Indented, Compact }; +#else + using JsonFormat = QJsonValue::JsonFormat; +# ifdef __cpp_using_enum + using enum QJsonValue::JsonFormat; +# else + // keep in sync with qjsonvalue.h + static constexpr auto Indented = JsonFormat::Indented; + static constexpr auto Compact = JsonFormat::Compact; +# endif +#endif static QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error = nullptr); #if !defined(QT_JSON_READONLY) || defined(Q_QDOC) - QByteArray toJson(JsonFormat format = Indented) const; + QByteArray toJson(JsonFormat format = JsonFormat::Indented) const; #endif bool isEmpty() const; diff --git a/src/corelib/serialization/qjsonvalue.cpp b/src/corelib/serialization/qjsonvalue.cpp index 1ae6cef7ef2..199e5d3154b 100644 --- a/src/corelib/serialization/qjsonvalue.cpp +++ b/src/corelib/serialization/qjsonvalue.cpp @@ -616,6 +616,7 @@ QJsonValue QJsonValue::fromJson(QByteArrayView json, QJsonParseError *error) } /*! +\if defined(qt7) \enum QJsonValue::JsonFormat \since 6.9 @@ -627,7 +628,13 @@ QJsonValue QJsonValue::fromJson(QByteArrayView json, QJsonParseError *error) \value Compact Defines a compact output as follows: \snippet code/src_corelib_serialization_qjsondocument.cpp 1 - */ +\else + \typealias QJsonValue::JsonFormat + \since 6.9 + + Same as \l QJsonDocument::JsonFormat. +\endif +*/ /*! \since 6.9 @@ -640,7 +647,7 @@ QByteArray QJsonValue::toJson(JsonFormat format) const { QByteArray json; - QJsonPrivate::Writer::valueToJson(value, json, 0, (format == Compact)); + QJsonPrivate::Writer::valueToJson(value, json, 0, (format == JsonFormat::Compact)); return json; } diff --git a/src/corelib/serialization/qjsonvalue.h b/src/corelib/serialization/qjsonvalue.h index 8e0f6d879f9..16e5cffed81 100644 --- a/src/corelib/serialization/qjsonvalue.h +++ b/src/corelib/serialization/qjsonvalue.h @@ -7,6 +7,9 @@ #include #include #include +#if (QT_VERSION < QT_VERSION_CHECK(7, 0, 0)) && !defined(QT_BOOTSTRAPPED) +#include +#endif #include #include #include @@ -35,10 +38,14 @@ public: Undefined = 0x80 }; - enum JsonFormat { +#if (QT_VERSION < QT_VERSION_CHECK(7, 0, 0)) && !defined(QT_BOOTSTRAPPED) + using JsonFormat = QJsonDocument::JsonFormat; +#else + enum class JsonFormat { Indented, Compact, }; +#endif QJsonValue(Type = Null); QJsonValue(bool b); diff --git a/tests/auto/corelib/serialization/json/tst_qtjson.cpp b/tests/auto/corelib/serialization/json/tst_qtjson.cpp index f49c24597d9..b7a5d844133 100644 --- a/tests/auto/corelib/serialization/json/tst_qtjson.cpp +++ b/tests/auto/corelib/serialization/json/tst_qtjson.cpp @@ -1989,7 +1989,7 @@ void tst_QtJson::toJson() array.append(QLatin1String("\\\a\n\r\b\tabcABC\"")); object.insert("Array", array); - QByteArray json = QJsonValue(object).toJson(QJsonValue::Compact); + QByteArray json = QJsonValue(object).toJson(QJsonValue::JsonFormat::Compact); QByteArray expected = "{\"Array\":[true,999,\"string\",null,\"\\\\\\u0007\\n\\r\\b\\tabcABC\\\"\"],\"\\\\Key\\n\":\"Value\",\"null\":null}"; QCOMPARE(json, expected); @@ -2004,7 +2004,7 @@ void tst_QtJson::toJson() json = doc.toJson(QJsonDocument::Compact); expected = "[true,999,\"string\",null,\"\\\\\\u0007\\n\\r\\b\\tabcABC\\\"\"]"; QCOMPARE(json, expected); - QCOMPARE(QJsonValue(array).toJson(QJsonValue::Compact), expected); + QCOMPARE(QJsonValue(array).toJson(QJsonValue::JsonFormat::Compact), expected); } } @@ -2155,7 +2155,7 @@ void tst_QtJson::toJsonTopLevel() QFETCH(QByteArray, result); QCOMPARE(value.toJson(), result); - QCOMPARE(value.toJson(QJsonValue::Compact), result); + QCOMPARE(value.toJson(QJsonValue::JsonFormat::Compact), result); } void tst_QtJson::fromJson() @@ -3213,7 +3213,7 @@ void tst_QtJson::makeEscapes() QByteArray resultStr = result; QJsonArray array = { input }; - QByteArray json = QJsonValue(array).toJson(QJsonValue::Compact); + QByteArray json = QJsonValue(array).toJson(QJsonValue::JsonFormat::Compact); QCOMPARE(QJsonDocument(array).toJson(QJsonDocument::Compact), json); QByteArray jsonStr = QJsonValue(input).toJson();