https://github.com/remexre/json-pointer
A crate for parsing and using JSON pointers, as specified in RFC 6901.
https://github.com/remexre/json-pointer
json-pointer rfc6901
Last synced: 6 months ago
JSON representation
A crate for parsing and using JSON pointers, as specified in RFC 6901.
- Host: GitHub
- URL: https://github.com/remexre/json-pointer
- Owner: remexre
- License: mit
- Created: 2017-05-26T20:53:02.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-12-22T14:43:27.000Z (about 2 years ago)
- Last Synced: 2025-04-12T07:44:01.231Z (11 months ago)
- Topics: json-pointer, rfc6901
- Language: Rust
- Size: 32.2 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# json-pointer
A crate for parsing and using JSON pointers, as specified in [RFC
6901](https://tools.ietf.org/html/rfc6901). Unlike the `pointer` method
built into `serde_json`, this handles both validating JSON Pointers before
use and the URI Fragment Identifier Representation.
[](https://travis-ci.org/remexre/json-pointer)
[](https://crates.io/crates/json-pointer)
[](https://docs.rs/json-pointer)
## Creating a JSON Pointer
JSON pointers can be created with a literal `[&str]`, or parsed from a `String`.
```rust
let from_strs = JsonPointer::new([
"foo",
"bar",
]);
let parsed = "/foo/bar".parse::>().unwrap();
assert_eq!(from_strs.to_string(), parsed.to_string());
}
```
## Using a JSON Pointer
The `JsonPointer` type provides `.get()` and `.get_mut()`, to get references
and mutable references to the appropriate value, respectively.
```rust
let ptr = "/foo/bar".parse::>().unwrap();
let document = json!({
"foo": {
"bar": 0,
"baz": 1,
},
"quux": "xyzzy"
});
let indexed = ptr.get(&document).unwrap();
assert_eq!(indexed, &json!(0));
```
## URI Fragment Identifier Representation
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.
```rust
let str_ptr = "/f%o".parse::>().unwrap();
let uri_ptr = "#/f%25o".parse::>().unwrap();
assert_eq!(str_ptr, uri_ptr);
```