Repository: xconnio/wampproto-python (language: python)

Issue:
implement Msgpack serializer



Discussion / hints:

Fix patch:
diff --git a/requirements-pinned.txt b/requirements-pinned.txt
index 9ae3d00..62ec7cf 100644
--- a/requirements-pinned.txt
+++ b/requirements-pinned.txt
@@ -3,6 +3,7 @@ cffi==1.16.0
 coverage==7.4.3
 exceptiongroup==1.2.0
 iniconfig==2.0.0
+msgpack==1.0.8
 packaging==23.2
 pip==24.0
 pluggy==1.4.0
diff --git a/requirements.txt b/requirements.txt
index 91a2ea8..25a1a3c 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,3 +1,4 @@
 ruff
 cbor2
 pynacl
+msgpack
diff --git a/wampproto/serializers/msgpack.py b/wampproto/serializers/msgpack.py
new file mode 100644
index 0000000..6bcb923
--- /dev/null
+++ b/wampproto/serializers/msgpack.py
@@ -0,0 +1,13 @@
+import msgpack
+
+from wampproto import messages, serializers
+from wampproto.serializers.serializer import to_message
+
+
+class MsgPackSerializer(serializers.Serializer):
+    def serialize(self, message: messages.Message) -> bytes:
+        return msgpack.dumps(message.marshal())
+
+    def deserialize(self, data: bytes) -> messages.Message:
+        wamp_message = msgpack.loads(data)
+        return to_message(wamp_message)


Test patch:
diff --git a/tests/serializers/test_msgpack.py b/tests/serializers/test_msgpack.py
new file mode 100644
index 0000000..0dea336
--- /dev/null
+++ b/tests/serializers/test_msgpack.py
@@ -0,0 +1,15 @@
+from wampproto.messages import Hello
+from wampproto.serializers.msgpack import MsgPackSerializer
+
+
+def test_serializer():
+    serializer = MsgPackSerializer()
+    hello = Hello.parse([1, "realm1", {"roles": {"callee": {}}}])
+
+    data = serializer.serialize(hello)
+    assert isinstance(data, bytes)
+
+    obj = serializer.deserialize(data)
+    assert isinstance(obj, Hello)
+    assert obj.realm == hello.realm
+    assert obj.roles == hello.roles