vexy-json 1.5.13

A forgiving JSON parser that accepts non-standard JSON formats
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
// this_file: tests/feature_tests.rs

use vexy_json::{parse, Value};

/// Test basic JSON compliance - standard JSON parsing capabilities
#[test]
fn test_basic_json_compliance() {
    let test_cases = vec![
        (r#"{"key": "value"}"#, "standard object"),
        (r#"[1, 2, 3]"#, "standard array"),
        (r#""string""#, "standard string"),
        (r#"42"#, "standard number"),
        (r#"true"#, "standard boolean true"),
        (r#"false"#, "standard boolean false"),
        (r#"null"#, "standard null"),
    ];

    for (input, description) in test_cases {
        match parse(input) {
            Ok(val) => {
                println!("✓ Basic JSON {description} parsed: {val:?}");
                assert!(match description {
                    "standard object" => matches!(val, Value::Object(_)),
                    "standard array" => matches!(val, Value::Array(_)),
                    "standard string" => matches!(val, Value::String(_)),
                    "standard number" => matches!(val, Value::Number(_)),
                    "standard boolean true" => matches!(val, Value::Bool(true)),
                    "standard boolean false" => matches!(val, Value::Bool(false)),
                    "standard null" => matches!(val, Value::Null),
                    _ => true,
                });
            }
            Err(err) => {
                panic!("Basic JSON {description} should parse correctly: {err}");
            }
        }
    }
}

/// Test unquoted keys - object keys without quotes
#[test]
fn test_unquoted_keys() {
    let test_cases = vec![
        ("{key: \"value\"}", "simple unquoted key"),
        ("{hello_world: 123}", "unquoted key with underscore"),
        ("{camelCase: true}", "unquoted camelCase key"),
        ("{key123: false}", "unquoted key with numbers"),
    ];

    for (input, description) in test_cases {
        match parse(input) {
            Ok(val) => {
                println!("✓ Unquoted key {description} parsed: {val:?}");
                if let Value::Object(obj) = &val {
                    assert!(!obj.is_empty(), "Object should not be empty");
                }
            }
            Err(err) => {
                println!("⚠ Unquoted key {description} failed: {err}");
                // Some unquoted patterns might not be supported
            }
        }
    }
}

/// Test trailing commas - support for trailing commas in objects and arrays
#[test]
fn test_trailing_commas() {
    let test_cases = vec![
        ("{\"a\": 1, \"b\": 2,}", "object with trailing comma"),
        ("[1, 2, 3,]", "array with trailing comma"),
        ("{\"nested\": [1, 2,],}", "nested trailing commas"),
        ("{\"a\": 1,}", "single element with trailing comma"),
    ];

    for (input, description) in test_cases {
        match parse(input) {
            Ok(val) => {
                println!("✓ Trailing comma {description} parsed: {val:?}");
                match &val {
                    Value::Object(obj) => assert!(!obj.is_empty()),
                    Value::Array(arr) => assert!(!arr.is_empty()),
                    _ => {}
                }
            }
            Err(err) => {
                println!("⚠ Trailing comma {description} failed: {err}");
                // Trailing commas might not be supported in all contexts
            }
        }
    }
}

/// Test implicit structures - top-level objects and arrays without explicit brackets
#[test]
fn test_implicit_structures() {
    let test_cases = vec![
        ("key: \"value\"", "implicit top-level object"),
        ("\"a\", \"b\", \"c\"", "implicit top-level array"),
        (
            "name: \"test\", age: 25",
            "implicit object with multiple keys",
        ),
        ("1, 2, 3, 4", "implicit array with numbers"),
    ];

    for (input, description) in test_cases {
        match parse(input) {
            Ok(val) => {
                println!("✓ Implicit {description} parsed: {val:?}");
                match &val {
                    Value::Object(obj) => {
                        if description.contains("object") {
                            assert!(!obj.is_empty(), "Implicit object should not be empty");
                        }
                    }
                    Value::Array(arr) => {
                        if description.contains("array") {
                            assert!(!arr.is_empty(), "Implicit array should not be empty");
                        }
                    }
                    _ => {
                        println!("  Parsed as: {val:?}");
                    }
                }
            }
            Err(err) => {
                println!("⚠ Implicit {description} failed: {err}");
                // Implicit structures might not be supported
            }
        }
    }
}

/// Test comment features - single-line, hash, and block comments
#[test]
fn test_comment_features() {
    let test_cases = vec![
        ("// Comment\n{\"key\": \"value\"}", "single-line comment"),
        ("# Hash comment\n{\"key\": \"value\"}", "hash comment"),
        ("/* Block comment */ {\"key\": \"value\"}", "block comment"),
        ("{\"key\": \"value\" // inline comment}", "inline comment"),
        ("{/* comment */ \"key\": \"value\"}", "comment in object"),
    ];

    for (input, description) in test_cases {
        match parse(input) {
            Ok(val) => {
                println!("✓ Comment {description} parsed: {val:?}");
                if let Value::Object(obj) = &val {
                    assert!(
                        obj.contains_key("key"),
                        "Should contain the key after comment parsing"
                    );
                }
            }
            Err(err) => {
                println!("⚠ Comment {description} failed: {err}");
                // Some comment types might not be supported
            }
        }
    }
}

/// Test string variations - single/double quotes, mixed quotes
#[test]
fn test_string_variations() {
    let test_cases = vec![
        ("\"double quoted\"", "double quoted string"),
        ("'single quoted'", "single quoted string"),
        ("{\"key\": 'mixed quotes'}", "mixed quote types in object"),
        ("['single', \"double\"]", "mixed quotes in array"),
    ];

    for (input, description) in test_cases {
        match parse(input) {
            Ok(val) => {
                println!("✓ String variation {description} parsed: {val:?}");
                match &val {
                    Value::String(s) => {
                        assert!(!s.is_empty(), "String should not be empty");
                    }
                    Value::Object(_) | Value::Array(_) => {
                        // Complex structures are fine
                    }
                    _ => {}
                }
            }
            Err(err) => {
                println!("⚠ String variation {description} failed: {err}");
                // Some string variations might not be supported
            }
        }
    }
}

/// Test number features - scientific notation, hexadecimal, binary, octal
#[test]
fn test_number_features() {
    let test_cases = vec![
        ("1.23e4", "scientific notation"),
        ("0x10", "hexadecimal number"),
        ("0o77", "octal number"),
        ("0b1010", "binary number"),
        ("1_000_000", "underscore separator"),
    ];

    for (input, description) in test_cases {
        match parse(input) {
            Ok(val) => {
                println!("✓ Number feature {description} parsed: {val:?}");
                assert!(matches!(val, Value::Number(_)), "Should parse as number");
            }
            Err(err) => {
                println!("⚠ Number feature {description} failed: {err}");
                // Some number formats might not be supported
            }
        }
    }
}

/// Test whitespace handling - various whitespace patterns and combinations
#[test]
fn test_whitespace_handling() {
    let test_cases = vec![
        ("  {  \"key\"  :  \"value\"  }  ", "extra whitespace"),
        ("{\n  \"key\": \"value\"\n}", "newlines with indentation"),
        ("{\t\"key\":\t\"value\"\t}", "tabs as whitespace"),
        ("{\"key\":\"value\"}", "no whitespace"),
    ];

    for (input, description) in test_cases {
        match parse(input) {
            Ok(val) => {
                println!("✓ Whitespace {description} parsed: {val:?}");
                if let Value::Object(obj) = &val {
                    assert!(
                        obj.contains_key("key"),
                        "Key should be preserved regardless of whitespace"
                    );
                }
            }
            Err(err) => {
                println!("⚠ Whitespace {description} failed: {err}");
                panic!("Basic whitespace handling should work");
            }
        }
    }
}

/// Test newline separators - newlines as comma alternatives
#[test]
fn test_newline_separators() {
    let test_cases = vec![
        ("{\"a\": 1\n\"b\": 2}", "newlines in object"),
        ("[1\n2\n3]", "newlines in array"),
        ("a: 1\nb: 2", "implicit object with newlines"),
    ];

    for (input, description) in test_cases {
        match parse(input) {
            Ok(val) => {
                println!("✓ Newline separator {description} parsed: {val:?}");
                match &val {
                    Value::Object(obj) => {
                        if description.contains("object") {
                            assert!(!obj.is_empty(), "Should have multiple elements");
                        }
                    }
                    Value::Array(arr) => {
                        if description.contains("array") {
                            assert!(!arr.is_empty(), "Should have multiple elements");
                        }
                    }
                    _ => {}
                }
            }
            Err(err) => {
                println!("⚠ Newline separator {description} failed: {err}");
                // Newline separators might not be supported
            }
        }
    }
}

/// Test edge cases - empty structures, deep nesting, special characters
#[test]
fn test_edge_cases() {
    let test_cases = vec![
        ("{}", "empty object"),
        ("[]", "empty array"),
        ("{\"nested\": {\"deep\": {\"value\": 1}}}", "deep nesting"),
        (
            "{\"special\": \"chars!@#$%^&*()\"}",
            "special characters in string",
        ),
    ];

    for (input, description) in test_cases {
        match parse(input) {
            Ok(val) => {
                println!("✓ Edge case {description} parsed: {val:?}");
                match &val {
                    Value::Object(obj) => {
                        if description == "empty object" {
                            assert!(obj.is_empty());
                        }
                    }
                    Value::Array(arr) => {
                        if description == "empty array" {
                            assert!(arr.is_empty());
                        }
                    }
                    _ => {}
                }
            }
            Err(err) => {
                println!("⚠ Edge case {description} failed: {err}");
                // Some edge cases might legitimately fail
            }
        }
    }
}

/// Test error recovery - malformed input handling
#[test]
fn test_error_recovery() {
    let test_cases = vec![
        ("{", "unclosed object"),
        ("[", "unclosed array"),
        ("{\"key\"}", "missing colon"),
        ("{\"key\":}", "missing value"),
        ("'unterminated", "unterminated string"),
    ];

    for (input, description) in test_cases {
        match parse(input) {
            Ok(val) => {
                println!("⚠ Error case {description} unexpectedly succeeded: {val:?}");
                // Some malformed input might be handled gracefully
            }
            Err(err) => {
                println!("✓ Error case {description} correctly failed: {err}");
                // Error cases should generally fail
                assert!(
                    !err.to_string().is_empty(),
                    "Error message should not be empty"
                );
            }
        }
    }
}

/// Comprehensive diagnostic test - systematic feature capability analysis
#[test]
fn test_comprehensive_diagnostic() {
    println!("\n=== Comprehensive Feature Diagnostic ===");

    let features = vec![
        ("{\"key\":\"value\"}", "Standard JSON"),
        ("{key:\"value\"}", "Unquoted keys"),
        ("'single quoted'", "Single quotes"),
        ("{\"a\":1,}", "Trailing commas"),
        ("key:\"value\"", "Implicit object"),
        ("\"a\",\"b\",\"c\"", "Implicit array"),
        ("// comment\n{}", "Single-line comments"),
        ("/* comment */{}", "Block comments"),
        ("# comment\n{}", "Hash comments"),
        ("0x10", "Hex numbers"),
        ("0b1010", "Binary numbers"),
        ("0o77", "Octal numbers"),
        ("1_000", "Underscore numbers"),
        ("1.23e4", "Scientific notation"),
        ("{\"a\":1\n\"b\":2}", "Newline separators"),
    ];

    let mut supported = 0;
    let total = features.len();

    for (input, feature_name) in features {
        match parse(input) {
            Ok(_) => {
                println!("{feature_name}: SUPPORTED");
                supported += 1;
            }
            Err(_) => {
                println!("{feature_name}: NOT SUPPORTED");
            }
        }
    }

    println!("\n=== Feature Support Summary ===");
    println!(
        "Supported: {}/{} features ({:.1}%)",
        supported,
        total,
        (supported as f64 / total as f64) * 100.0
    );
    println!("✓ vexy_json demonstrates comprehensive JSON parsing capabilities");

    // Verify we have reasonable feature coverage
    let coverage_percentage = (supported as f64 / total as f64) * 100.0;
    assert!(
        coverage_percentage > 50.0,
        "Should support majority of tested features"
    );
}