Add conversion between QVariantHash and QJsonObject

Adds automatic conversion from QVariants with a QVariantHash to
QJsonValue and explicit methods for conversion between QVariantHash
and QJsonObject.

[ChangeLog][QtCore][QJsonObject] Added conversion to and from QVariantHash

Change-Id: I140ef881463acabaab2648e28209487d8ad18e0d
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
This commit is contained in:
Allan Sandfeld Jensen 2014-10-30 10:59:11 +01:00 committed by Allan Sandfeld Jensen
parent d7ca800a87
commit ae981f224d
4 changed files with 91 additions and 1 deletions

View File

@ -192,7 +192,7 @@ QJsonObject &QJsonObject::operator =(const QJsonObject &other)
The keys in \a map will be used as the keys in the JSON object,
and the QVariant values will be converted to JSON values.
\sa toVariantMap(), QJsonValue::fromVariant()
\sa fromVariantHash(), toVariantMap(), QJsonValue::fromVariant()
*/
QJsonObject QJsonObject::fromVariantMap(const QVariantMap &map)
{
@ -208,6 +208,8 @@ QJsonObject QJsonObject::fromVariantMap(const QVariantMap &map)
Converts this object to a QVariantMap.
Returns the created map.
\sa toVariantHash()
*/
QVariantMap QJsonObject::toVariantMap() const
{
@ -221,6 +223,45 @@ QVariantMap QJsonObject::toVariantMap() const
return map;
}
/*!
Converts the variant hash \a hash to a QJsonObject.
\since 5.5
The keys in \a hash will be used as the keys in the JSON object,
and the QVariant values will be converted to JSON values.
\sa fromVariantMap(), toVariantHash(), QJsonValue::fromVariant()
*/
QJsonObject QJsonObject::fromVariantHash(const QVariantHash &hash)
{
// ### this is implemented the trivial way, not the most efficient way
QJsonObject object;
for (QVariantHash::const_iterator it = hash.constBegin(); it != hash.constEnd(); ++it)
object.insert(it.key(), QJsonValue::fromVariant(it.value()));
return object;
}
/*!
Converts this object to a QVariantHash.
\since 5.5
Returns the created hash.
\sa toVariantMap()
*/
QVariantHash QJsonObject::toVariantHash() const
{
QVariantHash hash;
if (o) {
for (uint i = 0; i < o->length; ++i) {
QJsonPrivate::Entry *e = o->entryAt(i);
hash.insert(e->key(), QJsonValue(d, o, e->value).toVariant());
}
}
return hash;
}
/*!
Returns a list of all keys in this object.
*/

View File

@ -46,6 +46,8 @@ QT_BEGIN_NAMESPACE
class QDebug;
template <class Key, class T> class QMap;
typedef QMap<QString, QVariant> QVariantMap;
template <class Key, class T> class QHash;
typedef QHash<QString, QVariant> QVariantHash;
class Q_CORE_EXPORT QJsonObject
{
@ -68,6 +70,8 @@ public:
static QJsonObject fromVariantMap(const QVariantMap &map);
QVariantMap toVariantMap() const;
static QJsonObject fromVariantHash(const QVariantHash &map);
QVariantHash toVariantHash() const;
QStringList keys() const;
int size() const;

View File

@ -373,6 +373,7 @@ QJsonValue &QJsonValue::operator =(const QJsonValue &other)
\li
\list
\li QMetaType::QVariantMap
\li QMetaType::QVariantHash
\endlist
\li QJsonValue::Object
\endtable
@ -402,6 +403,8 @@ QJsonValue QJsonValue::fromVariant(const QVariant &variant)
return QJsonValue(QJsonArray::fromVariantList(variant.toList()));
case QVariant::Map:
return QJsonValue(QJsonObject::fromVariantMap(variant.toMap()));
case QVariant::Hash:
return QJsonValue(QJsonObject::fromVariantHash(variant.toHash()));
default:
break;
}

View File

@ -90,7 +90,9 @@ private Q_SLOTS:
void fromVariant();
void fromVariantMap();
void fromVariantHash();
void toVariantMap();
void toVariantHash();
void toVariantList();
void toJson();
@ -1165,6 +1167,17 @@ void tst_QtJson::fromVariantMap()
QCOMPARE(array.at(3).toString(), QLatin1String("foo"));
}
void tst_QtJson::fromVariantHash()
{
QVariantHash map;
map.insert(QLatin1String("key1"), QLatin1String("value1"));
map.insert(QLatin1String("key2"), QLatin1String("value2"));
QJsonObject object = QJsonObject::fromVariantHash(map);
QCOMPARE(object.size(), 2);
QCOMPARE(object.value(QLatin1String("key1")), QJsonValue(QLatin1String("value1")));
QCOMPARE(object.value(QLatin1String("key2")), QJsonValue(QLatin1String("value2")));
}
void tst_QtJson::toVariantMap()
{
QCOMPARE(QMetaType::Type(QJsonValue(QJsonObject()).toVariant().type()), QMetaType::QVariantMap); // QTBUG-32524
@ -1196,6 +1209,35 @@ void tst_QtJson::toVariantMap()
QCOMPARE(list.at(3), QVariant());
}
void tst_QtJson::toVariantHash()
{
QJsonObject object;
QVariantHash hash = object.toVariantHash();
QVERIFY(hash.isEmpty());
object.insert("Key", QString("Value"));
object.insert("null", QJsonValue());
QJsonArray array;
array.append(true);
array.append(999.);
array.append(QLatin1String("string"));
array.append(QJsonValue());
object.insert("Array", array);
hash = object.toVariantHash();
QCOMPARE(hash.size(), 3);
QCOMPARE(hash.value("Key"), QVariant(QString("Value")));
QCOMPARE(hash.value("null"), QVariant());
QCOMPARE(hash.value("Array").type(), QVariant::List);
QVariantList list = hash.value("Array").toList();
QCOMPARE(list.size(), 4);
QCOMPARE(list.at(0), QVariant(true));
QCOMPARE(list.at(1), QVariant(999.));
QCOMPARE(list.at(2), QVariant(QLatin1String("string")));
QCOMPARE(list.at(3), QVariant());
}
void tst_QtJson::toVariantList()
{
QCOMPARE(QMetaType::Type(QJsonValue(QJsonArray()).toVariant().type()), QMetaType::QVariantList); // QTBUG-32524