Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails to parse number with + prefix #38

Open
polarathene opened this issue Oct 19, 2023 · 1 comment
Open

Fails to parse number with + prefix #38

polarathene opened this issue Oct 19, 2023 · 1 comment

Comments

@polarathene
Copy link

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.",
    },
)

jsonc-parser/src/scanner.rs

Lines 272 to 293 in abd65a8

match self.current_char() {
Some('e') | Some('E') => {
match self.move_next_char() {
Some('-') | Some('+') => {
self.move_next_char();
if !self.is_digit() {
return Err(self.create_error_for_current_char("Expected a digit"));
}
}
_ => {
if !self.is_digit() {
return Err(self.create_error_for_current_char("Expected plus, minus, or digit in number literal"));
}
}
}
while self.is_digit() {
self.move_next_char();
}
}
_ => {}
}


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
Copy link
Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant