diff --git a/src/appose/types.py b/src/appose/types.py index 0de6bd4..1f7c324 100644 --- a/src/appose/types.py +++ b/src/appose/types.py @@ -35,7 +35,7 @@ def encode(data: Args) -> str: - return json.dumps(data) + return json.dumps(data, cls=_ApposeJSONEncoder) def decode(the_json: str) -> Args: @@ -88,6 +88,22 @@ def ndarray(self): raise ImportError("NumPy is not available.") +class _ApposeJSONEncoder(json.JSONEncoder): + def default(self, obj): + if isinstance(obj, SharedMemory): + return { + "name": obj.name, + "size": obj.size, + } + if isinstance(obj, NDArray): + return { + "dtype": obj.dtype, + "shape": obj.shape, + "shm": obj.shm, + } + return super().default(obj) + + def _appose_object_hook(obj: Dict): atype = obj.get("appose_type") if atype == "shm":