Skip to content

Commit

Permalink
optimize parseLiteral for number-heavy JSON files (ala GeoJSON)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbullington committed Jun 9, 2020
1 parent e38baa7 commit 0eaf189
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/impl/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,16 +482,21 @@ export function visit(text: string, visitor: JSONVisitor, options: ParseOptions
function parseLiteral(): boolean {
switch (_scanner.getToken()) {
case SyntaxKind.NumericLiteral:
let value = 0;
try {
value = JSON.parse(_scanner.getTokenValue());
if (typeof value !== 'number') {
const tokenValue = _scanner.getTokenValue();
let value = parseFloat(tokenValue);

if (isNaN(value)) {
try {
value = JSON.parse(tokenValue);
if (typeof value !== 'number') {
handleError(ParseErrorCode.InvalidNumberFormat);
value = 0;
}
} catch (e) {
handleError(ParseErrorCode.InvalidNumberFormat);
value = 0;
}
} catch (e) {
handleError(ParseErrorCode.InvalidNumberFormat);
}

onLiteralValue(value);
break;
case SyntaxKind.NullKeyword:
Expand Down

0 comments on commit 0eaf189

Please sign in to comment.