pub struct JsonAst<'a> { /* private fields */ }Expand description
A parsed JSON/JSONC document, used only to recover a named top-level section’s exact
source positions — never a substitute for a caller’s own serde_json parse of the
document’s semantic content.
§Examples
use deps_core::json_ast::JsonAst;
let content = r#"{"dependencies": {"express": "^4.18.2"}}"#;
let ast = JsonAst::parse(content).unwrap();
assert!(ast.section("dependencies").is_some());
assert!(ast.section("devDependencies").is_none());Implementations§
Source§impl<'a> JsonAst<'a>
impl<'a> JsonAst<'a>
Sourcepub fn parse(content: &'a str) -> Option<Self>
pub fn parse(content: &'a str) -> Option<Self>
Parses content. Returns None if it isn’t valid JSONC, or its root value isn’t an
object — a caller that reached this via crate::parse_json_checked should not
normally see None here (jsonc-parser’s grammar is a strict superset of JSON), but must
still degrade gracefully (every dependency’s position falling back to the default,
zero Range) rather than assume it.
Relies on the caller having already bounded content’s nesting depth (e.g. via
crate::parse_json_checked/crate::check_json_nesting_depth) — jsonc-parser’s own
internal recursion cap (512, hardcoded, not configurable) is a last resort, not this
workspace’s first line of defense against a stack-overflowing payload.
§Examples
use deps_core::json_ast::JsonAst;
assert!(JsonAst::parse(r#"{"a": 1}"#).is_some());
assert!(JsonAst::parse("not json").is_none());
assert!(JsonAst::parse("[1, 2, 3]").is_none(), "root value must be an object");Sourcepub fn section(&self, key: &str) -> Option<JsonSection<'_>>
pub fn section(&self, key: &str) -> Option<JsonSection<'_>>
Indexes key‘s top-level section by its direct properties’ own names, for O(1)
per-dependency position lookup instead of an O(section length) rescan per dependency.
None if key is absent at the top level or its value isn’t an object. A duplicate key
within the section resolves to its last occurrence too, matching key’s own
last-key-wins resolution (see find_last_prop) — the same rule applied one level
deeper.
§Examples
use deps_core::json_ast::JsonAst;
use deps_core::lsp_helpers::LineOffsetTable;
let content = r#"{"require": {"vendor/pkg": "^1.0"}}"#;
let table = LineOffsetTable::new(content);
let ast = JsonAst::parse(content).unwrap();
let section = ast.section("require").unwrap();
let (name_range, version_range) = section.position("vendor/pkg", content, &table).unwrap();
let name_start = content.find("vendor/pkg").unwrap() as u32;
assert_eq!(name_range.start.character, name_start);
assert!(version_range.is_some());