Permalink
Browse files

always emit 'root' event: function(root, count)

  • Loading branch information...
1 parent e24e92f commit 7481cf119ab30f476d00b7d4236c2ad6c8c312ea @skenqbx skenqbx committed Jul 15, 2012
Showing with 28 additions and 21 deletions.
  1. +15 −18 index.js
  2. +13 −3 readme.markdown
View
@@ -4,14 +4,14 @@ var Parser = require('jsonparse')
/*
the value of this.stack that creationix's jsonparse has is weird.
-
+
it makes this code ugly, but his problem is way harder that mine,
so i'll forgive him.
*/
exports.parse = function (path) {
-
+
var stream = new Stream()
var parser = new Parser()
var count = 0
@@ -27,19 +27,19 @@ exports.parse = function (path) {
for( var i = 0; i < (path.length - 1); i++) {
var key = path[i]
var c = this.stack[1 + (+i)]
-
+
if(!c) {
return
}
var m = check(key, c.key)
_path.push(c.key)
-
+
if(!m)
return
-
+
}
var c = this
-
+
var key = path[path.length - 1]
var m = check(key, c.key)
if(!m)
@@ -71,10 +71,7 @@ exports.parse = function (path) {
stream.end = function (data) {
if(data)
stream.write(data)
- if(!count && !path)
- stream.emit('data', stream.root)
- else if(!count)
- stream.emit('root', stream.root)
+ stream.emit('root', stream.root, count)
stream.emit('end')
}
return stream
@@ -98,15 +95,15 @@ exports.stringify = function (op, sep, cl) {
sep = '\n'
cl = ''
} else if (op == null) {
-
+
op = '[\n'
sep = '\n,\n'
cl = '\n]\n'
-
+
}
//else, what ever you like
-
+
var stream = new Stream ()
, first = true
, ended = false
@@ -124,7 +121,7 @@ exports.stringify = function (op, sep, cl) {
if(data) stream.write(data)
if(!anyData) stream.emit('data', op)
stream.emit('data', cl)
-
+
stream.emit('end')
}
stream.writable = true
@@ -139,15 +136,15 @@ exports.stringifyObject = function (op, sep, cl) {
sep = '\n'
cl = ''
} else if (op == null) {
-
+
op = '{\n'
sep = '\n,\n'
cl = '\n}\n'
-
+
}
//else, what ever you like
-
+
var stream = new Stream ()
, first = true
, ended = false
@@ -164,7 +161,7 @@ exports.stringifyObject = function (op, sep, cl) {
if(data) stream.write(data)
if(!anyData) stream.emit('data', op)
stream.emit('data', cl)
-
+
stream.emit('end')
}
stream.writable = true
View
@@ -43,9 +43,9 @@ usally, a json API will return a list of objects.
`path` should be an array of property names, `RegExp`s, booleans, and/or functions.
any object that matches the path will be emitted as 'data' (and `pipe`d down stream)
-if `path` is empty or null, JSONStream.parse will only one 'data': the root object.
+a 'root' event is emitted when all data has been received. The 'root' event passes the root object & the count of matched objects.
-if `path` was supplied, but there where no matches for some reason, the 'root' event will be emitted with the root object. This is for cases where an external API like CouchDB returns an error or differently formatted result instead of the expected result.
+if `path` is empty or null, no 'data' events are emitted.
### example
@@ -83,7 +83,17 @@ we are probably most interested in the `rows.*.docs`
create a `Stream` that parses the documents from the feed like this:
``` js
-JSONStream.parse(['rows', true, 'doc']) //rows, ANYTHING, doc
+var stream = JSONStream.parse(['rows', true, 'doc']) //rows, ANYTHING, doc
+
+stream.on('data', function(data) {
+ console.log('received:', data);
+});
+
+stream.on('root', function(root, count) {
+ if (!count) {
+ console.log('no matches found:', root);
+ }
+});
```
awesome!

0 comments on commit 7481cf1

Please sign in to comment.