nautilus_core/python/
serialization.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use pyo3::{prelude::*, types::PyDict};
17use serde::{Serialize, de::DeserializeOwned};
18
19use crate::python::to_pyvalue_err;
20
21/// Convert a Python dictionary to a Rust type that implements `DeserializeOwned`.
22///
23/// # Errors
24///
25/// Returns an error if:
26/// - The Python dictionary cannot be serialized to JSON.
27/// - The JSON string cannot be deserialized to type `T`.
28/// - The Python `json` module fails to import or execute.
29pub fn from_dict_pyo3<T>(py: Python<'_>, values: Py<PyDict>) -> Result<T, PyErr>
30where
31    T: DeserializeOwned,
32{
33    // Extract to JSON bytes
34    use crate::python::to_pyvalue_err;
35    let json_str: String = PyModule::import(py, "json")?
36        .call_method("dumps", (values,), None)?
37        .extract()?;
38
39    // Deserialize to object
40    let instance = serde_json::from_str(&json_str).map_err(to_pyvalue_err)?;
41    Ok(instance)
42}
43
44/// Convert a Rust type that implements `Serialize` to a Python dictionary.
45///
46/// # Errors
47///
48/// Returns an error if:
49/// - The Rust value cannot be serialized to JSON.
50/// - The JSON string cannot be parsed into a Python dictionary.
51/// - The Python `json` module fails to import or execute.
52pub fn to_dict_pyo3<T>(py: Python<'_>, value: &T) -> PyResult<Py<PyDict>>
53where
54    T: Serialize,
55{
56    let json_str = serde_json::to_string(value).map_err(to_pyvalue_err)?;
57
58    // Parse JSON into a Python dictionary
59    let py_dict: Py<PyDict> = PyModule::import(py, "json")?
60        .call_method("loads", (json_str,), None)?
61        .extract()?;
62    Ok(py_dict)
63}