diff --git a/src/private/pack_p.cpp b/src/private/pack_p.cpp index 4a84c87..ca0065d 100644 --- a/src/private/pack_p.cpp +++ b/src/private/pack_p.cpp @@ -19,7 +19,10 @@ QReadWriteLock MsgPackPrivate::packers_lock; quint8 *MsgPackPrivate::pack(const QVariant &v, quint8 *p, bool wr, QVector &user_data) { QMetaType::Type t = (QMetaType::Type)v.type(); - if (t == QMetaType::Int) + + if (v.isNull()) + p = pack_nil(p, wr); + else if (t == QMetaType::Int) p = pack_int(v.toInt(), p, wr); else if (t == QMetaType::UInt) p = pack_uint(v.toUInt(), p, wr); @@ -49,6 +52,13 @@ quint8 *MsgPackPrivate::pack(const QVariant &v, quint8 *p, bool wr, QVector= -32 && i <= 127) { diff --git a/src/private/pack_p.h b/src/private/pack_p.h index db29911..7550377 100644 --- a/src/private/pack_p.h +++ b/src/private/pack_p.h @@ -25,6 +25,7 @@ extern bool compatibilityMode; quint8 * pack(const QVariant &v, quint8 *p, bool wr, QVector &user_data); +quint8 * pack_nil(quint8 *p, bool wr); quint8 * pack_int(qint32 i, quint8 *p, bool wr); quint8 * pack_uint(quint32 i, quint8 *p, bool wr); quint8 * pack_longlong(qint64 i, quint8 *p, bool wr); diff --git a/src/private/unpack_p.cpp b/src/private/unpack_p.cpp index cefdb99..8d2cc22 100644 --- a/src/private/unpack_p.cpp +++ b/src/private/unpack_p.cpp @@ -67,7 +67,7 @@ quint8 *MsgPackPrivate::unpack_type(QVariant &v, quint8 *p) quint8 * MsgPackPrivate::unpack_nil(QVariant &v, quint8 *p) { Q_UNUSED(p) - Q_UNUSED(v) + v = QVariant(); return p + 1; } diff --git a/tests/pack/pack_test.cpp b/tests/pack/pack_test.cpp index 235dc63..2769ffb 100644 --- a/tests/pack/pack_test.cpp +++ b/tests/pack/pack_test.cpp @@ -9,6 +9,7 @@ class PackTest : public QObject Q_OBJECT private Q_SLOTS: + void test_nil(); void test_bool(); void test_fixint(); void test_integer8(); @@ -21,6 +22,14 @@ private Q_SLOTS: void test_array(); }; +void PackTest::test_nil() +{ + QByteArray arr = MsgPack::pack(QVariant()); + quint8 *p = (quint8 *)arr.data(); + QVERIFY(arr.size() == 1); + QVERIFY(p[0] == 0xc0); +} + void PackTest::test_bool() { QByteArray arr = MsgPack::pack(false); diff --git a/tests/unpack/unpack_test.cpp b/tests/unpack/unpack_test.cpp index 10e8c48..02c91ab 100644 --- a/tests/unpack/unpack_test.cpp +++ b/tests/unpack/unpack_test.cpp @@ -9,6 +9,7 @@ class UnpackTest : public QObject Q_OBJECT private Q_SLOTS: + void test_nil(); void test_booleans(); void test_integers(); void test_floats(); @@ -17,6 +18,13 @@ private Q_SLOTS: void test_array(); }; +void UnpackTest::test_nil() +{ + QByteArray pack = MsgPack::pack(QVariantList() << true << QVariant()); + QVariantList u = MsgPack::unpack(pack).toList(); + QVERIFY(u[0] == true); + QVERIFY(u[1].isNull()); +} void UnpackTest::test_booleans() {