A safe serialization library for Python
Project description
SafeSerialize
SafeSerialize is a safe and extensible binary serialization library for Python.
Ever got an error like TypeError: Object of type set is not JSON serializable? - No more!
This library supports
- Python's builtin data types (
set,frozenset,dict,bytes, ...), - many types from Python's standard library (
datetime,decimal,Counter,deque, ...), - NumPy arrays and scalar data types,
- SciPy BSR, CSR, CSC and COO sparse matrices,
- custom user-defined types.
Unlike pickle,
this library is designed to be safe and does not execute arbitrary code when loading untrusted data.
Installation
You can install SafeSerialize from PyPI:
pip install safeserialize
Third party libraries (e.g. NumPy) are optional. Support will automatically be enabled once they are installed.
Usage
Here is a quick example of how to use SafeSerialize.
It should mostly be a drop-in replacement for pickle.
from safeserialize import dumps, loads
from datetime import datetime
from decimal import Decimal
from collections import Counter
# Create a complex object
data = {
"an_integer": 42,
"a_string": "Hello, World!",
"a_list": [1, 2.0, "three"],
frozenset("a_set"): {"foo", "bar"},
"a_datetime": datetime.now(),
"a_counter": Counter("banana"),
"a_decimal": Decimal("3.14159"),
}
# Serialize the data as a bytes
serialized_bytes = dumps(data)
# Deserialize the object
deserialized_data = loads(serialized_bytes)
assert data == deserialized_data
print("Serialization and deserialization successful!")
Serialization directly to files is also supported.
from safeserialize import dump, load
data = {1, 2.0, ..., "four!"}
filename = "data.safeserialize"
with open(filename, "wb") as f:
dump(data, f)
with open(filename, "rb") as f:
deserialized_data = load(f)
assert data == deserialized_data
print("Serialization and deserialization successful!")
Running Tests
To run the tests, first clone the repository and install the development dependencies:
git clone https://github.com/your-username/safeserialize.git
cd safeserialize
pip install -e .[test]
Then, run pytest from the root directory:
pytest
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file safeserialize-0.0.1.tar.gz.
File metadata
- Download URL: safeserialize-0.0.1.tar.gz
- Upload date:
- Size: 12.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
224bbe3aeb2d4ad0d89ddf76dce755444135025f788d7d7004766af2c04b5b33
|
|
| MD5 |
7f7807d94d540534504a42445aef9cb9
|
|
| BLAKE2b-256 |
0ac43d9a56c85e1c631654ff09b4477a0d63338bdd21e997ed41d377c7fc4713
|
File details
Details for the file safeserialize-0.0.1-py3-none-any.whl.
File metadata
- Download URL: safeserialize-0.0.1-py3-none-any.whl
- Upload date:
- Size: 11.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ce50a1f66d0671687be50a677eaeb02f4ccdf071074985a217240fd5edb3c43
|
|
| MD5 |
80d24da2f89ab3404116416e533802a2
|
|
| BLAKE2b-256 |
c70b4413e6fbb59043bc771b8db4ed8411e61a76ed2fe94eb223ceed290e5409
|