#json-pointer #6901 #rfc-6901

no-std jsonptr

Data structures and logic for resolving, assigning, and deleting by JSON Pointers (RFC 6901)

25 releases

new 0.4.5 Feb 23, 2024
0.4.4 Oct 13, 2023
0.4.3 Aug 20, 2023
0.4.2 Jun 23, 2023
0.1.5 Jun 30, 2022

#103 in Data structures

Download history 954/week @ 2023-11-06 1590/week @ 2023-11-13 1472/week @ 2023-11-20 876/week @ 2023-11-27 669/week @ 2023-12-04 502/week @ 2023-12-11 608/week @ 2023-12-18 624/week @ 2023-12-25 1024/week @ 2024-01-01 887/week @ 2024-01-08 1014/week @ 2024-01-15 1599/week @ 2024-01-22 1596/week @ 2024-01-29 1333/week @ 2024-02-05 1466/week @ 2024-02-12 2086/week @ 2024-02-19

6,565 downloads per month
Used in 2 crates

MIT/Apache

93KB
2K SLoC

jsonptr - JSON Pointers for Rust

github crates.io docs.rs build status

Data structures and logic for resolving, assigning, and deleting by JSON Pointers (RFC 6901).

Usage

JSON Pointers can be created either with a slice of strings or directly from a properly encoded string representing a JSON Pointer.

Resolve values

Pointer::resolve

use jsonptr::Pointer;
use serde_json::json;

let mut data = json!({"foo": { "bar": "baz" }});
let ptr = Pointer::new(["foo", "bar"]);
let bar = ptr.resolve(&data).unwrap();
assert_eq!(bar, "baz");

Resolve::resolve

use jsonptr::{Pointer, Resolve};
use serde_json::json;

let mut data = json!({ "foo": { "bar": "baz" }});
let ptr = Pointer::new(["foo", "bar"]);
let bar = data.resolve(&ptr).unwrap();
assert_eq!(bar, "baz");

ResolveMut::resolve_mut

use jsonptr::{Pointer, ResolveMut};
use serde_json::json;

let ptr = Pointer::try_from("/foo/bar").unwrap();
let mut data = json!({ "foo": { "bar": "baz" }});
let mut bar = data.resolve_mut(&ptr).unwrap();
assert_eq!(bar, "baz");

Assign

Pointer::assign

use jsonptr::Pointer;
use serde_json::json;

let ptr = Pointer::try_from("/foo/bar").unwrap();
let mut data = json!({});
let _previous = ptr.assign(&mut data, "qux").unwrap();
assert_eq!(data, json!({ "foo": { "bar": "qux" }}))

Assign::asign

use jsonptr::{Assign, Pointer};
use serde_json::json;

let ptr = Pointer::try_from("/foo/bar").unwrap();
let mut data = json!({});
let _previous = data.assign(&ptr, "qux").unwrap();
assert_eq!(data, json!({ "foo": { "bar": "qux" }}))

Delete

Pointer::delete

use jsonptr::Pointer;
use serde_json::json;

let mut data = json!({ "foo": { "bar": { "baz": "qux" } } });
let ptr = Pointer::new(&["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);

Delete::delete

use jsonptr::{Pointer, Delete};
use serde_json::json;

let mut data = json!({ "foo": { "bar": { "baz": "qux" } } });
let ptr = Pointer::new(["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::default();
let deleted = json!({ "foo": { "bar": {} } });
assert_eq!(data.delete(&ptr), Some(deleted));
assert!(data.is_null());

Feature Flags

Flag Enables
"std" implements std::error::Error for errors
"url" TryFrom<url::Url> for Pointer
"uniresid" TryFrom<uniresid::Uri> + TryFrom<uniresid::AbsoluteUri> for Pointer
"fluent-uri" TryFrom<fluent_uri::Uri<&str>> + TryFrom<fluent_uri::Uri<String>> + TryFrom<fluent_uri::Uri<&mut [u8]>> for Pointer

Contributions / Issues

Contributions and feedback are always welcome and appreciated.

If you find an issue, please open a ticket or a pull request.

License

MIT or Apache 2.0.


Dependencies

~0.4–1.1MB
~28K SLoC