diff --git a/src/py_adapter/plugin/_csv.py b/src/py_adapter/plugin/_csv.py index d9fa56c..b18176a 100644 --- a/src/py_adapter/plugin/_csv.py +++ b/src/py_adapter/plugin/_csv.py @@ -39,3 +39,18 @@ def serialize(obj: py_adapter.Basic, stream: BinaryIO) -> BinaryIO: stream.write(text_stream.read().encode("utf-8")) stream.flush() return stream + + +@py_adapter.plugin.hook +def deserialize(stream: BinaryIO) -> py_adapter.Basic: + """ + Deserialize CSV data as an object of basic Python types + + :param stream: File-like object to deserialize + """ + import csv + + text_stream = io.StringIO(stream.read().decode("utf-8")) + csv_reader = csv.DictReader(text_stream) + obj = next(csv_reader) + return obj diff --git a/tests/test_csv.py b/tests/test_csv.py index 83b32ba..80db732 100644 --- a/tests/test_csv.py +++ b/tests/test_csv.py @@ -40,3 +40,5 @@ def test_serialize_1_record(simple_ship): data = py_adapter.serialize(simple_ship, format="CSV") expected_lines = [b"name,build_on", b"Elvira,1970-12-31"] assert data.splitlines() == expected_lines + obj_out = py_adapter.deserialize(data, SimpleShip, format="CSV") + assert obj_out == simple_ship