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
// 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::*};

/// Checks whether it is possible to deserialize a `GBoxed` of
/// type `gboxed_type` from a [`Node`][crate::Node] of type `node_type`.
/// ## `gboxed_type`
/// a boxed type
/// ## `node_type`
/// a node type
///
/// # Returns
///
/// `TRUE` if the type can be deserialized, and `FALSE` otherwise
#[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(),
        ))
    }
}

/// Checks whether it is possible to serialize a `GBoxed` of
/// type `gboxed_type` into a [`Node`][crate::Node].
///
/// The type of the node is placed inside `node_type` if the function
/// returns `TRUE`, and it's undefined otherwise.
/// ## `gboxed_type`
/// a boxed type
///
/// # Returns
///
/// `TRUE` if the type can be serialized, and `FALSE` otherwise
///
/// ## `node_type`
/// the node type to which the boxed type
///   can be serialized into
#[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() }
//}

/// Parses the given string and returns the corresponding JSON tree.
///
/// If the string is empty, this function will return `NULL`.
///
/// In case of parsing error, this function returns `NULL` and sets
/// the error appropriately.
/// ## `str`
/// a valid UTF-8 string containing JSON data
///
/// # Returns
///
/// the root node of the JSON tree
#[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))
        }
    }
}

/// Creates a new `GObject` instance of the given type, and constructs it
/// using the members of the object in the given node.
/// ## `gtype`
/// the type of the object to create
/// ## `node`
/// a node of type `JSON_NODE_OBJECT` describing the
///   object instance for the given type
///
/// # Returns
///
/// The newly created instance
#[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,
        ))
    }
}

/// Deserializes a JSON data stream and creates an instance of the
/// given type.
///
/// If the type implements the [`Serializable`][crate::Serializable] interface, it will
/// be asked to deserialize all the JSON members into their respective properties;
/// otherwise, the default implementation will be used to translate the
/// compatible JSON native types.
///
/// **Note**: the JSON data stream must be an object
/// ## `gtype`
/// the type of the object to construct
/// ## `data`
/// a JSON data stream
/// ## `length`
/// length of the data stream, or -1 if it is `NUL`-terminated
///
/// # Returns
///
/// a new object instance of the given type
#[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))
        }
    }
}

/// Creates a JSON tree representing the passed object instance.
///
/// Each member of the returned JSON object will map to a property of
/// the object type.
///
/// The returned JSON tree will be returned as a `JsonNode` with a type
/// of `JSON_NODE_OBJECT`.
/// ## `gobject`
/// the object to serialize
///
/// # Returns
///
/// the newly created JSON tree
#[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,
        ))
    }
}

/// Serializes a `GObject` instance into a JSON data stream, iterating
/// recursively over each property.
///
/// If the given object implements the [`Serializable`][crate::Serializable] interface,
/// it will be asked to serialize all its properties; otherwise, the default
/// implementation will be use to translate the compatible types into
/// JSON native types.
/// ## `gobject`
/// the object to serialize
///
/// # Returns
///
/// a JSON data stream representing the given object
///
/// ## `length`
/// return value for the length of the buffer
#[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())
    }
}

/// Converts a JSON data structure to a `GVariant`.
///
/// If `signature` is not `NULL`, it will be used to resolve ambiguous
/// data types.
///
/// If no error occurs, the resulting `GVariant` is guaranteed to conform
/// to `signature`.
///
/// If `signature` is not `NULL` but does not represent a valid `GVariant` type
/// string, `NULL` is returned and the `error` is set to
/// `G_IO_ERROR_INVALID_ARGUMENT`.
///
/// If a `signature` is provided but the JSON structure cannot be mapped to it,
/// `NULL` is returned and the `error` is set to `G_IO_ERROR_INVALID_DATA`.
///
/// If `signature` is `NULL`, the conversion is done based strictly on the types
/// in the JSON nodes.
///
/// The returned variant has a floating reference that will need to be sunk
/// by the caller code.
/// ## `json_node`
/// the node to convert
/// ## `signature`
/// a valid `GVariant` type string
///
/// # Returns
///
/// A newly created `GVariant`
#[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))
        }
    }
}

/// Converts a JSON string to a `GVariant` value.
///
/// This function works exactly like [`gvariant_deserialize()`][crate::gvariant_deserialize()], but
/// takes a JSON encoded string instead.
///
/// The string is first converted to a [`Node`][crate::Node] using
/// [`Parser`][crate::Parser], and then `json_gvariant_deserialize` is called on
/// the node.
///
/// The returned variant has a floating reference that will need to be sunk
/// by the caller code.
/// ## `json`
/// A JSON data string
/// ## `length`
/// The length of @json, or -1 if `NUL`-terminated
/// ## `signature`
/// A valid `GVariant` type string
///
/// # Returns
///
/// A newly created `GVariant`D compliant
#[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))
        }
    }
}

/// Converts `variant` to a JSON tree.
/// ## `variant`
/// A `GVariant` to convert
///
/// # Returns
///
/// the root of the JSON data structure
///   obtained from `variant`
#[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)) }
}

/// Converts @variant to its JSON encoded string representation.
///
/// This is a convenience function around [`gvariant_serialize()`][crate::gvariant_serialize()], to
/// obtain the JSON tree, and then [`Generator`][crate::Generator] to stringify it.
/// ## `variant`
/// A #GVariant to convert
///
/// # Returns
///
/// The JSON encoded string corresponding to
///   the given variant
///
/// ## `length`
/// the length of the returned string
#[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()) }
}

/// Generates a stringified JSON representation of the contents of
/// the given `node`.
/// ## `node`
/// a JSON tree
/// ## `pretty`
/// whether the output should be prettyfied for printing
///
/// # Returns
///
/// the string representation of the node
#[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(),
        ))
    }
}