-
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
"json" encoding support for fs.readX functions #44150
Comments
This can be roughly achieved via one helper function in userspace without external packages, e.g.: const readJSON = async path => JSON.parse(await fsPromises.readFile(path));
// or
const readJSONSync = path => JSON.parse(fs.readFileSync(path)); Parsing JSON format probably would not have any performance benefits from being included in core, and there might be some discrepancy between possible/expected behaviours (e.g. should we strip |
For what it's worth you can do |
Except that it would depends on the module cache, which may or may not be in sync with the actual file. You can also |
Yoooooo that's cool |
Probably not, it's all about conviniency |
I think Also, as shown in the document, fs is a POSIX-like API. But JSON is an RFC standard. The node:fs module enables interacting with the file system in a way modeled on standard POSIX functions. So, I don't think we need to add JSON support for reading files. If we really do that, what about writing a JSON file? What about other formats like XML, CSV... |
My opinion hasn't changed from the last time this feature was suggested: if this were to be implemented in Node.js core, it should have significant benefits over |
I'd be fine with |
There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment. For more information on how the project manages feature requests, please consult the feature request management document. |
What is the problem this feature will solve?
The
JSON.parse(fs.readFileSync(path))
,fs.readFile(path, "utf-8", res => { res = JSON.parse(res); /* ... */ })
look just way too long and complex.What is the feature you are proposing to solve the problem?
The "json" encoding for the builtin
fs
module. Here's how it might look like:As you can see, it's much shorter and more clear.
What alternatives have you considered?
As I've shown above,
JSON.parse
is a variant right now, but it's just way too long. There's alsor-json
package, but having a separate package just for reading JSON files takes much more space.The text was updated successfully, but these errors were encountered: