diff --git a/README.md b/README.md index d440265..87cc3ae 100644 --- a/README.md +++ b/README.md @@ -49,14 +49,21 @@ We use [pickle](https://docs.python.org/3/library/pickle.html) as default to rea You can create a custom serializer by implementing the `dumps` and `loads` methods. ```python +NULL_BYTE: Final = b"\x00" + + class JSONSerializer: def dumps(self, obj: dict) -> bytes: - return json.dumps(obj).encode() + return json.dumps(obj).encode() + NULL_BYTE def loads(self, data: bytes) -> dict: + data = data.split(NULL_BYTE, 1)[0] return json.loads(data) + ``` +Note: A null byte is used to separate the dictionary contents from the bytes that are in memory. + To use the custom serializer you must set it when creating a new shared memory dict instance: ```python diff --git a/shared_memory_dict/serializers.py b/shared_memory_dict/serializers.py index 617e64f..7fc0b74 100644 --- a/shared_memory_dict/serializers.py +++ b/shared_memory_dict/serializers.py @@ -18,7 +18,7 @@ def dumps(self, obj: dict) -> bytes: return json.dumps(obj).encode() + NULL_BYTE def loads(self, data: bytes) -> dict: - data = bytes(data).split(NULL_BYTE, 1)[0] + data = data.split(NULL_BYTE, 1)[0] return json.loads(data)