Try this
import msgpack
import decimal
x = Decimal("1.3")
def decode_decimal(obj):
if b'__decimal__' in obj:
obj = decimal.Decimal(obj["as_str"])
return obj
def encode_decimal(obj):
if isinstance(obj, decimal.Decimal):
return {'__decimal__': True, 'as_str': str(x)}
return obj
print(x)
packed = msgpack.packb(x, default=encode_decimal)
print(packed)
unpacked = msgpack.unpackb(packed, object_hook=decode_decimal)
print(unpacked)