chg: globals - datetime serialization support without orjson

This commit is contained in:
RazCrimson 2024-09-08 15:22:39 +05:30 committed by RazCrimson
parent 1fa173edbc
commit e9ae9ff61b
1 changed files with 15 additions and 8 deletions

View File

@ -28,7 +28,7 @@ from configparser import ConfigParser, NoOptionError, NoSectionError
from datetime import datetime
from operator import itemgetter, methodcaller
from statistics import mean
from typing import Dict, List, Union
from typing import Any, Dict, List, Union
from urllib.error import HTTPError, URLError
from urllib.parse import urlparse
from urllib.request import Request, urlopen
@ -50,14 +50,21 @@ except ImportError:
# Need to log info but importing logger will cause cyclic imports
pass
try:
import ujson as json
except ImportError:
# Need to log info but importing logger will cause cyclic imports
pass
if 'json' not in globals():
import json
try:
# Note: ujson is not officially supported
# Available as a fallback to allow orjson's unsupported platforms to use a faster serialization lib
import ujson as json
except ImportError:
import json
# To allow ujson & json dumps to serialize datetime
def _json_default(v: Any) -> Any:
if isinstance(v, datetime):
return v.isoformat()
return v
json.dumps = functools.partial(json.dumps, default=_json_default)
##############
# GLOBALS VARS