babbel_json 0.2.0

Fast, modular JSON toolkit with streaming parsers, JSON Pointer (RFC 6901), and Merge Patch
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
//! JSON Merge Patch (RFC 7386) implementation
//!
//! Simpler alternative to JSON Patch for partial updates.
//! Uses JSON structure itself to describe the changes.

use crate::nodes::node::Node;

#[cfg(feature = "std")]
use std::collections::HashMap;

#[cfg(not(feature = "std"))]
use alloc::collections::BTreeMap as HashMap;

/// Apply a merge patch to a target document
///
/// # Arguments
/// * `target` - The document to be patched (modified in place)
/// * `patch` - The merge patch to apply
///
/// # Examples
/// ```
/// use babbel_json::{json, nodes::merge_patch::merge_patch};
///
/// let mut doc = json!({"a": 1, "b": 2});
/// let patch = json!({"b": 3, "c": 4});
/// merge_patch(&mut doc, &patch);
/// // doc is now {"a": 1, "b": 3, "c": 4}
/// ```
pub fn merge_patch(target: &mut Node, patch: &Node) {
    if !patch.is_object() {
        // If patch is not an object, replace target entirely
        *target = patch.clone();
        return;
    }

    // Ensure target is an object
    if !target.is_object() {
        *target = Node::Object(HashMap::new());
    }

    let target_obj = target.as_object_mut().unwrap();
    let patch_obj = patch.as_object().unwrap();

    for (key, patch_value) in patch_obj.iter() {
        if patch_value.is_null() {
            // Remove key if patch value is null
            target_obj.remove(key);
        } else if patch_value.is_object() {
            // Recursively merge if both are objects
            if let Some(target_value) = target_obj.get_mut(key) {
                if target_value.is_object() {
                    merge_patch(target_value, patch_value);
                } else {
                    *target_value = patch_value.clone();
                }
            } else {
                target_obj.insert(key.clone(), patch_value.clone());
            }
        } else {
            // Replace/add value
            target_obj.insert(key.clone(), patch_value.clone());
        }
    }
}

/// Create a merge patch that transforms source into target
///
/// # Arguments
/// * `source` - The original document
/// * `target` - The desired document
///
/// # Returns
/// A merge patch that, when applied to source, produces target
///
/// # Examples
/// ```
/// use babbel_json::{json, nodes::merge_patch::create_merge_patch};
///
/// let source = json!({"a": 1, "b": 2});
/// let target = json!({"a": 1, "c": 3});
/// let patch = create_merge_patch(&source, &target);
/// // patch is {"b": null, "c": 3}
/// ```
pub fn create_merge_patch(source: &Node, target: &Node) -> Node {
    // If target is not an object, the patch is just the target
    if !target.is_object() {
        return target.clone();
    }

    // If source is not an object, the patch is the entire target
    if !source.is_object() {
        return target.clone();
    }

    let source_obj = source.as_object().unwrap();
    let target_obj = target.as_object().unwrap();
    let mut patch = HashMap::new();

    // Add/modify keys
    for (key, target_value) in target_obj.iter() {
        if let Some(source_value) = source_obj.get(key) {
            if source_value != target_value {
                if source_value.is_object() && target_value.is_object() {
                    // Recursively create patch for nested objects
                    let nested_patch = create_merge_patch(source_value, target_value);
                    if !nested_patch.as_object().unwrap().is_empty() {
                        patch.insert(key.clone(), nested_patch);
                    }
                } else {
                    patch.insert(key.clone(), target_value.clone());
                }
            }
        } else {
            // Key exists in target but not source - add it
            patch.insert(key.clone(), target_value.clone());
        }
    }

    // Remove keys that exist in source but not in target
    for key in source_obj.keys() {
        if !target_obj.contains_key(key) {
            patch.insert(key.clone(), Node::None);
        }
    }

    Node::Object(patch)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::json;

    #[test]
    fn test_merge_patch_simple() {
        let mut doc = json!({"a": 1, "b": 2});
        let patch = json!({"b": 3, "c": 4});
        merge_patch(&mut doc, &patch);

        assert_eq!(doc["a"], json!(1));
        assert_eq!(doc["b"], json!(3));
        assert_eq!(doc["c"], json!(4));
    }

    #[test]
    fn test_merge_patch_remove() {
        let mut doc = json!({"a": 1, "b": 2});
        let patch = json!({"b": null});
        merge_patch(&mut doc, &patch);

        assert_eq!(doc["a"], json!(1));
        assert!(doc["b"].is_null());
    }

    #[test]
    fn test_merge_patch_nested() {
        let mut doc = json!({
            "user": {
                "name": "Alice",
                "age": 30
            }
        });
        let patch = json!({
            "user": {
                "age": 31,
                "email": "alice@example.com"
            }
        });
        merge_patch(&mut doc, &patch);

        assert_eq!(doc["user"]["name"], json!("Alice"));
        assert_eq!(doc["user"]["age"], json!(31));
        assert_eq!(doc["user"]["email"], json!("alice@example.com"));
    }

    #[test]
    fn test_merge_patch_replace() {
        let mut doc = json!({"a": [1, 2, 3]});
        let patch = json!({"a": [4, 5]});
        merge_patch(&mut doc, &patch);

        assert_eq!(doc["a"], json!([4, 5]));
    }

    #[test]
    fn test_create_merge_patch() {
        let source = json!({"a": 1, "b": 2, "c": 3});
        let target = json!({"a": 1, "b": 4, "d": 5});
        let patch = create_merge_patch(&source, &target);

        // Patch should update b, add d, and remove c
        assert_eq!(patch["b"], json!(4));
        assert_eq!(patch["d"], json!(5));
        assert!(patch["c"].is_null());
        assert!(patch.get("a").is_none() || patch["a"].is_null());
    }

    #[test]
    fn test_create_merge_patch_nested() {
        let source = json!({
            "user": {
                "name": "Alice",
                "age": 30
            }
        });
        let target = json!({
            "user": {
                "name": "Alice",
                "age": 31
            }
        });
        let patch = create_merge_patch(&source, &target);

        assert_eq!(patch["user"]["age"], json!(31));
    }

    #[test]
    fn test_merge_patch_idempotent() {
        let mut doc = json!({"a": 1});
        let patch = json!({"b": 2});

        merge_patch(&mut doc, &patch);
        let first_result = doc.clone();

        merge_patch(&mut doc, &patch);
        assert_eq!(doc, first_result);
    }

    // ─── merge_patch: edge cases ──────────────────────────────────────────────

    #[test]
    fn test_merge_patch_with_empty_patch() {
        let mut doc = json!({"a": 1, "b": 2});
        let original = doc.clone();
        merge_patch(&mut doc, &json!({}));
        assert_eq!(doc, original);
    }

    #[test]
    fn test_merge_patch_onto_empty_target() {
        let mut doc = json!({});
        merge_patch(&mut doc, &json!({"x": 10, "y": 20}));
        assert_eq!(doc["x"], json!(10));
        assert_eq!(doc["y"], json!(20));
    }

    #[test]
    fn test_merge_patch_non_object_patch_replaces_target() {
        let mut doc = json!({"a": 1});
        merge_patch(&mut doc, &json!(42));
        assert_eq!(doc, json!(42));
    }

    #[test]
    fn test_merge_patch_non_object_patch_array_replaces_target() {
        let mut doc = json!({"a": 1});
        merge_patch(&mut doc, &json!([1, 2, 3]));
        assert_eq!(doc, json!([1, 2, 3]));
    }

    #[test]
    fn test_merge_patch_non_object_patch_bool_replaces_target() {
        let mut doc = json!({"a": 1});
        merge_patch(&mut doc, &json!(true));
        assert_eq!(doc, json!(true));
    }

    #[test]
    fn test_merge_patch_non_object_patch_null_replaces_target() {
        let mut doc = json!({"a": 1});
        merge_patch(&mut doc, &json!(null));
        assert!(doc.is_null());
    }

    #[test]
    fn test_merge_patch_target_non_object_becomes_object() {
        // target is a scalar; patch is object → target becomes object
        let mut doc = json!(42);
        merge_patch(&mut doc, &json!({"key": "value"}));
        assert!(doc.is_object());
        assert_eq!(doc["key"], json!("value"));
    }

    #[test]
    fn test_merge_patch_removes_multiple_keys_with_null() {
        let mut doc = json!({"a": 1, "b": 2, "c": 3});
        merge_patch(&mut doc, &json!({"a": null, "c": null}));
        assert!(doc.get("a").is_none() || doc["a"].is_null());
        assert!(doc.get("c").is_none() || doc["c"].is_null());
        assert_eq!(doc["b"], json!(2));
    }

    #[test]
    fn test_merge_patch_adds_new_nested_object() {
        let mut doc = json!({"a": 1});
        merge_patch(&mut doc, &json!({"nested": {"x": 10}}));
        assert_eq!(doc["nested"]["x"], json!(10));
    }

    #[test]
    fn test_merge_patch_replaces_nested_non_object_with_value() {
        let mut doc = json!({"a": {"b": 1}});
        // Patch replaces the nested object's b with a string
        merge_patch(&mut doc, &json!({"a": {"b": "hello"}}));
        assert_eq!(doc["a"]["b"], json!("hello"));
    }

    #[test]
    fn test_merge_patch_overwrites_nested_non_object_with_object() {
        // "a" is a scalar in doc; patch sets it to an object
        let mut doc = json!({"a": 5});
        merge_patch(&mut doc, &json!({"a": {"x": 1}}));
        assert_eq!(doc["a"]["x"], json!(1));
    }

    #[test]
    fn test_merge_patch_string_value() {
        let mut doc = json!({"greeting": "hello"});
        merge_patch(&mut doc, &json!({"greeting": "world"}));
        assert_eq!(doc["greeting"], json!("world"));
    }

    #[test]
    fn test_merge_patch_boolean_value() {
        let mut doc = json!({"active": false});
        merge_patch(&mut doc, &json!({"active": true}));
        assert_eq!(doc["active"], json!(true));
    }

    #[test]
    fn test_merge_patch_array_value_replaced_not_merged() {
        // RFC 7396: arrays are treated as atomic values – replaced, not merged
        let mut doc = json!({"list": [1, 2, 3]});
        merge_patch(&mut doc, &json!({"list": [4, 5]}));
        assert_eq!(doc["list"], json!([4, 5]));
    }

    #[test]
    fn test_merge_patch_deep_nested() {
        let mut doc = json!({"a": {"b": {"c": {"d": 1}}}});
        merge_patch(&mut doc, &json!({"a": {"b": {"c": {"d": 99}}}}));
        assert_eq!(doc["a"]["b"]["c"]["d"], json!(99));
    }

    // ─── create_merge_patch: edge cases ───────────────────────────────────────

    #[test]
    fn test_create_merge_patch_identical_docs_produces_empty_patch() {
        let source = json!({"a": 1, "b": "hello"});
        let target = json!({"a": 1, "b": "hello"});
        let patch = create_merge_patch(&source, &target);
        assert!(patch.is_object());
        assert!(patch.as_object().unwrap().is_empty());
    }

    #[test]
    fn test_create_merge_patch_target_adds_key() {
        let source = json!({"a": 1});
        let target = json!({"a": 1, "b": 2});
        let patch = create_merge_patch(&source, &target);
        assert_eq!(patch["b"], json!(2));
        assert!(patch.get("a").is_none());
    }

    #[test]
    fn test_create_merge_patch_source_has_extra_key() {
        let source = json!({"a": 1, "b": 2});
        let target = json!({"a": 1});
        let patch = create_merge_patch(&source, &target);
        // "b" should be nulled out
        assert!(patch["b"].is_null());
    }

    #[test]
    fn test_create_merge_patch_non_object_target_returns_target() {
        let source = json!({"a": 1});
        let target = json!(42);
        let patch = create_merge_patch(&source, &target);
        assert_eq!(patch, json!(42));
    }

    #[test]
    fn test_create_merge_patch_non_object_source_returns_target() {
        let source = json!(42);
        let target = json!({"a": 1});
        let patch = create_merge_patch(&source, &target);
        assert_eq!(patch, json!({"a": 1}));
    }

    #[test]
    fn test_create_merge_patch_value_type_change() {
        let source = json!({"a": 1});
        let target = json!({"a": "one"});
        let patch = create_merge_patch(&source, &target);
        assert_eq!(patch["a"], json!("one"));
    }

    #[test]
    fn test_create_merge_patch_nested_no_change_excluded() {
        let source = json!({"nested": {"x": 1, "y": 2}});
        let target = json!({"nested": {"x": 1, "y": 2}});
        let patch = create_merge_patch(&source, &target);
        // No changes – patch should be empty
        assert!(patch.as_object().unwrap().is_empty());
    }

    #[test]
    fn test_create_then_apply_roundtrip() {
        let source = json!({"a": 1, "b": 2, "c": {"d": 3}});
        let target = json!({"a": 1, "b": 99, "e": 5});

        let patch = create_merge_patch(&source, &target);
        let mut result = source.clone();
        merge_patch(&mut result, &patch);

        // After applying the patch, result["a"] and result["b"] and result["e"]
        // should match target; result["c"] should be gone (nulled)
        assert_eq!(result["a"], json!(1));
        assert_eq!(result["b"], json!(99));
        assert_eq!(result["e"], json!(5));
        // "c" was in source but not target – patch nulls it
        assert!(result.get("c").is_none() || result["c"].is_null());
    }
}