Function jsonpath_lib::compile

source ·
pub fn compile(
    path: &str
) -> impl FnMut(&Value) -> Result<Vec<&Value>, JsonPathError>
👎Deprecated since 0.2.5: Please use the PathCompiled::compile function instead. It will be removed from 0.4.1
Expand description

It is a high-order function. it compile a jsonpath and then returns a closure that has JSON as argument. if you need to reuse a jsonpath, it is good for performance.

extern crate jsonpath_lib as jsonpath;
#[macro_use] extern crate serde_json;

let mut first_firend = jsonpath::compile("$..friends[0]");

let json_obj = json!({
    "school": {
        "friends": [
            {"name": "친구1", "age": 20},
            {"name": "친구2", "age": 20}
        ]
    },
    "friends": [
        {"name": "친구3", "age": 30},
        {"name": "친구4"}
]});

let json = first_firend(&json_obj).unwrap();

assert_eq!(json, vec![
    &json!({"name": "친구3", "age": 30}),
    &json!({"name": "친구1", "age": 20})
]);