json-glib 0.1.1

Rust bindings for the Glib Json library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// 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::{ffi,Node,Object};
use glib::{translate::*};

glib::wrapper! {
    /// `JsonArray` is the representation of the array type inside JSON.
    ///
    /// A `JsonArray` contains [`Node`][crate::Node] elements, which may contain
    /// fundamental types, other arrays or objects.
    ///
    /// Since arrays can be arbitrarily big, copying them can be expensive; for
    /// this reason, they are reference counted. You can control the lifetime of
    /// a `JsonArray` using `Json::Array::ref()` and `Json::Array::unref()`.
    ///
    /// To append an element, use [`add_element()`][Self::add_element()].
    ///
    /// To extract an element at a given index, use [`element()`][Self::element()].
    ///
    /// To retrieve the entire array in list form, use [`elements()`][Self::elements()].
    ///
    /// To retrieve the length of the array, use [`length()`][Self::length()].
    #[derive(Debug, PartialOrd, Ord)]
    pub struct Array(Shared<ffi::JsonArray>);

    match fn {
        ref => |ptr| ffi::json_array_ref(ptr),
        unref => |ptr| ffi::json_array_unref(ptr),
        type_ => || ffi::json_array_get_type(),
    }
}

impl Array {
    /// Creates a new array.
    ///
    /// # Returns
    ///
    /// the newly created array
    #[doc(alias = "json_array_new")]
    pub fn new() -> Array {
        assert_initialized_main_thread!();
        unsafe {
            from_glib_full(ffi::json_array_new())
        }
    }

    /// Creates a new array with `n_elements` slots already allocated.
    /// ## `n_elements`
    /// number of slots to pre-allocate
    ///
    /// # Returns
    ///
    /// the newly created array
    #[doc(alias = "json_array_sized_new")]
    pub fn sized_new(n_elements: u32) -> Array {
        assert_initialized_main_thread!();
        unsafe {
            from_glib_full(ffi::json_array_sized_new(n_elements))
        }
    }

    /// Conveniently adds an array element into an array.
    ///
    /// If `value` is `NULL`, a `null` element will be added instead.
    ///
    /// See also: [`add_element()`][Self::add_element()], [`Node::take_array()`][crate::Node::take_array()]
    /// ## `value`
    /// the array to add
    #[doc(alias = "json_array_add_array_element")]
    pub fn add_array_element(&self, value: Option<Array>) {
        unsafe {
            ffi::json_array_add_array_element(self.to_glib_none().0, value.into_glib_ptr());
        }
    }

    /// Conveniently adds the given boolean value into an array.
    ///
    /// See also: [`add_element()`][Self::add_element()], [`Node::set_boolean()`][crate::Node::set_boolean()]
    /// ## `value`
    /// the boolean value to add
    #[doc(alias = "json_array_add_boolean_element")]
    pub fn add_boolean_element(&self, value: bool) {
        unsafe {
            ffi::json_array_add_boolean_element(self.to_glib_none().0, value.into_glib());
        }
    }

    /// Conveniently adds the given floating point value into an array.
    ///
    /// See also: [`add_element()`][Self::add_element()], [`Node::set_double()`][crate::Node::set_double()]
    /// ## `value`
    /// the floating point value to add
    #[doc(alias = "json_array_add_double_element")]
    pub fn add_double_element(&self, value: f64) {
        unsafe {
            ffi::json_array_add_double_element(self.to_glib_none().0, value);
        }
    }

    /// Appends the given `node` inside an array.
    /// ## `node`
    /// the element to add
    #[doc(alias = "json_array_add_element")]
    pub fn add_element(&self, node: Node) {
        unsafe {
            ffi::json_array_add_element(self.to_glib_none().0, node.into_glib_ptr());
        }
    }

    /// Conveniently adds the given integer value into an array.
    ///
    /// See also: [`add_element()`][Self::add_element()], [`Node::set_int()`][crate::Node::set_int()]
    /// ## `value`
    /// the integer value to add
    #[doc(alias = "json_array_add_int_element")]
    pub fn add_int_element(&self, value: i64) {
        unsafe {
            ffi::json_array_add_int_element(self.to_glib_none().0, value);
        }
    }

    /// Conveniently adds a `null` element into an array
    ///
    /// See also: [`add_element()`][Self::add_element()], `JSON_NODE_NULL`
    #[doc(alias = "json_array_add_null_element")]
    pub fn add_null_element(&self) {
        unsafe {
            ffi::json_array_add_null_element(self.to_glib_none().0);
        }
    }

    /// Conveniently adds an object into an array.
    ///
    /// If `value` is `NULL`, a `null` element will be added instead.
    ///
    /// See also: [`add_element()`][Self::add_element()], [`Node::take_object()`][crate::Node::take_object()]
    /// ## `value`
    /// the object to add
    #[doc(alias = "json_array_add_object_element")]
    pub fn add_object_element(&self, value: Option<Object>) {
        unsafe {
            ffi::json_array_add_object_element(self.to_glib_none().0, value.into_glib_ptr());
        }
    }

    /// Conveniently adds the given string value into an array.
    ///
    /// See also: [`add_element()`][Self::add_element()], [`Node::set_string()`][crate::Node::set_string()]
    /// ## `value`
    /// the string value to add
    #[doc(alias = "json_array_add_string_element")]
    pub fn add_string_element(&self, value: &str) {
        unsafe {
            ffi::json_array_add_string_element(self.to_glib_none().0, value.to_glib_none().0);
        }
    }

    /// Retrieves a copy of the element at the given position in the array.
    /// ## `index_`
    /// the index of the element to retrieve
    ///
    /// # Returns
    ///
    /// a copy of the element at the given position
    #[doc(alias = "json_array_dup_element")]
    pub fn dup_element(&self, index_: u32) -> Node {
        unsafe {
            from_glib_full(ffi::json_array_dup_element(self.to_glib_none().0, index_))
        }
    }

    #[cfg(feature = "v1_2")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
    #[doc(alias = "json_array_equal")]
     fn equal(&self, b: &Array) -> bool {
        unsafe {
            from_glib(ffi::json_array_equal(ToGlibPtr::<*mut ffi::JsonArray>::to_glib_none(self).0 as glib::ffi::gconstpointer, ToGlibPtr::<*mut ffi::JsonArray>::to_glib_none(b).0 as glib::ffi::gconstpointer))
        }
    }

    /// Iterates over all elements of an array, and calls a function on
    /// each one of them.
    ///
    /// It is safe to change the value of an element of the array while
    /// iterating over it, but it is not safe to add or remove elements
    /// from the array.
    /// ## `func`
    /// the function to be called on each element
    #[doc(alias = "json_array_foreach_element")]
    pub fn foreach_element<P: FnMut(&Array, u32, &Node)>(&self, func: P) {
        let mut func_data: P = func;
        unsafe extern "C" fn func_func<P: FnMut(&Array, u32, &Node)>(array: *mut ffi::JsonArray, index_: std::ffi::c_uint, element_node: *mut ffi::JsonNode, user_data: glib::ffi::gpointer) {
            let array = from_glib_borrow(array);
            let element_node = from_glib_borrow(element_node);
            let callback = user_data as *mut P;
            (*callback)(&array, index_, &element_node)
        }
        let func = Some(func_func::<P> as _);
        let super_callback0: &mut P = &mut func_data;
        unsafe {
            ffi::json_array_foreach_element(self.to_glib_none().0, func, super_callback0 as *mut _ as *mut _);
        }
    }

    /// Conveniently retrieves the array at the given position inside an array.
    ///
    /// See also: [`element()`][Self::element()], [`Node::array()`][crate::Node::array()]
    /// ## `index_`
    /// the index of the element to retrieve
    ///
    /// # Returns
    ///
    /// the array
    #[doc(alias = "json_array_get_array_element")]
    #[doc(alias = "get_array_element")]
#[must_use]
    pub fn array_element(&self, index_: u32) -> Array {
        unsafe {
            from_glib_none(ffi::json_array_get_array_element(self.to_glib_none().0, index_))
        }
    }

    /// Conveniently retrieves the boolean value of the element at the given
    /// position inside an array.
    ///
    /// See also: [`element()`][Self::element()], [`Node::is_boolean()`][crate::Node::is_boolean()]
    /// ## `index_`
    /// the index of the element to retrieve
    ///
    /// # Returns
    ///
    /// the boolean value
    #[doc(alias = "json_array_get_boolean_element")]
    #[doc(alias = "get_boolean_element")]
    pub fn is_boolean_element(&self, index_: u32) -> bool {
        unsafe {
            from_glib(ffi::json_array_get_boolean_element(self.to_glib_none().0, index_))
        }
    }

    /// Conveniently retrieves the floating point value of the element at
    /// the given position inside an array.
    ///
    /// See also: [`element()`][Self::element()], [`Node::double()`][crate::Node::double()]
    /// ## `index_`
    /// the index of the element to retrieve
    ///
    /// # Returns
    ///
    /// the floating point value
    #[doc(alias = "json_array_get_double_element")]
    #[doc(alias = "get_double_element")]
    pub fn double_element(&self, index_: u32) -> f64 {
        unsafe {
            ffi::json_array_get_double_element(self.to_glib_none().0, index_)
        }
    }

    /// Retrieves the element at the given position in the array.
    /// ## `index_`
    /// the index of the element to retrieve
    ///
    /// # Returns
    ///
    /// the element at the given position
    #[doc(alias = "json_array_get_element")]
    #[doc(alias = "get_element")]
    pub fn element(&self, index_: u32) -> Node {
        unsafe {
            from_glib_none(ffi::json_array_get_element(self.to_glib_none().0, index_))
        }
    }

    /// Retrieves all the elements of an array as a list of nodes.
    ///
    /// # Returns
    ///
    /// the elements
    ///   of the array
    #[doc(alias = "json_array_get_elements")]
    #[doc(alias = "get_elements")]
    pub fn elements(&self) -> Vec<Node> {
        unsafe {
            FromGlibPtrContainer::from_glib_container(ffi::json_array_get_elements(self.to_glib_none().0))
        }
    }

    /// Conveniently retrieves the integer value of the element at the given
    /// position inside an array.
    ///
    /// See also: [`element()`][Self::element()], [`Node::int()`][crate::Node::int()]
    /// ## `index_`
    /// the index of the element to retrieve
    ///
    /// # Returns
    ///
    /// the integer value
    #[doc(alias = "json_array_get_int_element")]
    #[doc(alias = "get_int_element")]
    pub fn int_element(&self, index_: u32) -> i64 {
        unsafe {
            ffi::json_array_get_int_element(self.to_glib_none().0, index_)
        }
    }

    /// Retrieves the length of the given array
    ///
    /// # Returns
    ///
    /// the length of the array
    #[doc(alias = "json_array_get_length")]
    #[doc(alias = "get_length")]
    pub fn length(&self) -> u32 {
        unsafe {
            ffi::json_array_get_length(self.to_glib_none().0)
        }
    }

    /// Conveniently checks whether the element at the given position inside the
    /// array contains a `null` value.
    ///
    /// See also: [`element()`][Self::element()], [`Node::is_null()`][crate::Node::is_null()]
    /// ## `index_`
    /// the index of the element to retrieve
    ///
    /// # Returns
    ///
    /// `TRUE` if the element is `null`
    #[doc(alias = "json_array_get_null_element")]
    #[doc(alias = "get_null_element")]
    pub fn is_null_element(&self, index_: u32) -> bool {
        unsafe {
            from_glib(ffi::json_array_get_null_element(self.to_glib_none().0, index_))
        }
    }

    /// Conveniently retrieves the object at the given position inside an array.
    ///
    /// See also: [`element()`][Self::element()], [`Node::object()`][crate::Node::object()]
    /// ## `index_`
    /// the index of the element to retrieve
    ///
    /// # Returns
    ///
    /// the object
    #[doc(alias = "json_array_get_object_element")]
    #[doc(alias = "get_object_element")]
    pub fn object_element(&self, index_: u32) -> Object {
        unsafe {
            from_glib_none(ffi::json_array_get_object_element(self.to_glib_none().0, index_))
        }
    }

    /// Conveniently retrieves the string value of the element at the given
    /// position inside an array.
    ///
    /// See also: [`element()`][Self::element()], [`Node::string()`][crate::Node::string()]
    /// ## `index_`
    /// the index of the element to retrieve
    ///
    /// # Returns
    ///
    /// the string value
    #[doc(alias = "json_array_get_string_element")]
    #[doc(alias = "get_string_element")]
    pub fn string_element(&self, index_: u32) -> glib::GString {
        unsafe {
            from_glib_none(ffi::json_array_get_string_element(self.to_glib_none().0, index_))
        }
    }

    #[cfg(feature = "v1_2")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
    #[doc(alias = "json_array_hash")]
     fn hash(&self) -> u32 {
        unsafe {
            ffi::json_array_hash(ToGlibPtr::<*mut ffi::JsonArray>::to_glib_none(self).0 as glib::ffi::gconstpointer)
        }
    }

    /// Check whether the given `array` has been marked as immutable by calling
    /// [`seal()`][Self::seal()] on it.
    ///
    /// # Returns
    ///
    /// [`true`] if the array is immutable
    #[cfg(feature = "v1_2")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
    #[doc(alias = "json_array_is_immutable")]
    pub fn is_immutable(&self) -> bool {
        unsafe {
            from_glib(ffi::json_array_is_immutable(self.to_glib_none().0))
        }
    }

    /// Removes the element at the given position inside an array.
    ///
    /// This function will release the reference held on the element.
    /// ## `index_`
    /// the position of the element to be removed
    #[doc(alias = "json_array_remove_element")]
    pub fn remove_element(&self, index_: u32) {
        unsafe {
            ffi::json_array_remove_element(self.to_glib_none().0, index_);
        }
    }

    /// Seals the given array, making it immutable to further changes.
    ///
    /// This function will recursively seal all elements in the array too.
    ///
    /// If the `array` is already immutable, this is a no-op.
    #[cfg(feature = "v1_2")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
    #[doc(alias = "json_array_seal")]
    pub fn seal(&self) {
        unsafe {
            ffi::json_array_seal(self.to_glib_none().0);
        }
    }
}

impl Default for Array {
                     fn default() -> Self {
                         Self::new()
                     }
                 }

#[cfg(feature = "v1_2")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
impl PartialEq for Array {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.equal(other)
    }
}
#[cfg(feature = "v1_2")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]

impl Eq for Array {}

#[cfg(feature = "v1_2")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
impl std::hash::Hash for Array {
    #[inline]
    fn hash<H>(&self, state: &mut H) where H: std::hash::Hasher {
        std::hash::Hash::hash(&self.hash(), state)
    }
}