-
-
Notifications
You must be signed in to change notification settings - Fork 53
StreamValues
StreamValues assumes that a token stream represents subsequent values and stream them out one by one.
1 "a" [] {} true
// StreamValues will produce an object stream:
{key: 0, value: 1}
{key: 1, value: 'a'}
{key: 2, value: []}
{key: 3, value: {}}
{key: 4, value: true}StreamValues is a usual companion for Pick, which streams selected subobjects individually.
As every streamer, it assumes that individual objects can fit in memory, but the whole file, or any other source, should be streamed.
The prelude:
const {streamValues} = require('stream-json/streamers/stream-values.js');
const fs = require('fs');Take a JSON streaming pipeline and pipe it into the parser:
const pipeline = fs.createReadStream('sample.json').pipe(streamValues.withParserAsStream());
pipeline.on('data', data => console.log(data));Alternatively, first create the streaming pipeline and then pipe JSON messages into it:
const q = streamValues.withParserAsStream();
q.on('data', data => console.log(data));
fs.createReadStream('sample_a.json').pipe(q);Being based on StreamBase, StreamValues has no special API.
streamValues() is the factory function. It takes options described above and returns a function for use in chain():
const {chain} = require('stream-chain');
const {parser} = require('stream-json');
const {streamValues} = require('stream-json/streamers/stream-values.js');
const fs = require('fs');
const pipeline = chain([fs.createReadStream('sample.json'), parser(), streamValues()]);
let objectCounter = 0;
pipeline.on('data', () => ++objectCounter);
pipeline.on('end', () => console.log(`Found ${objectCounter} objects.`));Returns a Duplex stream (object-mode both sides) wrapping streamValues() for .pipe() usage.
withParser() takes one argument:
-
options— combined Parser and streamer options. Passed to both the parser andstreamValues().-
jsonStreamingis always set totruebecause otherwise it doesn't make sense to stream just one object.
-
Returns a Duplex stream (text-mode writable, object-mode readable) wrapping a parser({jsonStreaming: true}) + streamValues() pipeline via stream-chain.
Built with the withParser() utility.
const {streamValues} = require('stream-json/streamers/stream-values.js');
const fs = require('fs');
const pipeline = fs.createReadStream('sample.json').pipe(streamValues.withParserAsStream());
let objectCounter = 0;
pipeline.on('data', () => ++objectCounter);
pipeline.on('end', () => console.log(`Found ${objectCounter} objects.`));streamValues ships in two substrate-specific entries with the same factory shape:
-
Node —
stream-json/streamers/stream-values.js. HasasStream,asWebStream,withParser,withParserAsStream,withParserAsWebStream. -
Web —
stream-json/web/streamers/stream-values.js. HasasWebStream,withParser,withParserAsWebStream. Pulls in no Node-stream imports.
Both factories return the same flushable, so chain on either substrate auto-wraps it. Use chain from stream-chain on Node and from stream-chain/web on Web.
// Web
import {chain} from 'stream-chain/web';
import {parser} from 'stream-json/web/parser.js';
import {streamValues} from 'stream-json/web/streamers/stream-values.js';
const pipeline = chain([source, parser({jsonStreaming: true}), streamValues()]);
for await (const item of pipeline.readable) console.log(item);withParserAsWebStream sets jsonStreaming: true automatically:
const {readable, writable} = streamValues.withParserAsWebStream();
sourceReadable.pipeTo(writable);
for await (const item of readable) console.log(item);Start here
Core
Filters
Streamers
Essentials
Utilities
File I/O (Node-only)
JSONC
JSONL (use stream-chain)
Reference
Built on stream-chain