Skip to main content

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,
  • (experimental) Pandas support,
  • 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!")

For more usage examples, see the tests.

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

safeserialize-0.0.2.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

safeserialize-0.0.2-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file safeserialize-0.0.2.tar.gz.

File metadata

  • Download URL: safeserialize-0.0.2.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for safeserialize-0.0.2.tar.gz
Algorithm Hash digest
SHA256 03b2ebac3bc6ae63efc256c841ad30efb8a72127e96793a819c9a0ab3e2cba8b
MD5 986eb83b8cd897e7c462e737872ea957
BLAKE2b-256 482262e04aacf1a72e5876870a8777ce9edf41ff54290bbd78eb7f09568bb4fd

See more details on using hashes here.

File details

Details for the file safeserialize-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: safeserialize-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for safeserialize-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 efd57dcb7b1bbb8f872ef2c48a610cd62a46a04fd405b6a1a23ad697b4bc516f
MD5 6dfd965d30b6c813d10dd55b7f0010da
BLAKE2b-256 9b48baa11ebff890992f2f166021f9f4d48a0779c449caa407f60d965d1ec68d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page