pub fn parse_jsonc(
text: &str,
fmt_options: Option<FormatOptions>,
jsonc_options: Option<JsoncExtraOptions>,
) -> Result<Formatted<Value>, Box<dyn Error>>Expand description
Parses a JSONC string into a serde_json::Value, capturing formatting.
Examples found in repository?
examples/parse_jsonc.rs (line 15)
5fn main() {
6 let text = r#"
7{
8 // config for the app
9 "name": "my-app",
10 "port": 3000,
11 "debug": true
12}
13"#;
14
15 let formatted = parse_jsonc(text, None, None).expect("parse");
16 println!(
17 "Parsed: {} port={}",
18 formatted.value["name"], formatted.value["port"]
19 );
20
21 let out = stringify_jsonc(&formatted, None).expect("stringify");
22 println!("Stringify (comments stripped, valid JSON):\n{}", out);
23
24 let opts = JsoncExtraOptions {
25 allow_trailing_comma: true,
26 ..Default::default()
27 };
28 let with_trailing = r#"{ "x": 1, }"#;
29 let parsed = parse_jsonc(with_trailing, None, Some(opts)).expect("parse with trailing comma");
30 println!("With trailing comma: {:?}", parsed.value);
31}