JSON: remove braces from UUID text representations

[ChangeLog][QtCore][QJsonValue] fromVariant() conversion now converts
from QUrl and QUuid using special encoding forms to ensure best JSON
compatibility.

Change-Id: I56b444f9d6274221a3b7fffd150cdc5ca1f87ff1
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
This commit is contained in:
Thiago Macieira 2018-01-24 13:31:04 -08:00
parent a2ffb35ac2
commit 4e02c8d5b8
2 changed files with 11 additions and 0 deletions

View File

@ -41,6 +41,7 @@
#include <qjsonvalue.h>
#include <qjsonarray.h>
#include <qurl.h>
#include <quuid.h>
#include <qvariant.h>
#include <qstringlist.h>
#include <qdebug.h>
@ -416,6 +417,12 @@ QJsonValue &QJsonValue::operator =(const QJsonValue &other)
\endlist
\li QJsonValue::String. The conversion will use QUrl::toString() with flag
QUrl::FullyEncoded, so as to ensure maximum compatibility in parsing the URL
\row
\li
\list
\li QMetaType::QUuid
\endlist
\li QJsonValue::String. Since Qt 5.11, the resulting string will not include braces
\endtable
For all other QVariant types a conversion to a QString will be attempted. If the returned string
@ -450,6 +457,8 @@ QJsonValue QJsonValue::fromVariant(const QVariant &variant)
#ifndef QT_BOOTSTRAPPED
case QVariant::Url:
return QJsonValue(variant.toUrl().toString(QUrl::FullyEncoded));
case QVariant::Uuid:
return variant.toUuid().toString(QUuid::WithoutBraces);
case QMetaType::QJsonValue:
return variant.toJsonValue();
case QMetaType::QJsonObject:

View File

@ -1184,6 +1184,8 @@ void tst_QtJson::fromVariantSpecial_data()
// Qt types with special encoding
QTest::newRow("url") << QVariant(QUrl("https://example.com/\xc2\xa9 "))
<< QJsonValue("https://example.com/%C2%A9%20");
QTest::newRow("uuid") << QVariant(QUuid(0x40c01df6, 0x1ad5, 0x4762, 0x9c, 0xfe, 0xfd, 0xba, 0xfa, 0xb5, 0xde, 0xf8))
<< QJsonValue("40c01df6-1ad5-4762-9cfe-fdbafab5def8");
}
void tst_QtJson::fromVariantSpecial()