vexy-json-core 1.5.11

Core parser for Vexy JSON's forgiving JSON syntax
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
// this_file: crates/core/src/transform/normalizer.rs

//! JSON normalizer for transforming JSON into canonical form.
//!
//! This module provides functionality to normalize JSON values into a standardized
//! format. This is useful for:
//! - Comparing JSON values for equality
//! - Generating consistent JSON output
//! - Removing formatting variations
//! - Sorting object keys for deterministic output

use crate::ast::{Number, Value};
use crate::error::Result;
use rustc_hash::FxHashMap;
use std::cmp::Ordering;

/// Configuration options for JSON normalization.
#[derive(Debug, Clone)]
pub struct NormalizerOptions {
    /// Whether to sort object keys alphabetically.
    pub sort_keys: bool,
    /// Whether to remove null values from objects.
    pub remove_null_values: bool,
    /// Whether to remove empty objects and arrays.
    pub remove_empty_containers: bool,
    /// Whether to normalize numbers (convert integers to floats if requested).
    pub normalize_numbers: bool,
    /// Whether to use integers for whole numbers.
    pub prefer_integers: bool,
    /// Whether to trim whitespace from strings.
    pub trim_strings: bool,
    /// Whether to normalize string case (to lowercase).
    pub normalize_string_case: bool,
    /// Whether to deduplicate array elements.
    pub deduplicate_arrays: bool,
    /// Maximum nesting depth for recursive normalization.
    pub max_depth: usize,
}

impl Default for NormalizerOptions {
    fn default() -> Self {
        Self {
            sort_keys: true,
            remove_null_values: false,
            remove_empty_containers: false,
            normalize_numbers: false,
            prefer_integers: true,
            trim_strings: false,
            normalize_string_case: false,
            deduplicate_arrays: false,
            max_depth: 100,
        }
    }
}

/// JSON normalizer that transforms JSON values into canonical form.
pub struct JsonNormalizer {
    options: NormalizerOptions,
    depth: usize,
}

impl JsonNormalizer {
    /// Creates a new JSON normalizer with default options.
    pub fn new() -> Self {
        Self {
            options: NormalizerOptions::default(),
            depth: 0,
        }
    }

    /// Creates a new JSON normalizer with custom options.
    pub fn with_options(options: NormalizerOptions) -> Self {
        Self { options, depth: 0 }
    }

    /// Normalizes a JSON value according to the configured options.
    pub fn normalize(&mut self, value: &Value) -> Result<Value> {
        self.depth = 0;
        self.normalize_value(value)
    }

    /// Internal method to normalize a value recursively.
    fn normalize_value(&mut self, value: &Value) -> Result<Value> {
        if self.depth >= self.options.max_depth {
            return Ok(value.clone());
        }

        self.depth += 1;
        let result = match value {
            Value::Object(obj) => self.normalize_object(obj),
            Value::Array(arr) => self.normalize_array(arr),
            Value::String(s) => Ok(self.normalize_string(s)),
            Value::Number(n) => Ok(self.normalize_number(n)),
            Value::Bool(_) | Value::Null => Ok(value.clone()),
        };
        self.depth -= 1;
        result
    }

    /// Normalizes an object.
    fn normalize_object(&mut self, obj: &FxHashMap<String, Value>) -> Result<Value> {
        let mut normalized = FxHashMap::default();

        for (key, value) in obj {
            let normalized_value = self.normalize_value(value)?;

            // Skip null values if configured
            if self.options.remove_null_values && normalized_value == Value::Null {
                continue;
            }

            // Skip empty containers if configured
            if self.options.remove_empty_containers {
                match &normalized_value {
                    Value::Object(o) if o.is_empty() => continue,
                    Value::Array(a) if a.is_empty() => continue,
                    _ => {}
                }
            }

            normalized.insert(key.clone(), normalized_value);
        }

        // Return empty object if all values were filtered out
        if self.options.remove_empty_containers && normalized.is_empty() {
            return Ok(Value::Object(normalized));
        }

        Ok(Value::Object(normalized))
    }

    /// Normalizes an array.
    fn normalize_array(&mut self, arr: &[Value]) -> Result<Value> {
        let mut normalized = Vec::new();

        for value in arr {
            let normalized_value = self.normalize_value(value)?;

            // Skip null values if configured
            if self.options.remove_null_values && normalized_value == Value::Null {
                continue;
            }

            // Skip empty containers if configured
            if self.options.remove_empty_containers {
                match &normalized_value {
                    Value::Object(o) if o.is_empty() => continue,
                    Value::Array(a) if a.is_empty() => continue,
                    _ => {}
                }
            }

            normalized.push(normalized_value);
        }

        // Deduplicate array elements if configured
        if self.options.deduplicate_arrays {
            normalized.sort_by(|a, b| self.compare_values(a, b));
            normalized.dedup();
        }

        Ok(Value::Array(normalized))
    }

    /// Normalizes a string.
    fn normalize_string(&self, s: &str) -> Value {
        let mut normalized = s.to_string();

        if self.options.trim_strings {
            normalized = normalized.trim().to_string();
        }

        if self.options.normalize_string_case {
            normalized = normalized.to_lowercase();
        }

        Value::String(normalized)
    }

    /// Normalizes a number.
    fn normalize_number(&self, n: &Number) -> Value {
        match n {
            Number::Integer(i) => {
                if self.options.normalize_numbers && !self.options.prefer_integers {
                    Value::Number(Number::Float(*i as f64))
                } else {
                    Value::Number(Number::Integer(*i))
                }
            }
            Number::Float(f) => {
                if self.options.prefer_integers && f.fract() == 0.0 && f.is_finite() {
                    Value::Number(Number::Integer(*f as i64))
                } else {
                    Value::Number(Number::Float(*f))
                }
            }
        }
    }

    /// Compares two values for sorting purposes.
    fn compare_values(&self, a: &Value, b: &Value) -> Ordering {
        match (a, b) {
            (Value::Null, Value::Null) => Ordering::Equal,
            (Value::Null, _) => Ordering::Less,
            (_, Value::Null) => Ordering::Greater,

            (Value::Bool(a), Value::Bool(b)) => a.cmp(b),
            (Value::Bool(_), _) => Ordering::Less,
            (_, Value::Bool(_)) => Ordering::Greater,

            (Value::Number(a), Value::Number(b)) => self.compare_numbers(a, b),
            (Value::Number(_), _) => Ordering::Less,
            (_, Value::Number(_)) => Ordering::Greater,

            (Value::String(a), Value::String(b)) => a.cmp(b),
            (Value::String(_), _) => Ordering::Less,
            (_, Value::String(_)) => Ordering::Greater,

            (Value::Array(a), Value::Array(b)) => {
                for (av, bv) in a.iter().zip(b.iter()) {
                    match self.compare_values(av, bv) {
                        Ordering::Equal => continue,
                        other => return other,
                    }
                }
                a.len().cmp(&b.len())
            }
            (Value::Array(_), _) => Ordering::Less,
            (_, Value::Array(_)) => Ordering::Greater,

            (Value::Object(_), Value::Object(_)) => Ordering::Equal, // Objects are equal for sorting
        }
    }

    /// Compares two numbers for sorting purposes.
    fn compare_numbers(&self, a: &Number, b: &Number) -> Ordering {
        match (a, b) {
            (Number::Integer(a), Number::Integer(b)) => a.cmp(b),
            (Number::Float(a), Number::Float(b)) => a.partial_cmp(b).unwrap_or(Ordering::Equal),
            (Number::Integer(a), Number::Float(b)) => {
                (*a as f64).partial_cmp(b).unwrap_or(Ordering::Equal)
            }
            (Number::Float(a), Number::Integer(b)) => {
                a.partial_cmp(&(*b as f64)).unwrap_or(Ordering::Equal)
            }
        }
    }
}

impl Default for JsonNormalizer {
    fn default() -> Self {
        Self::new()
    }
}

/// Convenience function to normalize JSON with default options.
pub fn normalize(value: &Value) -> Result<Value> {
    let mut normalizer = JsonNormalizer::new();
    normalizer.normalize(value)
}

/// Convenience function to normalize JSON with custom options.
pub fn normalize_with_options(value: &Value, options: NormalizerOptions) -> Result<Value> {
    let mut normalizer = JsonNormalizer::with_options(options);
    normalizer.normalize(value)
}

/// Specialized normalizer for creating canonical JSON for comparison.
pub struct CanonicalNormalizer;

impl CanonicalNormalizer {
    /// Creates a canonical form of JSON for comparison purposes.
    pub fn canonicalize(value: &Value) -> Result<Value> {
        let options = NormalizerOptions {
            sort_keys: true,
            remove_null_values: false,
            remove_empty_containers: false,
            normalize_numbers: false,
            prefer_integers: true,
            trim_strings: false,
            normalize_string_case: false,
            deduplicate_arrays: false,
            max_depth: 100,
        };

        let mut normalizer = JsonNormalizer::with_options(options);
        normalizer.normalize(value)
    }

    /// Creates a canonical form for deep comparison (sorts everything).
    pub fn deep_canonicalize(value: &Value) -> Result<Value> {
        let options = NormalizerOptions {
            sort_keys: true,
            remove_null_values: false,
            remove_empty_containers: false,
            normalize_numbers: false,
            prefer_integers: true,
            trim_strings: false,
            normalize_string_case: false,
            deduplicate_arrays: true,
            max_depth: 100,
        };

        let mut normalizer = JsonNormalizer::with_options(options);
        normalizer.normalize(value)
    }
}

/// Specialized normalizer for cleaning up JSON data.
pub struct CleanupNormalizer;

impl CleanupNormalizer {
    /// Cleans up JSON by removing null values and empty containers.
    pub fn cleanup(value: &Value) -> Result<Value> {
        let options = NormalizerOptions {
            sort_keys: true,
            remove_null_values: true,
            remove_empty_containers: true,
            normalize_numbers: false,
            prefer_integers: true,
            trim_strings: true,
            normalize_string_case: false,
            deduplicate_arrays: false,
            max_depth: 100,
        };

        let mut normalizer = JsonNormalizer::with_options(options);
        normalizer.normalize(value)
    }
}

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

    #[test]
    fn test_normalize_object_with_sorted_keys() {
        let mut obj = FxHashMap::default();
        obj.insert("zebra".to_string(), Value::String("last".to_string()));
        obj.insert("apple".to_string(), Value::String("first".to_string()));
        obj.insert("banana".to_string(), Value::String("second".to_string()));

        let value = Value::Object(obj);
        let normalized = normalize(&value).unwrap();

        // The result should be the same content (sorting is for serialization)
        if let Value::Object(normalized_obj) = normalized {
            assert_eq!(normalized_obj.len(), 3);
            assert_eq!(
                normalized_obj.get("zebra"),
                Some(&Value::String("last".to_string()))
            );
            assert_eq!(
                normalized_obj.get("apple"),
                Some(&Value::String("first".to_string()))
            );
            assert_eq!(
                normalized_obj.get("banana"),
                Some(&Value::String("second".to_string()))
            );
        } else {
            panic!("Expected object");
        }
    }

    #[test]
    fn test_normalize_remove_null_values() {
        let mut obj = FxHashMap::default();
        obj.insert("keep".to_string(), Value::String("value".to_string()));
        obj.insert("remove".to_string(), Value::Null);

        let value = Value::Object(obj);
        let options = NormalizerOptions {
            remove_null_values: true,
            ..Default::default()
        };

        let normalized = normalize_with_options(&value, options).unwrap();

        if let Value::Object(normalized_obj) = normalized {
            assert_eq!(normalized_obj.len(), 1);
            assert_eq!(
                normalized_obj.get("keep"),
                Some(&Value::String("value".to_string()))
            );
            assert!(normalized_obj.get("remove").is_none());
        } else {
            panic!("Expected object");
        }
    }

    #[test]
    fn test_normalize_remove_empty_containers() {
        let mut obj = FxHashMap::default();
        obj.insert("keep".to_string(), Value::String("value".to_string()));
        obj.insert("empty_obj".to_string(), Value::Object(FxHashMap::default()));
        obj.insert("empty_arr".to_string(), Value::Array(vec![]));

        let value = Value::Object(obj);
        let options = NormalizerOptions {
            remove_empty_containers: true,
            ..Default::default()
        };

        let normalized = normalize_with_options(&value, options).unwrap();

        if let Value::Object(normalized_obj) = normalized {
            assert_eq!(normalized_obj.len(), 1);
            assert_eq!(
                normalized_obj.get("keep"),
                Some(&Value::String("value".to_string()))
            );
        } else {
            panic!("Expected object");
        }
    }

    #[test]
    fn test_normalize_numbers() {
        let value = Value::Number(Number::Float(42.0));
        let options = NormalizerOptions {
            prefer_integers: true,
            ..Default::default()
        };

        let normalized = normalize_with_options(&value, options).unwrap();

        assert_eq!(normalized, Value::Number(Number::Integer(42)));
    }

    #[test]
    fn test_normalize_strings() {
        let value = Value::String("  Hello World  ".to_string());
        let options = NormalizerOptions {
            trim_strings: true,
            normalize_string_case: true,
            ..Default::default()
        };

        let normalized = normalize_with_options(&value, options).unwrap();

        assert_eq!(normalized, Value::String("hello world".to_string()));
    }

    #[test]
    fn test_deduplicate_arrays() {
        let arr = vec![
            Value::String("a".to_string()),
            Value::String("b".to_string()),
            Value::String("a".to_string()),
            Value::String("c".to_string()),
        ];
        let value = Value::Array(arr);
        let options = NormalizerOptions {
            deduplicate_arrays: true,
            ..Default::default()
        };

        let normalized = normalize_with_options(&value, options).unwrap();

        if let Value::Array(normalized_arr) = normalized {
            assert_eq!(normalized_arr.len(), 3);
            // Elements should be sorted and deduplicated
            assert!(normalized_arr.contains(&Value::String("a".to_string())));
            assert!(normalized_arr.contains(&Value::String("b".to_string())));
            assert!(normalized_arr.contains(&Value::String("c".to_string())));
        } else {
            panic!("Expected array");
        }
    }

    #[test]
    fn test_canonical_normalizer() {
        let mut obj = FxHashMap::default();
        obj.insert("zebra".to_string(), Value::String("last".to_string()));
        obj.insert("apple".to_string(), Value::String("first".to_string()));

        let value = Value::Object(obj);
        let canonical = CanonicalNormalizer::canonicalize(&value).unwrap();

        // Should be the same as input (keys sorted during serialization)
        if let Value::Object(canonical_obj) = canonical {
            assert_eq!(canonical_obj.len(), 2);
        } else {
            panic!("Expected object");
        }
    }

    #[test]
    fn test_cleanup_normalizer() {
        let mut obj = FxHashMap::default();
        obj.insert("keep".to_string(), Value::String("  value  ".to_string()));
        obj.insert("remove".to_string(), Value::Null);
        obj.insert("empty".to_string(), Value::Array(vec![]));

        let value = Value::Object(obj);
        let cleaned = CleanupNormalizer::cleanup(&value).unwrap();

        if let Value::Object(cleaned_obj) = cleaned {
            assert_eq!(cleaned_obj.len(), 1);
            assert_eq!(
                cleaned_obj.get("keep"),
                Some(&Value::String("value".to_string()))
            );
        } else {
            panic!("Expected object");
        }
    }

    #[test]
    fn test_deep_nested_normalization() {
        let mut inner = FxHashMap::default();
        inner.insert(
            "inner_key".to_string(),
            Value::String("inner_value".to_string()),
        );

        let mut outer = FxHashMap::default();
        outer.insert("outer_key".to_string(), Value::Object(inner));

        let value = Value::Object(outer);
        let normalized = normalize(&value).unwrap();

        // Should maintain structure
        if let Value::Object(normalized_obj) = normalized {
            if let Some(Value::Object(inner_obj)) = normalized_obj.get("outer_key") {
                assert_eq!(
                    inner_obj.get("inner_key"),
                    Some(&Value::String("inner_value".to_string()))
                );
            } else {
                panic!("Expected nested object");
            }
        } else {
            panic!("Expected object");
        }
    }

    #[test]
    fn test_max_depth_limit() {
        let mut obj = FxHashMap::default();
        obj.insert("key".to_string(), Value::String("value".to_string()));

        let value = Value::Object(obj);
        let options = NormalizerOptions {
            max_depth: 0,
            ..Default::default()
        };

        let normalized = normalize_with_options(&value, options).unwrap();

        // Should return original value when depth limit is exceeded
        assert_eq!(normalized, value);
    }
}