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
/// JSON processing utilities.
///
/// This module provides comprehensive JSON parsing, serialization,
/// validation, and manipulation utilities with error handling.
///
/// # Examples
///
/// Basic JSON operations:
/// ```rust
/// use trash_utilities::common::json::*;
/// use serde::{Serialize, Deserialize};
///
/// #[derive(Serialize, Deserialize)]
/// struct Person { name: String, age: u32 }
///
/// let person = Person { name: "Alice".to_string(), age: 30 };
///
/// // Serialize to JSON
/// let json = to_json_value(&person).unwrap();
/// println!("JSON: {}", json);
///
/// // Parse from JSON
/// let parsed: Person = parse_json_value(&json).unwrap();
/// assert_eq!(parsed.name, "Alice");
///
/// // Pretty print
/// let pretty = pretty_json_value(&person).unwrap();
/// println!("Pretty JSON:\n{}", pretty);
///
/// // Validate JSON
/// assert!(validate_json(&json));
///
/// // Extract by path
/// let nested_json = r#"{"user": {"name": "Bob", "age": 25}}"#;
/// let name = extract_json_path(nested_json, "user.name").unwrap();
/// assert_eq!(name.unwrap().as_str().unwrap(), "Bob");
/// ```
// External crate imports
use ;
use serde_json;
/// JSON utilities with error handling
///
/// Parses a JSON string into the specified type using serde.
///
/// # Type Parameters
///
/// * `T` - The type to deserialize into (must implement `serde::Deserialize`).
///
/// # Parameters
///
/// * `json` - The JSON string to parse.
///
/// # Returns
///
/// The deserialized value on success.
///
/// # Errors
///
/// Returns a `serde_json::Error` if parsing fails.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::common::json::parse_json_value;
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct Config { debug: bool, port: u16 }
///
/// let json = r#"{"debug":true,"port":8080}"#;
/// let config: Config = parse_json_value(json).unwrap();
/// assert!(config.debug);
/// assert_eq!(config.port, 8080);
/// ```
/// Serialize a value to JSON string
///
/// Converts a value that implements `serde::Serialize` into a compact JSON string.
///
/// # Type Parameters
///
/// * `T` - The type to serialize (must implement `serde::Serialize`).
///
/// # Parameters
///
/// * `value` - The value to serialize.
///
/// # Returns
///
/// A compact JSON string representation on success.
///
/// # Errors
///
/// Returns a `serde_json::Error` if serialization fails.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::common::json::to_json_value;
/// use serde::Serialize;
///
/// #[derive(Serialize)]
/// struct Point { x: i32, y: i32 }
///
/// let point = Point { x: 10, y: 20 };
/// let json = to_json_value(&point).unwrap();
/// assert_eq!(json, r#"{"x":10,"y":20}"#);
/// ```
/// Serialize a value to pretty-printed JSON string
///
/// Converts a value into a human-readable JSON string with proper indentation.
///
/// # Type Parameters
///
/// * `T` - The type to serialize (must implement `serde::Serialize`).
///
/// # Parameters
///
/// * `value` - The value to serialize.
///
/// # Returns
///
/// A pretty-printed JSON string on success.
///
/// # Errors
///
/// Returns a `serde_json::Error` if serialization fails.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::common::json::pretty_json_value;
///
/// let data = vec![1, 2, 3];
/// let pretty = pretty_json_value(&data).unwrap();
/// println!("Pretty JSON:\n{}", pretty);
/// // Output will be formatted with indentation
/// ```
/// Validate JSON structure
///
/// Checks if a string contains valid JSON without deserializing it to a specific type.
/// Useful for quick validation before further processing.
///
/// # Parameters
///
/// * `json` - The JSON string to validate.
///
/// # Returns
///
/// `true` if the string is valid JSON, `false` otherwise.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::common::json::validate_json;
///
/// assert!(validate_json(r#"{"name":"Alice","age":30}"#));
/// assert!(validate_json(r#"[1,2,3]"#));
/// assert!(!validate_json(r#"{"invalid": json}"#));
/// assert!(!validate_json("not json at all"));
/// ```
/// Merge two JSON objects
///
/// Merges two JSON objects by combining their properties.
/// If both inputs are objects, properties from the second object are added to the first.
/// Other JSON value types are not supported for merging.
///
/// # Parameters
///
/// * `a` - The base JSON string (must be a JSON object).
/// * `b` - The JSON string to merge into the base (must be a JSON object).
///
/// # Returns
///
/// A new JSON string with the merged objects on success.
///
/// # Errors
///
/// Returns a `serde_json::Error` if parsing or serialization fails,
/// or if either input is not a JSON object.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::common::json::merge_json;
///
/// let base = r#"{"name":"Alice","age":30}"#;
/// let extra = r#"{"city":"New York","job":"Engineer"}"#;
/// let merged = merge_json(base, extra).unwrap();
/// // Result: {"name":"Alice","age":30,"city":"New York","job":"Engineer"}
/// println!("Merged: {}", merged);
/// ```
/// Extract values from JSON by path
///
/// Traverses a JSON object using dot-notation paths to extract nested values.
/// For example, "user.name" would extract the "name" field from within a "user" object.
///
/// # Parameters
///
/// * `json` - The JSON string to traverse.
/// * `path` - The dot-separated path to the desired value (e.g., "user.profile.name").
///
/// # Returns
///
/// `Some(value)` if the path exists and can be traversed, `None` if the path doesn't exist.
///
/// # Errors
///
/// Returns an error if the JSON is invalid, or if traversal encounters a non-object value
/// when trying to access a property.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::common::json::extract_json_path;
///
/// let json = r#"{
/// "user": {
/// "name": "Alice",
/// "profile": {
/// "age": 30,
/// "city": "Boston"
/// }
/// },
/// "active": true
/// }"#;
///
/// let name = extract_json_path(json, "user.name").unwrap();
/// assert_eq!(name.unwrap().as_str().unwrap(), "Alice");
///
/// let age = extract_json_path(json, "user.profile.age").unwrap();
/// assert_eq!(age.unwrap().as_u64().unwrap(), 30);
///
/// let missing = extract_json_path(json, "user.missing").unwrap();
/// assert!(missing.is_none());
/// ```