msgpack_serializer.py 文件源码

python
阅读 17 收藏 0 点赞 0 评论 0

项目:pysoa 作者: eventbrite 项目源码 文件源码
def default(self, obj):
        """
        Encodes unknown object types (we use it to make extended types)
        """
        if isinstance(obj, datetime.datetime):
            # Datetimes. Make sure it's naive.
            if obj.tzinfo is not None:
                raise TypeError("Cannot encode timezone-aware datetimes to msgpack")
            # Then, work out the timestamp in seconds
            seconds = (obj - datetime.datetime(1970, 1, 1)).total_seconds()
            microseconds = int(seconds * 1000000)
            # Then pack it into a big-endian signed 64-bit integer
            return msgpack.ExtType(self.EXT_DATETIME, self.STRUCT_DATETIME.pack(microseconds))
        elif isinstance(obj, currint.Amount):
            # Currint. Start with the lowercased currency code as bytes
            code = obj.currency.code.upper()
            if isinstance(code, six.text_type):
                code = code.encode("ascii")
            # Then pack it in with the minor value
            return msgpack.ExtType(self.EXT_CURRINT, self.STRUCT_CURRINT.pack(code, obj.value))
        else:
            # Wuh-woh
            raise TypeError("Cannot encode value to msgpack: %r" % (obj,))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号