Crate pythonize

Source
Expand description

§Pythonize

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.

§Usage

This crate converts Rust types which implement the Serde serialization traits into Python objects using the PyO3 library.

Pythonize has two main public APIs: pythonize and depythonize.

§Examples

use serde::{Serialize, Deserialize};
use pyo3::prelude::*;
use pythonize::{depythonize, pythonize};

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Sample {
    foo: String,
    bar: Option<usize>
}

let sample = Sample {
    foo: "Foo".to_string(),
    bar: None
};

Python::with_gil(|py| {
    // Rust -> Python
    let obj =  pythonize(py, &sample).unwrap();

    assert_eq!("{'foo': 'Foo', 'bar': None}", &format!("{}", obj.repr().unwrap()));

    // Python -> Rust
    let new_sample: Sample = depythonize(&obj).unwrap();

    assert_eq!(new_sample, sample);
})

Structs§

Depythonizer
A structure that deserializes Python objects into Rust values
PythonizeDefault
PythonizeError
Errors that can occur when serializing/deserializing Python objects
PythonizeUnnamedMappingAdapter
Adapter type to use an unnamed mapping type, i.e. one that implements PythonizeMappingType, as a named mapping type, i.e. one that implements PythonizeNamedMappingType. The adapter simply drops the provided name.
Pythonizer
A structure that serializes Rust values into Python objects

Traits§

PythonizeListType
Trait for types which can represent a Python sequence
PythonizeMappingType
Trait for types which can represent a Python mapping
PythonizeNamedMappingType
Trait for types which can represent a Python mapping and have a name
PythonizeTypes
Custom types for serialization

Functions§

depythonize
Attempt to convert a Python object to an instance of T
pythonize
Attempt to convert the given data into a Python object
pythonize_custom
Attempt to convert the given data into a Python object. Also uses custom mapping python class for serialization.

Type Aliases§

Result
Alias for std::result::Result with error type PythonizeError