Last active
September 13, 2018 11:26
-
-
Save hhromic/63512a24097b37500fc4296eafa12984 to your computer and use it in GitHub Desktop.
MessagePack encoding/decoding with extended and binary types support in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import msgpack | |
def _encode_ext_type(obj): | |
if isinstance(obj, set): # encode 'set' with typecode 10 | |
return msgpack.ExtType(10, packb(tuple(obj))) | |
raise TypeError("unknown extended type for %r" % type(obj)) | |
def _decode_ext_type(typecode, data): | |
if typecode == 10: # decode a 'set' type | |
return set(unpackb(data)) | |
raise TypeError("unknown extended typecode %d" % typecode) | |
def pack(obj, stream, **kwargs): | |
"""Pack object using MessagePack (with extended types support) | |
and write packed bytes to a stream.""" | |
msgpack.pack(obj, stream, default=_encode_ext_type, use_bin_type=True, **kwargs) | |
def packb(obj, **kwargs): | |
"""Pack object using MessagePack (with extended types support) | |
and return packed bytes.""" | |
return msgpack.packb(obj, default=_encode_ext_type, use_bin_type=True, **kwargs) | |
def packer(*args, **kwargs): | |
"""Return a MessagePack (with extended types support) Packer object.""" | |
return msgpack.Packer(*args, default=_encode_ext_type, use_bin_type=True, **kwargs) | |
def unpack(stream, **kwargs): | |
"""Unpack a stream of packed bytes using MessagePack (with extended types support) | |
and return unpacked object.""" | |
return msgpack.unpack(stream, ext_hook=_decode_ext_type, raw=False, **kwargs) | |
def unpackb(packed, **kwargs): | |
"""Unpack packed bytes using MessagePack (with extended types support) | |
and return unpacked object.""" | |
return msgpack.unpackb(packed, ext_hook=_decode_ext_type, raw=False, **kwargs) | |
def unpacker(*args, **kwargs): | |
"""Return a MessagePack (with extended types support) Unpacker object.""" | |
return msgpack.Unpacker(*args, ext_hook=_decode_ext_type, raw=False, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment