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,))
评论列表
文章目录