added ExtendedJSONEncoder

This commit is contained in:
2025-11-03 16:39:40 +01:00
parent aac524dd36
commit 941ab51856
2 changed files with 23 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ from .bsreadext import make_bsread_sender, pack_bsread_data
from .bits import read_bit
from .bufjson import BufferedJSON
from .filehandler import FileHandler
from .jsonext import ExtendedJSONEncoder
from .randskip import randskip
from .sorter import Sorter
from .timestamp import make_bsread_timestamp

22
dap/utils/jsonext.py Normal file
View File

@@ -0,0 +1,22 @@
import json
import numpy as np
from pathlib import Path
class ExtendedJSONEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.ndarray):
return obj.tolist()
elif isinstance(obj, np.generic): # covers all numpy scalars
return obj.item()
elif isinstance(obj, complex): # covers builtin complex
return {"real": obj.real, "imag": obj.imag}
elif isinstance(obj, Path):
return str(obj)
elif isinstance(obj, set):
return sorted(obj)
return super().default(obj)