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
//! Helper utility functions for JSON processing.
//!
//! This module provides utility functions for cleaning up JSON data before
//! or after compression, particularly for handling null/undefined values.
//!
//! # Functions
//!
//! - [`trim_undefined`] - Remove null values from an object (shallow)
//! - [`trim_undefined_recursively`] - Remove null values from nested objects
//!
//! # Use Cases
//!
//! These functions are useful when:
//! - Working with data that may contain optional/null fields
//! - Reducing payload size by removing empty values
//! - Normalizing data before compression
//!
//! # Note on Naming
//!
//! The functions are named `trim_undefined` for compatibility with the
//! JavaScript version, where `undefined` and `null` have different semantics.
//! In Rust/JSON, these functions operate on `null` values.
use ;
/// Remove keys with null values from a JSON object (shallow).
///
/// This function modifies the object in place, removing any key-value pairs
/// where the value is `null`. It only operates on the top level of the object;
/// nested objects are not affected.
///
/// # Arguments
///
/// * `object` - Mutable reference to a JSON object map
///
/// # Example
///
/// ```rust
/// use compress_json_rs::trim_undefined;
/// use serde_json::{json, Map, Value};
///
/// let mut data: Map<String, Value> = serde_json::from_value(json!({
/// "name": "Alice",
/// "email": null,
/// "age": 30,
/// "phone": null
/// })).unwrap();
///
/// trim_undefined(&mut data);
///
/// // Only non-null values remain
/// assert_eq!(data.len(), 2);
/// assert!(data.contains_key("name"));
/// assert!(data.contains_key("age"));
/// assert!(!data.contains_key("email"));
/// assert!(!data.contains_key("phone"));
/// ```
///
/// # Note
///
/// Arrays within the object are not modified. If an object contains an array
/// with null elements, those null elements will remain.
/// Recursively remove keys with null values in nested JSON objects.
///
/// This function traverses the entire object tree, removing any key-value
/// pairs where the value is `null` at all levels of nesting.
///
/// # Arguments
///
/// * `object` - Mutable reference to a JSON object map
///
/// # Example
///
/// ```rust
/// use compress_json_rs::trim_undefined_recursively;
/// use serde_json::{json, Map, Value};
///
/// let mut data: Map<String, Value> = serde_json::from_value(json!({
/// "user": {
/// "name": "Bob",
/// "middleName": null,
/// "address": {
/// "street": "123 Main St",
/// "apt": null
/// }
/// },
/// "metadata": null
/// })).unwrap();
///
/// trim_undefined_recursively(&mut data);
///
/// // Check structure
/// assert!(!data.contains_key("metadata"));
///
/// let user = data.get("user").unwrap().as_object().unwrap();
/// assert!(!user.contains_key("middleName"));
///
/// let address = user.get("address").unwrap().as_object().unwrap();
/// assert!(!address.contains_key("apt"));
/// assert!(address.contains_key("street"));
/// ```
///
/// # Behavior
///
/// - Only object values are traversed recursively
/// - Arrays are not traversed (null elements in arrays remain)
/// - The function handles cyclic references safely (via pointer tracking)