Cannot marshal <class 'decimal.Decimal'> objects

Skip Montanaro skip.montanaro at gmail.com
Fri Nov 27 18:28:26 EST 2020


> Because the marshaling is happening in the guts of xmlrpc.

Okay, I misunderstood your original message. You used the word
"marshal" (and the xmlrpc error did as well). I thought you were
referring to the marshal module. I didn't understand (and ignored) the
references to the xmlrpc module. In Python 3, the Marshaller class has
migrated into the xmlrpc.client module.

Your code worked for me in 3.8. Note, however, that this is not going
to round trip properly. You will stuff in a Decimal object at one end
and get out a double at the other end. Recent versions support
unmarshalling to Decimal objects in the Unmarshaller class. I don't
know why the Marshaller class doesn't accept Decimal objects for
serialization. You could try this:

def dump_decimal(self, value, write):
    write("<value><ex:bigdecimal>")
    write(str(value))
    write("</ex:bigdecimal></value>\n")

xmlrpc.client.Marshaller.dispatch[decimal.Decimal] = dump_decimal

I used the "ex:" prefix based on this document:

http://ws.apache.org/xmlrpc/types.html

YMMV. The tag name understood by the Unmarshaller class doesn't
include that prefix.

Skip


More information about the Python-list mailing list