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 <simon.hausmann@qt.io>
bb10
Thiago Macieira 2018-01-24 11:41:37 -08:00
parent 1c2499cbf1
commit a2ffb35ac2
2 changed files with 31 additions and 0 deletions

View File

@ -40,6 +40,7 @@
#include <qjsonobject.h>
#include <qjsonvalue.h>
#include <qjsonarray.h>
#include <qurl.h>
#include <qvariant.h>
#include <qstringlist.h>
#include <qdebug.h>
@ -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:

View File

@ -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<QVariant>("variant");
QTest::addColumn<QJsonValue>("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();