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
//! A module for writing JSON data to blocks and files.
use HashMap;
use File;
use Write;
use ;
use ;
//--------------------------------------------------------------------------------------------------
// Write functions
/// Writes a serializable value into a specified block and key within a JSON [`Value`].
///
/// This function ensures that the given `block` exists in the root JSON object. If it does not,
/// a new empty object is created for that block. The provided `key` and `value` are then inserted
/// into this block.
///
/// # Arguments
///
/// * `data` - A mutable reference to the root JSON [`Value`] where the block will be inserted.
/// * `block` - The name of the block (as a string slice) within which the key-value pair is stored.
/// * `key` - The key (as a string slice) under which the value is stored in the block.
/// * `value` - The value to be serialized and inserted into the block. Must implement [`Serialize`].
///
/// # Examples
///
/// ```ignore
/// use serde_json::Value;
/// use gng::aux::json_writer::write_value_to_block;
///
/// let mut data = serde_json::json!({});
/// write_value_to_block(&mut data, "settings", "debug", true);
/// assert_eq!(data["settings"]["debug"], true);
/// ```
/// Serializes a [`Value`] to pretty-printed JSON and writes it to a file.
///
/// This function takes a filename and a [`Value`], serializes the value into a formatted JSON string,
/// and writes that string to the specified file. If the file does not exist, it will be created.
/// If it exists, it will be overwritten.
///
/// # Arguments
///
/// * `file_name` - A string slice that holds the name of the file to write to.
/// * `content` - A reference to the [`Value`] to serialize and write.
///
/// # Returns
///
/// Returns a [`Result`] that is:
///
/// * [`Ok(())`] if the operation was successful.
/// * [`Err`] containing an error from file I/O or serialization if the operation failed.
///
/// # Examples
///
/// ```ignore
/// use serde_json::Value;
/// use gng::aux::json_writer::write_json_to_file;
///
/// let content = serde_json::json!({"name": "test", "value": 42});
/// let result = write_json_to_file("output.json", &content);
/// assert!(result.is_ok());
/// ```