Skip to content

Commit

Permalink
Support JSON-encoding of NDArrays
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrueden committed Jul 17, 2024
1 parent 1ed430e commit f561ced
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 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, separators=(",", ":"))
return json.dumps(data, cls=_ApposeJSONEncoder, separators=(",", ":"))


def decode(the_json: str) -> Args:
Expand Down Expand Up @@ -88,6 +88,24 @@ def ndarray(self):
raise ImportError("NumPy is not available.")


class _ApposeJSONEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, SharedMemory):
return {
"appose_type": "shm",
"name": obj.name,
"size": obj.size,
}
if isinstance(obj, NDArray):
return {
"appose_type": "ndarray",
"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 f561ced

Please sign in to comment.