# 9. Serialization and NumPy
**Outcome:** save and recover PiP values through validated Serde, then inspect
supported array documents from Python. File ownership remains in the application.
## Rust serialization
Add `serde_json = "1"` to the application's dependencies. This complete program
round-trips a matrix without any PiP-specific file API:
```rust
use physics_in_parallel::math::{Backend, Matrix};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let matrix = Matrix::from_values(2, 2, Backend::Dense, vec![1_i64, 2, 3, 4])?;
let file = std::fs::File::create("matrix.json")?;
serde_json::to_writer_pretty(file, &matrix)?;
let restored: Matrix<i64> = serde_json::from_reader(std::fs::File::open("matrix.json")?)?;
assert_eq!(restored.backend(), Backend::Dense);
assert_eq!(restored.get(1, 1)?, 4);
Ok(())
}
```
Direct validated Serde is the conversion contract. PiP does not supply JSON-string
helpers, output-directory policy, checkpoint schedules, or a second payload API.
Use atomic publication and scientific provenance appropriate to your application;
the example above demonstrates conversion only.
Tensor-family documents use schema version 2. Package versions and wire-schema
versions are independent. Do not rename a schema version to match `4.2.0-alpha`,
or assume old PiP 3.x documents can be decoded by current types.
## Read supported documents with NumPy
The repository contains `python/numpy_support.py`; it is a small external helper,
not a separately installed PiP Python simulation package. In an environment with
NumPy installed, run from the repository root:
```python
from python.numpy_support import to_ndarray
array = to_ndarray("/path/to/matrix.json", max_elements=1000)
assert array.shape == (2, 2)
assert array[1, 1] == 4
```
Replace the path with the matrix file created above. `payload_to_ndarray` accepts
an already-decoded document. Both routes check the allowed dense result size
before NumPy allocation. This is not a cap on JSON parsing or Python-object memory.
Dense and sparse tensors, matrices, vector lists, and square lattices are
supported. Sparse documents become dense arrays at this boundary. Large integer
values can need object arrays; lattice scalar widths may be inferred rather than
recovered. See the [NumPy reference](../NUMPY.md) for exact dtype and validation
limits. Do not claim zero-copy conversion or complete parity with every Serde type.
## Verify cross-language examples
From a repository checkout:
```sh
cargo run --release --example numpy_fixtures > /tmp/pip-fixtures.json
python3 -m unittest python/test_numpy_support.py
```
The test suite uses checked-in Rust-produced fixtures. To compare a freshly
produced document, explicitly pass it through the helper; writing a temporary
fixture does not replace the checked-in test input. Regenerate tracked fixtures
only for an intentional format/fixture update.
When PiP state is recorded through a workflow framework, use that framework's
verified readers/converter for the recording envelope. This standalone helper
reads PiP JSON documents, not Workflow recording directories or NPY manifests.
---
**Previous:** [8. Forces, integration, and thermostats](8-forces-integration-and-thermostats.md) ยท **Guide index:** [Documentation](../README.md)
**Next:** [10. Parallelism and performance](10-parallelism-and-performance.md)