Skip to main content

cells-json

English | 简体中文 | 日本語

JSON serialization utilities for Python applications, published as cells-json and imported as cells.json.

The library combines an orjson-first adapter with a configurable serializer for dates, decimals, identifiers, scientific data, and custom objects. It provides familiar dump, dumps, load, and loads entry points, plus recursive conversion when circular references need explicit handling.

Features

  • An orjson-first adapter with an explicitly selectable standard-library backend.
  • Extended type conversion with cached type handlers.
  • Recursive serialization with circular-reference markers or exceptions.
  • Configurable handling of unknown types and custom objects.
  • Shared encoding and decoding exceptions for the main APIs.
  • File helpers, formatting utilities, and class serialization helpers.

Installation

python -m pip install cells-json

orjson is a required package dependency. NumPy and pandas support is enabled when those packages are installed separately.

Use Python 3.14 for the current source tree: it uses exception syntax introduced in Python 3.14. Package metadata currently declares Python >=3.9; that declaration does not reflect the syntax requirement of the current source. The CI test job is configured for Python 3.14 on Linux and Windows.

Quick start

from datetime import datetime
from decimal import Decimal

from cells import json

payload = json.dumps(
    {"created_at": datetime(2026, 1, 1, 12, 0), "amount": Decimal("19.95")},
    ensure_ascii=False,
    indent=2,
)
data = json.loads(payload)

assert data["created_at"] == "2026-01-01T12:00:00"
assert data["amount"] == 19.95

dumps() returns str by default. With the orjson backend, ensure_str=False returns bytes; the standard-library backend returns str. loads() accepts str, bytes, or bytearray and returns ordinary Python values.

Choosing a serialization API

API Purpose Encoding path
dumps() / dump() Serialize with a familiar standard-library-style interface Prefers orjson; select backend="json" explicitly
loads() / load() Parse JSON text or a readable stream Same backend selection as the adapter
safe_json_dumps() Recursively convert an object tree and handle circular references Standard-library json after conversion
UniversalSerializer Reuse type conversion rules or provide a default callback Its dumps() and dump() use standard-library json
JSONAdapter Reuse an adapter with a fixed backend and serializer configuration Configurable adapter

For recursive conversion:

from datetime import date
from cells.json import safe_json_dumps

payload = safe_json_dumps(
    {"date": date(2026, 1, 1), "tags": {"python", "json"}},
    ensure_ascii=False,
    indent=2,
)

safe_json_dumps() stringifies dictionary keys and converts tuples and sets to lists before encoding. Set element order is not guaranteed. It does not preserve the original Python types when the JSON is loaded again.

Backend behavior and compatibility

The adapter resembles standard-library json, but does not promise identical behavior for every option or value.

  • backend="auto" prefers orjson when available and otherwise uses standard-library json.
  • backend="orjson" requires orjson; constructing the adapter without it raises RuntimeError.
  • backend="json" explicitly selects standard-library encoding and decoding while retaining the library's extended type callback.

Unsupported options do not trigger an automatic backend switch. Select backend="json" when standard-library parameter semantics are required.

Option Active orjson backend
ensure_ascii=True Supported by escaping non-ASCII characters after encoding; this is the default
sort_keys=True Supported
indent=2 Supported; other non-None indentation values require backend="json"
separators=(",", ":") Supported; other explicit separators require backend="json"
skipkeys=True Filters unsupported dictionary keys before encoding
check_circular=False Accepted; does not guarantee standard-library circular-reference semantics
allow_nan=False Requires backend="json"
Custom encoder/decoder cls Requires backend="json"
object_hook, object_pairs_hook, parse_float, parse_int, parse_constant Require backend="json"

For example, use the standard-library backend to preserve decimal values while parsing:

from decimal import Decimal
from cells.json import dumps, loads

payload = dumps({"b": 1, "a": 2}, sort_keys=True, indent=2)
data = loads('{"amount": 19.95}', parse_float=Decimal, backend="json")

assert data["amount"] == Decimal("19.95")

Backend-native behavior can also differ for non-finite numbers, integer ranges, and dictionary keys. Choose the backend explicitly when these details matter to your data contract.

Supported type conversions

These conversions describe UniversalSerializer and the recursive API. The adapter may use orjson's native support for types that backend already handles.

Python type JSON representation
datetime, date, time ISO-formatted string
timedelta Total seconds as a number
Decimal Floating-point number
UUID String
Enum Its value, serialized in turn
Path POSIX-style path string
set, tuple Array
NumPy ndarray / scalar Nested lists / native Python value
pandas DataFrame Array of row records
Named pandas Series Object mapping its stringified name to a value list
Unnamed pandas Series Index-to-value mapping
Custom object to_dict(), then __dict__, then __slots__, subject to configuration

Decimal conversion to float can lose precision. For exact decimal output, provide an explicit conversion such as default=str to dumps(), or convert values to strings before serialization.

Custom objects and unknown types

from cells.json import UniversalSerializer


class User:
    def __init__(self, name):
        self.name = name

    def to_dict(self):
        return {"name": self.name}


serializer = UniversalSerializer()
payload = serializer.dumps(User("Alice"))
  • strict=True disables automatic custom-object inspection, including to_dict(). Built-in supported conversions remain available.
  • ignore_unknown=True converts otherwise unhandled values to null and disables attribute inspection by default. A recognized to_dict() method can still be used unless strict mode is enabled.
  • use_dict=False disables __dict__ and __slots__ inspection while leaving to_dict() available outside strict mode.
  • UniversalSerializer(default=callback) supplies a fallback for unhandled types.

Unknown types raise JSONEncodeError when no fallback applies. Automatic attribute inspection can include internal attributes; define to_dict() or disable inspection when you need explicit field selection.

Circular references

Use safe_json_dumps() for recursive circular-reference handling. The default behavior replaces a cycle with a string such as "<CircularReference dict>"; fail_on_circular=True raises CircularReferenceError.

from cells.json import CircularReferenceError, safe_json_dumps

node = {}
node["self"] = node

payload = safe_json_dumps(node)
assert "<CircularReference dict>" in payload

try:
    safe_json_dumps(node, fail_on_circular=True)
except CircularReferenceError:
    pass

UniversalSerializer.dumps() does not recursively preconvert by default. Use recursive=True for that behavior. The adapter APIs do not provide the same recursive marker handling.

safe_json_dumps(ignore_errors=True, default_value="null") returns the fallback string for serialization exceptions handled by that helper. It does not suppress every possible exception.

Files and utilities

from pathlib import Path
from cells.json import dump, load
from cells.json.utils import load_json, save_json

path = Path("settings.json")

with path.open("w", encoding="utf-8") as stream:
    dump({"enabled": True}, stream, indent=2)

with path.open("r", encoding="utf-8") as stream:
    data = load(stream)

save_json({"enabled": True}, "output/settings.json", indent=2)
data = load_json("output/settings.json")

The main dump() / load() APIs operate on open streams. UTF-8 text streams work with both backends. save_json() creates parent directories and writes UTF-8 using UniversalSerializer; load_json() reads UTF-8 using standard-library json.

Additional helpers:

  • prettify_json(data, indent=2): serializes a Python value with indentation through safe_json_dumps(); also exported from cells.json.
  • JsonSerializable: base class providing to_dict(), to_json(), and from_dict().
  • @json_serializable: decorator adding the same methods to a class.

Import the class helpers from cells.json.utils. Their from_dict() methods call cls(**data); they do not automatically restore dates, decimals, or other original types.

Error handling

from cells.json import JSONDecodeError, JSONEncodeError, dumps, loads

try:
    dumps(object(), strict=True)
except JSONEncodeError as exc:
    print(exc.obj_type, str(exc))

try:
    loads("{invalid}")
except JSONDecodeError as exc:
    print(exc.pos, exc.msg)

JSONEncodeError inherits from TypeError; CircularReferenceError is a subclass of JSONEncodeError. JSONDecodeError inherits from ValueError and exposes msg, doc, and pos. It is not the standard library's json.JSONDecodeError class.

Unsupported encoding options on the active orjson path are reported as JSONEncodeError, which is also a TypeError. Unsupported decoding options raise TypeError. These messages direct callers to backend="json".

Development and performance

From a local checkout, using Python 3.14:

python -m pip install -e .
python -m pip install pytest
python -m pytest tests/test_serializer.py -v

Tests for NumPy and pandas return early when those optional packages are absent. Install them separately to exercise those conversions.

Run the included benchmark with:

python tests/test_performance.py

Use python tests/test_performance.py --markdown benchmark.md to write a report. The benchmark compares standard-library json with the two cells-json adapter modes, using equivalent conversions and string output, and reports the median of five repeats.

Pushes to v* tags publish a GitHub Release with the build archive and benchmark results in its notes. Pushes to main update the rolling prerelease and its tag to the built commit. GitHub releases created with GITHUB_TOKEN do not trigger the separate release-event PyPI workflow.

Performance depends on the payload, backend, conversion path, options, and environment. The benchmark is a local comparison tool; no fixed speedup is guaranteed.

License

The package metadata declares the Mozilla Public License 2.0 (MPL-2.0).

Metadata

Release files for cells-json 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cells-json 0.3.3
File Size Uploaded Provenance Actions
cells_json-0.3.3.tar.gz 26.8 kB Without provenance Details for cells_json-0.3.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cells-json 0.3.3
File Interpreter ABI Platform Actions
cells_json-0.3.3-py3-none-any.whl Python 3 none any Details for cells_json-0.3.3-py3-none-any.whl

Total release size: 44.9 kB

Release files / cells_json-0.3.3.tar.gz

Download URL cells_json-0.3.3.tar.gz
Size 26.8 kB
Tags Source
SHA-256 checksum
How to use checksums
4e086f64780c7125c4527c5b59c3a9584c302ce2c21fe53e166cc677dcfd3d64
BLAKE2b-256 checksum
How to use checksums
130bcd250155f38d3c2e61a87a1b6c2ef5a0dcd63c4925616fc99fb944be9fec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / cells_json-0.3.3-py3-none-any.whl

Download URL cells_json-0.3.3-py3-none-any.whl
Size 18.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
486e81b4d38ab71f8fb39d1849ce7466b0d186cf6bfee2fd8c88c4317fd74439
BLAKE2b-256 checksum
How to use checksums
378834e7cd30b35f27468ca0f08dd7326163f30c7201b1ede805d9ccd3f5e2ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for cells-json 0.3.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page