core/serialize: serialize stdlib Decimal class
This commit is contained in:
parent
7098d6831f
commit
716a2c82ba
1 changed files with 6 additions and 0 deletions
|
@ -1,6 +1,7 @@
|
||||||
import datetime
|
import datetime
|
||||||
import dataclasses
|
import dataclasses
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from decimal import Decimal
|
||||||
from typing import Any, Optional, Callable, NamedTuple
|
from typing import Any, Optional, Callable, NamedTuple
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
|
|
||||||
|
@ -36,6 +37,11 @@ def _default_encode(obj: Any) -> Any:
|
||||||
return dataclasses.asdict(obj)
|
return dataclasses.asdict(obj)
|
||||||
if isinstance(obj, Exception):
|
if isinstance(obj, Exception):
|
||||||
return error_to_json(obj)
|
return error_to_json(obj)
|
||||||
|
# if something was stored as 'decimal', you likely
|
||||||
|
# don't want to convert it to float since you're
|
||||||
|
# storing as decimal to not lose the precision
|
||||||
|
if isinstance(obj, Decimal):
|
||||||
|
return str(obj)
|
||||||
# note: _serialize would only be called for items which aren't already
|
# note: _serialize would only be called for items which aren't already
|
||||||
# serialized as a dataclass or namedtuple
|
# serialized as a dataclass or namedtuple
|
||||||
# discussion: https://github.com/karlicoss/HPI/issues/138#issuecomment-801704929
|
# discussion: https://github.com/karlicoss/HPI/issues/138#issuecomment-801704929
|
||||||
|
|
Loading…
Add table
Reference in a new issue