Data structures and logic for resolving, assigning, and deleting by JSON Pointers (RFC 6901).
JSON Pointers can be created either with a slice of strings or directly from a properly encoded string representing a JSON Pointer.
use jsonptr::Pointer;
use serde_json::json;
let mut data = json!({ "foo": { "bar": "baz" } });
let ptr = Pointer::from_static("/foo/bar");
let bar = ptr.resolve(&data).unwrap();
assert_eq!(bar, "baz");
use jsonptr::{ Pointer, resolve::Resolve };
use serde_json::json;
let mut data = json!({ "foo": { "bar": "baz" } });
let ptr = Pointer::from_static("/foo/bar");
let bar = data.resolve(&ptr).unwrap();
assert_eq!(bar, "baz");
use jsonptr::{Pointer, resolve::ResolveMut};
use serde_json::json;
let ptr = Pointer::from_static("/foo/bar");
let mut data = json!({ "foo": { "bar": "baz" }});
let mut bar = data.resolve_mut(&ptr).unwrap();
assert_eq!(bar, "baz");
use jsonptr::Pointer;
use serde_json::json;
let ptr = Pointer::from_static("/foo/bar");
let mut data = json!({});
let _previous = ptr.assign(&mut data, "qux").unwrap();
assert_eq!(data, json!({"foo": { "bar": "qux" }}))
use jsonptr::{assign::Assign, Pointer};
use serde_json::json;
let ptr = Pointer::from_static("/foo/bar");
let mut data = json!({});
let _previous = data.assign(&ptr, "qux").unwrap();
assert_eq!(data, json!({ "foo": { "bar": "qux" }}))
use jsonptr::Pointer;
use serde_json::json;
let mut data = json!({ "foo": { "bar": { "baz": "qux" } } });
let ptr = Pointer::from_static("/foo/bar/baz");
assert_eq!(ptr.delete(&mut data), Some("qux".into()));
assert_eq!(data, json!({ "foo": { "bar": {} } }));
// unresolved pointers return None
let mut data = json!({});
assert_eq!(ptr.delete(&mut data), None);
use jsonptr::{ Pointer, delete::Delete };
use serde_json::json;
let mut data = json!({ "foo": { "bar": { "baz": "qux" } } });
let ptr = Pointer::from_static("/foo/bar/baz");
assert_eq!(ptr.delete(&mut data), Some("qux".into()));
assert_eq!(data, json!({ "foo": { "bar": {} } }));
// replacing a root pointer replaces data with `Value::Null`
let ptr = Pointer::root();
let deleted = json!({ "foo": { "bar": {} } });
assert_eq!(data.delete(&ptr), Some(deleted));
assert!(data.is_null());
Flag | Enables |
---|---|
"std" |
implements std::error::Error for errors |
Contributions and feedback are always welcome and appreciated.
If you find an issue, please open a ticket or a pull request.
MIT or Apache 2.0.