-
-
Notifications
You must be signed in to change notification settings - Fork 736
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
Added support for external parsers to bodyParser.json() #281
Changes from 1 commit
d874969
4f18d51
1d47892
b9823e5
4b91d93
e786697
612e4da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,7 @@ function json (options) { | |
var strict = opts.strict !== false | ||
var type = opts.type || 'application/json' | ||
var verify = opts.verify || false | ||
var parser = opts.parser || JSON.parse | ||
|
||
if (verify !== false && typeof verify !== 'function') { | ||
throw new TypeError('option verify must be function') | ||
|
@@ -80,13 +81,13 @@ function json (options) { | |
|
||
if (first !== '{' && first !== '[') { | ||
debug('strict violation') | ||
throw createStrictSyntaxError(body, first) | ||
throw createStrictSyntaxError(parser, body, first) | ||
} | ||
} | ||
|
||
try { | ||
debug('parse json') | ||
return JSON.parse(body, reviver) | ||
return parser(body, reviver) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure this is right for custom parsers. Or at least it's not clear in the docs for the parser option that this is going to happen and how people should build their parser dunxtion to compensate. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The other option would be to remove the Maybe the right way is to make the docs more clear about this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I commented on the wrog line. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I don't think I can agree with you that the docs are not clear:
If you can suggest an update to that text I'm all ears. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved my comment down. The new comment for this line is to clarify in the docs the arguments the custom parser will be called with. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the line from the reviver docs and add in the specific signature into the parser docs. The current parser section doesn't help someone build their own, I think it assumes someone is going to understand JSON.parse and copy that? If so, just write that down. |
||
} catch (e) { | ||
throw normalizeJsonSyntaxError(e, { | ||
stack: e.stack | ||
|
@@ -149,12 +150,12 @@ function json (options) { | |
* @private | ||
*/ | ||
|
||
function createStrictSyntaxError (str, char) { | ||
function createStrictSyntaxError (parser, str, char) { | ||
var index = str.indexOf(char) | ||
var partial = str.substring(0, index) + '#' | ||
|
||
try { | ||
JSON.parse(partial); /* istanbul ignore next */ throw new SyntaxError('strict violation') | ||
parser(partial); /* istanbul ignore next */ throw new SyntaxError('strict violation') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the line my above comment was supposed to be on. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure this is right for custom parsers. Or at least it's not clear in the docs for the parser option that this is going to happen and how people should build their parser function to compensate. |
||
} catch (e) { | ||
return normalizeJsonSyntaxError(e, { | ||
message: e.message.replace('#', char), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No description of the first argument. Also doesn't describe the mechanism of how it needs to be made to construct the syntax error during the recalling mechanism with the # inserted into the original string.