[][src]Struct jsonpath_lib::Selector

pub struct Selector { /* fields omitted */ }

Utility. Functions like jsonpath::selector or jsonpath::compile are also implemented using this structure.

extern crate jsonpath_lib as jsonpath;
extern crate serde;
extern crate serde_json;

use serde::{Deserialize, Serialize};
use serde_json::Value;

use jsonpath::Selector;

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Person {
    name: String,
    age: u8,
    phone: String,
}

fn input_str() -> &'static str {
    r#"[
        {
            "name": "이름1",
            "age": 40,
            "phone": "+33 12341234"
        },
        {
            "name": "이름2",
            "age": 42,
            "phone": "++44 12341234"
        }
    ]"#
}

fn input_json() -> Value {
    serde_json::from_str(input_str()).unwrap()
}

fn input_person() -> Vec<Person> {
    serde_json::from_str(input_str()).unwrap()
}


let mut selector = Selector::new();

let result = selector
    .path("$..[?(@.age > 40)]").unwrap()
    .value_from_str(input_str()).unwrap()
    .select_to_value().unwrap();
assert_eq!(input_json()[1], result[0]);

let result = selector.select_to_str().unwrap();
assert_eq!(serde_json::to_string(&vec![&input_json()[1].clone()]).unwrap(), result);

let result = selector.select_to::<Vec<Person>>().unwrap();
assert_eq!(input_person()[1], result[0]);

let _ = selector.path("$..[?(@.age == 40)]");

let result = selector.select_to_value().unwrap();
assert_eq!(input_json()[0], result[0]);

let result = selector.select_to_str().unwrap();
assert_eq!(serde_json::to_string(&vec![&input_json()[0].clone()]).unwrap(), result);

let result = selector.select_to::<Vec<Person>>().unwrap();
assert_eq!(input_person()[0], result[0]);

Methods

impl Selector[src]

pub fn new() -> Self[src]

pub fn path(&mut self, path: &str) -> Result<&mut Self, String>[src]

pub fn value(&mut self, ref_value: RefValue) -> Result<&mut Self, String>[src]

pub fn value_from(
    &mut self,
    serializable: &impl Serialize
) -> Result<&mut Self, String>
[src]

pub fn value_from_str(&mut self, json_str: &str) -> Result<&mut Self, String>[src]

pub fn select_to_str(&mut self) -> Result<String, String>[src]

pub fn select_to_value(&mut self) -> Result<Value, String>[src]

pub fn select_to<T: DeserializeOwned>(&mut self) -> Result<T, String>[src]

Auto Trait Implementations

impl Send for Selector

impl Sync for Selector

Blanket Implementations

impl<T> From for T[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized
[src]