Trait jsonpath_rust::JsonPathQuery

source ·
pub trait JsonPathQuery {
    // Required method
    fn path(self, query: &str) -> Result<Value, String>;
}
Expand description

the trait allows to mix the method path to the value of Value and thus the using can be shortened to the following one:

§Examples:

use std::str::FromStr;
use serde_json::{json, Value};
use jsonpath_rust::jp_v;
use jsonpath_rust::{find_slice, JsonPathQuery, JsonPathInst, JsonPathValue};

fn test() -> Result<(), Box<dyn std::error::Error>> {
    let json: Value = serde_json::from_str("{}")?;
    let v = json.path("$..book[?(@.author size 10)].title")?;
    assert_eq!(v, json!([]));

    let json: Value = serde_json::from_str("{}")?;
    let path = json.path("$..book[?(@.author size 10)].title")?;

    assert_eq!(path, json!(["Sayings of the Century"]));

    let json: Value = serde_json::from_str("{}")?;
    let path: JsonPathInst = JsonPathInst::from_str("$..book[?(@.author size 10)].title")?;

    let v = find_slice(&path, &json);
    let js = json!("Sayings of the Century");
    assert_eq!(v, jp_v![&js;"",]);
}

#Note: the result is going to be cloned and therefore it can be significant for the huge queries

Required Methods§

source

fn path(self, query: &str) -> Result<Value, String>

Implementations on Foreign Types§

source§

impl JsonPathQuery for Value

source§

fn path(self, query: &str) -> Result<Value, String>

Implementors§