Expand description
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
.
§Examples
use serde::{Serialize, Deserialize};
use pyo3::Python;
use pythonize::{depythonize, pythonize};
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Sample {
foo: String,
bar: Option<usize>
}
Python::with_gil(|py| {
let sample = Sample {
foo: "Foo".to_string(),
bar: None
};
// Rust -> Python
let obj = pythonize(py, &sample).unwrap();
assert_eq!("{'foo': 'Foo', 'bar': None}", &format!("{}", obj.as_ref(py).repr().unwrap()));
// Python -> Rust
let new_sample: Sample = depythonize(obj.as_ref(py)).unwrap();
assert_eq!(new_sample, sample);
});
Structs§
- A structure that deserializes Python objects into Rust values
- Errors that can occur when serializing/deserializing Python objects
- A structure that serializes Rust values into Python objects
Traits§
- Trait for types which can represent a Python mapping
- Trait for types which can represent a Python sequence
- Custom types for serialization
Functions§
- depythonizeDeprecatedAttempt to convert a Python object to an instance of
T
- Attempt to convert a Python object to an instance of
T
- Attempt to convert the given data into a Python object
- Attempt to convert the given data into a Python object. Also uses custom mapping python class for serialization.
Type Aliases§
- Alias for
std::result::Result
with error typePythonizeError