-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
fs: introduce readJSON functions #37944
Conversation
/cc @nodejs/fs |
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.
I'm kind of +/-0 on this, but would we maybe consider only providing this for the fsPromises variants to encourage using those?
I initially considered this but implemented all of them because I was afraid of people asking for consistency. Happy to change that if we all agree. |
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.
Good work, I think we should add these as they are convenient.
I'd be fine with dropping the legacy fs variants and keeping only the promise version, and even then I'd be happy keeping only the 'filehandle.readJSON()` variant. |
I think |
Since the code is already written, it would be nice to keep them - at least the synchronous version. |
This adds `fs.readJSON`, `fs.readJSONSync`, `fsPromises.readJSON`, and `fileHandle.readJSON`. All of them take the usual parameters of their respective `readFile` equivalents, with the addition of `JSON.parse`'s `reviver` option. The file will be read as a string, with the encoding defaulting to `'utf8'`, and if the read succeeds, `JSON.parse` will be called on the result. Parsing errors are propagated the same way as reading errors.
test error in callback
Updated, PTAL. |
what's the motivation for this exactly? |
It is common to read and parse JSON in Node.js applications. However, instead of using the |
i think people use require because of the relative path resolution. either way though i'm not blocking this, just seems a bit derived. |
There's certainly a discussion to be had about how many pure convenience APIs our core should provide. On the other hand, if we implemented this using streaming JSON parsing, this would actually have benefits over typical implementations, especially memory-wise, but also when it comes to safety. For example, the current implementation would hang and eventually crash the process for |
For the most part, coming back to this, I'm generally -0 on it. I wouldn't block it but I don't think it adds much value overall. It would be a different matter if it were part of a web platform compatibility story but as it is, I think it's better left for userland. |
Closing as there seems to be no support from collaborators. |
I think I'd still support something like this if it was significantly better than |
I don't have the time to work on something like that. |
This adds
fs.readJSON
,fs.readJSONSync
,fsPromises.readJSON
, andfileHandle.readJSON
.All of them take the usual parameters of their respective
readFile
equivalents, with the addition of
JSON.parse
'sreviver
option.The file will be read as a string, with the encoding defaulting to
'utf-8'
, and if the read succeeds,JSON.parse
will be called on theresult. Parsing errors are propagated the same way as reading errors.