9 releases (breaking)

0.18.0 Jan 22, 2023
0.17.0 Aug 24, 2022
0.16.0 Mar 6, 2022
0.15.0 Nov 12, 2021
0.12.0 Nov 22, 2020

#100 in Parser implementations

Download history 4708/week @ 2022-12-07 4473/week @ 2022-12-14 3233/week @ 2022-12-21 4061/week @ 2022-12-28 5989/week @ 2023-01-04 9376/week @ 2023-01-11 6384/week @ 2023-01-18 6661/week @ 2023-01-25 7186/week @ 2023-02-01 7171/week @ 2023-02-08 7371/week @ 2023-02-15 7323/week @ 2023-02-22 6902/week @ 2023-03-01 7147/week @ 2023-03-08 7116/week @ 2023-03-15 6543/week @ 2023-03-22

29,213 downloads per month
Used in 11 crates (9 directly)

MIT license

45KB
1.5K SLoC

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 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>
}

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();

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);

Dependencies

~1.9–6MB
~97K SLoC