From a2ffb35ac2354735a95b21557a762aa16d7140a3 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Wed, 24 Jan 2018 11:41:37 -0800 Subject: [PATCH] QJsonValue: use the fully-encoded form of a URL in fromVariant() For compatibility with other parsers that may expect it to be so. Change-Id: I56b444f9d6274221a3b7fffd150cd66390f98fd5 Reviewed-by: Simon Hausmann --- src/corelib/serialization/qjsonvalue.cpp | 11 ++++++++++ .../corelib/serialization/json/tst_qtjson.cpp | 20 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/corelib/serialization/qjsonvalue.cpp b/src/corelib/serialization/qjsonvalue.cpp index 33707b6ec3..989d6d51db 100644 --- a/src/corelib/serialization/qjsonvalue.cpp +++ b/src/corelib/serialization/qjsonvalue.cpp @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -407,6 +408,14 @@ QJsonValue &QJsonValue::operator =(const QJsonValue &other) \li QMetaType::QVariantHash \endlist \li QJsonValue::Object + + \row + \li + \list + \li QMetaType::QUrl + \endlist + \li QJsonValue::String. The conversion will use QUrl::toString() with flag + QUrl::FullyEncoded, so as to ensure maximum compatibility in parsing the URL \endtable For all other QVariant types a conversion to a QString will be attempted. If the returned string @@ -439,6 +448,8 @@ QJsonValue QJsonValue::fromVariant(const QVariant &variant) case QVariant::Hash: return QJsonValue(QJsonObject::fromVariantHash(variant.toHash())); #ifndef QT_BOOTSTRAPPED + case QVariant::Url: + return QJsonValue(variant.toUrl().toString(QUrl::FullyEncoded)); case QMetaType::QJsonValue: return variant.toJsonValue(); case QMetaType::QJsonObject: diff --git a/tests/auto/corelib/serialization/json/tst_qtjson.cpp b/tests/auto/corelib/serialization/json/tst_qtjson.cpp index 1e3604ac9e..19e53ad2b6 100644 --- a/tests/auto/corelib/serialization/json/tst_qtjson.cpp +++ b/tests/auto/corelib/serialization/json/tst_qtjson.cpp @@ -82,6 +82,8 @@ private Q_SLOTS: void fromVariant_data(); void fromVariant(); + void fromVariantSpecial_data(); + void fromVariantSpecial(); void toVariant_data(); void toVariant(); void fromVariantMap(); @@ -1174,6 +1176,24 @@ void tst_QtJson::fromVariant() QCOMPARE(variant.toJsonValue(), jsonvalue); } +void tst_QtJson::fromVariantSpecial_data() +{ + QTest::addColumn("variant"); + QTest::addColumn("jsonvalue"); + + // Qt types with special encoding + QTest::newRow("url") << QVariant(QUrl("https://example.com/\xc2\xa9 ")) + << QJsonValue("https://example.com/%C2%A9%20"); +} + +void tst_QtJson::fromVariantSpecial() +{ + QFETCH( QVariant, variant ); + QFETCH( QJsonValue, jsonvalue ); + + QCOMPARE(QJsonValue::fromVariant(variant), jsonvalue); +} + void tst_QtJson::toVariant_data() { fromVariant_data();