Crates.io | json-ptr |
lib.rs | json-ptr |
version | 0.3.6 |
source | src |
created_at | 2021-03-31 23:37:33.287808 |
updated_at | 2021-04-05 22:04:20.789483 |
description | A crate for parsing and using JSON pointers, as specified in RFC 6901. |
homepage | https://github.com/JeremyRubin/json-pointer |
repository | https://github.com/JeremyRubin/json-pointer |
max_upload_size | |
id | 376390 |
size | 20,125 |
A crate for parsing and using JSON pointers, as specified in RFC
6901. Unlike the pointer
method
built into serde_json
, this handles both validating JSON Pointers before
use and the URI Fragment Identifier Representation.
JSON pointers can be created with a literal [&str]
, or parsed from a String
.
let from_strs = JsonPointer::new([
"foo",
"bar",
]);
let parsed = "/foo/bar".parse::<JsonPointer<_, _>>().unwrap();
assert_eq!(from_strs.to_string(), parsed.to_string());
}
The JsonPointer
type provides .get()
and .get_mut()
, to get references
and mutable references to the appropriate value, respectively.
let ptr = "/foo/bar".parse::<JsonPointer<_, _>>().unwrap();
let document = json!({
"foo": {
"bar": 0,
"baz": 1,
},
"quux": "xyzzy"
});
let indexed = ptr.get(&document).unwrap();
assert_eq!(indexed, &json!(0));
JSON Pointers can be embedded in the fragment portion of a URI. This is the
reason why most JSON pointer libraries require a #
character at the beginning
of a JSON pointer. The crate will detect the leading #
as an indicator to
parse in URI Fragment Identifier Representation. Note that this means that this
crate does not support parsing full URIs.
let str_ptr = "/f%o".parse::<JsonPointer<_, _>>().unwrap();
let uri_ptr = "#/f%25o".parse::<JsonPointer<_, _>>().unwrap();
assert_eq!(str_ptr, uri_ptr);