Function permissive_json_pointer::map_leaf_values
source · [−]pub fn map_leaf_values<'a>(
value: &mut Map<String, Value>,
selectors: impl IntoIterator<Item = &'a str>,
mapper: impl FnMut(&str, &mut Value)
)
Expand description
Map the selected leaf values of a json allowing you to update only the fields that were selected.
use serde_json::{Value, json};
use permissive_json_pointer::map_leaf_values;
let mut value: Value = json!({
"jean": {
"age": 8,
"race": {
"name": "bernese mountain",
"size": "80cm",
}
}
});
map_leaf_values(
value.as_object_mut().unwrap(),
["jean.race.name"],
|key, value| match (value, dbg!(key)) {
(Value::String(name), "jean.race.name") => *name = "patou".to_string(),
_ => unreachable!(),
},
);
assert_eq!(
value,
json!({
"jean": {
"age": 8,
"race": {
"name": "patou",
"size": "80cm",
}
}
})
);