mirror of
https://github.com/msgpack/msgpack-python.git
synced 2025-10-20 04:13:16 +00:00
Add object_hook
option to unpack and default
option to pack.
(see simplejson for how to use).
This commit is contained in:
parent
367f15c247
commit
fa157082ac
4 changed files with 92 additions and 10 deletions
31
test/test_obj.py
Normal file
31
test/test_obj.py
Normal file
|
@ -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()
|
Loading…
Add table
Add a link
Reference in a new issue