jsonposition 0.1.0

Finds a path to a JSON element at a provided string index
Documentation
  • Coverage
  • 66.67%
    4 out of 6 items documented3 out of 4 items with examples
  • Size
  • Source code size: 9.8 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.43 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 12s Average build duration of successful builds.
  • all releases: 12s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • commonkestrel/jsonposition
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • commonkestrel

JSON Position

JSON Position is json library for finding the path to JSON at an index in the original string. Similar to extensions in most IDEs to get path to cursor position.

Examples

use jsonposition::{ path, dot_path, Index };

let json = r#"[9, {"name": "b", "fields": [null, null, 87, 4], "path": "file.txt"}]"#;
let position = json.find("87").unwrap();

let vec_path = path(json, position).expect("Invalid JSON");
assert_eq!(vec_path, [Index::Array(1), Index::Object("fields".to_string()), Index::Array(2)]);

let dotted = dot_path(json, position).expect("Invalid JSON");
assert_eq!(dotted, "$.1.fields.2");

In this example we start with the raw JSON string [9, {"name": "b", "fields": [null, null, 87, 4]}] We are trying to find the path to the first 87 contained in the string, which starts at char 42. This can be indexed to with the path json[1]["fields"][2]

The path function returns this path in a Vec<Index>. Index is an enum with two varients. One is Array, which is an index into an array, and Object which is a key in an object.

The dot_path function returns this path in a format used by most JsonPath libraries: "$.1.fields.2"