Skip to content

crv.core.serde

Experimental API

crv.core.serde

Lightweight JSON serialization/deserialization utilities.

Provides json_loads as a thin wrapper around the stdlib json module and re-exports json_dumps_canonical from crv.core.hashing to ensure a single canonical JSON policy across the codebase. This module is zero-IO.

Notes
  • Use json_dumps_canonical for deterministic JSON strings prior to hashing, caching, or persistence.
  • No side effects; stdlib-only.
References
  • specs: src/crv/core/.specs/spec-0.1.md, spec-0.2.md

crv.core.serde.json_dumps_canonical

json_dumps_canonical(obj: typing.Any) -> str

Serialize an object to a canonical JSON string.

Parameters:

Name Type Description Default
obj typing.Any

JSON-serializable object.

required

Returns:

Name Type Description
str str

Canonical JSON string with sort_keys=True, compact separators,

str

and ensure_ascii=False.

Notes

This function assumes the input is JSON-serializable and does not perform coercion of unsupported types.

Source code in src/crv/core/hashing.py
def json_dumps_canonical(obj: Any) -> str:
    """
    Serialize an object to a canonical JSON string.

    Args:
        obj (Any): JSON-serializable object.

    Returns:
        str: Canonical JSON string with sort_keys=True, compact separators,
        and ensure_ascii=False.

    Notes:
        This function assumes the input is JSON-serializable and does not perform
        coercion of unsupported types.
    """
    return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=False)

crv.core.serde.json_loads

json_loads(s: str) -> Any

Deserialize a JSON string to Python objects using the stdlib json module.

Parameters:

Name Type Description Default
s str

JSON string to parse.

required

Returns:

Name Type Description
Any typing.Any

Decoded Python object (dict, list, str, int, float, bool, or None).

Notes
  • No datetime parsing or custom hooks here; the core remains pure.
  • For canonical serialization (for hashing), use json_dumps_canonical.
Source code in src/crv/core/serde.py
def json_loads(s: str) -> Any:
    """
    Deserialize a JSON string to Python objects using the stdlib json module.

    Args:
        s (str): JSON string to parse.

    Returns:
        Any: Decoded Python object (dict, list, str, int, float, bool, or None).

    Notes:
        - No datetime parsing or custom hooks here; the core remains pure.
        - For canonical serialization (for hashing), use `json_dumps_canonical`.
    """
    return json.loads(s)