Implement QJsonValue data stream operator

Change-Id: I9cff40828ab68b4e2474da506b2da2cfed479f2c
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Luca Beldi <v.ronin@yahoo.it>
This commit is contained in:
Jędrzej Nowacki 2018-09-03 17:16:19 +02:00
parent 4981c2c8b5
commit 0e84e3866c
5 changed files with 165 additions and 1 deletions

View File

@ -45,6 +45,7 @@
#include <qvariant.h>
#include <qstringlist.h>
#include <qdebug.h>
#include "qdatastream.h"
#ifndef QT_BOOTSTRAPPED
# include <qcborarray.h>
@ -933,4 +934,78 @@ QDebug operator<<(QDebug dbg, const QJsonValue &o)
}
#endif
#ifndef QT_NO_DATASTREAM
QDataStream &operator<<(QDataStream &stream, const QJsonValue &v)
{
quint8 type = v.t;
stream << type;
switch (type) {
case QJsonValue::Undefined:
case QJsonValue::Null:
break;
case QJsonValue::Bool:
stream << v.toBool();
break;
case QJsonValue::Double:
stream << v.toDouble();
break;
case QJsonValue::String:
stream << v.toString();
break;
case QJsonValue::Array:
stream << v.toArray();
break;
case QJsonValue::Object:
stream << v.toObject();
break;
}
return stream;
}
QDataStream &operator>>(QDataStream &stream, QJsonValue &v)
{
quint8 type;
stream >> type;
switch (type) {
case QJsonValue::Undefined:
case QJsonValue::Null:
v = QJsonValue{QJsonValue::Type(type)};
break;
case QJsonValue::Bool: {
bool b;
stream >> b;
v = QJsonValue(b);
break;
} case QJsonValue::Double: {
double d;
stream >> d;
v = QJsonValue{d};
break;
} case QJsonValue::String: {
QString s;
stream >> s;
v = QJsonValue{s};
break;
}
case QJsonValue::Array: {
QJsonArray a;
stream >> a;
v = QJsonValue{a};
break;
}
case QJsonValue::Object: {
QJsonObject o;
stream >> o;
v = QJsonValue{o};
break;
}
default: {
stream.setStatus(QDataStream::ReadCorruptData);
v = QJsonValue{QJsonValue::Undefined};
}
}
return stream;
}
#endif
QT_END_NAMESPACE

View File

@ -152,6 +152,7 @@ private:
friend class QJsonObject;
friend class QCborValue;
friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &);
friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonValue &);
QJsonValue(QJsonPrivate::Data *d, QJsonPrivate::Base *b, const QJsonPrivate::Value& v);
void stringDataFromQStringHelper(const QString &string);
@ -253,6 +254,11 @@ Q_CORE_EXPORT uint qHash(const QJsonValue &value, uint seed = 0);
Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &);
#endif
#ifndef QT_NO_DATASTREAM
Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonValue &);
Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonValue &);
#endif
QT_END_NAMESPACE
#endif // QJSONVALUE_H

View File

@ -1821,7 +1821,6 @@ DECLARE_NONSTREAMABLE(void)
DECLARE_NONSTREAMABLE(void*)
DECLARE_NONSTREAMABLE(QModelIndex)
DECLARE_NONSTREAMABLE(QPersistentModelIndex)
DECLARE_NONSTREAMABLE(QJsonValue)
DECLARE_NONSTREAMABLE(QCborValue)
DECLARE_NONSTREAMABLE(QCborArray)
DECLARE_NONSTREAMABLE(QCborMap)

View File

@ -159,6 +159,9 @@ private Q_SLOTS:
void streamSerializationQJsonArray();
void streamSerializationQJsonObject_data();
void streamSerializationQJsonObject();
void streamSerializationQJsonValue_data();
void streamSerializationQJsonValue();
void streamSerializationQJsonValueEmpty();
void streamVariantSerialization();
private:
@ -3082,6 +3085,51 @@ void tst_QtJson::streamSerializationQJsonObject()
QCOMPARE(output, object);
}
void tst_QtJson::streamSerializationQJsonValue_data()
{
QTest::addColumn<QJsonValue>("value");
QTest::newRow("double") << QJsonValue{665};
QTest::newRow("bool") << QJsonValue{true};
QTest::newRow("string") << QJsonValue{QStringLiteral("bum")};
QTest::newRow("array") << QJsonValue{QJsonArray{12,1,5,6,7}};
QTest::newRow("object") << QJsonValue{QJsonObject{{"foo", 665}, {"bar", 666}}};
}
void tst_QtJson::streamSerializationQJsonValue()
{
QByteArray buffer;
QFETCH(QJsonValue, value);
QJsonValue output;
QDataStream save(&buffer, QIODevice::WriteOnly);
save << value;
QDataStream load(buffer);
load >> output;
QCOMPARE(output, value);
}
void tst_QtJson::streamSerializationQJsonValueEmpty()
{
QByteArray buffer;
{
QJsonValue undef{QJsonValue::Undefined};
QDataStream save(&buffer, QIODevice::WriteOnly);
save << undef;
QDataStream load(buffer);
QJsonValue output;
load >> output;
QVERIFY(output.isUndefined());
}
{
QJsonValue null{QJsonValue::Null};
QDataStream save(&buffer, QIODevice::WriteOnly);
save << null;
QDataStream load(buffer);
QJsonValue output;
load >> output;
QVERIFY(output.isNull());
}
}
void tst_QtJson::streamVariantSerialization()
{
// Check interface only, implementation is tested through to and from
@ -3120,6 +3168,17 @@ void tst_QtJson::streamVariantSerialization()
QCOMPARE(output.userType(), QMetaType::QJsonObject);
QCOMPARE(output.toJsonObject(), obj);
}
{
QJsonValue value{42};
QVariant output;
QVariant variant(value);
QDataStream save(&buffer, QIODevice::WriteOnly);
save << variant;
QDataStream load(buffer);
load >> output;
QCOMPARE(output.userType(), QMetaType::QJsonValue);
QCOMPARE(output.toJsonValue(), value);
}
}
QTEST_MAIN(tst_QtJson)

View File

@ -138,6 +138,7 @@ private slots:
void stream_QJsonDocument();
void stream_QJsonArray();
void stream_QJsonObject();
void stream_QJsonValue();
void setVersion_data();
void setVersion();
@ -2171,6 +2172,30 @@ void tst_QDataStream::stream_QJsonObject()
}
}
void tst_QDataStream::stream_QJsonValue()
{
QByteArray buffer;
{
QDataStream save(&buffer, QIODevice::WriteOnly);
save << quint8(42);
QDataStream load(&buffer, QIODevice::ReadOnly);
QJsonValue value;
load >> value;
QVERIFY(value.isUndefined());
QVERIFY(load.status() != QDataStream::Ok);
QCOMPARE(load.status(), QDataStream::ReadCorruptData);
}
{
QDataStream save(&buffer, QIODevice::WriteOnly);
QJsonValue valueSave{42};
save << valueSave;
QDataStream load(&buffer, QIODevice::ReadOnly);
QJsonValue valueLoad;
load >> valueLoad;
QCOMPARE(valueLoad, valueSave);
}
}
void tst_QDataStream::setVersion_data()
{
QTest::addColumn<int>("vers");