Skip to main content

find_last_prop

Function find_last_prop 

Source
pub fn find_last_prop<'a, 'b>(
    object: &'a Object<'b>,
    key: &str,
) -> Option<&'a ObjectProp<'b>>
Expand description

Finds the property named key among object’s own direct children, taking the last one if key occurs more than once.

This is JSON’s last-key-wins semantics, which jsonc-parser’s Vec<ObjectProp> does not enforce on its own (unlike serde_json::Map, which dedupes during deserialization).

§Examples

use deps_core::json_ast::find_last_prop;
use jsonc_parser::ast::Value;
use jsonc_parser::{CollectOptions, ParseOptions, parse_to_ast};

let content = r#"{"a": 1, "a": 2}"#;
let parsed = parse_to_ast(content, &CollectOptions::default(), &ParseOptions::default()).unwrap();
let Some(Value::Object(root)) = parsed.value else { panic!("expected an object") };

let prop = find_last_prop(&root, "a").unwrap();
assert!(matches!(&prop.value, Value::NumberLit(lit) if lit.value == "2"));