ZeroRPC can not handle decimals

98 views
Skip to first unread message

Mauricio de Abreu Antunes

unread,
Apr 30, 2014, 5:02:39 PM4/30/14
to zer...@googlegroups.com
Well, 

I am querying the database and reading the ZeroRPC source code trying to find why it happens.
I know it is a feature not implemented (and will not ever be I guess so) in MsgPack.
Is there a way to handle this in MsgPack since MsgPack is implemented inside ZeroRPC.

Mauricio de Abreu Antunes

unread,
May 6, 2014, 5:27:54 PM5/6/14
to zer...@googlegroups.com
Anyone please?

Sébastien

unread,
Jul 1, 2014, 3:35:27 PM7/1/14
to zer...@googlegroups.com
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)

Sébastien

unread,
Jul 1, 2014, 3:37:12 PM7/1/14
to zer...@googlegroups.com
sorry
def encode_decimal(obj):
    if isinstance(obj, decimal.Decimal):
        return {'__decimal__': True, 'as_str': str(obj)}
    return obj
Reply all
Reply to author
Forward
0 new messages