These are used by tests. However it could even be an idea to use serde_json + transcoding and get rid of the C version...
Co-authored-by: Marc-André Lureau <[email protected]> Signed-off-by: Marc-André Lureau <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]> --- rust/util/src/qobject/mod.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/rust/util/src/qobject/mod.rs b/rust/util/src/qobject/mod.rs index 76b167104d1..d2dde9ba0f1 100644 --- a/rust/util/src/qobject/mod.rs +++ b/rust/util/src/qobject/mod.rs @@ -23,6 +23,7 @@ use common::assert_field_type; pub use deserializer::from_qobject; pub use error::{Error, Result}; +use foreign::prelude::*; pub use serializer::to_qobject; use crate::bindings; @@ -114,6 +115,22 @@ fn refcnt(&self) -> &AtomicUsize { let qobj = self.0.get(); unsafe { AtomicUsize::from_ptr(addr_of_mut!((*qobj).base.refcnt)) } } + + pub fn to_json(&self) -> String { + let qobj = self.0.get(); + unsafe { + let json = bindings::qobject_to_json(qobj); + glib_sys::g_string_free(json, glib_sys::GFALSE).into_native() + } + } + + pub fn from_json(json: &str) -> std::result::Result<Self, crate::Error> { + let c_json = std::ffi::CString::new(json)?; + unsafe { + crate::Error::with_errp(|errp| bindings::qobject_from_json(c_json.as_ptr(), errp)) + .map(|qobj| QObject::from_raw(qobj)) + } + } } /// Rust equivalent of the C `QOBJECT` macro; for internal use only, because -- 2.54.0
