mirror of
https://github.com/aljazceru/bitfinex-api-py.git
synced 2025-12-20 07:14:20 +01:00
Improve JSONEncoder class in bfxapi.utils.json_encoder.
This commit is contained in:
@@ -9,18 +9,20 @@ JSON = Union[Dict[str, "JSON"], List["JSON"], bool, int, float, str, Type[None]]
|
||||
def _strip(dictionary: Dict) -> Dict:
|
||||
return { key: value for key, value in dictionary.items() if value is not None }
|
||||
|
||||
def _convert_float_to_str(data: JSON) -> JSON:
|
||||
def _convert_data_to_json(data: JSON) -> JSON:
|
||||
if isinstance(data, bool):
|
||||
return int(data)
|
||||
if isinstance(data, float):
|
||||
return format(Decimal(repr(data)), "f")
|
||||
if isinstance(data, list):
|
||||
return [ _convert_float_to_str(sub_data) for sub_data in data ]
|
||||
return [ _convert_data_to_json(sub_data) for sub_data in data ]
|
||||
if isinstance(data, dict):
|
||||
return _strip({ key: _convert_float_to_str(value) for key, value in data.items() })
|
||||
return _strip({ key: _convert_data_to_json(value) for key, value in data.items() })
|
||||
return data
|
||||
|
||||
class JSONEncoder(json.JSONEncoder):
|
||||
def encode(self, o: JSON) -> str:
|
||||
return json.JSONEncoder.encode(self, _convert_float_to_str(o))
|
||||
return json.JSONEncoder.encode(self, _convert_data_to_json(o))
|
||||
|
||||
def default(self, o: Any) -> Any:
|
||||
if isinstance(o, Decimal):
|
||||
|
||||
Reference in New Issue
Block a user