#json

json-pointer

A crate for parsing and using JSON pointers, as specified in RFC 6901

7 releases

0.3.4 Sep 5, 2020
0.3.3 Sep 5, 2020
0.3.2 Jun 1, 2017
0.3.1 May 30, 2017
0.1.0 May 26, 2017

#400 in Encoding

Download history 37626/week @ 2023-10-20 42901/week @ 2023-10-27 45960/week @ 2023-11-03 49275/week @ 2023-11-10 39954/week @ 2023-11-17 45208/week @ 2023-11-24 81209/week @ 2023-12-01 58662/week @ 2023-12-08 56397/week @ 2023-12-15 22030/week @ 2023-12-22 37718/week @ 2023-12-29 57203/week @ 2024-01-05 55326/week @ 2024-01-12 61208/week @ 2024-01-19 52413/week @ 2024-01-26 48099/week @ 2024-02-02

228,492 downloads per month
Used in 52 crates (10 directly)

MIT license

19KB
287 lines

json-pointer

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.

pipeline status crates.io Documentation

Creating a JSON Pointer

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());
}

Using a JSON Pointer

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));

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.

let str_ptr = "/f%o".parse::<JsonPointer<_, _>>().unwrap();
let uri_ptr = "#/f%25o".parse::<JsonPointer<_, _>>().unwrap();

assert_eq!(str_ptr, uri_ptr);

Dependencies

~360–760KB
~17K SLoC