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 <thiago.macieira@intel.com>
(cherry picked from commit 3cb87d891bb040f73fb68b6c5e7e82518f603c59)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Ivan Solovev 2024-12-18 15:32:02 +01:00 committed by Qt Cherry-pick Bot
parent 523f711295
commit 73b8c83ec7
5 changed files with 51 additions and 10 deletions

View File

@ -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
<< ')';

View File

@ -6,8 +6,12 @@
#include <QtCore/qcompare.h>
#include <QtCore/qjsonparseerror.h>
#if (QT_VERSION >= QT_VERSION_CHECK(7, 0, 0)) || defined(QT_BOOTSTRAPPED)
#include <QtCore/qjsonvalue.h>
#endif
#include <QtCore/qlatin1stringview.h>
#include <QtCore/qscopedpointer.h>
#include <QtCore/qstringview.h>
#include <memory>
@ -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;

View File

@ -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;
}

View File

@ -7,6 +7,9 @@
#include <QtCore/qcborvalue.h>
#include <QtCore/qcompare.h>
#include <QtCore/qglobal.h>
#if (QT_VERSION < QT_VERSION_CHECK(7, 0, 0)) && !defined(QT_BOOTSTRAPPED)
#include <QtCore/qjsondocument.h>
#endif
#include <QtCore/qjsonparseerror.h>
#include <QtCore/qstring.h>
#include <QtCore/qshareddata.h>
@ -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);

View File

@ -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();