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

fs: introduce readJSON functions #37944

Closed
wants to merge 2 commits into from
Closed

Conversation

targos
Copy link
Member

@targos targos commented Mar 27, 2021

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.

@nodejs-github-bot nodejs-github-bot added fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run. labels Mar 27, 2021
@targos targos added the semver-minor PRs that contain new features and should be released in the next minor version. label Mar 27, 2021
@targos
Copy link
Member Author

targos commented Mar 27, 2021

/cc @nodejs/fs

Copy link
Member

@addaleax addaleax left a 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?

lib/fs.js Outdated Show resolved Hide resolved
@targos
Copy link
Member Author

targos commented Mar 27, 2021

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.

doc/api/fs.md Show resolved Hide resolved
doc/api/fs.md Outdated Show resolved Hide resolved
Copy link
Member

@mcollina mcollina left a 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.

doc/api/fs.md Show resolved Hide resolved
doc/api/fs.md Show resolved Hide resolved
@jasnell
Copy link
Member

jasnell commented Mar 27, 2021

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.

doc/api/fs.md Outdated Show resolved Hide resolved
doc/api/fs.md Outdated Show resolved Hide resolved
@targos
Copy link
Member Author

targos commented Mar 27, 2021

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 fsPromises.readJSON() is still useful as a simple way to read and parse a JSON file in one line.

@cjihrig
Copy link
Contributor

cjihrig commented Mar 27, 2021

would we maybe consider only providing this for the fsPromises variants to encourage using those?

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.
@targos
Copy link
Member Author

targos commented Mar 31, 2021

Updated, PTAL.

@nodejs-github-bot
Copy link
Collaborator

@devsnek
Copy link
Member

devsnek commented Mar 31, 2021

what's the motivation for this exactly?

@targos
Copy link
Member Author

targos commented Mar 31, 2021

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.
The populare fs-extra module provides a very similar functionality.

@devsnek
Copy link
Member

devsnek commented Mar 31, 2021

i think people use require because of the relative path resolution. either way though i'm not blocking this, just seems a bit derived.

@tniessen
Copy link
Member

tniessen commented Apr 8, 2021

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.

@jasnell
Copy link
Member

jasnell commented Apr 28, 2021

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.

@targos
Copy link
Member Author

targos commented May 1, 2021

Closing as there seems to be no support from collaborators.

@targos targos closed this May 1, 2021
@tniessen
Copy link
Member

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)).

@targos
Copy link
Member Author

targos commented May 27, 2021

I don't have the time to work on something like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants