Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

use object iterator to stringify bstream

  • Loading branch information...
commit 241dbe63d72b999afefd9e73259bc68e0ba430e7 1 parent 90896d0
@Floby authored
View
104 lib/StringifyStream.js
@@ -1,46 +1,76 @@
-var streamify = require('json-streamify').streamify;
-var Stream = require('stream').Stream;
+var stream = require('stream');
var util = require('util');
+var oi = require('object-iterator');
-function StringifyStream (object) {
- Stream.apply(this);
- this.on('newListener', function(event, listener) {
- if(event === 'data') {
- this._start();
- }
- });
+function StringifyStream (source, options) {
+ stream.Readable.call(this, options);
+ this.iterator = oi(source);
+
+ this._inCompose = 0;
+ this._isFirst = true;
}
+util.inherits(StringifyStream, stream.Readable);
-StringifyStream.prototype._start = function _start() {
- var self = this;
- process.nextTick(function() {
- // streamify is synchronous so this is somewhat
- // not what we want to do but this is on the
- // TODO list
- streamify(self._object, function(data) {
- self.emit(data);
- })
- })
-};
+StringifyStream.prototype._read = function _read(size) {
+ var chunk = '';
+ var ended = false;
+ while(chunk.length < size) {
+ var v = this.iterator();
+ if(!v) {
+ ended = true;
+ break;
+ }
+ var print = null;
+ var novalue = false;
+ switch(v.type) {
+ case 'end-object':
+ print = print || '}';
+ case 'end-array':
+ print = print || ']';
+ novalue = true;
-util.inherits(StringifyStream, Stream);
+ case 'object':
+ print = print || '{';
+ case 'array':
+ print = print || '[';
-StringifyStream.prototype.readable = true;
+ default:
+ if(novalue) break;
+ print = print || JSON.stringify(v.value);
+ if(this._inCompose && !this._isFirst) {
+ chunk += ',';
+ }
+ else {
+ this._isFirst = false;
+ }
+ if(v.key && typeof v.key == 'string') {
+ print = JSON.stringify(v.key) + ':' + print;
+ }
+ break;
+ }
-StringifyStream.prototype.setEncoding = function setEncoding() {
- // no support for encoding since it makes little sense for
- // something supposed to yield JSON. So utf8 it is
-};
-StringifyStream.prototype.pause = function pause() {
- // this will come later, haha!
-};
-StringifyStream.prototype.resume = function resume() {
- // same as above
-};
-StringifyStream.prototype.destroy = function destroy() {
- // yeah... well
-};
-StringifyStream.prototype.destroySoon = function destroySoon() {
- // meh
+ chunk += print;
+
+ switch(v.type) {
+ case 'object':
+ print = '{'
+ case 'array':
+ ++this._inCompose;
+ this._isFirst = true;
+ break;
+
+ case 'end-object':
+ case 'end-array':
+ --this._inCompose;
+
+ default:
+ break;
+ }
+ }
+ this.push(chunk);
+ if(ended) this.push(null);
};
+
+
+module.exports = StringifyStream;
View
2  lib/index.js
@@ -1,5 +1,5 @@
var ParseStream = exports.ParseStream = require('./ParseStream');
-//exports.StringifyStream = require('./StringifyStream');
+exports.StringifyStream = require('./StringifyStream');
exports.createParseStream = function createParseStream(noStore) {
return new ParseStream(noStore);
View
6 package.json
@@ -17,10 +17,12 @@
},
"dependencies": {
"tokenizer": "0.1",
- "parser": "0.1"
+ "parser": "0.1",
+ "object-iterator":"0.2"
},
"devDependencies": {
- "vows": "0.6"
+ "vows": "0.6",
+ "nodeunit":"0.7"
},
"scripts": {
"test": "nodeunit test/test-*.js"
View
20 test/sink.js
@@ -0,0 +1,20 @@
+var util = require('util');
+var stream = require('stream');
+
+function Sink () {
+ if(!(this instanceof Sink)) return new Sink()
+ stream.Writable.call(this);
+ this._result = [];
+ this.on('finish', function() {
+ this.emit('data', this._result.join(''));
+ });
+}
+util.inherits(Sink, stream.Writable);
+
+Sink.prototype._write = function _write(chunk, encoding, callback) {
+ this._result.push(chunk.toString('utf8'));
+ return callback();
+};
+
+module.exports = Sink;
+
View
56 test/test-stringify.js
@@ -0,0 +1,56 @@
+var stringify = require('../').StringifyStream;
+var sink = require('./sink');
+
+exports.testStringify = function(test) {
+ var source = {
+ one: 1,
+ two: 'two',
+ three: [
+ true,
+ null,
+ "Hello"
+ ]
+ };
+ var s = new stringify(source);
+ s.pipe(sink()).on('data', function(data) {
+ test.equal(data, JSON.stringify(source), "JSON should be identical");
+ clearTimeout(to);
+ test.done();
+ });
+ var to = setTimeout(function() {
+ if(!done) {
+ test.fail('No end detected');
+ test.done();
+ }
+ }, 20)
+}
+
+exports.testLongObject = function(test) {
+ var source = {
+ one: 1,
+ two: 'two',
+ three: [
+ true,
+ null,
+ "Hello",
+ {
+ Hello: "Nothing to see here",
+ a: 68988754,
+ "null":null
+ }
+ ]
+ };
+ var s = new stringify(source);
+ s.pipe(sink()).on('data', function(data) {
+ test.equal(data, JSON.stringify(source), "JSON should be identical");
+ clearTimeout(to);
+ test.done();
+ });
+ var to = setTimeout(function() {
+ if(!done) {
+ test.fail('No end detected');
+ test.done();
+ }
+ }, 20)
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.