summaryrefslogtreecommitdiff
path: root/test/test_obj.py
diff options
context:
space:
mode:
authorINADA Naoki <songofacandy@gmail.com>2010-10-26 01:26:06 +0900
committerINADA Naoki <songofacandy@gmail.com>2010-10-26 01:26:06 +0900
commitfa157082ac8db71e3312ca97fe1ceb7f56546fcb (patch)
treeee9cfaa2f1470e2c6229cb5bf7bfcfaf7404f96f /test/test_obj.py
parent367f15c247bf37beb37e2a81d1d707bc8ef2e085 (diff)
downloadmsgpack-python-fa157082ac8db71e3312ca97fe1ceb7f56546fcb.tar.gz
Add `object_hook` option to unpack and `default` option to pack.
(see simplejson for how to use).
Diffstat (limited to 'test/test_obj.py')
-rw-r--r--test/test_obj.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/test/test_obj.py b/test/test_obj.py
new file mode 100644
index 0000000..64a6390
--- /dev/null
+++ b/test/test_obj.py
@@ -0,0 +1,31 @@
+#!/usr/bin/env python
+# coding: utf-8
+
+from nose import main
+from nose.tools import *
+
+from msgpack import packs, unpacks
+
+def _decode_complex(obj):
+ if '__complex__' in obj:
+ return complex(obj['real'], obj['imag'])
+ return obj
+
+def _encode_complex(obj):
+ if isinstance(obj, complex):
+ return {'__complex__': True, 'real': 1, 'imag': 2}
+ return obj
+
+def test_encode_hook():
+ packed = packs([3, 1+2j], default=_encode_complex)
+ unpacked = unpacks(packed)
+ eq_(unpacked[1], {'__complex__': True, 'real': 1, 'imag': 2})
+
+def test_decode_hook():
+ packed = packs([3, {'__complex__': True, 'real': 1, 'imag': 2}])
+ unpacked = unpacks(packed, object_hook=_decode_complex)
+ eq_(unpacked[1], 1+2j)
+
+if __name__ == '__main__':
+ #main()
+ test_decode_hook()