jsonc-parser icon indicating copy to clipboard operation
jsonc-parser copied to clipboard

Fails to parse number with `+` prefix

Open polarathene opened this issue 1 year ago • 1 comments

While this is a JSON5 feature rather than JSONC, the parser seems to implement support but fails?:

let parsed = jsonc_parser::parse_to_value(r#"{ "test": +42 }"#, &Default::default());
println!("{:#?}", parsed);

Outputs error:

Err(
    ParseError {
        range: Range {
            start: 10,
            end: 11,
        },
        message: "Unexpected token",
        display_message: "Unexpected token on line 1 column 11.",
    },
)

https://github.com/dprint/jsonc-parser/blob/abd65a888339815d3a8c80359ddc918c2b4c25c2/src/scanner.rs#L272-L293


Or is this observation a misunderstanding? (it was first noted here)

It does seem accurate that this crate is more of a JSON5 parser than a JSONC one?

polarathene avatar Oct 19 '23 04:10 polarathene

Just after handling this report I recognized the Some('e') line as the E notation:

let parsed = jsonc_parser::parse_to_serde_value("1e-3", &Default::default()).expect("parse to value");
let num: f64 = serde_json::from_value(parsed.into()).expect("parse to float");

// 1e3 => 1000.0
// 1e+3 => 1000.0
// 1e-3 => 0.001
println!("{:#?}", num);

While comparing this crate to json5, I was thrown off a bit by all the extra parsing supported beyond what is expected for jsonc 😅 (most features seem to parse at a glance, beyond the number examples)

polarathene avatar Oct 19 '23 04:10 polarathene