-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mutable NodeList or modifying query results #66
Comments
Hey, thanks for raising the issue. Currently, yielding mutable references through the query is not supported by the crate. I will keep this issue open to track and discuss that possibility, but I can't make any guarantees on whether/when it can be implemented. For now, the closest option is to clone the references of the node list, however, that may not be desirable. |
Thank you! I'll try out this approach 😉 |
Mutable
|
After some digging, I am not sure that this is possible without the use of unsafe, nor that it can be done safely, as it is in the standard library. There is a good discussion about this here. One of the key points in that discussion that I am concerned about, when writing our own mutable iterator:
JSONPath allows for queries that yield the same thing more than once, i.e., that alias. |
@robert-mac-wbt - the recent release of The new Here is an example, use serde_json::json;
use serde_json_path::JsonPath;
fn main() {
// Some JSON data:
let mut v = json!({
"foo": {
"bar": {
"baz": 1,
},
"baz": 2,
},
"baz": 3,
});
// locate a set of nodes using your JSONPath query:
let path = JsonPath::parse("$..baz").expect("valid JSONPath");
// get their locations, as JSON Pointer strings:
let locations = path
.query_located(&v)
.locations()
.map(|l| l.to_json_pointer())
.collect::<Vec<_>>();
// iterate over each location, and get a mutable ref to the node in the original object:
for l in locations {
if let Some(n) = v.pointer_mut(&l) {
*n = false.into(); // <-- update your nodes in-place here
}
}
println!("{}", serde_json::to_string_pretty(&v).unwrap());
} This will print the following: {
"baz": false,
"foo": {
"bar": {
"baz": false
},
"baz": false
}
} |
Thank you! |
Hello 👋
I wanted to ask if it's possible to do something following with this crate.
I want to be able to run multiple queries on some json in order to eventually find a node that I'd like to modify/remove.
In the following example, I'm trying to remove
genre
of the second book in the list.Do you have any suggestion how to achieve this?
Even after changing
second_node
to an object, I'm getting an error saying that it's not mutable.Or maybe adding mutable nodelists could help here? Currently after querying
&Value
are returned.Thanks!
The text was updated successfully, but these errors were encountered: