From ff033830176aaa2e7a525042c5c40a6517cb7858 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Nowacki?= Date: Mon, 3 Sep 2018 15:17:57 +0200 Subject: [PATCH] Implement QJsonArray data stream operator Change-Id: Ib3c83c06605f0fe39108855bc3416bf453cab043 Reviewed-by: Thiago Macieira --- src/corelib/serialization/qjsonarray.cpp | 17 +++++ src/corelib/serialization/qjsonarray.h | 5 ++ .../kernel/qmetatype/tst_qmetatype.cpp | 1 - .../corelib/serialization/json/tst_qtjson.cpp | 62 +++++++++++++++---- .../qdatastream/tst_qdatastream.cpp | 25 ++++++++ 5 files changed, 96 insertions(+), 14 deletions(-) diff --git a/src/corelib/serialization/qjsonarray.cpp b/src/corelib/serialization/qjsonarray.cpp index 1187bb03a3..5ba83c41b9 100644 --- a/src/corelib/serialization/qjsonarray.cpp +++ b/src/corelib/serialization/qjsonarray.cpp @@ -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 diff --git a/src/corelib/serialization/qjsonarray.h b/src/corelib/serialization/qjsonarray.h index 5dff4a0aa9..0a17951fb7 100644 --- a/src/corelib/serialization/qjsonarray.h +++ b/src/corelib/serialization/qjsonarray.h @@ -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 diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp index 4a027474a8..8c1ee17daa 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp @@ -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) diff --git a/tests/auto/corelib/serialization/json/tst_qtjson.cpp b/tests/auto/corelib/serialization/json/tst_qtjson.cpp index 713c69149b..5b03964140 100644 --- a/tests/auto/corelib/serialization/json/tst_qtjson.cpp +++ b/tests/auto/corelib/serialization/json/tst_qtjson.cpp @@ -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("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("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) diff --git a/tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp b/tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp index a689b0b620..88fe57d668 100644 --- a/tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp +++ b/tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp @@ -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("vers");