Skip to content

Commit

Permalink
Merge pull request #39 from shimaore/number-reviver
Browse files Browse the repository at this point in the history
add number reviver
  • Loading branch information
creationix committed Apr 27, 2020
2 parents 5c80370 + a980a17 commit b2d8bc6
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions jsonparse.js
Expand Up @@ -265,17 +265,9 @@ proto.write = function (buffer) {
break;
default:
this.tState = START;
var result = Number(this.string);

if (isNaN(result)){
return this.charError(buffer, i);
}

if ((this.string.match(/[0-9]+/) == this.string) && (result.toString() != this.string)) {
// Long string of digits which is an ID string and not valid and/or safe JavaScript integer Number
this.onToken(STRING, this.string);
} else {
this.onToken(NUMBER, result);
var error = this.numberReviver(this.string);
if (error){
return error;
}

this.offset += this.string.length - 1;
Expand Down Expand Up @@ -412,6 +404,23 @@ proto.onToken = function (token, value) {
}
};

// Override to implement your own number reviver.
// Any value returned is treated as error and will interrupt parsing.
proto.numberReviver = function (text) {
var result = Number(text);

if (isNaN(result)) {
return this.charError(buffer, i);
}

if ((text.match(/[0-9]+/) == text) && (result.toString() != text)) {
// Long string of digits which is an ID string and not valid and/or safe JavaScript integer Number
this.onToken(STRING, text);
} else {
this.onToken(NUMBER, result);
}
}

Parser.C = C;

module.exports = Parser;

0 comments on commit b2d8bc6

Please sign in to comment.