Skip to main content

json_glib/auto/
builder.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::*,translate::*};
8
9glib::wrapper! {
10    /// `JsonBuilder` provides an object for generating a JSON tree.
11    ///
12    /// The root of the JSON tree can be either a [`Object`][crate::Object] or a [`Array`][crate::Array].
13    /// Thus the first call must necessarily be either
14    /// [`BuilderExt::begin_object()`][crate::prelude::BuilderExt::begin_object()] or [`BuilderExt::begin_array()`][crate::prelude::BuilderExt::begin_array()].
15    ///
16    /// For convenience to language bindings, most `JsonBuilder` method return the
17    /// instance, making it easy to chain function calls.
18    ///
19    /// ## Using `JsonBuilder`
20    ///
21    /// **⚠️ The following code is in c ⚠️**
22    ///
23    /// ```c
24    /// g_autoptr(JsonBuilder) builder = json_builder_new ();
25    ///
26    /// json_builder_begin_object (builder);
27    ///
28    /// json_builder_set_member_name (builder, "url");
29    /// json_builder_add_string_value (builder, "http://www.gnome.org/img/flash/two-thirty.png");
30    ///
31    /// json_builder_set_member_name (builder, "size");
32    /// json_builder_begin_array (builder);
33    /// json_builder_add_int_value (builder, 652);
34    /// json_builder_add_int_value (builder, 242);
35    /// json_builder_end_array (builder);
36    ///
37    /// json_builder_end_object (builder);
38    ///
39    /// g_autoptr(JsonNode) root = json_builder_get_root (builder);
40    ///
41    /// g_autoptr(JsonGenerator) gen = json_generator_new ();
42    /// json_generator_set_root (gen, root);
43    /// g_autofree char *str = json_generator_to_data (gen, NULL);
44    ///
45    /// // str now contains the following JSON data
46    /// // { "url" : "http://www.gnome.org/img/flash/two-thirty.png", "size" : [ 652, 242 ] }
47    /// ```
48    ///
49    /// ## Properties
50    ///
51    ///
52    /// #### `immutable`
53    ///  Whether the tree should be immutable when created.
54    ///
55    /// Making the output immutable on creation avoids the expense
56    /// of traversing it to make it immutable later.
57    ///
58    /// Readable | Writeable | Construct Only
59    ///
60    /// # Implements
61    ///
62    /// [`BuilderExt`][trait@crate::prelude::BuilderExt], [`trait@glib::ObjectExt`]
63    #[doc(alias = "JsonBuilder")]
64    pub struct Builder(Object<ffi::JsonBuilder, ffi::JsonBuilderClass>);
65
66    match fn {
67        type_ => || ffi::json_builder_get_type(),
68    }
69}
70
71impl Builder {
72        pub const NONE: Option<&'static Builder> = None;
73    
74
75    /// Creates a new `JsonBuilder`.
76    ///
77    /// You can use this object to generate a JSON tree and obtain the root node.
78    ///
79    /// # Returns
80    ///
81    /// the newly created builder instance
82    #[doc(alias = "json_builder_new")]
83    pub fn new() -> Builder {
84        assert_initialized_main_thread!();
85        unsafe {
86            from_glib_full(ffi::json_builder_new())
87        }
88    }
89
90    /// Creates a new, immutable `JsonBuilder` instance.
91    ///
92    /// It is equivalent to setting the [`immutable`][struct@crate::Builder#immutable] property
93    /// set to `TRUE` at construction time.
94    ///
95    /// # Returns
96    ///
97    /// the newly create builder instance
98    #[cfg(feature = "v1_2")]
99    #[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
100    #[doc(alias = "json_builder_new_immutable")]
101    pub fn new_immutable() -> Builder {
102        assert_initialized_main_thread!();
103        unsafe {
104            from_glib_full(ffi::json_builder_new_immutable())
105        }
106    }
107
108            // rustdoc-stripper-ignore-next
109            /// Creates a new builder-pattern struct instance to construct [`Builder`] objects.
110            ///
111            /// This method returns an instance of [`BuilderBuilder`](crate::builders::BuilderBuilder) which can be used to create [`Builder`] objects.
112            pub fn builder() -> BuilderBuilder {
113                BuilderBuilder::new()
114            }
115        
116}
117
118impl Default for Builder {
119                     fn default() -> Self {
120                         Self::new()
121                     }
122                 }
123
124// rustdoc-stripper-ignore-next
125        /// A [builder-pattern] type to construct [`Builder`] objects.
126        ///
127        /// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
128#[must_use = "The builder must be built to be used"]
129pub struct BuilderBuilder {
130            builder: glib::object::ObjectBuilder<'static, Builder>,
131        }
132
133        impl BuilderBuilder {
134        fn new() -> Self {
135            Self { builder: glib::object::Object::builder() }
136        }
137
138                            /// Whether the tree should be immutable when created.
139                            ///
140                            /// Making the output immutable on creation avoids the expense
141                            /// of traversing it to make it immutable later.
142                            #[cfg(feature = "v1_2")]
143    #[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
144    pub fn immutable(self, immutable: bool) -> Self {
145                            Self { builder: self.builder.property("immutable", immutable), }
146                        }
147
148    // rustdoc-stripper-ignore-next
149    /// Build the [`Builder`].
150    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
151    pub fn build(self) -> Builder {
152assert_initialized_main_thread!();
153    self.builder.build() }
154}
155
156/// Trait containing all [`struct@Builder`] methods.
157///
158/// # Implementors
159///
160/// [`Builder`][struct@crate::Builder]
161pub trait BuilderExt: IsA<Builder> + 'static {
162    /// Adds a boolean value to the currently open object member or array.
163    ///
164    /// If called after [`set_member_name()`][Self::set_member_name()], sets the given value
165    /// as the value of the current member in the open object; otherwise, the value
166    /// is appended to the elements of the open array.
167    ///
168    /// See also: [`add_value()`][Self::add_value()]
169    /// ## `value`
170    /// the value of the member or element
171    ///
172    /// # Returns
173    ///
174    /// the builder instance
175    #[doc(alias = "json_builder_add_boolean_value")]
176#[must_use]
177    fn add_boolean_value(&self, value: bool) -> Option<Builder> {
178        unsafe {
179            from_glib_none(ffi::json_builder_add_boolean_value(self.as_ref().to_glib_none().0, value.into_glib()))
180        }
181    }
182
183    /// Adds a floating point value to the currently open object member or array.
184    ///
185    /// If called after [`set_member_name()`][Self::set_member_name()], sets the given value
186    /// as the value of the current member in the open object; otherwise, the value
187    /// is appended to the elements of the open array.
188    ///
189    /// See also: [`add_value()`][Self::add_value()]
190    /// ## `value`
191    /// the value of the member or element
192    ///
193    /// # Returns
194    ///
195    /// the builder instance
196    #[doc(alias = "json_builder_add_double_value")]
197#[must_use]
198    fn add_double_value(&self, value: f64) -> Option<Builder> {
199        unsafe {
200            from_glib_none(ffi::json_builder_add_double_value(self.as_ref().to_glib_none().0, value))
201        }
202    }
203
204    /// Adds an integer value to the currently open object member or array.
205    ///
206    /// If called after [`set_member_name()`][Self::set_member_name()], sets the given value
207    /// as the value of the current member in the open object; otherwise, the value
208    /// is appended to the elements of the open array.
209    ///
210    /// See also: [`add_value()`][Self::add_value()]
211    /// ## `value`
212    /// the value of the member or element
213    ///
214    /// # Returns
215    ///
216    /// the builder instance
217    #[doc(alias = "json_builder_add_int_value")]
218#[must_use]
219    fn add_int_value(&self, value: i64) -> Option<Builder> {
220        unsafe {
221            from_glib_none(ffi::json_builder_add_int_value(self.as_ref().to_glib_none().0, value))
222        }
223    }
224
225    /// Adds a null value to the currently open object member or array.
226    ///
227    /// If called after [`set_member_name()`][Self::set_member_name()], sets the given value
228    /// as the value of the current member in the open object; otherwise, the value
229    /// is appended to the elements of the open array.
230    ///
231    /// See also: [`add_value()`][Self::add_value()]
232    ///
233    /// # Returns
234    ///
235    /// the builder instance
236    #[doc(alias = "json_builder_add_null_value")]
237#[must_use]
238    fn add_null_value(&self) -> Option<Builder> {
239        unsafe {
240            from_glib_none(ffi::json_builder_add_null_value(self.as_ref().to_glib_none().0))
241        }
242    }
243
244    /// Adds a string value to the currently open object member or array.
245    ///
246    /// If called after [`set_member_name()`][Self::set_member_name()], sets the given value
247    /// as the value of the current member in the open object; otherwise, the value
248    /// is appended to the elements of the open array.
249    ///
250    /// See also: [`add_value()`][Self::add_value()]
251    /// ## `value`
252    /// the value of the member or element
253    ///
254    /// # Returns
255    ///
256    /// the builder instance
257    #[doc(alias = "json_builder_add_string_value")]
258#[must_use]
259    fn add_string_value(&self, value: &str) -> Option<Builder> {
260        unsafe {
261            from_glib_none(ffi::json_builder_add_string_value(self.as_ref().to_glib_none().0, value.to_glib_none().0))
262        }
263    }
264
265    /// Adds a value to the currently open object member or array.
266    ///
267    /// If called after [`set_member_name()`][Self::set_member_name()], sets the given node
268    /// as the value of the current member in the open object; otherwise, the node
269    /// is appended to the elements of the open array.
270    ///
271    /// The builder will take ownership of the node.
272    /// ## `node`
273    /// the value of the member or element
274    ///
275    /// # Returns
276    ///
277    /// the builder instance
278    #[doc(alias = "json_builder_add_value")]
279#[must_use]
280    fn add_value(&self, node: Node) -> Option<Builder> {
281        unsafe {
282            from_glib_none(ffi::json_builder_add_value(self.as_ref().to_glib_none().0, node.into_glib_ptr()))
283        }
284    }
285
286    /// Opens an array inside the given builder.
287    ///
288    /// You can add a new element to the array by using [`add_value()`][Self::add_value()].
289    ///
290    /// Once you added all elements to the array, you must call
291    /// [`end_array()`][Self::end_array()] to close the array.
292    ///
293    /// # Returns
294    ///
295    /// the builder instance
296    #[doc(alias = "json_builder_begin_array")]
297#[must_use]
298    fn begin_array(&self) -> Option<Builder> {
299        unsafe {
300            from_glib_none(ffi::json_builder_begin_array(self.as_ref().to_glib_none().0))
301        }
302    }
303
304    /// Opens an object inside the given builder.
305    ///
306    /// You can add a new member to the object by using [`set_member_name()`][Self::set_member_name()],
307    /// followed by [`add_value()`][Self::add_value()].
308    ///
309    /// Once you added all members to the object, you must call [`end_object()`][Self::end_object()]
310    /// to close the object.
311    ///
312    /// If the builder is in an inconsistent state, this function will return `NULL`.
313    ///
314    /// # Returns
315    ///
316    /// the builder instance
317    #[doc(alias = "json_builder_begin_object")]
318#[must_use]
319    fn begin_object(&self) -> Option<Builder> {
320        unsafe {
321            from_glib_none(ffi::json_builder_begin_object(self.as_ref().to_glib_none().0))
322        }
323    }
324
325    /// Closes the array inside the given builder that was opened by the most
326    /// recent call to [`begin_array()`][Self::begin_array()].
327    ///
328    /// This function cannot be called after [`set_member_name()`][Self::set_member_name()].
329    ///
330    /// # Returns
331    ///
332    /// the builder instance
333    #[doc(alias = "json_builder_end_array")]
334#[must_use]
335    fn end_array(&self) -> Option<Builder> {
336        unsafe {
337            from_glib_none(ffi::json_builder_end_array(self.as_ref().to_glib_none().0))
338        }
339    }
340
341    /// Closes the object inside the given builder that was opened by the most
342    /// recent call to [`begin_object()`][Self::begin_object()].
343    ///
344    /// This function cannot be called after [`set_member_name()`][Self::set_member_name()].
345    ///
346    /// # Returns
347    ///
348    /// the builder instance
349    #[doc(alias = "json_builder_end_object")]
350#[must_use]
351    fn end_object(&self) -> Option<Builder> {
352        unsafe {
353            from_glib_none(ffi::json_builder_end_object(self.as_ref().to_glib_none().0))
354        }
355    }
356
357    /// Returns the root of the currently constructed tree.
358    ///
359    /// if the build is incomplete (ie: if there are any opened objects, or any
360    /// open object members and array elements) then this function will return
361    /// `NULL`.
362    ///
363    /// # Returns
364    ///
365    /// the root node
366    #[doc(alias = "json_builder_get_root")]
367    #[doc(alias = "get_root")]
368    fn root(&self) -> Option<Node> {
369        unsafe {
370            from_glib_full(ffi::json_builder_get_root(self.as_ref().to_glib_none().0))
371        }
372    }
373
374    /// Resets the state of the builder back to its initial state.
375    #[doc(alias = "json_builder_reset")]
376    fn reset(&self) {
377        unsafe {
378            ffi::json_builder_reset(self.as_ref().to_glib_none().0);
379        }
380    }
381
382    /// Sets the name of the member in an object.
383    ///
384    /// This function must be followed by of these functions:
385    ///
386    ///  - [`add_value()`][Self::add_value()], to add a scalar value to the member
387    ///  - [`begin_object()`][Self::begin_object()], to add an object to the member
388    ///  - [`begin_array()`][Self::begin_array()], to add an array to the member
389    ///
390    /// This function can only be called within an open object.
391    /// ## `member_name`
392    /// the name of the member
393    ///
394    /// # Returns
395    ///
396    /// the builder instance
397    #[doc(alias = "json_builder_set_member_name")]
398#[must_use]
399    fn set_member_name(&self, member_name: &str) -> Option<Builder> {
400        unsafe {
401            from_glib_none(ffi::json_builder_set_member_name(self.as_ref().to_glib_none().0, member_name.to_glib_none().0))
402        }
403    }
404
405    /// Whether the tree should be immutable when created.
406    ///
407    /// Making the output immutable on creation avoids the expense
408    /// of traversing it to make it immutable later.
409    #[cfg(feature = "v1_2")]
410    #[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
411    fn is_immutable(&self) -> bool {
412        ObjectExt::property(self.as_ref(), "immutable")
413    }
414}
415
416impl<O: IsA<Builder>> BuilderExt for O {}