diff options
author | Antonio Cuni <anto.cuni@gmail.com> | 2013-10-15 16:59:43 +0200 |
---|---|---|
committer | Antonio Cuni <anto.cuni@gmail.com> | 2013-10-15 16:59:43 +0200 |
commit | d61097511a1caa0e3bc5a70c1d2d92f448bd5025 (patch) | |
tree | e6d384d7b70c81a38e8f94c2f25ba6d5704df458 /test/test_extension.py | |
parent | f45d7b4e2d362222698b755444ffb61f1cf74b02 (diff) | |
download | msgpack-python-d61097511a1caa0e3bc5a70c1d2d92f448bd5025.tar.gz |
add support for extended types: you can now pack/unpack custom python objects by subclassing Packer and Unpacker
Diffstat (limited to 'test/test_extension.py')
-rw-r--r-- | test/test_extension.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/test/test_extension.py b/test/test_extension.py new file mode 100644 index 0000000..45e6027 --- /dev/null +++ b/test/test_extension.py @@ -0,0 +1,24 @@ +import array +import msgpack + +def test_extension_type(): + class MyPacker(msgpack.Packer): + def handle_extended_type(self, obj): + if isinstance(obj, array.array): + fmt = "ext 32" + typecode = 123 # application specific typecode + data = obj.tostring() + return fmt, typecode, data + + class MyUnpacker(msgpack.Unpacker): + def handle_extended_type(self, typecode, data): + assert typecode == 123 + obj = array.array('d') + obj.fromstring(data) + return obj + + obj = [42, 'hello', array.array('d', [1.1, 2.2, 3.3])] + s = msgpack.packb(obj, MyPacker) + obj2 = msgpack.unpackb(s, MyUnpacker) + assert obj == obj2 + |