Compare commits

...

2 Commits

Author SHA1 Message Date
cce141423d use ExtendedJSONEncoder 2025-11-03 16:39:47 +01:00
941ab51856 added ExtendedJSONEncoder 2025-11-03 16:39:40 +01:00
3 changed files with 27 additions and 2 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)

View File

@@ -1,6 +1,8 @@
import numpy as np
import zmq
from .utils import ExtendedJSONEncoder
FLAGS = 0
@@ -61,10 +63,10 @@ class ZMQSocketsWorker:
def send_accumulator(self, results):
self.accumulator_socket.send_json(results, FLAGS)
self.accumulator_socket.send_json(results, FLAGS, cls=ExtendedJSONEncoder)
def send_visualisation(self, results, data):
self.visualisation_socket.send_json(results, FLAGS | zmq.SNDMORE)
self.visualisation_socket.send_json(results, FLAGS | zmq.SNDMORE, cls=ExtendedJSONEncoder)
self.visualisation_socket.send(data, FLAGS, copy=True, track=True)