ormsgpack icon indicating copy to clipboard operation
ormsgpack copied to clipboard

Add support for `decimal.Decimal`

Open scnerd opened this issue 1 year ago • 1 comments

ormsgpack version: 1.2.3

Current behavior

>>> import decimal
>>> ormsgpack.packb(decimal.Decimal("12.345"))
Traceback (most recent call last):
  File "...", in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-119-58a3aa663d1a>", line 1, in <module>
    ormsgpack.packb(decimal.Decimal("12.345"))
TypeError: Type is not msgpack serializable: decimal.Decimal

Desired behavior

Decimals seem to be primarily stored as strings. It should be lossless and reasonably fast to cast decimal.Decimal to str during serialization, and back during deserialization.

scnerd avatar Aug 08 '22 20:08 scnerd

hey @scnerd, thanks for the suggestion. I don't have time to add more features currently, but if someone wants to send a PR it'd be great!

aviramha avatar Aug 18 '22 14:08 aviramha

Decimals seem to be primarily stored as strings. It should be lossless and reasonably fast to cast decimal.Decimal to str during serialization, and back during deserialization.

Weezytoughtem avatar Jun 23 '23 07:06 Weezytoughtem

Arbitrary types can be serialized using the default callable, see https://github.com/aviramha/ormsgpack#default (the example uses decimal).

exg avatar Sep 24 '23 16:09 exg