Skip to main content

nautilus_core/python/
serialization.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 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
16//! (De)serialization utilities bridging Rust ↔︎ Python types.
17
18use pyo3::{prelude::*, types::PyDict};
19use serde::{Serialize, de::DeserializeOwned};
20
21use crate::python::to_pyvalue_err;
22
23/// Convert a Python dictionary to a Rust type that implements `DeserializeOwned`.
24///
25/// # Errors
26///
27/// Returns an error if:
28/// - The Python dictionary cannot be serialized to JSON.
29/// - The JSON string cannot be deserialized to type `T`.
30/// - The Python `json` module fails to import or execute.
31pub fn from_dict_pyo3<T>(py: Python<'_>, values: Py<PyDict>) -> Result<T, PyErr>
32where
33    T: DeserializeOwned,
34{
35    // `ensure_ascii=False` keeps non-ASCII characters as raw UTF-8 in the JSON output.
36    // Without this, `\uXXXX` escapes force `serde_json` onto the owned-string path,
37    // which `visit_str`-only visitors like `ustr::Ustr` reject as "expected a borrowed string".
38    let kwargs = PyDict::new(py);
39    kwargs.set_item("ensure_ascii", false)?;
40    let json_str: String = PyModule::import(py, "json")?
41        .call_method("dumps", (values,), Some(&kwargs))?
42        .extract()?;
43
44    // Deserialize to object
45    let instance = serde_json::from_str(&json_str).map_err(to_pyvalue_err)?;
46    Ok(instance)
47}
48
49/// Convert a Rust type that implements `Serialize` to a Python dictionary.
50///
51/// # Errors
52///
53/// Returns an error if:
54/// - The Rust value cannot be serialized to JSON.
55/// - The JSON string cannot be parsed into a Python dictionary.
56/// - The Python `json` module fails to import or execute.
57pub fn to_dict_pyo3<T>(py: Python<'_>, value: &T) -> PyResult<Py<PyDict>>
58where
59    T: Serialize,
60{
61    let json_str = serde_json::to_string(value).map_err(to_pyvalue_err)?;
62
63    // Parse JSON into a Python dictionary
64    let py_dict: Py<PyDict> = PyModule::import(py, "json")?
65        .call_method("loads", (json_str,), None)?
66        .extract()?;
67    Ok(py_dict)
68}