-
Notifications
You must be signed in to change notification settings - Fork 795
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement Serialize, Deserialize for Py<T>
- Loading branch information
1 parent
c8172e9
commit 1719ba3
Showing
4 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use crate::type_object::PyBorrowFlagLayout; | ||
use crate::{Py, PyClass, PyClassInitializer, PyTypeInfo, Python}; | ||
use serde::{de, ser, Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
impl<T> Serialize for Py<T> | ||
where | ||
T: Serialize + PyClass, | ||
{ | ||
fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
Python::with_gil(|py| { | ||
self.try_borrow(py) | ||
.map_err(|e| ser::Error::custom(e.to_string()))? | ||
.serialize(serializer) | ||
}) | ||
} | ||
} | ||
|
||
impl<'de, T> Deserialize<'de> for Py<T> | ||
where | ||
T: Into<PyClassInitializer<T>> + PyClass + Deserialize<'de>, | ||
<T as PyTypeInfo>::BaseLayout: PyBorrowFlagLayout<<T as PyTypeInfo>::BaseType>, | ||
{ | ||
fn deserialize<D>(deserializer: D) -> Result<Py<T>, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let deserialized = T::deserialize(deserializer)?; | ||
|
||
Python::with_gil(|py| { | ||
Py::new(py, deserialized).map_err(|e| de::Error::custom(e.to_string())) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#[cfg(feature = "serde")] | ||
mod test_serde { | ||
use pyo3::prelude::*; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
use serde_json; | ||
|
||
#[pyclass] | ||
#[derive(Debug, Serialize, Deserialize)] | ||
struct Group { | ||
name: String, | ||
} | ||
|
||
#[pyclass] | ||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
struct User { | ||
username: String, | ||
group: Option<Py<Group>>, | ||
friends: Vec<Py<User>>, | ||
} | ||
|
||
#[test] | ||
fn test_serialize() { | ||
let friend1 = User { | ||
username: "friend 1".into(), | ||
group: None, | ||
friends: vec![], | ||
}; | ||
let friend2 = User { | ||
username: "friend 2".into(), | ||
..friend1.clone() | ||
}; | ||
|
||
let user = Python::with_gil(|py| { | ||
let py_friend1 = Py::new(py, friend1).expect("failed to create friend 1"); | ||
let py_friend2 = Py::new(py, friend2).expect("failed to create friend 2"); | ||
|
||
let friends = vec![py_friend1, py_friend2]; | ||
let py_group = Py::new( | ||
py, | ||
Group { | ||
name: "group name".into(), | ||
}, | ||
) | ||
.unwrap(); | ||
|
||
User { | ||
username: "danya".into(), | ||
group: Some(py_group), | ||
friends, | ||
} | ||
}); | ||
|
||
let serialized = serde_json::to_string(&user).expect("failed to serialize"); | ||
assert_eq!( | ||
serialized, | ||
r#"{"username":"danya","group":{"name":"group name"},"friends":[{"username":"friend 1","group":null,"friends":[]},{"username":"friend 2","group":null,"friends":[]}]}"# | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_deserialize() { | ||
let serialized = r#"{"username": "danya", "friends": | ||
[{"username": "friend", "group": {"name": "danya's friends"}, "friends": []}]}"#; | ||
let user: User = serde_json::from_str(serialized).expect("failed to deserialize"); | ||
|
||
assert_eq!(user.username, "danya"); | ||
assert_eq!(user.group, None); | ||
assert_eq!(user.friends.len(), 1usize); | ||
let friend = user.friends.get(0).unwrap(); | ||
|
||
Python::with_gil(|py| { | ||
assert_eq!(friend.borrow(py).username, "friend"); | ||
assert_eq!( | ||
friend.borrow(py).group.as_ref().unwrap().borrow(py).name, | ||
"danya's friends" | ||
) | ||
}); | ||
} | ||
} |