sfcode
An Online Competing and Development Environment
|
streaming JSON.parse and stringify
parse stream of values that match a path
The ..
operator is the recursive descent operator from JSONPath, which will match a child at any depth (see examples below).
If your keys have keys that include .
or *
etc, use an array instead. ‘['row’, true, /^doc/]`.
If you use an array, RegExp
s, booleans, and/or functions. The ..
operator is also available in array representation, using {recurse: true}
. any object that matches the path will be emitted as 'data' (and pipe
d down stream)
If path
is empty or null, no 'data' events are emitted.
If you want to have keys emitted, you can prefix your *
operator with $
: obj.$*
- in this case the data passed to the stream is an object with a key
holding the key and a value
property holding the data.
query a couchdb view:
you will get something like this:
we are probably most interested in the rows.*.doc
create a Stream
that parses the documents from the feed like this:
awesome!
In case you wanted the contents the doc emitted:
You can also emit the path:
‘JSONStream.parse('docs..value’) (or
JSONStream.parse(['docs', {recurse: true}, 'value'])using an array) will emit every
valueobject that is a child, grand-child, etc. of the
docs` object. In this example, it will match exactly 5 times at various depth levels, emitting 0, 1, 2, 3 and 4 as results.
provide a function that can be used to map or filter the json output. map
is passed the value at that node of the pattern, if map
return non-nullish (anything but null
or undefined
) that value will be emitted in the stream. If it returns a nullish value, nothing will be emitted.
JSONStream
also emits ‘'header’and
'footer'events, the
'header'event contains anything in the output that was before the first match, and the
'footer'`, is anything after the last match.
Create a writable stream.
you may pass in custom open
, close
, and seperator
strings. But, by default, JSONStream.stringify()
will create an array, (with default options ‘open=’[
', sep='
,
', close='
]
'`)
If you call JSONStream.stringify(false)
the elements will only be seperated by a newline.
If you only write one item this will be valid JSON.
If you write many items, you can use a RegExp
to split it into valid chunks.
Very much like JSONStream.stringify
, but creates a writable stream for objects instead of arrays.
Accordingly, ‘open=’{
', sep='
,
', close='
}
'`.
When you .write()
to the stream you must supply an array with [ key, data ]
as the first argument.
query npm to see all the modules that browserify has ever depended on.
numbers will be emitted as numbers. huge numbers that cannot be represented in memory as javascript numbers will be emitted as strings. cf https://github.com/creationix/jsonparse/commit/044b268f01c4b8f97fb936fc85d3bcfba179e5bb for details.
this module depends on https://github.com/creationix/jsonparse by Tim Caswell and also thanks to Florent Jaby for teaching me about parsing with: https://github.com/Floby/node-json-streams
Dual-licensed under the MIT License or the Apache License, version 2.0