ejkernel.ops.utils.serialize#
Simple JSON serialization utilities for configuration persistence.
This module provides basic JSON serialization functions for converting Python objects to and from JSON strings. It includes support for dataclasses and provides a fallback to string representation for complex objects.
- Functions:
to_json: Convert Python objects to JSON strings from_json: Parse JSON strings back to Python objects
- These utilities are used primarily for:
Configuration serialization in persistent caches
Simple object persistence across program runs
Debug output and logging of complex objects
Note
For more comprehensive serialization needs (including JAX arrays, functions, and complex nested structures), use the fingerprint.stable_json function which provides deterministic serialization.
- Example Usage:
>>> @dataclass >>> class Config: ... lr: float = 0.01 ... batch_size: int = 32 >>> >>> config = Config(lr=0.001, batch_size=64) >>> json_str = to_json(config) >>> restored = from_json(json_str)
- ejkernel.ops.utils.serialize.from_json(s: str) Any[source]#
Parse a JSON string and return the corresponding Python object.
- Parameters
s – JSON string to parse
- Returns
Python object parsed from the JSON string
- Raises
json.JSONDecodeError – If the string is not valid JSON
Note
This is a simple wrapper around json.loads() and returns standard Python types (dict, list, str, int, float, bool, None). It does not attempt to reconstruct original object types.
- ejkernel.ops.utils.serialize.to_json(obj: Any) str[source]#
Convert a Python object to a JSON string.
Provides JSON serialization with support for dataclasses and fallback string representation for objects that aren’t directly JSON-serializable.
- Parameters
obj – Object to serialize to JSON
- Returns
JSON string representation of the object
Note
Dataclasses are converted using dataclasses.asdict()
Other non-serializable objects fall back to repr()
Output uses sorted keys and compact separators for consistency