Srsly: High-Performance Python Serialization Utilities

2.5.3 · active · verified Sun Apr 05

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

Install

Imports

Quickstart

This quickstart demonstrates how to use `srsly` to write and read data in JSON and JSONL formats. It covers basic file operations using `srsly.write_json`, `srsly.read_json`, `srsly.write_jsonl`, and `srsly.read_jsonl`.

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)

view raw JSON →