besok / jsonpath-rust

Support for json-path in Rust

Home Page:https://crates.io/crates/jsonpath-rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Regex parentheses escape

catalinstochita opened this issue · comments

Hello. It looks like escaping characters in regex is problematic

#[test]
fn regex_filter_test(){
    let json: Box<Value> = Box::new(json!({
        "author":"d(Rees)",
    }));

    let path: Box<JsonPathInst> = Box::from(
        JsonPathInst::from_str("$.[?(@.author ~= 'd(Rees)')]").expect("the path is correct"),
    );
    let finder = JsonPathFinder::new(json.clone(), path);
    assert_eq!(finder.find_slice(),vec![Slice(&json!({"author":"d(Rees)",}))]);
}

assertion left == right failed left: [NoValue] right: [Slice(Object {"author": String("d(Rees)")})]

If you try to escape with \ it won't work either
'd\(Rees\)' won't compile, expecting an atom
'd\\(Rees\\)' compiles but it will look for \ in value and () is still treated like a capture group
And if you don't escape the () is considered a capture group

After some testing, looks like we can change the char in .pest to accept it
From

char = _{
    !("\"" | "\\" | "\'") ~ ANY
    | "\\" ~ ("\"" | "\'" |  "\\" | "/" | "b" | "f" | "n" | "r" | "t")
    | "\\" ~ ("u" ~ ASCII_HEX_DIGIT{4})
}

To

char = _{
    !("\"" | "\\" | "\'") ~ ANY
    | "\\" ~ ("\"" | "\'" |  "\\" | "/" | "b" | "f" | "n" | "r" | "t" | "(" | ")")
    | "\\" ~ ("u" ~ ASCII_HEX_DIGIT{4})
}

Hi @catalinstochita ! Thanks for bringing this up.
Yeah, it seems to be a bug in the parser.