Skip to content

Commit

Permalink
WIP: support JSON-encoding of NDArrays
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrueden committed Jul 16, 2024
1 parent 2d74715 commit 14dda06
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/appose/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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":
Expand Down

0 comments on commit 14dda06

Please sign in to comment.