From c1b75cf60e0d116db8f43c7023fb507149091019 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Sat, 7 Nov 2026 15:20:36 +0100 Subject: fix(yt_dlp/info_json): Allow loading and dumping any type --- crates/yt_dlp/src/info_json.rs | 27 ++++++++++++++++++++++++++- 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,12 +9,37 @@ // If not, see . 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; +/// # 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] 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] -- cgit v1.3.1