| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
|
/cc @nodejs/fs |
Sorry, something went wrong.
There was a problem hiding this comment.
I'm kind of +/-0 on this, but would we maybe consider only providing this for the fsPromises variants to encourage using those?
Sorry, something went wrong.
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. |
Sorry, something went wrong.
There was a problem hiding this comment.
Good work, I think we should add these as they are convenient.
Sorry, something went wrong.
|
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. |
Sorry, something went wrong.
I think fsPromises.readJSON() is still useful as a simple way to read and parse a JSON file in one line. |
Sorry, something went wrong.
Since the code is already written, it would be nice to keep them - at least the synchronous version. |
Sorry, something went wrong.
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. |
Sorry, something went wrong.
Sorry, something went wrong.
|
what's the motivation for this exactly? |
Sorry, something went wrong.
|
It is common to read and parse JSON in Node.js applications. However, instead of using the fs module, people tend to instead require() the files because it's simpler. |
Sorry, something went wrong.
|
i think people use require because of the relative path resolution. either way though i'm not blocking this, just seems a bit derived. |
Sorry, something went wrong.
|
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 readJSON('/dev/zero'), whereas a streaming implementation would result in virtually no resource usage and an immediate parsing error. |
Sorry, something went wrong.
|
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. |
Sorry, something went wrong.
|
Closing as there seems to be no support from collaborators. |
Sorry, something went wrong.
|
I think I'd still support something like this if it was significantly better than readFile + JSON.parse, e.g., by failing early if there is a syntax error (see #37944 (comment)). |
Sorry, something went wrong.
|
I don't have the time to work on something like that. |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
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
'utf-8', and if the read succeeds, JSON.parse will be called on the
result. Parsing errors are propagated the same way as reading errors.