-
Notifications
You must be signed in to change notification settings - Fork 345
Proposal to add JSON.safe_parse which rescues exceptions #785
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
Comments
I'm not sure if this is really worth providing in JSON itself. I'll think about it. The Something more consistent with Ruby core methods would be Another possible name is
Yes, which is why I don't see a very high value in this. |
Thanks for consideration, I really like name Again, almost every time I use I know some libraries provide alternative methods which do not raise exceptions, but not sure about ruby core. The value is only in convenience, specifically when user doesn't need to handle exceptions / read docs / figure out which error should be rescued, etc. |
I would think so yes. That's why I'm not very enthusiastic about adding such method, I don't think it's a particularly common need, and only saves a rescue. |
Alright. Thanks for the suggestion, but I don't think this feature is worth it. |
I propose adding helper method which rescues parsing error and returns nil by default.
Motivation
In almost every project I deal with JSON-ish strings, which may or may not be a JSON. And every time I end up writing custom helper method just to rescue possible errors. I would love if this method was built-in.
Example usage
Notes
safe_parse
as in it's safe, to call this method without errors. (Cannot use bang notation since bothparse
andparse!
already exists and their behaviour is already defined.)JSONError
which should cover any error which might raise fromJSON.parse
operation. Any possible other errors should still be raised.I would love feedback if this is something which could be added. Thanks!
The text was updated successfully, but these errors were encountered: