json-glib 0.1.0

Rust bindings for the Glib Json library
Documentation
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir
// from gtk-girs (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::{Node, NodeType, ffi};
use glib::{prelude::*, translate::*};

#[doc(alias = "json_boxed_can_deserialize")]
pub fn boxed_can_deserialize(gboxed_type: glib::types::Type, node_type: NodeType) -> bool {
    assert_initialized_main_thread!();
    unsafe {
        from_glib(ffi::json_boxed_can_deserialize(
            gboxed_type.into_glib(),
            node_type.into_glib(),
        ))
    }
}

#[doc(alias = "json_boxed_can_serialize")]
pub fn boxed_can_serialize(gboxed_type: glib::types::Type) -> Option<NodeType> {
    assert_initialized_main_thread!();
    unsafe {
        let mut node_type = std::mem::MaybeUninit::uninit();
        let ret = from_glib(ffi::json_boxed_can_serialize(
            gboxed_type.into_glib(),
            node_type.as_mut_ptr(),
        ));
        if ret {
            Some(from_glib(node_type.assume_init()))
        } else {
            None
        }
    }
}

//#[doc(alias = "json_boxed_deserialize")]
//pub fn boxed_deserialize(gboxed_type: glib::types::Type, node: &Node) -> /*Unimplemented*/Option<Basic: Pointer> {
//    unsafe { TODO: call ffi:json_boxed_deserialize() }
//}

//#[doc(alias = "json_boxed_register_deserialize_func")]
//pub fn boxed_register_deserialize_func(gboxed_type: glib::types::Type, node_type: NodeType, deserialize_func: /*Unimplemented*/Fn() -> /*Unimplemented*/Option<Basic: Pointer>) {
//    unsafe { TODO: call ffi:json_boxed_register_deserialize_func() }
//}

//#[doc(alias = "json_boxed_register_serialize_func")]
//pub fn boxed_register_serialize_func<P: Fn() -> Node + Send + Sync + 'static>(gboxed_type: glib::types::Type, node_type: NodeType, serialize_func: P) {
//    unsafe { TODO: call ffi:json_boxed_register_serialize_func() }
//}

//#[doc(alias = "json_boxed_serialize")]
//pub fn boxed_serialize(gboxed_type: glib::types::Type, boxed: /*Unimplemented*/Option<Basic: Pointer>) -> Option<Node> {
//    unsafe { TODO: call ffi:json_boxed_serialize() }
//}

#[cfg(feature = "v1_2")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
#[doc(alias = "json_from_string")]
pub fn from_string(str: &str) -> Result<Option<Node>, glib::Error> {
    assert_initialized_main_thread!();
    unsafe {
        let mut error = std::ptr::null_mut();
        let ret = ffi::json_from_string(str.to_glib_none().0, &mut error);
        if error.is_null() {
            Ok(from_glib_full(ret))
        } else {
            Err(from_glib_full(error))
        }
    }
}

#[doc(alias = "json_gobject_deserialize")]
pub fn gobject_deserialize(gtype: glib::types::Type, node: &Node) -> glib::Object {
    assert_initialized_main_thread!();
    unsafe {
        from_glib_full(ffi::json_gobject_deserialize(
            gtype.into_glib(),
            node.to_glib_none().0,
        ))
    }
}

#[doc(alias = "json_gobject_from_data")]
pub fn gobject_from_data(
    gtype: glib::types::Type,
    data: &str,
) -> Result<Option<glib::Object>, glib::Error> {
    assert_initialized_main_thread!();
    let length = data.len() as _;
    unsafe {
        let mut error = std::ptr::null_mut();
        let ret = ffi::json_gobject_from_data(
            gtype.into_glib(),
            data.to_glib_none().0,
            length,
            &mut error,
        );
        if error.is_null() {
            Ok(from_glib_full(ret))
        } else {
            Err(from_glib_full(error))
        }
    }
}

#[doc(alias = "json_gobject_serialize")]
pub fn gobject_serialize(gobject: &impl IsA<glib::Object>) -> Node {
    assert_initialized_main_thread!();
    unsafe {
        from_glib_full(ffi::json_gobject_serialize(
            gobject.as_ref().to_glib_none().0,
        ))
    }
}

#[doc(alias = "json_gobject_to_data")]
pub fn gobject_to_data(gobject: &impl IsA<glib::Object>) -> (glib::GString, usize) {
    assert_initialized_main_thread!();
    unsafe {
        let mut length = std::mem::MaybeUninit::uninit();
        let ret = from_glib_full(ffi::json_gobject_to_data(
            gobject.as_ref().to_glib_none().0,
            length.as_mut_ptr(),
        ));
        (ret, length.assume_init())
    }
}

#[doc(alias = "json_gvariant_deserialize")]
pub fn gvariant_deserialize(
    json_node: &Node,
    signature: Option<&str>,
) -> Result<Option<glib::Variant>, glib::Error> {
    assert_initialized_main_thread!();
    unsafe {
        let mut error = std::ptr::null_mut();
        let ret = ffi::json_gvariant_deserialize(
            json_node.to_glib_none().0,
            signature.to_glib_none().0,
            &mut error,
        );
        if error.is_null() {
            Ok(from_glib_none(ret))
        } else {
            Err(from_glib_full(error))
        }
    }
}

#[doc(alias = "json_gvariant_deserialize_data")]
pub fn gvariant_deserialize_data(
    json: &str,
    signature: Option<&str>,
) -> Result<Option<glib::Variant>, glib::Error> {
    assert_initialized_main_thread!();
    let length = json.len() as _;
    unsafe {
        let mut error = std::ptr::null_mut();
        let ret = ffi::json_gvariant_deserialize_data(
            json.to_glib_none().0,
            length,
            signature.to_glib_none().0,
            &mut error,
        );
        if error.is_null() {
            Ok(from_glib_none(ret))
        } else {
            Err(from_glib_full(error))
        }
    }
}

#[doc(alias = "json_gvariant_serialize")]
pub fn gvariant_serialize(variant: &glib::Variant) -> Node {
    assert_initialized_main_thread!();
    unsafe { from_glib_full(ffi::json_gvariant_serialize(variant.to_glib_none().0)) }
}

#[doc(alias = "json_gvariant_serialize_data")]
pub fn gvariant_serialize_data(variant: &glib::Variant) -> (glib::GString, usize) {
    assert_initialized_main_thread!();
    unsafe {
        let mut length = std::mem::MaybeUninit::uninit();
        let ret = from_glib_full(ffi::json_gvariant_serialize_data(
            variant.to_glib_none().0,
            length.as_mut_ptr(),
        ));
        (ret, length.assume_init())
    }
}

#[cfg(feature = "v1_2")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
#[doc(alias = "json_string_compare")]
pub fn string_compare(a: &str, b: &str) -> i32 {
    assert_initialized_main_thread!();
    unsafe {
        let s1: *mut i8 = a.to_glib_none().0;
        let s2: *mut i8 = b.to_glib_none().0;
        ffi::json_string_compare(s1.cast(), s2.cast())
    }
}

#[cfg(feature = "v1_2")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
#[doc(alias = "json_string_equal")]
pub fn string_equal(a: &str, b: &str) -> bool {
    assert_initialized_main_thread!();
    unsafe {
        let s1: *mut i8 = a.to_glib_none().0;
        let s2: *mut i8 = b.to_glib_none().0;
        from_glib(ffi::json_string_equal(s1.cast(), s2.cast()))
    }
}

#[cfg(feature = "v1_2")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
#[doc(alias = "json_string_hash")]
pub fn string_hash(key: &str) -> u32 {
    assert_initialized_main_thread!();
    let s: *mut i8 = key.to_glib_none().0;
    unsafe { ffi::json_string_hash(s.cast()) }
}

#[cfg(feature = "v1_2")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
#[doc(alias = "json_to_string")]
pub fn to_string(node: &Node, pretty: bool) -> glib::GString {
    assert_initialized_main_thread!();
    unsafe {
        from_glib_full(ffi::json_to_string(
            node.to_glib_none().0,
            pretty.into_glib(),
        ))
    }
}