9 breaking releases

0.20.0 Oct 15, 2023
0.19.0 Jun 11, 2023
0.18.0 Jan 22, 2023
0.17.0 Aug 24, 2022
0.12.0 Nov 22, 2020

#55 in FFI

Download history 11721/week @ 2023-11-20 16564/week @ 2023-11-27 23727/week @ 2023-12-04 12389/week @ 2023-12-11 11626/week @ 2023-12-18 6623/week @ 2023-12-25 8994/week @ 2024-01-01 11086/week @ 2024-01-08 10152/week @ 2024-01-15 12116/week @ 2024-01-22 10877/week @ 2024-01-29 11816/week @ 2024-02-05 10936/week @ 2024-02-12 11300/week @ 2024-02-19 12234/week @ 2024-02-26 10725/week @ 2024-03-04

45,845 downloads per month
Used in 24 crates (20 directly)

MIT license

46KB
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

~3–10MB
~73K SLoC