Skip to content

Commit e9ae9ff

Browse files
committed
chg: globals - datetime serialization support without orjson
1 parent 1fa173e commit e9ae9ff

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

glances/globals.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from datetime import datetime
2929
from operator import itemgetter, methodcaller
3030
from statistics import mean
31-
from typing import Dict, List, Union
31+
from typing import Any, Dict, List, Union
3232
from urllib.error import HTTPError, URLError
3333
from urllib.parse import urlparse
3434
from urllib.request import Request, urlopen
@@ -50,14 +50,21 @@
5050
# Need to log info but importing logger will cause cyclic imports
5151
pass
5252

53-
try:
54-
import ujson as json
55-
except ImportError:
56-
# Need to log info but importing logger will cause cyclic imports
57-
pass
58-
5953
if 'json' not in globals():
60-
import json
54+
try:
55+
# Note: ujson is not officially supported
56+
# Available as a fallback to allow orjson's unsupported platforms to use a faster serialization lib
57+
import ujson as json
58+
except ImportError:
59+
import json
60+
61+
# To allow ujson & json dumps to serialize datetime
62+
def _json_default(v: Any) -> Any:
63+
if isinstance(v, datetime):
64+
return v.isoformat()
65+
return v
66+
67+
json.dumps = functools.partial(json.dumps, default=_json_default)
6168

6269
##############
6370
# GLOBALS VARS

0 commit comments

Comments
 (0)