-
-
Notifications
You must be signed in to change notification settings - Fork 32
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
Document how to stream JSON arrays #96
Conversation
readme.md
Outdated
|
||
const stream = fs.createReadStream('big-array-of-objects.json', {encoding: 'utf8'}); | ||
console.log(await getStreamAsArray( | ||
stream.pipe(streamJson.parser()).pipe(streamJsonArray.streamArray()), |
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.
pipeline
is generally recommended instead of pipe methods.
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.
That's right! Good catch.
What about compose()
instead? While pipeline()
consumes the stream and returns a promise resolving when done (which would conflict with getStreamAsArray()
consuming it too), compose()
does not consume it and returns the stream.
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.
Yeah, compose
is probably the right choice. I've been too used to (apparently) incorrectly using pipeline
.
Co-authored-by: Sindre Sorhus <[email protected]>
Fixed 👍 |
This PR documents how to parse JSON array streams incrementally, as opposed to blocking the event loop once at the end using
JSON.parse()
.This does not cover actual JSON streams formats (where each line is a single object, and there are no
[
]
delimiters) although the utility mentioned supports those too.