Skip to content

add number reviver #39

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

Merged
merged 1 commit into from
Apr 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions jsonparse.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,17 +261,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 @@ -408,6 +400,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;