diff --git a/src/py_adapter/__init__.py b/src/py_adapter/__init__.py index 7b85c81..966e584 100644 --- a/src/py_adapter/__init__.py +++ b/src/py_adapter/__init__.py @@ -22,7 +22,18 @@ import inspect import logging import uuid -from typing import Any, Callable, Dict, List, Optional, Type, TypeVar, Union, cast +from typing import ( + Any, + Callable, + Dict, + List, + Optional, + Sequence, + Type, + TypeVar, + Union, + cast, +) import avro.schema import dateutil.parser @@ -93,7 +104,7 @@ def serialize(obj: Any, *, format: str, writer_schema: bytes = b"") -> bytes: Serialize an object using a serialization format supported by **py-adapter** :param obj: Python object to serialize - :param format: Serialization format as supported by a **py-adpater** plugin, e.g. ``JSON``. + :param format: Serialization format as supported by a **py-adapter** plugin, e.g. ``JSON``. :param writer_schema: Data schema to serialize the data with, as JSON bytes. """ serialize_fn = py_adapter.plugin.plugin_hook(format, "serialize") @@ -102,13 +113,27 @@ def serialize(obj: Any, *, format: str, writer_schema: bytes = b"") -> bytes: return data +def serialize_many(objs: Sequence[Any], *, format: str, writer_schema: bytes = b"") -> bytes: + """ + Serialize multiple objects using a serialization format supported by **py-adapter** + + :param objs: Python objects to serialize + :param format: Serialization format as supported by a **py-adapter** plugin, e.g. ``JSON``. + :param writer_schema: Data schema to serialize the data with, as JSON bytes. + """ + serialize_fn = py_adapter.plugin.plugin_hook(format, "serialize_many") + basic_objs = [to_basic_type(obj) for obj in objs] + data = serialize_fn(objs=basic_objs, writer_schema=writer_schema) + return data + + def deserialize(data: bytes, py_type: Type[Obj], *, format: str, writer_schema: bytes = b"") -> Obj: """ Deserialize bytes as a Python object of a given type from a serialization format supported by **py-adapter** :param data: Serialized data :param py_type: The Python class to create an instance from - :param format: Serialization format as supported by a **py-adpater** plugin, e.g. ``JSON``. + :param format: Serialization format as supported by a **py-adapter** plugin, e.g. ``JSON``. :param writer_schema: Data schema used to serialize the data with, as JSON bytes. """ deserialize_fn = py_adapter.plugin.plugin_hook(format, "deserialize") diff --git a/tests/test_serialize.py b/tests/test_serialize.py index 16be5fe..c21c2da 100644 --- a/tests/test_serialize.py +++ b/tests/test_serialize.py @@ -97,7 +97,6 @@ def test_serialize_stream_avro(ship_obj, ship_class): assert obj_out == ship_obj -@pytest.mark.skip("TODO") def test_serialize_many_json(ship_obj, ship_class): ship_objs = [ship_obj, ship_obj] data = py_adapter.serialize_many(ship_objs, format="JSON")