-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_helpers.py
More file actions
60 lines (47 loc) · 1.84 KB
/
json_helpers.py
File metadata and controls
60 lines (47 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
JSON serialization helpers for repository-wide precompute outputs.
Provides a `json_default` function suitable for `json.dump(..., default=...)`
that converts common numpy / pandas scalar and array types to native
Python types (int/float/bool/list/ISO strings). Also exposes `safe_dump`
which writes JSON using the helper by default.
"""
from __future__ import annotations
from pathlib import Path
import json
import datetime
import numpy as np
import pandas as pd
def json_default(obj):
"""Return a JSON-serializable representation for known types.
Intended for use as the ``default`` parameter to ``json.dump``/``dumps``.
"""
# numpy / pandas scalar types
if isinstance(obj, (np.bool_, bool)):
return bool(obj)
if isinstance(obj, (np.integer,)):
return int(obj)
if isinstance(obj, (np.floating,)):
return float(obj)
# numpy arrays -> lists
if isinstance(obj, (np.ndarray,)):
return obj.tolist()
# pandas / datetime -> ISO string
if isinstance(obj, (pd.Timestamp, datetime.datetime, datetime.date)):
return obj.isoformat()
# pandas NA
if obj is pd.NA:
return None
# Fallback for small containers
if isinstance(obj, (list, tuple, set)):
return list(obj)
raise TypeError(f"Object of type {obj.__class__.__name__} is not JSON serializable")
def safe_dump(obj, fp_or_path, **kwargs):
"""Write `obj` to `fp_or_path` using `json_default` for non-serializable types.
`fp_or_path` may be a file-like object or a path-like (str / Path).
Additional keyword args are forwarded to ``json.dump``.
"""
if isinstance(fp_or_path, (str, Path)):
with open(fp_or_path, 'w', encoding='utf-8') as f:
json.dump(obj, f, default=json_default, **kwargs)
else:
json.dump(obj, fp_or_path, default=json_default, **kwargs)