Skip to content

Commit a980a17

Browse files
committed
add number reviver
1 parent 1480064 commit a980a17

1 file changed

Lines changed: 20 additions & 11 deletions

File tree

jsonparse.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -261,17 +261,9 @@ proto.write = function (buffer) {
261261
break;
262262
default:
263263
this.tState = START;
264-
var result = Number(this.string);
265-
266-
if (isNaN(result)){
267-
return this.charError(buffer, i);
268-
}
269-
270-
if ((this.string.match(/[0-9]+/) == this.string) && (result.toString() != this.string)) {
271-
// Long string of digits which is an ID string and not valid and/or safe JavaScript integer Number
272-
this.onToken(STRING, this.string);
273-
} else {
274-
this.onToken(NUMBER, result);
264+
var error = this.numberReviver(this.string);
265+
if (error){
266+
return error;
275267
}
276268

277269
this.offset += this.string.length - 1;
@@ -408,6 +400,23 @@ proto.onToken = function (token, value) {
408400
}
409401
};
410402

403+
// Override to implement your own number reviver.
404+
// Any value returned is treated as error and will interrupt parsing.
405+
proto.numberReviver = function (text) {
406+
var result = Number(text);
407+
408+
if (isNaN(result)) {
409+
return this.charError(buffer, i);
410+
}
411+
412+
if ((text.match(/[0-9]+/) == text) && (result.toString() != text)) {
413+
// Long string of digits which is an ID string and not valid and/or safe JavaScript integer Number
414+
this.onToken(STRING, text);
415+
} else {
416+
this.onToken(NUMBER, result);
417+
}
418+
}
419+
411420
Parser.C = C;
412421

413422
module.exports = Parser;

0 commit comments

Comments
 (0)