mirror of
https://github.com/aljazceru/bitfinex-api-py.git
synced 2025-12-19 14:54:21 +01:00
37 lines
1.0 KiB
Python
37 lines
1.0 KiB
Python
from typing import \
|
|
Union, List, Dict, \
|
|
Any
|
|
|
|
import json
|
|
|
|
from decimal import Decimal
|
|
|
|
_ExtJSON = Union[Dict[str, "_ExtJSON"], List["_ExtJSON"], \
|
|
bool, int, float, str, Decimal, None]
|
|
|
|
_StrictJSON = Union[Dict[str, "_StrictJSON"], List["_StrictJSON"], \
|
|
int, str, None]
|
|
|
|
def _clear(dictionary: Dict[str, Any]) -> Dict[str, Any]:
|
|
return { key: value for key, value in dictionary.items() \
|
|
if value is not None }
|
|
|
|
def _adapter(data: _ExtJSON) -> _StrictJSON:
|
|
if isinstance(data, bool):
|
|
return int(data)
|
|
if isinstance(data, float):
|
|
return format(Decimal(repr(data)), "f")
|
|
if isinstance(data, Decimal):
|
|
return format(data, "f")
|
|
|
|
if isinstance(data, list):
|
|
return [ _adapter(sub_data) for sub_data in data ]
|
|
if isinstance(data, dict):
|
|
return _clear({ key: _adapter(value) for key, value in data.items() })
|
|
|
|
return data
|
|
|
|
class JSONEncoder(json.JSONEncoder):
|
|
def encode(self, o: _ExtJSON) -> str:
|
|
return super().encode(_adapter(o))
|