diff options
| author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2026-11-07 15:20:36 +0100 |
|---|---|---|
| committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2026-11-07 15:20:36 +0100 |
| commit | c1b75cf60e0d116db8f43c7023fb507149091019 (patch) | |
| tree | e3becc3c45a5985066c38c86ff4b7846f75d27d8 | |
| parent | fix(yt/storage/db/insert/subs): Warn when trying to mark an sub with the same... (diff) | |
| download | yt-c1b75cf60e0d116db8f43c7023fb507149091019.zip | |
fix(yt_dlp/info_json): Allow loading and dumping any type
| -rw-r--r-- | crates/yt_dlp/src/info_json.rs | 27 | ||||
| -rw-r--r-- | crates/yt_dlp/src/lib.rs | 4 |
2 files changed, 28 insertions, 3 deletions
diff --git a/crates/yt_dlp/src/info_json.rs b/crates/yt_dlp/src/info_json.rs index df49218..c25ecab 100644 --- a/crates/yt_dlp/src/info_json.rs +++ b/crates/yt_dlp/src/info_json.rs @@ -9,15 +9,40 @@ // If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>. use pyo3::{ - Bound, Python, intern, + Bound, PyAny, Python, intern, types::{PyAnyMethods, PyDict}, }; +use serde_json::json; + +use crate::json_cast; pub type InfoJson = serde_json::Map<String, serde_json::Value>; /// # Panics /// If expectation about python operations fail. #[must_use] +pub fn json_loads_any(input: serde_json::Value, py: Python<'_>) -> Bound<'_, PyAny> { + let json = json!( {"tmp": input} ); + let json = json_cast!(json, as_object); + let dict = json_loads(json.to_owned(), py); + dict.get_item(intern!(py, "tmp")) + .expect("was just assigned") +} + +/// # Panics +/// If expectation about python operations fail. +#[must_use] +pub fn json_dumps_any(input: Bound<'_, PyAny>, py: Python<'_>) -> serde_json::Value { + let tmp_dict = PyDict::new(py); + tmp_dict.set_item("tmp", input).expect("This should never throw an exception"); + let mut out = json_dumps(&tmp_dict); + + out.remove("tmp").expect("was just assigned") +} + +/// # Panics +/// If expectation about python operations fail. +#[must_use] pub fn json_loads( input: serde_json::Map<String, serde_json::Value>, py: Python<'_>, diff --git a/crates/yt_dlp/src/lib.rs b/crates/yt_dlp/src/lib.rs index 57ddddb..f055f3d 100644 --- a/crates/yt_dlp/src/lib.rs +++ b/crates/yt_dlp/src/lib.rs @@ -20,14 +20,14 @@ use pyo3::{ use url::Url; use crate::{ - info_json::{InfoJson, json_dumps, json_loads}, + info_json::{InfoJson, json_dumps, json_dumps_any, json_loads, json_loads_any}, python_error::{IntoPythonError, PythonError}, }; +pub mod hooks; pub mod info_json; pub mod options; pub mod post_processors; -pub mod hooks; pub mod python_error; #[macro_export] |
