[][src]Function jsonpath_lib::selector_as

pub fn selector_as<T: DeserializeOwned>(
    json: &Value
) -> impl FnMut(&str) -> Result<T, String>

It returns highorder function that returns a function.

this function has a jsonpath as argument and return a serde::Deserialize. so you can use different JsonPath for one JsonObject.

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

use serde::{Deserialize, Serialize};

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

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Friend {
    name: String,
    age: Option<u8>,
}

let mut selector = jsonpath::selector_as::<Vec<Friend>>(&json_obj);

let json = selector("$..friends[0]").unwrap();
let ret = vec!(
    Friend { name: "친구3".to_string(), age: Some(30) },
    Friend { name: "친구1".to_string(), age: Some(20) }
);
assert_eq!(json, ret);

let json = selector("$..friends[1]").unwrap();
let ret = vec!(
    Friend { name: "친구4".to_string(), age: None },
    Friend { name: "친구2".to_string(), age: Some(20) }
);
assert_eq!(json, ret);