Implement QJsonObject data stream operator
Change-Id: I8528f18ad72828cd97a5ac00e1925958acf73f9f Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>bb10
parent
ff03383017
commit
4981c2c8b5
|
|
@ -1320,4 +1320,21 @@ QDebug operator<<(QDebug dbg, const QJsonObject &o)
|
|||
}
|
||||
#endif
|
||||
|
||||
#ifndef QT_NO_DATASTREAM
|
||||
QDataStream &operator<<(QDataStream &stream, const QJsonObject &object)
|
||||
{
|
||||
QJsonDocument doc{object};
|
||||
stream << doc.toJson(QJsonDocument::Compact);
|
||||
return stream;
|
||||
}
|
||||
|
||||
QDataStream &operator>>(QDataStream &stream, QJsonObject &object)
|
||||
{
|
||||
QJsonDocument doc;
|
||||
stream >> doc;
|
||||
object = doc.object();
|
||||
return stream;
|
||||
}
|
||||
#endif
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
|
|
|||
|
|
@ -268,6 +268,11 @@ Q_CORE_EXPORT uint qHash(const QJsonObject &object, uint seed = 0);
|
|||
Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &);
|
||||
#endif
|
||||
|
||||
#ifndef QT_NO_DATASTREAM
|
||||
Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonObject &);
|
||||
Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonObject &);
|
||||
#endif
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
||||
#endif // QJSONOBJECT_H
|
||||
|
|
|
|||
|
|
@ -1822,7 +1822,6 @@ DECLARE_NONSTREAMABLE(void*)
|
|||
DECLARE_NONSTREAMABLE(QModelIndex)
|
||||
DECLARE_NONSTREAMABLE(QPersistentModelIndex)
|
||||
DECLARE_NONSTREAMABLE(QJsonValue)
|
||||
DECLARE_NONSTREAMABLE(QJsonObject)
|
||||
DECLARE_NONSTREAMABLE(QCborValue)
|
||||
DECLARE_NONSTREAMABLE(QCborArray)
|
||||
DECLARE_NONSTREAMABLE(QCborMap)
|
||||
|
|
|
|||
|
|
@ -157,6 +157,8 @@ private Q_SLOTS:
|
|||
void streamSerializationQJsonDocument();
|
||||
void streamSerializationQJsonArray_data();
|
||||
void streamSerializationQJsonArray();
|
||||
void streamSerializationQJsonObject_data();
|
||||
void streamSerializationQJsonObject();
|
||||
void streamVariantSerialization();
|
||||
|
||||
private:
|
||||
|
|
@ -3059,6 +3061,27 @@ void tst_QtJson::streamSerializationQJsonArray()
|
|||
QCOMPARE(output, array);
|
||||
}
|
||||
|
||||
void tst_QtJson::streamSerializationQJsonObject_data()
|
||||
{
|
||||
QTest::addColumn<QJsonObject>("object");
|
||||
QTest::newRow("empty") << QJsonObject();
|
||||
QTest::newRow("non-empty") << QJsonObject{{"foo", 665}, {"bar", 666}};
|
||||
}
|
||||
|
||||
void tst_QtJson::streamSerializationQJsonObject()
|
||||
{
|
||||
// Check interface only, implementation is tested through to and from
|
||||
// json functions.
|
||||
QByteArray buffer;
|
||||
QFETCH(QJsonObject, object);
|
||||
QJsonObject output;
|
||||
QDataStream save(&buffer, QIODevice::WriteOnly);
|
||||
save << object;
|
||||
QDataStream load(buffer);
|
||||
load >> output;
|
||||
QCOMPARE(output, object);
|
||||
}
|
||||
|
||||
void tst_QtJson::streamVariantSerialization()
|
||||
{
|
||||
// Check interface only, implementation is tested through to and from
|
||||
|
|
@ -3086,6 +3109,17 @@ void tst_QtJson::streamVariantSerialization()
|
|||
QCOMPARE(output.userType(), QMetaType::QJsonArray);
|
||||
QCOMPARE(output.toJsonArray(), array);
|
||||
}
|
||||
{
|
||||
QJsonObject obj{{"foo", 42}};
|
||||
QVariant output;
|
||||
QVariant variant(obj);
|
||||
QDataStream save(&buffer, QIODevice::WriteOnly);
|
||||
save << variant;
|
||||
QDataStream load(buffer);
|
||||
load >> output;
|
||||
QCOMPARE(output.userType(), QMetaType::QJsonObject);
|
||||
QCOMPARE(output.toJsonObject(), obj);
|
||||
}
|
||||
}
|
||||
|
||||
QTEST_MAIN(tst_QtJson)
|
||||
|
|
|
|||
|
|
@ -137,6 +137,7 @@ private slots:
|
|||
|
||||
void stream_QJsonDocument();
|
||||
void stream_QJsonArray();
|
||||
void stream_QJsonObject();
|
||||
|
||||
void setVersion_data();
|
||||
void setVersion();
|
||||
|
|
@ -2146,6 +2147,30 @@ void tst_QDataStream::stream_QJsonArray()
|
|||
}
|
||||
}
|
||||
|
||||
void tst_QDataStream::stream_QJsonObject()
|
||||
{
|
||||
QByteArray buffer;
|
||||
{
|
||||
QDataStream save(&buffer, QIODevice::WriteOnly);
|
||||
save << QByteArrayLiteral("invalidJson");
|
||||
QDataStream load(&buffer, QIODevice::ReadOnly);
|
||||
QJsonObject object;
|
||||
load >> object;
|
||||
QVERIFY(object.isEmpty());
|
||||
QVERIFY(load.status() != QDataStream::Ok);
|
||||
QCOMPARE(load.status(), QDataStream::ReadCorruptData);
|
||||
}
|
||||
{
|
||||
QDataStream save(&buffer, QIODevice::WriteOnly);
|
||||
QJsonObject objSave{{"foo", 1}, {"bar", 2}};
|
||||
save << objSave;
|
||||
QDataStream load(&buffer, QIODevice::ReadOnly);
|
||||
QJsonObject objLoad;
|
||||
load >> objLoad;
|
||||
QCOMPARE(objLoad, objSave);
|
||||
}
|
||||
}
|
||||
|
||||
void tst_QDataStream::setVersion_data()
|
||||
{
|
||||
QTest::addColumn<int>("vers");
|
||||
|
|
|
|||
Loading…
Reference in New Issue