Source code for smartrpyc.utils.serialization

"""
Serialization utilities
"""
import json
import pickle

import msgpack


__all__ = ['MsgPackSerializer', 'CustomMsgPackSerializer', 'JsonSerializer',
           'PickleSerializer']


[docs]class MsgPackSerializer(object): """ Messagepack-based serializer. Thanks to the new features added in msgpack, we now support binary vs unicode strings natively, and other nice things. """ @staticmethod
[docs] def packb(o): #return msgpack.packb(o, encoding='utf-8') msgpack_packer = msgpack.Packer(encoding='utf-8', use_bin_type=True) return msgpack_packer.pack(o)
@staticmethod
[docs] def unpackb(packed): #return msgpack.unpackb(packed, encoding='utf-8') msgpack_unpacker = msgpack.Unpacker(encoding='utf-8') msgpack_unpacker.feed(packed) return msgpack_unpacker.unpack() ## For backwards compatibility. ## This is now deprecated in favor of standard ## MsgPackSerializer.
CustomMsgPackSerializer = MsgPackSerializer
[docs]class JsonSerializer(object): """ .. warning:: We have a problem with blobs here, since all the strings are automatically converted to unicode... """ @staticmethod
[docs] def packb(o): return json.dumps(o).encode('utf-8')
@staticmethod
[docs] def unpackb(packed): return json.loads(packed.decode('utf-8'))
[docs]class PickleSerializer(object): """ Pickle-powered serializer .. warning:: Never, ever, use this for untrusted data!! Big security risk!! """ @staticmethod
[docs] def packb(o): return pickle.dumps(o)
@staticmethod
[docs] def unpackb(packed): return pickle.loads(packed)