Encode decimals as number in JSON

This commit is contained in:
Oliver Traber 2025-04-28 17:52:35 +00:00
parent 148275dd98
commit b1a94c5359
Signed by: Bluemedia
GPG key ID: C0674B105057136C
6 changed files with 36 additions and 1 deletions

View file

@ -0,0 +1,22 @@
from decimal import Decimal
from typing import Union
def decimal_encoder(dec_value: Decimal) -> Union[int, float]:
"""Encodes a Decimal as int of there's no exponent, otherwise float.
This is useful when we use ConstrainedDecimal to represent Numeric(x,0)
where a integer (but not int typed) is used. Encoding this as a float
results in failed round-tripping between encode and parse.
Our Id type is a prime example of this.
>>> decimal_encoder(Decimal("1.0"))
1.0
>>> decimal_encoder(Decimal("1"))
1
"""
exponent = dec_value.as_tuple().exponent
if isinstance(exponent, int) and exponent >= 0:
return int(dec_value)
else:
return float(dec_value)