Skip to content

Serialization

PyRapide computations, including all events, causal links, and constraint results, can be serialized to JSON for persistent storage, transfer between systems, or offline analysis.


serialize_computation

Converts a Computation object into a JSON-serializable dictionary. Every event, its causal predecessors, its data payload, and all constraint evaluation results are preserved.

serialize.py python
from pyrapide import serialize_computation
import json

data = serialize_computation(computation)

# Write to file
with open("computation.json", "w") as f:
    json.dump(data, f, indent=2)

Serialized Format

The JSON output contains three top-level keys:

computation.json python
{
  "events": [
    {
      "id": "evt_001",
      "name": "Sensor.reading",
      "timestamp": "2025-01-15T10:30:00.123Z",
      "data": {"temperature": 98.6},
      "causes": []
    },
    {
      "id": "evt_002",
      "name": "Alerter.alert",
      "timestamp": "2025-01-15T10:30:00.456Z",
      "data": {"message": "High temp", "severity": "warning"},
      "causes": ["evt_001"]
    }
  ],
  "constraints": [
    {
      "name": "reading_must_alert",
      "type": "must_match",
      "satisfied": true
    }
  ],
  "metadata": {
    "architecture": "MonitoringSystem",
    "event_count": 2,
    "causal_density": 0.5,
    "serialized_at": "2025-01-15T10:31:00.000Z"
  }
}

deserialize_computation

Reconstructs a full Computation object from serialized JSON. The restored computation supports all query functions, including critical_path, root_causes, forward_slice, and so on.

deserialize.py python
from pyrapide import deserialize_computation
import json

with open("computation.json", "r") as f:
    data = json.load(f)

computation = deserialize_computation(data)

# Full analysis is available on the restored computation
from pyrapide import critical_path, root_causes
path = critical_path(computation)
causes = root_causes(computation, computation.events[-1])
💡 Tip
Serialization is useful for building audit trails. Serialize every computation to a data store, and you have a complete, queryable causal record of everything that happened in your system.

Next Steps