json_repair/
sanitize.rs

1crate::ix!();
2
3pub fn sanitize_json_str(s: &str) -> String {
4    s.chars()
5        .filter(|c| {
6            // Allow standard whitespace characters and visible characters
7            !c.is_control() || *c == '\n' || *c == '\r' || *c == '\t'
8        })
9        .collect()
10}
11
12pub fn assert_expected_matches_output_result<X: Clone + Debug + PartialEq>(
13    input:    &str, 
14    output:   &Result<X,JsonRepairError>, 
15    expected: &X
16
17) {
18    if output != &Ok(expected.clone()) {
19        println!("input: {:#?}", input);
20        println!("output: {:#?}", output);
21        println!("expected: {:#?}", expected);
22        assert_eq!(output,&Ok(expected.clone()));
23    }
24}
25
26pub fn assert_expected_value_matches_output_result(
27    input:    &str, 
28    output:   &Result<String,JsonRepairError>, 
29    expected: &Value
30
31) {
32    assert!(output.is_ok());
33
34    let expected = expected.to_string();
35    let output   = output.as_ref().unwrap();
36
37    if output != &expected {
38        println!("input: {:#?}", input);
39        println!("output: {:#?}", output);
40        println!("expected: {:#?}", expected);
41        assert_eq!(output,&expected);
42    }
43}
44
45
46pub fn skip_whitespace(
47    chars:    &mut Peekable<Chars>, 
48    repaired: &mut String
49) {
50    while let Some(&nc) = chars.peek() {
51        if nc.is_whitespace() {
52            repaired.push(chars.next().unwrap());
53        } else {
54            break;
55        }
56    }
57}
58
59pub fn is_valid_json_value_start(c: char) -> bool {
60    c == '"' || c == '\'' || c == '{' || c == '[' || c.is_digit(10) || c == '-' || matches!(c, 't' | 'f' | 'n')
61}