Skip to content

Commit 0eaf189

Browse files
committed
optimize parseLiteral for number-heavy JSON files (ala GeoJSON)
1 parent e38baa7 commit 0eaf189

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/impl/parser.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -482,16 +482,21 @@ export function visit(text: string, visitor: JSONVisitor, options: ParseOptions
482482
function parseLiteral(): boolean {
483483
switch (_scanner.getToken()) {
484484
case SyntaxKind.NumericLiteral:
485-
let value = 0;
486-
try {
487-
value = JSON.parse(_scanner.getTokenValue());
488-
if (typeof value !== 'number') {
485+
const tokenValue = _scanner.getTokenValue();
486+
let value = parseFloat(tokenValue);
487+
488+
if (isNaN(value)) {
489+
try {
490+
value = JSON.parse(tokenValue);
491+
if (typeof value !== 'number') {
492+
handleError(ParseErrorCode.InvalidNumberFormat);
493+
value = 0;
494+
}
495+
} catch (e) {
489496
handleError(ParseErrorCode.InvalidNumberFormat);
490-
value = 0;
491497
}
492-
} catch (e) {
493-
handleError(ParseErrorCode.InvalidNumberFormat);
494498
}
499+
495500
onLiteralValue(value);
496501
break;
497502
case SyntaxKind.NullKeyword:

0 commit comments

Comments
 (0)