Skip to content

Commit

Permalink
Merge pull request #34 from mbullington/optimize-parse-literal
Browse files Browse the repository at this point in the history
Optimize parseLiteral for number-heavy JSON files (ala GeoJSON)
  • Loading branch information
aeschli committed Sep 16, 2020
2 parents 318ee82 + 7505449 commit ad06ba4
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/impl/parser.ts
Expand Up @@ -482,16 +482,14 @@ 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') {
handleError(ParseErrorCode.InvalidNumberFormat);
value = 0;
}
} catch (e) {
const tokenValue = _scanner.getTokenValue();
let value = Number(tokenValue);

if (isNaN(value)) {
handleError(ParseErrorCode.InvalidNumberFormat);
value = 0;
}

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

0 comments on commit ad06ba4

Please sign in to comment.