Skip to main content

json_glib/auto/
functions.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir
3// from gtk-girs (https://github.com/gtk-rs/gir-files)
4// DO NOT EDIT
5
6use crate::{Node, NodeType, ffi};
7use glib::{prelude::*, translate::*};
8
9/// Checks whether it is possible to deserialize a `GBoxed` of
10/// type `gboxed_type` from a [`Node`][crate::Node] of type `node_type`.
11/// ## `gboxed_type`
12/// a boxed type
13/// ## `node_type`
14/// a node type
15///
16/// # Returns
17///
18/// `TRUE` if the type can be deserialized, and `FALSE` otherwise
19#[doc(alias = "json_boxed_can_deserialize")]
20pub fn boxed_can_deserialize(gboxed_type: glib::types::Type, node_type: NodeType) -> bool {
21    assert_initialized_main_thread!();
22    unsafe {
23        from_glib(ffi::json_boxed_can_deserialize(
24            gboxed_type.into_glib(),
25            node_type.into_glib(),
26        ))
27    }
28}
29
30/// Checks whether it is possible to serialize a `GBoxed` of
31/// type `gboxed_type` into a [`Node`][crate::Node].
32///
33/// The type of the node is placed inside `node_type` if the function
34/// returns `TRUE`, and it's undefined otherwise.
35/// ## `gboxed_type`
36/// a boxed type
37///
38/// # Returns
39///
40/// `TRUE` if the type can be serialized, and `FALSE` otherwise
41///
42/// ## `node_type`
43/// the node type to which the boxed type
44///   can be serialized into
45#[doc(alias = "json_boxed_can_serialize")]
46pub fn boxed_can_serialize(gboxed_type: glib::types::Type) -> Option<NodeType> {
47    assert_initialized_main_thread!();
48    unsafe {
49        let mut node_type = std::mem::MaybeUninit::uninit();
50        let ret = from_glib(ffi::json_boxed_can_serialize(
51            gboxed_type.into_glib(),
52            node_type.as_mut_ptr(),
53        ));
54        if ret {
55            Some(from_glib(node_type.assume_init()))
56        } else {
57            None
58        }
59    }
60}
61
62//#[doc(alias = "json_boxed_deserialize")]
63//pub fn boxed_deserialize(gboxed_type: glib::types::Type, node: &Node) -> /*Unimplemented*/Option<Basic: Pointer> {
64//    unsafe { TODO: call ffi:json_boxed_deserialize() }
65//}
66
67//#[doc(alias = "json_boxed_register_deserialize_func")]
68//pub fn boxed_register_deserialize_func(gboxed_type: glib::types::Type, node_type: NodeType, deserialize_func: /*Unimplemented*/Fn() -> /*Unimplemented*/Option<Basic: Pointer>) {
69//    unsafe { TODO: call ffi:json_boxed_register_deserialize_func() }
70//}
71
72//#[doc(alias = "json_boxed_register_serialize_func")]
73//pub fn boxed_register_serialize_func<P: Fn() -> Node + Send + Sync + 'static>(gboxed_type: glib::types::Type, node_type: NodeType, serialize_func: P) {
74//    unsafe { TODO: call ffi:json_boxed_register_serialize_func() }
75//}
76
77//#[doc(alias = "json_boxed_serialize")]
78//pub fn boxed_serialize(gboxed_type: glib::types::Type, boxed: /*Unimplemented*/Option<Basic: Pointer>) -> Option<Node> {
79//    unsafe { TODO: call ffi:json_boxed_serialize() }
80//}
81
82/// Parses the given string and returns the corresponding JSON tree.
83///
84/// If the string is empty, this function will return `NULL`.
85///
86/// In case of parsing error, this function returns `NULL` and sets
87/// the error appropriately.
88/// ## `str`
89/// a valid UTF-8 string containing JSON data
90///
91/// # Returns
92///
93/// the root node of the JSON tree
94#[cfg(feature = "v1_2")]
95#[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
96#[doc(alias = "json_from_string")]
97pub fn from_string(str: &str) -> Result<Option<Node>, glib::Error> {
98    assert_initialized_main_thread!();
99    unsafe {
100        let mut error = std::ptr::null_mut();
101        let ret = ffi::json_from_string(str.to_glib_none().0, &mut error);
102        if error.is_null() {
103            Ok(from_glib_full(ret))
104        } else {
105            Err(from_glib_full(error))
106        }
107    }
108}
109
110/// Creates a new `GObject` instance of the given type, and constructs it
111/// using the members of the object in the given node.
112/// ## `gtype`
113/// the type of the object to create
114/// ## `node`
115/// a node of type `JSON_NODE_OBJECT` describing the
116///   object instance for the given type
117///
118/// # Returns
119///
120/// The newly created instance
121#[doc(alias = "json_gobject_deserialize")]
122pub fn gobject_deserialize(gtype: glib::types::Type, node: &Node) -> glib::Object {
123    assert_initialized_main_thread!();
124    unsafe {
125        from_glib_full(ffi::json_gobject_deserialize(
126            gtype.into_glib(),
127            node.to_glib_none().0,
128        ))
129    }
130}
131
132/// Deserializes a JSON data stream and creates an instance of the
133/// given type.
134///
135/// If the type implements the [`Serializable`][crate::Serializable] interface, it will
136/// be asked to deserialize all the JSON members into their respective properties;
137/// otherwise, the default implementation will be used to translate the
138/// compatible JSON native types.
139///
140/// **Note**: the JSON data stream must be an object
141/// ## `gtype`
142/// the type of the object to construct
143/// ## `data`
144/// a JSON data stream
145/// ## `length`
146/// length of the data stream, or -1 if it is `NUL`-terminated
147///
148/// # Returns
149///
150/// a new object instance of the given type
151#[doc(alias = "json_gobject_from_data")]
152pub fn gobject_from_data(
153    gtype: glib::types::Type,
154    data: &str,
155) -> Result<Option<glib::Object>, glib::Error> {
156    assert_initialized_main_thread!();
157    let length = data.len() as _;
158    unsafe {
159        let mut error = std::ptr::null_mut();
160        let ret = ffi::json_gobject_from_data(
161            gtype.into_glib(),
162            data.to_glib_none().0,
163            length,
164            &mut error,
165        );
166        if error.is_null() {
167            Ok(from_glib_full(ret))
168        } else {
169            Err(from_glib_full(error))
170        }
171    }
172}
173
174/// Creates a JSON tree representing the passed object instance.
175///
176/// Each member of the returned JSON object will map to a property of
177/// the object type.
178///
179/// The returned JSON tree will be returned as a `JsonNode` with a type
180/// of `JSON_NODE_OBJECT`.
181/// ## `gobject`
182/// the object to serialize
183///
184/// # Returns
185///
186/// the newly created JSON tree
187#[doc(alias = "json_gobject_serialize")]
188pub fn gobject_serialize(gobject: &impl IsA<glib::Object>) -> Node {
189    assert_initialized_main_thread!();
190    unsafe {
191        from_glib_full(ffi::json_gobject_serialize(
192            gobject.as_ref().to_glib_none().0,
193        ))
194    }
195}
196
197/// Serializes a `GObject` instance into a JSON data stream, iterating
198/// recursively over each property.
199///
200/// If the given object implements the [`Serializable`][crate::Serializable] interface,
201/// it will be asked to serialize all its properties; otherwise, the default
202/// implementation will be use to translate the compatible types into
203/// JSON native types.
204/// ## `gobject`
205/// the object to serialize
206///
207/// # Returns
208///
209/// a JSON data stream representing the given object
210///
211/// ## `length`
212/// return value for the length of the buffer
213#[doc(alias = "json_gobject_to_data")]
214pub fn gobject_to_data(gobject: &impl IsA<glib::Object>) -> (glib::GString, usize) {
215    assert_initialized_main_thread!();
216    unsafe {
217        let mut length = std::mem::MaybeUninit::uninit();
218        let ret = from_glib_full(ffi::json_gobject_to_data(
219            gobject.as_ref().to_glib_none().0,
220            length.as_mut_ptr(),
221        ));
222        (ret, length.assume_init())
223    }
224}
225
226/// Converts a JSON data structure to a `GVariant`.
227///
228/// If `signature` is not `NULL`, it will be used to resolve ambiguous
229/// data types.
230///
231/// If no error occurs, the resulting `GVariant` is guaranteed to conform
232/// to `signature`.
233///
234/// If `signature` is not `NULL` but does not represent a valid `GVariant` type
235/// string, `NULL` is returned and the `error` is set to
236/// `G_IO_ERROR_INVALID_ARGUMENT`.
237///
238/// If a `signature` is provided but the JSON structure cannot be mapped to it,
239/// `NULL` is returned and the `error` is set to `G_IO_ERROR_INVALID_DATA`.
240///
241/// If `signature` is `NULL`, the conversion is done based strictly on the types
242/// in the JSON nodes.
243///
244/// The returned variant has a floating reference that will need to be sunk
245/// by the caller code.
246/// ## `json_node`
247/// the node to convert
248/// ## `signature`
249/// a valid `GVariant` type string
250///
251/// # Returns
252///
253/// A newly created `GVariant`
254#[doc(alias = "json_gvariant_deserialize")]
255pub fn gvariant_deserialize(
256    json_node: &Node,
257    signature: Option<&str>,
258) -> Result<Option<glib::Variant>, glib::Error> {
259    assert_initialized_main_thread!();
260    unsafe {
261        let mut error = std::ptr::null_mut();
262        let ret = ffi::json_gvariant_deserialize(
263            json_node.to_glib_none().0,
264            signature.to_glib_none().0,
265            &mut error,
266        );
267        if error.is_null() {
268            Ok(from_glib_none(ret))
269        } else {
270            Err(from_glib_full(error))
271        }
272    }
273}
274
275/// Converts a JSON string to a `GVariant` value.
276///
277/// This function works exactly like [`gvariant_deserialize()`][crate::gvariant_deserialize()], but
278/// takes a JSON encoded string instead.
279///
280/// The string is first converted to a [`Node`][crate::Node] using
281/// [`Parser`][crate::Parser], and then `json_gvariant_deserialize` is called on
282/// the node.
283///
284/// The returned variant has a floating reference that will need to be sunk
285/// by the caller code.
286/// ## `json`
287/// A JSON data string
288/// ## `length`
289/// The length of @json, or -1 if `NUL`-terminated
290/// ## `signature`
291/// A valid `GVariant` type string
292///
293/// # Returns
294///
295/// A newly created `GVariant`D compliant
296#[doc(alias = "json_gvariant_deserialize_data")]
297pub fn gvariant_deserialize_data(
298    json: &str,
299    signature: Option<&str>,
300) -> Result<Option<glib::Variant>, glib::Error> {
301    assert_initialized_main_thread!();
302    let length = json.len() as _;
303    unsafe {
304        let mut error = std::ptr::null_mut();
305        let ret = ffi::json_gvariant_deserialize_data(
306            json.to_glib_none().0,
307            length,
308            signature.to_glib_none().0,
309            &mut error,
310        );
311        if error.is_null() {
312            Ok(from_glib_none(ret))
313        } else {
314            Err(from_glib_full(error))
315        }
316    }
317}
318
319/// Converts `variant` to a JSON tree.
320/// ## `variant`
321/// A `GVariant` to convert
322///
323/// # Returns
324///
325/// the root of the JSON data structure
326///   obtained from `variant`
327#[doc(alias = "json_gvariant_serialize")]
328pub fn gvariant_serialize(variant: &glib::Variant) -> Node {
329    assert_initialized_main_thread!();
330    unsafe { from_glib_full(ffi::json_gvariant_serialize(variant.to_glib_none().0)) }
331}
332
333/// Converts @variant to its JSON encoded string representation.
334///
335/// This is a convenience function around [`gvariant_serialize()`][crate::gvariant_serialize()], to
336/// obtain the JSON tree, and then [`Generator`][crate::Generator] to stringify it.
337/// ## `variant`
338/// A #GVariant to convert
339///
340/// # Returns
341///
342/// The JSON encoded string corresponding to
343///   the given variant
344///
345/// ## `length`
346/// the length of the returned string
347#[doc(alias = "json_gvariant_serialize_data")]
348pub fn gvariant_serialize_data(variant: &glib::Variant) -> (glib::GString, usize) {
349    assert_initialized_main_thread!();
350    unsafe {
351        let mut length = std::mem::MaybeUninit::uninit();
352        let ret = from_glib_full(ffi::json_gvariant_serialize_data(
353            variant.to_glib_none().0,
354            length.as_mut_ptr(),
355        ));
356        (ret, length.assume_init())
357    }
358}
359
360#[cfg(feature = "v1_2")]
361#[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
362#[doc(alias = "json_string_compare")]
363pub fn string_compare(a: &str, b: &str) -> i32 {
364    assert_initialized_main_thread!();
365    unsafe {
366        let s1: *mut i8 = a.to_glib_none().0;
367        let s2: *mut i8 = b.to_glib_none().0;
368        ffi::json_string_compare(s1.cast(), s2.cast())
369    }
370}
371
372#[cfg(feature = "v1_2")]
373#[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
374#[doc(alias = "json_string_equal")]
375pub fn string_equal(a: &str, b: &str) -> bool {
376    assert_initialized_main_thread!();
377    unsafe {
378        let s1: *mut i8 = a.to_glib_none().0;
379        let s2: *mut i8 = b.to_glib_none().0;
380        from_glib(ffi::json_string_equal(s1.cast(), s2.cast()))
381    }
382}
383
384#[cfg(feature = "v1_2")]
385#[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
386#[doc(alias = "json_string_hash")]
387pub fn string_hash(key: &str) -> u32 {
388    assert_initialized_main_thread!();
389    let s: *mut i8 = key.to_glib_none().0;
390    unsafe { ffi::json_string_hash(s.cast()) }
391}
392
393/// Generates a stringified JSON representation of the contents of
394/// the given `node`.
395/// ## `node`
396/// a JSON tree
397/// ## `pretty`
398/// whether the output should be prettyfied for printing
399///
400/// # Returns
401///
402/// the string representation of the node
403#[cfg(feature = "v1_2")]
404#[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
405#[doc(alias = "json_to_string")]
406pub fn to_string(node: &Node, pretty: bool) -> glib::GString {
407    assert_initialized_main_thread!();
408    unsafe {
409        from_glib_full(ffi::json_to_string(
410            node.to_glib_none().0,
411            pretty.into_glib(),
412        ))
413    }
414}