Skip to content
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

JSON parser throws exception if JSON is malformed #36

Closed
wants to merge 1 commit into from

Conversation

baz
Copy link

@baz baz commented Jul 1, 2011

Currently, the parser just assumes that the input is valid JSON.
Great library BTW.

JSON could be malformed.
@lalitkapoor
Copy link

Shouldn't this result in an error?

@baz
Copy link
Author

baz commented Jul 31, 2011

Hmm well, the XML and YAML parsers are both wrapped in a try/catch block. Why not the JSON parser?

@lalitkapoor
Copy link

should they also return an error then? if parsing the data fails when the content-type states that this is the format, shouldn't you be notified that the content is not valid json/xml/yaml?

@baz
Copy link
Author

baz commented Jul 31, 2011

Yes I agree, that would be ideal.

@lalitkapoor
Copy link

possible fix: lalitkapoor@3e9bd13 (just did it for the json parser)

@ctavan
Copy link

ctavan commented Sep 20, 2011

@lalitkapoor I like your fix and I think it's good practice to catch the error and pass it as a first argument to the callback! I'd love to see that fix pulled into upstream although it breaks backwards compatibility with previous versions since the parameter order of the callback has changed... Would you mind sending a pull request?

@lalitkapoor
Copy link

@ctavan hey, I found node-wwwdude to be more reliable and more compliant: https://github.com/pfleidi/node-wwwdude

note that the author of node-wwwdude hasn't published the updated version with my patches to wwwdude (proper handling of compressed data) to npmjs, so you'll have to pull from github to get the latest.

@ctavan
Copy link

ctavan commented Sep 20, 2011

@lalitkapoor thanks for the hint, I'm definitely gonna give it a try!

@ayoung
Copy link
Collaborator

ayoung commented Nov 30, 2011

I really like lalitkapoor's implementation to pass an error in. But like it was mentioned it isn't backwards compatible. Should we just bite the bullet and change the API?

@ayoung
Copy link
Collaborator

ayoung commented Nov 30, 2011

If a fix like this gets implemented it would automatically fix issue #47 as well.

@ayoung
Copy link
Collaborator

ayoung commented Dec 10, 2011

@lalitkapoor @ctavan I created branch 0.3.x specifically for this fix as it makes a lot of sense to do this. Upping the minor version to 0.3.x will allow those on 0.2.x to stay compatible until they're ready to upgrade. Let me know if any of you are willing to implement a more legit implementation for it -- most likely using @lalitkapoor 's pull request as a starting point.

@ayoung ayoung closed this in 8b70a1a Jan 12, 2012
@gitmft
Copy link

gitmft commented Aug 28, 2016

I have a 3rd party REST service returning invalid JSON. Creating a custom json parser works fine but perhaps a new option jsonParseValidation=true|false could force it to return the data ASIS as shown below in the parsers function around line 412.

// ORIGINAL code that returns an error if JSON parse fails
// callback(err, null);
callback(null, data);
// above is modified code that simply returns data as it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants