mirror of
https://github.com/fluencelabs/jsonpath
synced 2025-03-27 12:21:03 +00:00
65 lines
1.6 KiB
Rust
65 lines
1.6 KiB
Rust
extern crate jsonpath_lib as jsonpath;
|
|
#[macro_use]
|
|
extern crate serde_json;
|
|
|
|
use common::{read_json, setup};
|
|
use jsonpath::{SelectorMut, Selector};
|
|
use serde_json::Value;
|
|
|
|
mod common;
|
|
|
|
#[test]
|
|
fn selector_mut() {
|
|
setup();
|
|
|
|
let mut selector_mut = SelectorMut::new();
|
|
|
|
let mut nums = Vec::new();
|
|
let result = selector_mut
|
|
.str_path(r#"$.store..price"#).unwrap()
|
|
.value(read_json("./benches/example.json"))
|
|
.replace_with(&mut |v| {
|
|
match v {
|
|
Value::Number(n) => {
|
|
nums.push(n.as_f64().unwrap());
|
|
}
|
|
_ => {}
|
|
}
|
|
Value::String("a".to_string())
|
|
}).unwrap()
|
|
.take().unwrap();
|
|
|
|
assert_eq!(nums, vec![8.95_f64, 12.99_f64, 8.99_f64, 22.99_f64, 19.95_f64]);
|
|
|
|
let mut selector = Selector::new();
|
|
let result = selector.str_path(r#"$.store..price"#).unwrap()
|
|
.value(&result)
|
|
.select().unwrap();
|
|
|
|
assert_eq!(vec![&json!("a"), &json!("a"), &json!("a"), &json!("a"), &json!("a")], result);
|
|
}
|
|
|
|
#[test]
|
|
fn selector_mut_delete_array() {
|
|
setup();
|
|
|
|
let json = serde_json::from_str(r#"{
|
|
"school": {
|
|
"friends": [
|
|
{"name": "친구1", "age": 20},
|
|
{"name": "친구2", "age": 20}
|
|
]
|
|
},
|
|
"friends": [
|
|
{"name": "친구3", "age": 30},
|
|
{"name": "친구4"}
|
|
]
|
|
}"#).unwrap();
|
|
|
|
let json1 = jsonpath::delete(json, "$..friends[0]").unwrap();
|
|
|
|
println!("{:?}", json1);
|
|
|
|
let mut json2 = jsonpath::delete(json1, "$..friends[1]").unwrap();
|
|
|
|
} |