[][src]Function jsonpath_lib::compile

pub fn compile<'a>(
    path: &'a str
) -> impl FnMut(&Value) -> Result<Value, String> + 'a

It is a highorder function that compile a JsonPath then returns a function.

this return function can be reused for different JsonObjects.

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

let mut template = 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 = template(&json_obj).unwrap();
let ret = json!([
    {"name": "친구3", "age": 30},
    {"name": "친구1", "age": 20}
]);
assert_eq!(json, ret);