Function jsonpath_lib::select_as

source ·
pub fn select_as<T: DeserializeOwned>(
    json_str: &str,
    path: &str
) -> Result<Vec<T>, JsonPathError>
Expand description

It is the same to select function but it deserialize the the result as given type T.

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

use serde::{Deserialize, Serialize};

#[derive(Deserialize, PartialEq, Debug)]
struct Person {
    name: String,
    age: u8,
    phones: Vec<String>,
}

let ret: Vec<Person> = jsonpath::select_as(r#"
{
    "person":
        {
            "name": "Doe John",
            "age": 44,
            "phones": [
                "+44 1234567",
                "+44 2345678"
            ]
        }
}
"#, "$.person").unwrap();

let person = Person {
    name: "Doe John".to_string(),
    age: 44,
    phones: vec!["+44 1234567".to_string(), "+44 2345678".to_string()],
};

assert_eq!(ret[0], person);