Skip to main content

json_glib/auto/
reader.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::{ffi,Node};
7use glib::{prelude::*,signal::{connect_raw, SignalHandlerId},translate::*};
8use std::{boxed::Box as Box_};
9
10glib::wrapper! {
11    /// `JsonReader` provides a simple, cursor-based API for parsing a JSON DOM.
12    ///
13    /// It is similar, in spirit, to the XML Reader API.
14    ///
15    /// The cursor is moved by the `json_reader_read_*` and the `json_reader_end_*`
16    /// functions. You can enter a JSON object using [`ReaderExt::read_member()`][crate::prelude::ReaderExt::read_member()]
17    /// with the name of the object member, access the value at that position, and
18    /// move the cursor back one level using [`ReaderExt::end_member()`][crate::prelude::ReaderExt::end_member()]; arrays
19    /// work in a similar way, using [`ReaderExt::read_element()`][crate::prelude::ReaderExt::read_element()] with the
20    /// index of the element, and using [`ReaderExt::end_element()`][crate::prelude::ReaderExt::end_element()] to move
21    /// the cursor back.
22    ///
23    /// ## Using `JsonReader`
24    ///
25    /// **⚠️ The following code is in c ⚠️**
26    ///
27    /// ```c
28    /// g_autoptr(JsonParser) parser = json_parser_new ();
29    ///
30    /// // str is defined elsewhere and contains:
31    /// // { "url" : "http://www.gnome.org/img/flash/two-thirty.png", "size" : [ 652, 242 ] }
32    /// json_parser_load_from_data (parser, str, -1, NULL);
33    ///
34    /// g_autoptr(JsonReader) reader = json_reader_new (json_parser_get_root (parser));
35    ///
36    /// // Enter the "url" member of the object
37    /// json_reader_read_member (reader, "url");
38    ///   const char *url = json_reader_get_string_value (reader);
39    ///   // url now contains "http://www.gnome.org/img/flash/two-thirty.png"
40    ///   json_reader_end_member (reader);
41    ///
42    /// // Enter the "size" member of the object
43    /// json_reader_read_member (reader, "size");
44    ///   // Enter the first element of the array
45    ///   json_reader_read_element (reader, 0);
46    ///     int width = json_reader_get_int_value (reader);
47    ///     // width now contains 652
48    ///     json_reader_end_element (reader);
49    ///   // Enter the second element of the array
50    ///   json_reader_read_element (reader, 1);
51    ///     int height = json_reader_get_int_value (reader);
52    ///     // height now contains 242
53    ///     json_reader_end_element (reader);
54    ///   json_reader_end_member (reader);
55    /// ```
56    ///
57    /// ## Error handling
58    ///
59    /// In case of error, `JsonReader` will be set in an error state; all subsequent
60    /// calls will simply be ignored until a function that resets the error state is
61    /// called, e.g.:
62    ///
63    /// **⚠️ The following code is in c ⚠️**
64    ///
65    /// ```c
66    /// // ask for the 7th element; if the element does not exist, the
67    /// // reader will be put in an error state
68    /// json_reader_read_element (reader, 6);
69    ///
70    /// // in case of error, this will return NULL, otherwise it will
71    /// // return the value of the element
72    /// str = json_reader_get_string_value (value);
73    ///
74    /// // this function resets the error state if any was set
75    /// json_reader_end_element (reader);
76    /// ```
77    ///
78    /// If you want to detect the error state as soon as possible, you can use
79    /// [`ReaderExt::error()`][crate::prelude::ReaderExt::error()]:
80    ///
81    /// **⚠️ The following code is in c ⚠️**
82    ///
83    /// ```c
84    /// // like the example above, but in this case we print out the
85    /// // error immediately
86    /// if (!json_reader_read_element (reader, 6))
87    ///   {
88    ///     const GError *error = json_reader_get_error (reader);
89    ///     g_print ("Unable to read the element: %s", error->message);
90    ///   }
91    /// ```
92    ///
93    /// ## Properties
94    ///
95    ///
96    /// #### `root`
97    ///  The root of the JSON tree that the reader should read.
98    ///
99    /// Readable | Writeable | Construct
100    ///
101    /// # Implements
102    ///
103    /// [`ReaderExt`][trait@crate::prelude::ReaderExt], [`trait@glib::ObjectExt`]
104    #[doc(alias = "JsonReader")]
105    pub struct Reader(Object<ffi::JsonReader, ffi::JsonReaderClass>);
106
107    match fn {
108        type_ => || ffi::json_reader_get_type(),
109    }
110}
111
112impl Reader {
113        pub const NONE: Option<&'static Reader> = None;
114    
115
116    /// Creates a new reader.
117    ///
118    /// You can use this object to read the contents of the JSON tree starting
119    /// from the given node.
120    /// ## `node`
121    /// the root node
122    ///
123    /// # Returns
124    ///
125    /// the newly created reader
126    #[doc(alias = "json_reader_new")]
127    pub fn new(node: Option<&Node>) -> Reader {
128        assert_initialized_main_thread!();
129        unsafe {
130            from_glib_full(ffi::json_reader_new(node.to_glib_none().0))
131        }
132    }
133
134            // rustdoc-stripper-ignore-next
135            /// Creates a new builder-pattern struct instance to construct [`Reader`] objects.
136            ///
137            /// This method returns an instance of [`ReaderBuilder`](crate::builders::ReaderBuilder) which can be used to create [`Reader`] objects.
138            pub fn builder() -> ReaderBuilder {
139                ReaderBuilder::new()
140            }
141        
142}
143
144impl Default for Reader {
145                     fn default() -> Self {
146                         glib::object::Object::new::<Self>()
147                     }
148                 }
149
150// rustdoc-stripper-ignore-next
151        /// A [builder-pattern] type to construct [`Reader`] objects.
152        ///
153        /// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
154#[must_use = "The builder must be built to be used"]
155pub struct ReaderBuilder {
156            builder: glib::object::ObjectBuilder<'static, Reader>,
157        }
158
159        impl ReaderBuilder {
160        fn new() -> Self {
161            Self { builder: glib::object::Object::builder() }
162        }
163
164                            /// The root of the JSON tree that the reader should read.
165                            pub fn root(self, root: &Node) -> Self {
166                            Self { builder: self.builder.property("root", root.clone()), }
167                        }
168
169    // rustdoc-stripper-ignore-next
170    /// Build the [`Reader`].
171    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
172    pub fn build(self) -> Reader {
173assert_initialized_main_thread!();
174    self.builder.build() }
175}
176
177/// Trait containing all [`struct@Reader`] methods.
178///
179/// # Implementors
180///
181/// [`Reader`][struct@crate::Reader]
182pub trait ReaderExt: IsA<Reader> + 'static {
183    /// Counts the elements of the current position, if the reader is
184    /// positioned on an array.
185    ///
186    /// In case of failure, the reader is set to an error state.
187    ///
188    /// # Returns
189    ///
190    /// the number of elements, or -1.
191    #[doc(alias = "json_reader_count_elements")]
192    fn count_elements(&self) -> i32 {
193        unsafe {
194            ffi::json_reader_count_elements(self.as_ref().to_glib_none().0)
195        }
196    }
197
198    /// Counts the members of the current position, if the reader is
199    /// positioned on an object.
200    ///
201    /// In case of failure, the reader is set to an error state.
202    ///
203    /// # Returns
204    ///
205    /// the number of members, or -1
206    #[doc(alias = "json_reader_count_members")]
207    fn count_members(&self) -> i32 {
208        unsafe {
209            ffi::json_reader_count_members(self.as_ref().to_glib_none().0)
210        }
211    }
212
213    /// Moves the cursor back to the previous node after being positioned
214    /// inside an array.
215    ///
216    /// This function resets the error state of the reader, if any was set.
217    #[doc(alias = "json_reader_end_element")]
218    fn end_element(&self) {
219        unsafe {
220            ffi::json_reader_end_element(self.as_ref().to_glib_none().0);
221        }
222    }
223
224    /// Moves the cursor back to the previous node after being positioned
225    /// inside an object.
226    ///
227    /// This function resets the error state of the reader, if any was set.
228    #[doc(alias = "json_reader_end_member")]
229    fn end_member(&self) {
230        unsafe {
231            ffi::json_reader_end_member(self.as_ref().to_glib_none().0);
232        }
233    }
234
235    /// Retrieves the boolean value of the current position of the reader.
236    ///
237    /// See also: [`value()`][Self::value()]
238    ///
239    /// # Returns
240    ///
241    /// the boolean value
242    #[doc(alias = "json_reader_get_boolean_value")]
243    #[doc(alias = "get_boolean_value")]
244    fn is_boolean_value(&self) -> bool {
245        unsafe {
246            from_glib(ffi::json_reader_get_boolean_value(self.as_ref().to_glib_none().0))
247        }
248    }
249
250    /// Retrieves the reader node at the current position.
251    ///
252    /// # Returns
253    ///
254    /// the current node of the reader
255    #[cfg(feature = "v1_8")]
256    #[cfg_attr(docsrs, doc(cfg(feature = "v1_8")))]
257    #[doc(alias = "json_reader_get_current_node")]
258    #[doc(alias = "get_current_node")]
259    fn current_node(&self) -> Option<Node> {
260        unsafe {
261            from_glib_none(ffi::json_reader_get_current_node(self.as_ref().to_glib_none().0))
262        }
263    }
264
265    /// Retrieves the floating point value of the current position of the reader.
266    ///
267    /// See also: [`value()`][Self::value()]
268    ///
269    /// # Returns
270    ///
271    /// the floating point value
272    #[doc(alias = "json_reader_get_double_value")]
273    #[doc(alias = "get_double_value")]
274    fn double_value(&self) -> f64 {
275        unsafe {
276            ffi::json_reader_get_double_value(self.as_ref().to_glib_none().0)
277        }
278    }
279
280    /// Retrieves the error currently set on the reader.
281    ///
282    /// # Returns
283    ///
284    /// the current error
285    #[doc(alias = "json_reader_get_error")]
286    #[doc(alias = "get_error")]
287    fn error(&self) -> Option<glib::Error> {
288        unsafe {
289            from_glib_none(ffi::json_reader_get_error(self.as_ref().to_glib_none().0))
290        }
291    }
292
293    /// Retrieves the integer value of the current position of the reader.
294    ///
295    /// See also: [`value()`][Self::value()]
296    ///
297    /// # Returns
298    ///
299    /// the integer value
300    #[doc(alias = "json_reader_get_int_value")]
301    #[doc(alias = "get_int_value")]
302    fn int_value(&self) -> i64 {
303        unsafe {
304            ffi::json_reader_get_int_value(self.as_ref().to_glib_none().0)
305        }
306    }
307
308    /// Retrieves the name of the current member.
309    ///
310    /// In case of failure, the reader is set to an error state.
311    ///
312    /// # Returns
313    ///
314    /// the name of the member
315    #[doc(alias = "json_reader_get_member_name")]
316    #[doc(alias = "get_member_name")]
317    fn member_name(&self) -> Option<glib::GString> {
318        unsafe {
319            from_glib_none(ffi::json_reader_get_member_name(self.as_ref().to_glib_none().0))
320        }
321    }
322
323    /// Checks whether the value of the current position of the reader is `null`.
324    ///
325    /// See also: [`value()`][Self::value()]
326    ///
327    /// # Returns
328    ///
329    /// `TRUE` if `null` is set, and `FALSE` otherwise
330    #[doc(alias = "json_reader_get_null_value")]
331    #[doc(alias = "get_null_value")]
332    fn is_null_value(&self) -> bool {
333        unsafe {
334            from_glib(ffi::json_reader_get_null_value(self.as_ref().to_glib_none().0))
335        }
336    }
337
338    /// Retrieves the string value of the current position of the reader.
339    ///
340    /// See also: [`value()`][Self::value()]
341    ///
342    /// # Returns
343    ///
344    /// the string value
345    #[doc(alias = "json_reader_get_string_value")]
346    #[doc(alias = "get_string_value")]
347    fn string_value(&self) -> glib::GString {
348        unsafe {
349            from_glib_none(ffi::json_reader_get_string_value(self.as_ref().to_glib_none().0))
350        }
351    }
352
353    /// Retrieves the value node at the current position of the reader.
354    ///
355    /// If the current position does not contain a scalar value, the reader
356    /// is set to an error state.
357    ///
358    /// # Returns
359    ///
360    /// the current value node
361    #[doc(alias = "json_reader_get_value")]
362    #[doc(alias = "get_value")]
363    fn value(&self) -> Option<Node> {
364        unsafe {
365            from_glib_none(ffi::json_reader_get_value(self.as_ref().to_glib_none().0))
366        }
367    }
368
369    /// Checks whether the reader is currently on an array.
370    ///
371    /// # Returns
372    ///
373    /// `TRUE` if the reader is on an array
374    #[doc(alias = "json_reader_is_array")]
375    fn is_array(&self) -> bool {
376        unsafe {
377            from_glib(ffi::json_reader_is_array(self.as_ref().to_glib_none().0))
378        }
379    }
380
381    /// Checks whether the reader is currently on an object.
382    ///
383    /// # Returns
384    ///
385    /// `TRUE` if the reader is on an object
386    #[doc(alias = "json_reader_is_object")]
387    fn is_object(&self) -> bool {
388        unsafe {
389            from_glib(ffi::json_reader_is_object(self.as_ref().to_glib_none().0))
390        }
391    }
392
393    /// Checks whether the reader is currently on a value.
394    ///
395    /// # Returns
396    ///
397    /// `TRUE` if the reader is on a value
398    #[doc(alias = "json_reader_is_value")]
399    fn is_value(&self) -> bool {
400        unsafe {
401            from_glib(ffi::json_reader_is_value(self.as_ref().to_glib_none().0))
402        }
403    }
404
405    /// Retrieves a list of member names from the current position, if the reader
406    /// is positioned on an object.
407    ///
408    /// In case of failure, the reader is set to an error state.
409    ///
410    /// # Returns
411    ///
412    /// the members of
413    ///   the object
414    #[doc(alias = "json_reader_list_members")]
415    fn list_members(&self) -> Vec<glib::GString> {
416        unsafe {
417            FromGlibPtrContainer::from_glib_full(ffi::json_reader_list_members(self.as_ref().to_glib_none().0))
418        }
419    }
420
421    /// Advances the cursor of the reader to the element of the array or
422    /// the member of the object at the given position.
423    ///
424    /// You can use [`value()`][Self::value()] and its wrapper functions to
425    /// retrieve the value of the element; for instance, the following code will
426    /// read the first element of the array at the current cursor position:
427    ///
428    /// **⚠️ The following code is in c ⚠️**
429    ///
430    /// ```c
431    /// json_reader_read_element (reader, 0);
432    /// int_value = json_reader_get_int_value (reader);
433    /// ```
434    ///
435    /// After reading the value, you should call [`end_element()`][Self::end_element()]
436    /// to reposition the cursor inside the reader, e.g.:
437    ///
438    /// **⚠️ The following code is in c ⚠️**
439    ///
440    /// ```c
441    /// const char *str_value = NULL;
442    ///
443    /// json_reader_read_element (reader, 1);
444    /// str_value = json_reader_get_string_value (reader);
445    /// json_reader_end_element (reader);
446    ///
447    /// json_reader_read_element (reader, 2);
448    /// str_value = json_reader_get_string_value (reader);
449    /// json_reader_end_element (reader);
450    /// ```
451    ///
452    /// If the reader is not currently on an array or an object, or if the index is
453    /// bigger than the size of the array or the object, the reader will be
454    /// put in an error state until [`end_element()`][Self::end_element()] is called. This
455    /// means that, if used conditionally, [`end_element()`][Self::end_element()] must be
456    /// called on all branches:
457    ///
458    /// **⚠️ The following code is in c ⚠️**
459    ///
460    /// ```c
461    /// if (!json_reader_read_element (reader, 1))
462    ///   {
463    ///     g_propagate_error (error, json_reader_get_error (reader));
464    ///     json_reader_end_element (reader);
465    ///     return FALSE;
466    ///   }
467    /// else
468    ///   {
469    ///     const char *str_value = json_reader_get_string_value (reader);
470    ///     json_reader_end_element (reader);
471    ///
472    ///     // use str_value
473    ///
474    ///     return TRUE;
475    ///   }
476    /// ```c
477    /// ## `index_`
478    /// the index of the element
479    ///
480    /// # Returns
481    ///
482    /// `TRUE` on success, and `FALSE` otherwise
483    #[doc(alias = "json_reader_read_element")]
484    fn read_element(&self, index_: u32) -> bool {
485        unsafe {
486            from_glib(ffi::json_reader_read_element(self.as_ref().to_glib_none().0, index_))
487        }
488    }
489
490    /// Advances the cursor of the reader to the `member_name` of the object at
491    /// the current position.
492    ///
493    /// You can use [`value()`][Self::value()] and its wrapper functions to
494    /// retrieve the value of the member; for instance:
495    ///
496    /// **⚠️ The following code is in c ⚠️**
497    ///
498    /// ```c
499    /// json_reader_read_member (reader, "width");
500    /// width = json_reader_get_int_value (reader);
501    /// ```
502    ///
503    /// After reading the value, `json_reader_end_member()` should be called to
504    /// reposition the cursor inside the reader, e.g.:
505    ///
506    /// **⚠️ The following code is in c ⚠️**
507    ///
508    /// ```c
509    /// json_reader_read_member (reader, "author");
510    /// author = json_reader_get_string_value (reader);
511    /// json_reader_end_member (reader);
512    ///
513    /// json_reader_read_member (reader, "title");
514    /// title = json_reader_get_string_value (reader);
515    /// json_reader_end_member (reader);
516    /// ```
517    ///
518    /// If the reader is not currently on an object, or if the `member_name` is not
519    /// defined in the object, the reader will be put in an error state until
520    /// [`end_member()`][Self::end_member()] is called. This means that if used
521    /// conditionally, [`end_member()`][Self::end_member()] must be called on all branches:
522    ///
523    /// **⚠️ The following code is in c ⚠️**
524    ///
525    /// ```c
526    /// if (!json_reader_read_member (reader, "title"))
527    ///   {
528    ///     g_propagate_error (error, json_reader_get_error (reader));
529    ///     json_reader_end_member (reader);
530    ///     return FALSE;
531    ///   }
532    /// else
533    ///   {
534    ///     const char *str_value = json_reader_get_string_value (reader);
535    ///     json_reader_end_member (reader);
536    ///
537    ///     // use str_value
538    ///
539    ///     return TRUE;
540    ///   }
541    /// ```
542    /// ## `member_name`
543    /// the name of the member to read
544    ///
545    /// # Returns
546    ///
547    /// `TRUE` on success, and `FALSE` otherwise
548    #[doc(alias = "json_reader_read_member")]
549    fn read_member(&self, member_name: &str) -> bool {
550        unsafe {
551            from_glib(ffi::json_reader_read_member(self.as_ref().to_glib_none().0, member_name.to_glib_none().0))
552        }
553    }
554
555    /// Sets the root node of the JSON tree to be read by @self.
556    ///
557    /// The reader will take a copy of the node.
558    /// ## `root`
559    /// the root node
560    #[doc(alias = "json_reader_set_root")]
561    #[doc(alias = "root")]
562    fn set_root(&self, root: Option<&Node>) {
563        unsafe {
564            ffi::json_reader_set_root(self.as_ref().to_glib_none().0, root.to_glib_none().0);
565        }
566    }
567
568    /// The root of the JSON tree that the reader should read.
569    fn root(&self) -> Option<Node> {
570        ObjectExt::property(self.as_ref(), "root")
571    }
572
573    #[doc(alias = "root")]
574    fn connect_root_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
575        unsafe extern "C" fn notify_root_trampoline<P: IsA<Reader>, F: Fn(&P) + 'static>(this: *mut ffi::JsonReader, _param_spec: glib::ffi::gpointer, f: glib::ffi::gpointer) {
576            let f: &F = &*(f as *const F);
577            f(Reader::from_glib_borrow(this).unsafe_cast_ref())
578        }
579        unsafe {
580            let f: Box_<F> = Box_::new(f);
581            connect_raw(self.as_ptr() as *mut _, c"notify::root".as_ptr() as *const _,
582                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(notify_root_trampoline::<Self, F> as *const ())), Box_::into_raw(f))
583        }
584    }
585}
586
587impl<O: IsA<Reader>> ReaderExt for O {}