Srsly: High-Performance Python Serialization Utilities
Srsly is a Python library providing modern, high-performance serialization utilities. It bundles optimized versions of popular serialization libraries (like ujson, msgpack, cloudpickle, and ruamel.yaml) into a single, standalone package, offering a consistent high-level API for handling JSON, JSONL, MessagePack, Pickle, and YAML data across different platforms and Python versions. It is actively maintained with frequent updates.
Warnings
- breaking Srsly v2.x (and later) dropped support for Python 2.x. Users migrating from v1.x must ensure their environment is Python 3.6 or newer.
- gotcha Versions prior to v1.0.7 and early v2.x contained security vulnerabilities (CVE-2022-31116, CVE-2021-4595) related to incorrect handling of surrogate pair characters and buffer overflows in the vendored `ujson` component.
- gotcha Srsly v2.4.7 introduced a restriction to Cython 0.29.x due to incompatibilities with Cython 3. This primarily affects users who compile srsly from source or have Cython 3 installed in their environment.
Install
-
pip install srsly
Imports
- json_loads
import srsly data = srsly.json_loads('{"key": "value"}') - read_json
import srsly data = srsly.read_json('path/to/file.json') - write_json
import srsly srsly.write_json('path/to/file.json', {'key': 'value'}) - msgpack_dumps
import srsly msg_bytes = srsly.msgpack_dumps({'key': 'value'})
Quickstart
import srsly
import os
# Example with JSON
file_path = 'example.json'
data = {"name": "Alice", "age": 30, "isStudent": False}
# Write JSON to a file
srsly.write_json(file_path, data)
print(f"Data written to {file_path}")
# Read JSON from a file
read_data = srsly.read_json(file_path)
print(f"Data read from {file_path}: {read_data}")
# Clean up
os.remove(file_path)
# Example with JSONL (newline-delimited JSON)
jsonl_path = 'example.jsonl'
lines = [{"id": 1, "text": "First line"}, {"id": 2, "text": "Second line"}]
# Write JSONL to a file
srsly.write_jsonl(jsonl_path, lines)
print(f"Data written to {jsonl_path}")
# Read JSONL from a file
read_lines = list(srsly.read_jsonl(jsonl_path))
print(f"Data read from {jsonl_path}: {read_lines}")
# Clean up
os.remove(jsonl_path)