mixle.utils.serialization module

Safe JSON serialization helpers for mixle distribution and estimator objects.

The legacy load_models(eval_string) path reconstructed models by executing their repr strings. This module instead serializes distribution state as a small tagged JSON value graph and reconstructs only classes registered from mixle’s own distribution modules.

exception SerializationError[source]

Bases: ValueError

Raised when an object cannot be serialized or decoded safely.

register_serializable_class(cls, type_id=None)[source]

Register a class that may be reconstructed from serialized state.

Deserialization never imports a class named in the payload. The class must already be present in this registry, which is populated from mixle package modules by ensure_pysp_serialization_registry.

Parameters:
Return type:

type[Any]

register_serializable_callable(fn, callable_id=None)[source]

Register a callable that may appear inside a serialized distribution.

This is intentionally explicit. Arbitrary lambdas or local functions cannot be made safe by JSON alone; callers that need SelectDistribution-like routing should register a stable process-local callable id.

Parameters:
Return type:

Callable[[…], Any]

serializable_class_ids()[source]

Return the registered class ids, primarily for diagnostics/tests.

Return type:

set[str]

ensure_pysp_serialization_registry()[source]

Populate the closed registry of mixle classes that can be decoded.

Return type:

None

to_serializable(value)[source]

Convert a mixle model/value to a JSON-compatible tagged value.

Parameters:

value (Any)

Return type:

Any

from_serializable(payload)[source]

Decode a value produced by to_serializable.

Parameters:

payload (Any)

Return type:

Any

to_json(value, **kwargs)[source]

Serialize a mixle model/value to strict JSON.

Parameters:
Return type:

str

from_json(text)[source]

Deserialize a value produced by to_json.

Parameters:

text (str)

Return type:

Any