Skip to content

Commit b2d8bc6

Browse files
authored
Merge pull request #39 from shimaore/number-reviver
add number reviver
2 parents 5c80370 + a980a17 commit b2d8bc6

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
@@ -265,17 +265,9 @@ proto.write = function (buffer) {
265265
break;
266266
default:
267267
this.tState = START;
268-
var result = Number(this.string);
269-
270-
if (isNaN(result)){
271-
return this.charError(buffer, i);
272-
}
273-
274-
if ((this.string.match(/[0-9]+/) == this.string) && (result.toString() != this.string)) {
275-
// Long string of digits which is an ID string and not valid and/or safe JavaScript integer Number
276-
this.onToken(STRING, this.string);
277-
} else {
278-
this.onToken(NUMBER, result);
268+
var error = this.numberReviver(this.string);
269+
if (error){
270+
return error;
279271
}
280272

281273
this.offset += this.string.length - 1;
@@ -412,6 +404,23 @@ proto.onToken = function (token, value) {
412404
}
413405
};
414406

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

417426
module.exports = Parser;

0 commit comments

Comments
 (0)