admin管理员组文章数量:1295272
I realize that there are a ton of Node modules that provide an async API for parsing JSON, but many of them seem to read the entire file or stream into memory, construct a giant string, and then pass it to JSON.parse()
. This is what the second answer to "How to parse JSON using NodeJS?" suggests, and is exactly what the jsonfile module does.
Constructing a giant string is exactly what I want to avoid. I want an API like:
parseJsonFile(pathToJsonFile): Promise
where the Promise
that is returned resolves to the parsed JSON object. This implementation should use a constant amount of memory. I'm not interested in any sort of SAX-like thing that broadcasts events as various pieces are parsed: just the end result.
I think jsonparse may do what I want (it clearly includes logic for parsing JSON without using JSON.parse()
), but there is no simple example in the README.md
, and the one file in the examples directory seems overly plicated.
I realize that there are a ton of Node modules that provide an async API for parsing JSON, but many of them seem to read the entire file or stream into memory, construct a giant string, and then pass it to JSON.parse()
. This is what the second answer to "How to parse JSON using NodeJS?" suggests, and is exactly what the jsonfile module does.
Constructing a giant string is exactly what I want to avoid. I want an API like:
parseJsonFile(pathToJsonFile): Promise
where the Promise
that is returned resolves to the parsed JSON object. This implementation should use a constant amount of memory. I'm not interested in any sort of SAX-like thing that broadcasts events as various pieces are parsed: just the end result.
I think jsonparse may do what I want (it clearly includes logic for parsing JSON without using JSON.parse()
), but there is no simple example in the README.md
, and the one file in the examples directory seems overly plicated.
2 Answers
Reset to default 6I've written a module that does this: BFJ (Big-Friendly JSON). It exports a bunch of functions that operate at different levels of abstraction, but are all asynchronous and streaming at their core.
At the highest level are two functions for reading from and writing to the file system, bfj.read
and bfj.write
. They each return a promise, so you call them like this:
var bfj = require('bfj');
// Asynchronously read from a JSON file on disk
bfj.read(path)
.then(data => {
// :)
})
.catch(error => {
// :(
});
// Asynchronously write to a JSON file on disk
bfj.write(path, data)
.then(data => {
// :)
})
.catch(error => {
// :(
});
Also at this level is a function for serializing data to a JSON string, called bfj.stringify
:
// Asynchronously serialize data to a JSON string
bfj.stringify(data)
.then(json => {
// :)
})
.catch(error => {
// :(
});
Beneath those are two more generic functions for reading from and writing to streams, bfj.parse
and bfj.streamify
. These serve as foundations for the higher level functions, but you can also call them directly:
// Asynchronously parse JSON from a readable stream
bfj.parse(readableStream).
.then(data => {
// :)
})
.catch(error => {
// :(
});
// Asynchronously serialize data to a writable stream of JSON
bfj.streamify(data).
.pipe(writableStream);
At the lowest level there are two functions analagous to SAX parsers/serializers, bfj.walk
and bfj.eventify
. It's unlikely you'd want to call these directly, they're just the guts of the implementation for the higher levels.
It's open-source and MIT-licensed. For more information, check the readme.
jsonparse is a streamed json parser, the sample code already shown the minimum of using Node stream.
- Change
client.request()
tofs.createReadStream()
. - Setup
on('data')
listeners on the file read stream similar to what's inon('response')
in the example.
本文标签:
版权声明:本文标题:javascript - Is there a Node module for an async JSON parser that does not load the entire JSON string into memory? - Stack Over 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741616984a2388580.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论