json_parser_with_pest 1.1.0

A JSON parser created in Rust using Pest grammar.
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
use std::fs::File;
use pest::Parser;
use json_parser_with_pest::parser::{JSONParser, Rule};
use anyhow::{Context, Result};
use serde_json::{json, Value};
use tempfile::tempdir;

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::{json, Value};
    use std::fs::File;
    use std::io::Write;
    use pest::Parser;
    use tempfile::tempdir;
    /// Tests basic JSON parsing functionality.
    #[test]
    fn test_parse_simple_json() -> Result<()> {
        let json_data = r#"{ "name": "John", "age": 30, "city": "New York" }"#;
        let result = json_parser_with_pest::parse_json(json_data)
            .context("Failed to parse valid JSON")?;
        assert!(result.is_object(), "Expected parsed JSON to be an object.");
        Ok(())
    }

    /// Tests JSON schema validation functionality.
    #[test]
    fn test_validate_json_schema() -> Result<()> {
        let json_data = json!({ "name": "John", "age": 30 });
        let schema = json!({ "name": "", "age": 0 });
        json_parser_with_pest::validate_json_schema(&json_data, &schema)
            .context("Schema validation failed for valid JSON and schema")?;
        Ok(())
    }

    /// Tests partial JSON parsing by a specific key.
    #[test]
    fn test_parse_partial_json() -> Result<()> {
        let json_data = json!({ "name": "John", "age": 30, "city": "New York" });
        let result = json_parser_with_pest::parse_partial_json(&json_data, "city")
            .context("Failed to retrieve value for the key 'city'")?;
        assert_eq!(result, json!("New York"), "Incorrect value retrieved for 'city'.");
        Ok(())
    }

    /// Tests editing a JSON object by modifying a specific key's value.
    #[test]
    fn test_edit_json() -> Result<()> {
        let mut json_data = json!({ "name": "John", "age": 30 });
        json_parser_with_pest::edit_json(&mut json_data, "age", json!(35))
            .context("Failed to edit JSON key 'age'")?;
        assert_eq!(
            json_data["age"], 35,
            "Value of key 'age' did not update correctly."
        );
        Ok(())
    }

    /// Tests conversion of JSON to YAML format.
    #[test]
    fn test_convert_to_yaml() -> Result<()> {
        let json_data = json!({ "name": "John", "age": 30 });
        let result = json_parser_with_pest::convert_to_format(&json_data, "yaml")
            .context("Failed to convert JSON to YAML")?;
        assert!(
            result.contains("name: John"),
            "YAML conversion did not contain expected data."
        );
        Ok(())
    }

    /// Tests conversion of JSON to XML format.
    #[test]
    fn test_convert_to_xml() -> Result<()> {
        let json_data = json!({ "name": "John", "age": 30 });
        let result = json_parser_with_pest::convert_to_format(&json_data, "xml")
            .context("Failed to convert JSON to XML")?;
        assert!(
            result.contains("<name>John</name>"),
            "XML conversion did not contain expected data."
        );
        Ok(())
    }

    /// Tests handling of large JSON files by parsing them in chunks.
    #[test]
    fn test_handle_large_json() -> Result<()> {
        let dir = tempdir().context("Failed to create temporary directory")?;
        let file_path = dir.path().join("large_test.json");
        let mut file = File::create(&file_path).context("Failed to create temporary file")?;
        write!(
            file,
            "[{}]",
            (0..1000)
                .map(|_| "{\"key\": \"value\"}")
                .collect::<Vec<_>>()
                .join(",")
        )
            .context("Failed to write large JSON data to temporary file")?;

        json_parser_with_pest::handle_large_json(&file_path)
            .context("Failed to handle large JSON file")?;
        Ok(())
    }

    /// Tests minifying JSON by removing whitespace.
    #[test]
    fn test_minify_json() -> Result<()> {
        let json_data = json!({ "name": "John", "age": 30, "city": "New York" });
        let minified = json_parser_with_pest::parser::minify_json(&json_data);
        let parsed_minified: Value =
            serde_json::from_str(&minified).context("Failed to parse minified JSON")?;
        assert_eq!(
            parsed_minified, json_data,
            "Minified JSON does not match the original structure."
        );
        Ok(())
    }

    /// Tests retrieving a value by JSON path.
    #[test]
    fn test_get_by_path() -> Result<()> {
        let json_data = json!({
            "data": {
                "items": [
                    { "name": "Item1" },
                    { "name": "Item2" }
                ]
            }
        });
        let result =
            json_parser_with_pest::parser::get_by_path(&json_data, "data.items[1].name")
                .context("Failed to retrieve value by JSON path")?;
        assert_eq!(result, json!("Item2"), "Incorrect value retrieved by JSON path.");
        Ok(())
    }

    /// Tests searching JSON by a specific value, retrieving paths where the value is found.
    #[test]
    fn test_search_by_value() -> Result<()> {
        let json_data = json!({
            "name": "John",
            "details": {
                "age": 30,
                "location": "New York",
                "nickname": "John"
            }
        });
        let mut results = json_parser_with_pest::parser::search_by_value(&json_data, "John");
        results.sort();
        assert_eq!(
            results,
            vec!["details.nickname", "name"],
            "Failed to find correct paths for value 'John'."
        );
        Ok(())
    }

    /// Tests invalid JSON input parsing.
    #[test]
    fn test_invalid_json_parse() -> Result<()> {
        let invalid_json_data = "{ name: John, age: 30 }"; // Invalid JSON
        let result = json_parser_with_pest::parse_json(invalid_json_data);
        assert!(
            result.is_err(),
            "Expected failure for invalid JSON input, but parsing succeeded."
        );
        Ok(())
    }

    /// Tests recursive JSON object parsing.
    #[test]
    fn test_recursive_object_parsing() -> Result<()> {
        let input = r#"
            {
                "key1": {
                    "nested_key": {
                        "deep_nested_key": "value"
                    }
                }
            }
        "#;
        JSONParser::parse(Rule::json, input).context("Failed to parse nested JSON object")?;
        Ok(())
    }

    /// Tests edge cases for empty objects and arrays.
    #[test]
    fn test_empty_structures() -> Result<()> {
        let valid_inputs = vec!["{}", "[]"];
        for input in valid_inputs {
            JSONParser::parse(Rule::json, input)
                .with_context(|| format!("Failed to parse empty structure: {}", input))?;
        }
        Ok(())
    }
}
    /// Tests basic JSON parsing functionality.
    #[test]
    fn test_parse_simple_json() {
        let json_data = r#"{ "name": "John", "age": 30, "city": "New York" }"#;
        let result = json_parser_with_pest::parse_json(json_data);
        assert!(
            result.is_ok(),
            "Failed to parse valid JSON: {:?}",
            result.err()
        );
    }

    /// Tests JSON schema validation functionality.
    #[test]
    fn test_validate_json_schema() {
        let json_data = json!({ "name": "John", "age": 30 });
        let schema = json!({ "name": "", "age": 0 });
        let result = json_parser_with_pest::validate_json_schema(&json_data, &schema);
        assert!(
            result.is_ok(),
            "Schema validation failed for valid JSON and schema."
        );
    }

    /// Tests partial JSON parsing by a specific key.
    #[test]
    fn test_parse_partial_json() {
        let json_data = json!({ "name": "John", "age": 30, "city": "New York" });
        let result = json_parser_with_pest::parse_partial_json(&json_data, "city");
        assert_eq!(
            result,
            Some(json!("New York")),
            "Failed to retrieve the correct value for the key 'city'."
        );
    }

    /// Tests editing a JSON object by modifying a specific key's value.
    #[test]
    fn test_edit_json() {
        let mut json_data = json!({ "name": "John", "age": 30 });
        let result = json_parser_with_pest::edit_json(&mut json_data, "age", json!(35));
        assert!(result.is_ok(), "Failed to edit JSON key 'age'.");
        assert_eq!(
            json_data["age"], 35,
            "Value of key 'age' did not update correctly."
        );
    }

    /// Tests conversion of JSON to YAML format.
    #[test]
    fn test_convert_to_yaml() {
        let json_data = json!({ "name": "John", "age": 30 });
        let result = json_parser_with_pest::convert_to_format(&json_data, "yaml");
        assert!(
            result.is_ok(),
            "Failed to convert JSON to YAML: {:?}",
            result.err()
        );
        assert!(
            result.unwrap().contains("name: John"),
            "YAML conversion did not contain expected data."
        );
    }

    /// Tests conversion of JSON to XML format.
    #[test]
    fn test_convert_to_xml() {
        let json_data = json!({ "name": "John", "age": 30 });
        let result = json_parser_with_pest::convert_to_format(&json_data, "xml");
        assert!(
            result.is_ok(),
            "Failed to convert JSON to XML: {:?}",
            result.err()
        );
        assert!(
            result.unwrap().contains("<name>John</name>"),
            "XML conversion did not contain expected data."
        );
    }

/// Tests handling of large JSON files by parsing them in chunks.
#[test]
fn test_handle_large_json() {
    use std::io::Write; // Імпорт для запису у файл

    // Створення тимчасового каталогу
    let dir = tempdir().expect("Failed to create temp directory");
    let file_path = dir.path().join("large_test.json");

    // Створення тимчасового файлу
    let mut file = File::create(&file_path).expect("Failed to create temp file");

    // Запис великих даних у файл
    let json_content = format!(
        "[{}]",
        (0..1000)
            .map(|_| "{\"key\": \"value\"}")
            .collect::<Vec<_>>()
            .join(",")
    );
    file.write_all(json_content.as_bytes())
        .expect("Failed to write to file");

    // Тестування функції handle_large_json
    let result = json_parser_with_pest::handle_large_json(&file_path);
    assert!(
        result.is_ok(),
        "Failed to handle large JSON file: {:?}",
        result.err()
    );
}


    /// Tests minifying JSON by removing whitespace.
    #[test]
    fn test_minify_json() {
        let json_data = json!({ "name": "John", "age": 30, "city": "New York" });
        let minified = json_parser_with_pest::parser::minify_json(&json_data);

        let parsed_minified: Value = serde_json::from_str(&minified).unwrap();
        assert_eq!(
            parsed_minified, json_data,
            "Minified JSON does not match the original structure."
        );
    }


    /// Tests retrieving a value by JSON path.
    #[test]
    fn test_get_by_path() {
        let json_data = json!({
            "data": {
                "items": [
                    { "name": "Item1" },
                    { "name": "Item2" }
                ]
            }
        });
        let result =
            json_parser_with_pest::parser::get_by_path(&json_data, "data.items[1].name");
        assert_eq!(
            result,
            Some(json!("Item2")),
            "Failed to retrieve value by JSON path."
        );
    }

    /// Tests searching JSON by a specific value, retrieving paths where the value is found.
    #[test]
    fn test_search_by_value() {
        let json_data = json!({
            "name": "John",
            "details": {
                "age": 30,
                "location": "New York",
                "nickname": "John"
            }
        });
        let mut results =
            json_parser_with_pest::parser::search_by_value(&json_data, "John");
        results.sort();
        assert_eq!(
            results,
            vec!["details.nickname", "name"],
            "Failed to find correct paths for value 'John'."
        );
    }

    /// Tests handling of invalid JSON input.
    /// Tests handling of invalid JSON input.
    #[test]
    fn test_invalid_json_parse() {
        let invalid_json_data = "{ name: John, age: 30 }"; // Invalid JSON due to missing quotes
        let result = json_parser_with_pest::parse_json(invalid_json_data);

        // Перевіряємо, чи повертається помилка
        assert!(
            result.is_err(),
            "Expected failure for invalid JSON input, but parsing succeeded."
        );

        // Перевіряємо, чи повернена помилка є саме ParsingError
        if let Err(err) = result {
            assert!(
                matches!(err, json_parser_with_pest::ParserError::JsonParseError),
                "Expected JsonParseError, but got: {:?}",
                err
            );
        }
    }


    /// Tests validation of JSON schema with extra keys in the schema.
    #[test]
    fn test_invalid_json_schema() {
        let json_data = json!({ "name": "John", "age": 30 });
        let schema = json!({ "name": "", "age": 0, "extra_key": "" });
/*
        // Якщо дозволяємо надлишкові ключі в схемі
        let result = json_parser_with_pest::validate_json_schema(&json_data, &schema);
        assert!(
            result.is_ok(),
            "Expected schema validation to succeed, but it failed: {:?}",
            result.err()
        );
*/
        // Якщо надлишкові ключі мають викликати помилку

        let result = json_parser_with_pest::validate_json_schema(&json_data, &schema);
        assert!(
            result.is_err(),
            "Expected schema validation to fail due to extra key in schema."
        );

    }

    /// Tests parsing invalid key-value arrays.
    #[test]
    fn test_key_value_array_invalid() {
        let inputs = vec![
            r#"
            [
                { "key1": "value1" },
                { "key2" "value2" }  // Missing colon
            ]
            "#,
            r#"
            [
                { "key1": "value1" },
                "not_an_object"
            ]
            "#,
            r#"
            [
                { "key1": "value1", "key2": "value2" }
            "# // Missing closing bracket
        ];

        for input in inputs {
            let result = JSONParser::parse(Rule::key_value_array, input);
            assert!(
                result.is_err(),
                "Expected failure for invalid key-value array: {}. Result: {:?}",
                input,
                result
            );
        }
    }
/// Test parsing of JSON date in format YYYY-MM-DD.
#[test]
fn test_date_parsing() {
    let valid_dates = vec!["2023-11-15", "2000-01-01", "1999-12-31"];
    for date in valid_dates {
        let result = JSONParser::parse(Rule::date, date);
        assert!(
            result.is_ok(),
            "Failed to parse valid date: {}. Error: {:?}",
            date,
            result.err()
        );
    }

    let invalid_dates = vec!["2023-13-01", "202-11-15", "2023-11", "abcd-11-15"];
    for date in invalid_dates {
        let result = JSONParser::parse(Rule::date, date);
        assert!(
            result.is_err(),
            "Parsed invalid date: {}. Result: {:?}",
            date,
            result
        );
    }
}

/// Test parsing of JSON identifier.
#[test]
fn test_identifier_parsing() {
    let valid_identifiers = vec!["key", "key1", "_key", "key_name", "Key123"];
    for identifier in valid_identifiers {
        let result = JSONParser::parse(Rule::identifier, identifier);
        assert!(
            result.is_ok(),
            "Failed to parse valid identifier: {}. Error: {:?}",
            identifier,
            result.err()
        );
    }

    let invalid_identifiers = vec!["1key", "-key", "key@", "key name"];
    for identifier in invalid_identifiers {
        let result = JSONParser::parse(Rule::identifier, identifier);
        assert!(
            result.is_err(),
            "Parsed invalid identifier: {}. Result: {:?}",
            identifier,
            result
        );
    }
}

/// Test parsing of SemVer version strings.
#[test]
fn test_version_parsing() {
    let valid_versions = vec![
        "1.0.0",
        "2.1.3-alpha",
        "3.2.1-beta.1",
        "4.5.6+build.123",
        "0.1.0",
        "10.20.30-pre.4+metadata.12345",
    ];
    for version in valid_versions {
        let result = JSONParser::parse(Rule::version, version);
        assert!(
            result.is_ok(),
            "Failed to parse valid version: {}. Error: {:?}",
            version,
            result.err()
        );
    }

    let invalid_versions = vec![
        "1.0",
        "v1.0.0",
        "1.0.0-",
        "1.0.0+",
        "1.0.0-beta..1",
        "1.0.0-alpha+build@123",
    ];
    for version in invalid_versions {
        let result = JSONParser::parse(Rule::version, version);
        assert!(
            result.is_err(),
            "Parsed invalid version: {}. Result: {:?}",
            version,
            result
        );
    }
}

/// Test parsing of key-value arrays.
#[test]
fn test_key_value_array_parsing() {
    let valid_arrays = vec![
        r#"[{ "key1": "value1" }, { "key2": "value2" }]"#,
        r#"[{ "key": "value" }]"#,
        r#"[]"#,
    ];
    for array in valid_arrays {
        let result = JSONParser::parse(Rule::key_value_array, array);
        assert!(
            result.is_ok(),
            "Failed to parse valid key-value array: {}. Error: {:?}",
            array,
            result.err()
        );
    }

    let invalid_arrays = vec![
        r#"[{ "key1": "value1" }, { "key2" "value2" }]"#, // Missing colon
        r#"[{ "key": "value" }, "string"]"#,              // Non-object element
        r#"[{ "key": "value""#,                           // Missing closing bracket
    ];
    for array in invalid_arrays {
        let result = JSONParser::parse(Rule::key_value_array, array);
        assert!(
            result.is_err(),
            "Parsed invalid key-value array: {}. Result: {:?}",
            array,
            result
        );
    }
}

/// Test recursive JSON object parsing.
#[test]
fn test_recursive_object_parsing() {
    let input = r#"
        {
            "key1": {
                "nested_key": {
                    "deep_nested_key": "value"
                }
            }
        }
        "#;
    let result = JSONParser::parse(Rule::json, input);
    assert!(
        result.is_ok(),
        "Failed to parse nested JSON object. Error: {:?}",
        result.err()
    );
}

/// Test parsing of JSON arrays with mixed types.
#[test]
fn test_mixed_type_array_parsing() {
    let input = r#"[123, "string", true, null, { "key": "value" }, [1, 2, 3]]"#;
    let result = JSONParser::parse(Rule::json, input);
    assert!(
        result.is_ok(),
        "Failed to parse mixed-type array. Error: {:?}",
        result.err()
    );
}

/// Test edge cases for empty objects and arrays.
#[test]
fn test_empty_structures() {
    let valid_inputs = vec!["{}", "[]"];
    for input in valid_inputs {
        let result = JSONParser::parse(Rule::json, input);
        assert!(
            result.is_ok(),
            "Failed to parse empty structure: {}. Error: {:?}",
            input,
            result.err()
        );
    }
}