This is an experimental serializer for Rust's serde ecosystem, which can convert Rust objects to Python values and back.
At the moment the Python structures it produces should be very similar to those which are produced by serde_json
; i.e. calling Python's json.loads()
on a value encoded by serde_json
should produce an identical structure to
that which is produced directly by pythonize
.
This crate converts Rust types which implement the [Serde] serialization traits into Python objects using the [PyO3] library.
Pythonize has two public APIs: pythonize
and depythonize
.
```rust use serde::{Serialize, Deserialize}; use pyo3::Python; use pythonize::{depythonize, pythonize};
struct Sample {
foo: String,
bar: Option
let gil = Python::acquire_gil(); let py = gil.python();
let sample = Sample { foo: "Foo".to_string(), bar: None };
// Rust -> Python let obj = pythonize(py, &sample).unwrap();
asserteq!("{'foo': 'Foo', 'bar': None}", &format!("{}", obj.asref(py).repr().unwrap()));
// Python -> Rust let newsample: Sample = depythonize(obj.asref(py)).unwrap();
asserteq!(newsample, sample); ```