Skip to content

Commit

Permalink
[api] use parted for streaming json parser. see #2.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Feb 10, 2013
1 parent f5ba003 commit 0cd3f4f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 39 deletions.
61 changes: 23 additions & 38 deletions lib/godot/common/json-parser.js
Expand Up @@ -6,17 +6,29 @@
*/

var utile = require('utile'),
ReadWriteStream = require('./read-write-stream');

var delimiter = /g\!\n\!t$/;
ReadWriteStream = require('./read-write-stream'),
parted = require('parted');

//
// ### function JsonParser ()
// Constructor function for the JsonParser prototype responsible
// for parsing `g!\n!t` delimited JSON.
//
var JsonParser = module.exports = function JsonParser() {
var JsonParser = module.exports = function JsonParser(options) {
var options = options || {},
self = this;

ReadWriteStream.call(this);

this._parser = parted.json.create(options);

this._parser.on('error', function (err) {
self.emit('error', err);
});

this._parser.on('output', function (data) {
self.emit('data', data);
});
};

//
Expand All @@ -30,39 +42,12 @@ utile.inherits(JsonParser, ReadWriteStream);
// fragments in the case of parser errors.
//
JsonParser.prototype.write = function (data) {
var lines = data.split('g!\n!t'),
self = this;

//
// If we have a fragment then concat the first
// line because it completes it.
//
if (this.last) {
this.last += lines.shift();
lines = this.last.split('g!\n!t').concat(lines);
}

//
// Grab the last data item because it is a partial
//
if (!delimiter.test(data)) {
this.last = lines.pop();
}

//
// If there are any lines attempt to parse
// them as JSON.
//
if (lines.length) {
lines.forEach(function (line) {
if (!line) { return }
var json;

try { json = JSON.parse(line) }
catch (ex) { return }

self.emit('data', json);
});
}
return this._parser.write(data);
};

//
// ### function end (data)
//
JsonParser.prototype.end = function (data) {
return this._parser.end(data);
};
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -18,7 +18,8 @@
"sendgrid-web": "0.0.2",
"telenode": "0.0.3",
"utile": "0.1.7",
"window-stream": "0.2.3"
"window-stream": "0.2.3",
"parted": "0.1.1"
},
"devDependencies": {
"optimist": "0.3.4",
Expand Down

0 comments on commit 0cd3f4f

Please sign in to comment.