Implement QJsonArray data stream operator

Change-Id: Ib3c83c06605f0fe39108855bc3416bf453cab043
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Jędrzej Nowacki 2018-09-03 15:17:57 +02:00
parent 1d0b5d26ec
commit ff03383017
5 changed files with 96 additions and 14 deletions

View File

@ -1259,5 +1259,22 @@ QDebug operator<<(QDebug dbg, const QJsonArray &a)
}
#endif
#ifndef QT_NO_DATASTREAM
QDataStream &operator<<(QDataStream &stream, const QJsonArray &array)
{
QJsonDocument doc{array};
stream << doc.toJson(QJsonDocument::Compact);
return stream;
}
QDataStream &operator>>(QDataStream &stream, QJsonArray &array)
{
QJsonDocument doc;
stream >> doc;
array = doc.array();
return stream;
}
#endif
QT_END_NAMESPACE

View File

@ -271,6 +271,11 @@ Q_CORE_EXPORT uint qHash(const QJsonArray &array, uint seed = 0);
Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonArray &);
#endif
#ifndef QT_NO_DATASTREAM
Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonArray &);
Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonArray &);
#endif
QT_END_NAMESPACE
#endif // QJSONARRAY_H

View File

@ -1823,7 +1823,6 @@ DECLARE_NONSTREAMABLE(QModelIndex)
DECLARE_NONSTREAMABLE(QPersistentModelIndex)
DECLARE_NONSTREAMABLE(QJsonValue)
DECLARE_NONSTREAMABLE(QJsonObject)
DECLARE_NONSTREAMABLE(QJsonArray)
DECLARE_NONSTREAMABLE(QCborValue)
DECLARE_NONSTREAMABLE(QCborArray)
DECLARE_NONSTREAMABLE(QCborMap)

View File

@ -153,8 +153,10 @@ private Q_SLOTS:
void implicitValueType();
void implicitDocumentType();
void streamSerialization_data();
void streamSerialization();
void streamSerializationQJsonDocument_data();
void streamSerializationQJsonDocument();
void streamSerializationQJsonArray_data();
void streamSerializationQJsonArray();
void streamVariantSerialization();
private:
@ -3015,14 +3017,14 @@ void tst_QtJson::implicitDocumentType()
QCOMPARE(arrayDocument[-1].toInt(123), 123);
}
void tst_QtJson::streamSerialization_data()
void tst_QtJson::streamSerializationQJsonDocument_data()
{
QTest::addColumn<QJsonDocument>("document");
QTest::newRow("empty") << QJsonDocument();
QTest::newRow("object") << QJsonDocument(QJsonObject{{"value", 42}});
}
void tst_QtJson::streamSerialization()
void tst_QtJson::streamSerializationQJsonDocument()
{
// Check interface only, implementation is tested through to and from
// json functions.
@ -3036,20 +3038,54 @@ void tst_QtJson::streamSerialization()
QCOMPARE(output, document);
}
void tst_QtJson::streamSerializationQJsonArray_data()
{
QTest::addColumn<QJsonArray>("array");
QTest::newRow("empty") << QJsonArray();
QTest::newRow("values") << QJsonArray{665, 666, 667};
}
void tst_QtJson::streamSerializationQJsonArray()
{
// Check interface only, implementation is tested through to and from
// json functions.
QByteArray buffer;
QFETCH(QJsonArray, array);
QJsonArray output;
QDataStream save(&buffer, QIODevice::WriteOnly);
save << array;
QDataStream load(buffer);
load >> output;
QCOMPARE(output, array);
}
void tst_QtJson::streamVariantSerialization()
{
// Check interface only, implementation is tested through to and from
// json functions.
QByteArray buffer;
QJsonDocument objectDoc(QJsonArray{665, 666, 667});
QVariant output;
QVariant variant(objectDoc);
QDataStream save(&buffer, QIODevice::WriteOnly);
save << variant;
QDataStream load(buffer);
load >> output;
QCOMPARE(output.userType(), QMetaType::QJsonDocument);
QCOMPARE(output.toJsonDocument(), objectDoc);
{
QJsonDocument objectDoc(QJsonArray{665, 666, 667});
QVariant output;
QVariant variant(objectDoc);
QDataStream save(&buffer, QIODevice::WriteOnly);
save << variant;
QDataStream load(buffer);
load >> output;
QCOMPARE(output.userType(), QMetaType::QJsonDocument);
QCOMPARE(output.toJsonDocument(), objectDoc);
}
{
QJsonArray array{665, 666, 667};
QVariant output;
QVariant variant(array);
QDataStream save(&buffer, QIODevice::WriteOnly);
save << variant;
QDataStream load(buffer);
load >> output;
QCOMPARE(output.userType(), QMetaType::QJsonArray);
QCOMPARE(output.toJsonArray(), array);
}
}
QTEST_MAIN(tst_QtJson)

View File

@ -136,6 +136,7 @@ private slots:
void stream_QByteArray2();
void stream_QJsonDocument();
void stream_QJsonArray();
void setVersion_data();
void setVersion();
@ -2121,6 +2122,30 @@ void tst_QDataStream::stream_QJsonDocument()
}
}
void tst_QDataStream::stream_QJsonArray()
{
QByteArray buffer;
{
QDataStream save(&buffer, QIODevice::WriteOnly);
save << QByteArrayLiteral("invalidJson");
QDataStream load(&buffer, QIODevice::ReadOnly);
QJsonArray array;
load >> array;
QVERIFY(array.isEmpty());
QVERIFY(load.status() != QDataStream::Ok);
QCOMPARE(load.status(), QDataStream::ReadCorruptData);
}
{
QDataStream save(&buffer, QIODevice::WriteOnly);
QJsonArray arraySave(QJsonArray{1,2,3});
save << arraySave;
QDataStream load(&buffer, QIODevice::ReadOnly);
QJsonArray arrayLoad;
load >> arrayLoad;
QCOMPARE(arrayLoad, arraySave);
}
}
void tst_QDataStream::setVersion_data()
{
QTest::addColumn<int>("vers");