mirror of
https://github.com/aljazceru/bitfinex-api-py.git
synced 2025-12-19 06:44:22 +01:00
Rename bfxapi.utils to _utils (and update references).
This commit is contained in:
32
bfxapi/_utils/json_encoder.py
Normal file
32
bfxapi/_utils/json_encoder.py
Normal file
@@ -0,0 +1,32 @@
|
||||
import json
|
||||
|
||||
from decimal import Decimal
|
||||
|
||||
from typing import List, Dict, Union
|
||||
|
||||
JSON = Union[Dict[str, "JSON"], List["JSON"], bool, int, float, str, None]
|
||||
|
||||
_CustomJSON = Union[Dict[str, "_CustomJSON"], List["_CustomJSON"], \
|
||||
bool, int, float, str, Decimal, None]
|
||||
|
||||
def _strip(dictionary: Dict) -> Dict:
|
||||
return { key: value for key, value in dictionary.items() if value is not None }
|
||||
|
||||
def _convert_data_to_json(data: _CustomJSON) -> JSON:
|
||||
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 [ _convert_data_to_json(sub_data) for sub_data in data ]
|
||||
if isinstance(data, dict):
|
||||
return _strip({ key: _convert_data_to_json(value) for key, value in data.items() })
|
||||
|
||||
return data
|
||||
|
||||
class JSONEncoder(json.JSONEncoder):
|
||||
def encode(self, o: _CustomJSON) -> str:
|
||||
return json.JSONEncoder.encode(self, _convert_data_to_json(o))
|
||||
Reference in New Issue
Block a user