12 breaking releases

new 0.23.0 Nov 22, 2024
0.21.1 Apr 2, 2024
0.20.0 Oct 15, 2023
0.19.0 Jun 11, 2023
0.12.0 Nov 22, 2020

#67 in FFI

Download history 12624/week @ 2024-08-02 16303/week @ 2024-08-09 17218/week @ 2024-08-16 20187/week @ 2024-08-23 17511/week @ 2024-08-30 23074/week @ 2024-09-06 15766/week @ 2024-09-13 19782/week @ 2024-09-20 18722/week @ 2024-09-27 21659/week @ 2024-10-04 19482/week @ 2024-10-11 19624/week @ 2024-10-18 20968/week @ 2024-10-25 22062/week @ 2024-11-01 23580/week @ 2024-11-08 22592/week @ 2024-11-15

92,713 downloads per month
Used in 28 crates (24 directly)

MIT license

61KB
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 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);
})

Dependencies

~2.5MB
~55K SLoC