Function serde_pyobject::from_pyobject

source ·
pub fn from_pyobject<'py, 'de, T: Deserialize<'de>, Any>(
    any: Bound<'py, Any>
) -> Result<T, Error>
Expand description

Deserialize a Python object into Rust type T: Deserialize.

§Examples

§primitive

use pyo3::{Python, Py, PyAny, IntoPy};
use serde_pyobject::from_pyobject;

Python::with_gil(|py| {
    // integer
    let any: Py<PyAny> = 42.into_py(py);
    let i: i32 = from_pyobject(any.into_bound(py)).unwrap();
    assert_eq!(i, 42);

    // float
    let any: Py<PyAny> = (0.1).into_py(py);
    let x: f32 = from_pyobject(any.into_bound(py)).unwrap();
    assert_eq!(x, 0.1);

    // bool
    let any: Py<PyAny> = true.into_py(py);
    let x: bool = from_pyobject(any.into_bound(py)).unwrap();
    assert_eq!(x, true);
});

§option

use pyo3::{Python, Py, PyAny, IntoPy};
use serde_pyobject::from_pyobject;

Python::with_gil(|py| {
    let none = py.None();
    let option: Option<i32> = from_pyobject(none.into_bound(py)).unwrap();
    assert_eq!(option, None);

    let py_int: Py<PyAny> = 42.into_py(py);
    let i: Option<i32> = from_pyobject(py_int.into_bound(py)).unwrap();
    assert_eq!(i, Some(42));
})

§unit

use pyo3::{Python, types::PyTuple};
use serde_pyobject::from_pyobject;

Python::with_gil(|py| {
    let py_unit = PyTuple::empty_bound(py);
    let unit: () = from_pyobject(py_unit).unwrap();
    assert_eq!(unit, ());
})

§unit_struct

use serde::Deserialize;
use pyo3::{Python, types::PyTuple};
use serde_pyobject::from_pyobject;

#[derive(Debug, PartialEq, Deserialize)]
struct UnitStruct;

Python::with_gil(|py| {
    let py_unit = PyTuple::empty_bound(py);
    let unit: UnitStruct = from_pyobject(py_unit).unwrap();
    assert_eq!(unit, UnitStruct);
})

§unit variant

use serde::Deserialize;
use pyo3::{Python, types::PyString};
use serde_pyobject::from_pyobject;

#[derive(Debug, PartialEq, Deserialize)]
enum E {
    A,
    B,
}

Python::with_gil(|py| {
    let any = PyString::new_bound(py, "A");
    let out: E = from_pyobject(any).unwrap();
    assert_eq!(out, E::A);
})

§newtype struct

use serde::Deserialize;
use pyo3::{Python, Bound, PyAny, IntoPy};
use serde_pyobject::from_pyobject;

#[derive(Debug, PartialEq, Deserialize)]
struct NewTypeStruct(u8);

Python::with_gil(|py| {
    let any: Bound<PyAny> = 1_u32.into_py(py).into_bound(py);
    let obj: NewTypeStruct = from_pyobject(any).unwrap();
    assert_eq!(obj, NewTypeStruct(1));
});

§newtype variant

use serde::Deserialize;
use pyo3::Python;
use serde_pyobject::{from_pyobject, pydict};

#[derive(Debug, PartialEq, Deserialize)]
enum NewTypeVariant {
    N(u8),
}

Python::with_gil(|py| {
    let dict = pydict! { py, "N" => 41 }.unwrap();
    let obj: NewTypeVariant = from_pyobject(dict).unwrap();
    assert_eq!(obj, NewTypeVariant::N(41));
});

§seq

use pyo3::Python;
use serde_pyobject::{from_pyobject, pylist};

Python::with_gil(|py| {
    let list = pylist![py; 1, 2, 3].unwrap();
    let seq: Vec<i32> = from_pyobject(list).unwrap();
    assert_eq!(seq, vec![1, 2, 3]);
});

§tuple

use pyo3::{Python, types::PyTuple};
use serde_pyobject::from_pyobject;

Python::with_gil(|py| {
    let tuple = PyTuple::new_bound(py, &[1, 2, 3]);
    let tuple: (i32, i32, i32) = from_pyobject(tuple).unwrap();
    assert_eq!(tuple, (1, 2, 3));
});

§tuple struct

use serde::Deserialize;
use pyo3::{Python, IntoPy, types::PyTuple};
use serde_pyobject::from_pyobject;

#[derive(Debug, PartialEq, Deserialize)]
struct T(u8, String);

Python::with_gil(|py| {
    let tuple = PyTuple::new_bound(py, &[1_u32.into_py(py), "test".into_py(py)]);
    let obj: T = from_pyobject(tuple).unwrap();
    assert_eq!(obj, T(1, "test".to_string()));
});

§tuple variant

use serde::Deserialize;
use pyo3::Python;
use serde_pyobject::{from_pyobject, pydict};

#[derive(Debug, PartialEq, Deserialize)]
enum TupleVariant {
    T(u8, u8),
}

Python::with_gil(|py| {
    let dict = pydict! { py, "T" => (1, 2) }.unwrap();
    let obj: TupleVariant = from_pyobject(dict).unwrap();
    assert_eq!(obj, TupleVariant::T(1, 2));
});

§map

use pyo3::Python;
use serde_pyobject::{from_pyobject, pydict};
use std::collections::BTreeMap;

Python::with_gil(|py| {
    let dict = pydict! { py,
        "a" => "hom",
        "b" => "test"
    }
    .unwrap();
    let map: BTreeMap<String, String> = from_pyobject(dict).unwrap();
    assert_eq!(map.get("a"), Some(&"hom".to_string()));
    assert_eq!(map.get("b"), Some(&"test".to_string()));
});

§struct

use serde::Deserialize;
use pyo3::Python;
use serde_pyobject::{from_pyobject, pydict};

#[derive(Debug, PartialEq, Deserialize)]
struct A {
    a: i32,
    b: String,
}

Python::with_gil(|py| {
    let dict = pydict! {
        "a" => 1,
        "b" => "test"
    }
    .unwrap();
    let a: A = from_pyobject(dict.into_bound(py)).unwrap();
    assert_eq!(
        a,
        A {
            a: 1,
            b: "test".to_string()
        }
    );
});

Python::with_gil(|py| {
    let dict = pydict! {
        "A" => pydict! {
            "a" => 1,
            "b" => "test"
        }
        .unwrap()
    }
    .unwrap();
    let a: A = from_pyobject(dict.into_bound(py)).unwrap();
    assert_eq!(
        a,
        A {
            a: 1,
            b: "test".to_string()
        }
    );
});

§struct variant

use serde::Deserialize;
use pyo3::Python;
use serde_pyobject::{from_pyobject, pydict};

#[derive(Debug, PartialEq, Deserialize)]
enum StructVariant {
    S { r: u8, g: u8, b: u8 },
}

Python::with_gil(|py| {
    let dict = pydict! {
        py,
        "S" => pydict! {
            "r" => 1,
            "g" => 2,
            "b" => 3
        }.unwrap()
    }
    .unwrap();
    let obj: StructVariant = from_pyobject(dict).unwrap();
    assert_eq!(obj, StructVariant::S { r: 1, g: 2, b: 3 });
});